Adaptagrams
cc_nonoverlapconstraints.h
1 /*
2  * vim: ts=4 sw=4 et tw=0 wm=0
3  *
4  * libcola - A library providing force-directed network layout using the
5  * stress-majorization method subject to separation constraints.
6  *
7  * Copyright (C) 2010-2014 Monash University
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  * See the file LICENSE.LGPL distributed with the library.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18  *
19  * Author(s): Michael Wybrow
20  *
21 */
22 
23 #ifndef COLA_CC_NONOVERLAPCONSTRAINTS_H
24 #define COLA_CC_NONOVERLAPCONSTRAINTS_H
25 
26 #include <vector>
27 
28 #include "libcola/cola.h"
29 #include "libcola/compound_constraints.h"
30 #include "libcola/shapepair.h"
31 
32 namespace vpsc {
33 class Rectangle;
34 }
35 
36 namespace cola {
37 
38 class Cluster;
39 
40 class OverlapShapeOffsets : public SubConstraintInfo
41 {
42  public:
43  OverlapShapeOffsets(unsigned ind, double xOffset, double yOffset,
44  unsigned int group)
45  : SubConstraintInfo(ind),
46  cluster(nullptr),
47  rectPadding(0),
48  group(group)
49  {
50  halfDim[0] = xOffset;
51  halfDim[1] = yOffset;
52  }
53  OverlapShapeOffsets(unsigned ind, Cluster *cluster, unsigned int group)
54  : SubConstraintInfo(ind),
55  cluster(cluster),
56  rectPadding(cluster->margin()),
57  group(group)
58  {
59  halfDim[0] = 0;
60  halfDim[1] = 0;
61  }
62  OverlapShapeOffsets()
63  : SubConstraintInfo(1000000),
64  cluster(nullptr),
65  rectPadding(0)
66  {
67  }
68  bool usesClusterBounds(void) const
69  {
70  return (cluster && !cluster->clusterIsFromFixedRectangle());
71  }
72  void resize(double xOffset, double yOffset)
73  {
74  halfDim[0] = xOffset;
75  halfDim[1] = yOffset;
76  }
77  Cluster *cluster;
78  double halfDim[2]; // Half width and height values.
79  Box rectPadding; // Used for cluster padding.
80  unsigned int group;
81 };
82 
83 
84 class ShapePairInfo
85 {
86  public:
87  ShapePairInfo(unsigned ind1, unsigned ind2, unsigned ord = 1)
88  : order(ord),
89  satisfied(false),
90  processed(false)
91  {
92  COLA_ASSERT(ind1 != ind2);
93  // Assign the lesser value to varIndex1.
94  varIndex1 = (ind1 < ind2) ? ind1 : ind2;
95  // Assign the greater value to varIndex2.
96  varIndex2 = (ind1 > ind2) ? ind1 : ind2;
97  }
98  bool operator<(const ShapePairInfo& rhs) const
99  {
100  // Make sure the processed ones are at the end after sorting.
101  int processedInt = processed ? 1 : 0;
102  int rhsProcessedInt = rhs.processed ? 1 : 0;
103  if (processedInt != rhsProcessedInt)
104  {
105  return processedInt < rhsProcessedInt;
106  }
107  // Use cluster ordering for primary sorting.
108  if (order != rhs.order)
109  {
110  return order < rhs.order;
111  }
112  return overlapMax > rhs.overlapMax;
113  }
114  unsigned short order;
115  unsigned short varIndex1;
116  unsigned short varIndex2;
117  bool satisfied;
118  bool processed;
119  double overlapMax;
120 };
121 
122 
123 // Stores IDs of all rectangles exempt from non-overlap constraints.
124 class NonOverlapConstraintExemptions {
125  public:
126  NonOverlapConstraintExemptions();
127  void addExemptGroupOfNodes(ListOfNodeIndexes listOfNodeGroups);
128  bool shapePairIsExempt(ShapePair shapePair) const;
129  std::set<ShapePair> getExemptPairs(void) {return m_exempt_pairs;}
130 
131  private:
132  std::set<ShapePair> m_exempt_pairs;
133 };
134 
135 // Non-overlap constraints prevent a set of given shapes from overlapping.
136 class NonOverlapConstraints : public CompoundConstraint {
137  public:
138  NonOverlapConstraints(NonOverlapConstraintExemptions *exemptions,
139  unsigned int priority = PRIORITY_NONOVERLAP);
155  void addShape(unsigned id, double halfW, double halfH,
156  unsigned int group = 1, std::set<unsigned> exemptions = std::set<unsigned>());
157  void resizeShape(unsigned id, double halfW, double halfH);
158  void removeShape(unsigned id);
159  void addCluster(Cluster *cluster, unsigned int group);
160  void computeAndSortOverlap(vpsc::Variables vs[]);
161  void markCurrSubConstraintAsActive(const bool satisfiable);
162  void markAllSubConstraintsAsInactive(void);
163  bool subConstraintsRemaining(void) const;
164  SubConstraintAlternatives getCurrSubConstraintAlternatives(
165  vpsc::Variables vs[]);
166  std::string toString(void) const;
167  void setClusterClusterExemptions(std::set<ShapePair> exemptions);
168 
169  void generateVariables(const vpsc::Dim dim, vpsc::Variables& vars);
171  vpsc::Variables& vars, vpsc::Constraints& gcs);
189  std::vector<vpsc::Rectangle*>& boundingBoxes);
190 
191  private:
192  void computeOverlapForShapePairInfo(ShapePairInfo& info,
193  vpsc::Variables vs[]);
194 
195  std::list<ShapePairInfo> pairInfoList;
196  std::map<unsigned, OverlapShapeOffsets> shapeOffsets;
197  bool pairInfoListSorted;
198  bool initialSortCompleted;
199 
200  // Cluster variables
201  size_t clusterVarStartIndex;
202  size_t currClusterIndex;
203  size_t clusterMode;
204 
205  NonOverlapConstraintExemptions *m_exemptions;
206  std::set<ShapePair> m_cluster_cluster_exemptions;
207 };
208 
209 } // namespace cola
210 #endif // COLA_CC_NONOVERLAPCONSTRAINTS
std::vector< NodeIndexes > ListOfNodeIndexes
A vector of NodeIndexes.
Definition: cola.h:65
libvpsc: Variable Placement with Separation Constraints quadratic program solver library.
Definition: assertions.h:61
std::vector< Variable * > Variables
A vector of pointers to Variable objects.
Definition: constraint.h:38
std::vector< Constraint * > Constraints
A vector of pointers to Constraint objects.
Definition: constraint.h:125
virtual void generateSeparationConstraints(const vpsc::Dim dim, vpsc::Variables &var, vpsc::Constraints &cs, vpsc::Rectangles &bbs)=0
Implemented by the compound constraint to generate the low-level separation constraints in the given ...
libcola: Force-directed network layout subject to separation constraints library. ...
Definition: box.cpp:25
Dim
Indicates the x- or y-dimension.
Definition: rectangle.h:41
void generateVariables(CompoundConstraints &ccs, const vpsc::Dim dim, vpsc::Variables &vars)
Generate just all the variables for a collection of CompoundConstraints.
Definition: compound_constraints.cpp:1466