1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
|
/**
* \brief Functions to automatically generate constraints for the rectangular
* node overlap removal problem.
*
* Authors:
* Tim Dwyer <tgdwyer@gmail.com>
*
* Copyright (C) 2005 Authors
*
* This version is released under the CPL (Common Public License) with
* the Graphviz distribution.
* A version is also available under the LGPL as part of the Adaptagrams
* project: http://sourceforge.net/projects/adaptagrams.
* If you make improvements or bug fixes to this code it would be much
* appreciated if you could also contribute those changes back to the
* Adaptagrams repository.
*/
#include <set>
#include <cassert>
#include <cstdlib>
#include "generate-constraints.h"
#include "constraint.h"
using std::set;
using std::vector;
std::ostream& operator <<(std::ostream &os, const Rectangle &r) {
os << "{"<<r.minX<<","<<r.maxX<<","<<r.minY<<","<<r.maxY<<"},";
return os;
}
Rectangle::Rectangle(double x, double X, double y, double Y)
: minX(x),maxX(X),minY(y),maxY(Y) {
assert(x<=X);
assert(y<=Y);
}
struct Node;
struct CmpNodePos { bool operator()(const Node* u, const Node* v) const; };
typedef set<Node*,CmpNodePos> NodeSet;
struct Node {
Variable *v;
Rectangle *r;
double pos;
Node *firstAbove, *firstBelow;
NodeSet *leftNeighbours, *rightNeighbours;
Node(Variable *v, Rectangle *r, double p) : v(v),r(r),pos(p) {
firstAbove=firstBelow=NULL;
leftNeighbours=rightNeighbours=NULL;
assert(r->width()<1e40);
}
~Node() {
delete leftNeighbours;
delete rightNeighbours;
}
void addLeftNeighbour(Node *u) {
leftNeighbours->insert(u);
}
void addRightNeighbour(Node *u) {
rightNeighbours->insert(u);
}
void setNeighbours(NodeSet *left, NodeSet *right) {
leftNeighbours=left;
rightNeighbours=right;
for(NodeSet::iterator i=left->begin();i!=left->end();i++) {
Node *v=*(i);
v->addRightNeighbour(this);
}
for(NodeSet::iterator i=right->begin();i!=right->end();i++) {
Node *v=*(i);
v->addLeftNeighbour(this);
}
}
};
bool CmpNodePos::operator() (const Node* u, const Node* v) const {
if (u->pos < v->pos) {
return true;
}
if (v->pos < u->pos) {
return false;
}
return u < v;
}
NodeSet* getLeftNeighbours(NodeSet &scanline,Node *v) {
NodeSet *leftv = new NodeSet;
NodeSet::iterator i=scanline.find(v);
while(i--!=scanline.begin()) {
Node *u=*(i);
if(u->r->overlapX(v->r)<=0) {
leftv->insert(u);
return leftv;
}
if(u->r->overlapX(v->r)<=u->r->overlapY(v->r)) {
leftv->insert(u);
}
}
return leftv;
}
NodeSet* getRightNeighbours(NodeSet &scanline,Node *v) {
NodeSet *rightv = new NodeSet;
NodeSet::iterator i=scanline.find(v);
for(i++;i!=scanline.end(); i++) {
Node *u=*(i);
if(u->r->overlapX(v->r)<=0) {
rightv->insert(u);
return rightv;
}
if(u->r->overlapX(v->r)<=u->r->overlapY(v->r)) {
rightv->insert(u);
}
}
return rightv;
}
typedef enum {Open, Close} EventType;
struct Event {
EventType type;
Node *v;
double pos;
Event(EventType t, Node *v, double p) : type(t),v(v),pos(p) {};
};
Event **events;
int compare_events(const void *a, const void *b) {
Event *ea=*(Event**)a;
Event *eb=*(Event**)b;
if(ea->v->r==eb->v->r) {
// when comparing opening and closing from the same rect
// open must come first
if(ea->type==Open) return -1;
return 1;
} else if(ea->pos > eb->pos) {
return 1;
} else if(ea->pos < eb->pos) {
return -1;
}
return 0;
}
/**
* Prepares constraints in order to apply VPSC horizontally. Assumes variables have already been created.
* useNeighbourLists determines whether or not a heuristic is used to deciding whether to resolve
* all overlap in the x pass, or leave some overlaps for the y pass.
*/
int generateXConstraints(const int n, Rectangle** rs, Variable** vars, Constraint** &cs, const bool useNeighbourLists) {
events=new Event*[2*n];
int i,m,ctr=0;
for(i=0;i<n;i++) {
vars[i]->desiredPosition=rs[i]->getCentreX();
Node *v = new Node(vars[i],rs[i],rs[i]->getCentreX());
events[ctr++]=new Event(Open,v,rs[i]->getMinY());
events[ctr++]=new Event(Close,v,rs[i]->getMaxY());
}
qsort((Event*)events, (size_t)2*n, sizeof(Event*), compare_events );
NodeSet scanline;
vector<Constraint*> constraints;
for(i=0;i<2*n;i++) {
Event *e=events[i];
Node *v=e->v;
if(e->type==Open) {
scanline.insert(v);
if(useNeighbourLists) {
v->setNeighbours(
getLeftNeighbours(scanline,v),
getRightNeighbours(scanline,v)
);
} else {
NodeSet::iterator it=scanline.find(v);
if(it--!=scanline.begin()) {
Node *u=*it;
v->firstAbove=u;
u->firstBelow=v;
}
it=scanline.find(v);
if(++it!=scanline.end()) {
Node *u=*it;
v->firstBelow=u;
u->firstAbove=v;
}
}
} else {
// Close event
int r;
if(useNeighbourLists) {
for(NodeSet::iterator i=v->leftNeighbours->begin();
i!=v->leftNeighbours->end();i++
) {
Node *u=*i;
double sep = (v->r->width()+u->r->width())/2.0;
constraints.push_back(new Constraint(u->v,v->v,sep));
r=u->rightNeighbours->erase(v);
}
for(NodeSet::iterator i=v->rightNeighbours->begin();
i!=v->rightNeighbours->end();i++
) {
Node *u=*i;
double sep = (v->r->width()+u->r->width())/2.0;
constraints.push_back(new Constraint(v->v,u->v,sep));
r=u->leftNeighbours->erase(v);
}
} else {
Node *l=v->firstAbove, *r=v->firstBelow;
if(l!=NULL) {
double sep = (v->r->width()+l->r->width())/2.0;
constraints.push_back(new Constraint(l->v,v->v,sep));
l->firstBelow=v->firstBelow;
}
if(r!=NULL) {
double sep = (v->r->width()+r->r->width())/2.0;
constraints.push_back(new Constraint(v->v,r->v,sep));
r->firstAbove=v->firstAbove;
}
}
r=scanline.erase(v);
delete v;
}
delete e;
}
delete [] events;
cs=new Constraint*[m=constraints.size()];
for(i=0;i<m;i++) cs[i]=constraints[i];
return m;
}
/**
* Prepares constraints in order to apply VPSC vertically to remove ALL overlap.
*/
int generateYConstraints(const int n, Rectangle** rs, Variable** vars, Constraint** &cs) {
events=new Event*[2*n];
int ctr=0,i,m;
for(i=0;i<n;i++) {
vars[i]->desiredPosition=rs[i]->getCentreY();
Node *v = new Node(vars[i],rs[i],rs[i]->getCentreY());
events[ctr++]=new Event(Open,v,rs[i]->getMinX());
events[ctr++]=new Event(Close,v,rs[i]->getMaxX());
}
qsort((Event*)events, (size_t)2*n, sizeof(Event*), compare_events );
NodeSet scanline;
vector<Constraint*> constraints;
for(i=0;i<2*n;i++) {
Event *e=events[i];
Node *v=e->v;
if(e->type==Open) {
scanline.insert(v);
NodeSet::iterator i=scanline.find(v);
if(i--!=scanline.begin()) {
Node *u=*i;
v->firstAbove=u;
u->firstBelow=v;
}
i=scanline.find(v);
if(++i!=scanline.end()) {
Node *u=*i;
v->firstBelow=u;
u->firstAbove=v;
}
} else {
// Close event
Node *l=v->firstAbove, *r=v->firstBelow;
if(l!=NULL) {
double sep = (v->r->height()+l->r->height())/2.0;
constraints.push_back(new Constraint(l->v,v->v,sep));
l->firstBelow=v->firstBelow;
}
if(r!=NULL) {
double sep = (v->r->height()+r->r->height())/2.0;
constraints.push_back(new Constraint(v->v,r->v,sep));
r->firstAbove=v->firstAbove;
}
scanline.erase(v);
delete v;
}
delete e;
}
delete [] events;
cs=new Constraint*[m=constraints.size()];
for(i=0;i<m;i++) cs[i]=constraints[i];
return m;
}
|