32 os <<
"{"<<r.minX<<
","<<r.maxX<<
","<<r.minY<<
","<<r.maxY<<
"},";
36: minX(x),maxX(
X),minY(y),maxY(
Y) {
43struct CmpNodePos {
bool operator()(
const Node* u,
const Node* v)
const; };
45typedef set<Node*,CmpNodePos> NodeSet;
51 Node *firstAbove, *firstBelow;
52 NodeSet leftNeighbours, rightNeighbours;
54 firstAbove=firstBelow=
nullptr;
55 assert(r.
width()<1e40);
57 void addLeftNeighbour(
Node *u) {
58 leftNeighbours.insert(u);
60 void addRightNeighbour(
Node *u) {
61 rightNeighbours.insert(u);
63 void setNeighbours(
const NodeSet &
left,
const NodeSet &
right) {
65 rightNeighbours=
right;
67 n->addRightNeighbour(
this);
70 n->addLeftNeighbour(
this);
74bool CmpNodePos::operator() (
const Node* u,
const Node* v)
const {
75 if (u->pos < v->pos) {
78 if (v->pos < u->pos) {
87 NodeSet::iterator i=scanline.find(v);
88 while(i!=scanline.begin()) {
90 if(u->r.overlapX(v->r)<=0) {
94 if(u->r.overlapX(v->r)<=u->r.overlapY(v->r)) {
102 NodeSet::iterator i=scanline.find(v);
103 for(i++;i!=scanline.end(); i++) {
105 if(u->r.overlapX(v->r)<=0) {
109 if(u->r.overlapX(v->r)<=u->r.overlapY(v->r)) {
119 std::shared_ptr<Node>
v;
126 if(&ea.
v->r==&eb.
v->r) {
131 }
else if(ea.
pos > eb.
pos) {
133 }
else if(ea.
pos < eb.
pos) {
145 Constraint** &cs,
const bool useNeighbourLists) {
147 vector<Event> events;
148 events.reserve(2 * rs.size());
149 for(
size_t i=0;i<rs.size();i++) {
151 auto v = std::make_shared<Node>(vars[i],rs[i],rs[i].getCentreX());
152 events.emplace_back(
Open,v,rs[i].getMinY());
153 events.emplace_back(
Close,v,rs[i].getMaxY());
158 vector<Constraint*> constraints;
159 for(
Event &e : events) {
163 if(useNeighbourLists) {
169 NodeSet::iterator it=scanline.find(v);
170 if(it!=scanline.begin()) {
176 if(++it!=scanline.end()) {
184 if(useNeighbourLists) {
185 for(
Node *u : v->leftNeighbours) {
186 double sep = (v->r.width()+u->r.width())/2.0;
187 constraints.push_back(
new Constraint(u->v,v->v,sep));
188 u->rightNeighbours.erase(v);
191 for(
Node *u : v->rightNeighbours) {
192 double sep = (v->r.width()+u->r.width())/2.0;
193 constraints.push_back(
new Constraint(v->v,u->v,sep));
194 u->leftNeighbours.erase(v);
197 Node *l=v->firstAbove, *r=v->firstBelow;
199 double sep = (v->r.width()+l->r.width())/2.0;
200 constraints.push_back(
new Constraint(l->v,v->v,sep));
201 l->firstBelow=v->firstBelow;
204 double sep = (v->r.width()+r->r.
width())/2.0;
205 constraints.push_back(
new Constraint(v->v,r->v,sep));
206 r->firstAbove=v->firstAbove;
212 int m =constraints.size();
214 for(
int i=0;i<m;i++) cs[i]=constraints[i];
224 vector<Event> events;
225 events.reserve(2 * rs.size());
226 for(
size_t i=0;i<rs.size();i++) {
228 auto v = std::make_shared<Node>(vars[i],rs[i],rs[i].getCentreY());
229 events.emplace_back(
Open,v,rs[i].getMinX());
230 events.emplace_back(
Close,v,rs[i].getMaxX());
234 vector<Constraint*> constraints;
235 for(
Event &e : events) {
239 NodeSet::iterator i=scanline.find(v);
240 if(i!=scanline.begin()) {
246 if(++i!=scanline.end()) {
253 Node *l=v->firstAbove, *r=v->firstBelow;
255 double sep = (v->r.height()+l->r.height())/2.0;
256 constraints.push_back(
new Constraint(l->v,v->v,sep));
257 l->firstBelow=v->firstBelow;
260 double sep = (v->r.height()+r->r.
height())/2.0;
261 constraints.push_back(
new Constraint(v->v,r->v,sep));
262 r->firstAbove=v->firstAbove;
267 int m =constraints.size();
269 for(
int i=0;i<m;i++) cs[i]=constraints[i];
Functions to automatically generate constraints for the rectangular node overlap removal problem.
Rectangle(double x, double X, double y, double Y)
#define X(prefix, name, str, type, subtype,...)
static NodeSet getRightNeighbours(NodeSet &scanline, Node *v)
static NodeSet getLeftNeighbours(NodeSet &scanline, Node *v)
static bool compare_events(const Event &ea, const Event &eb)
int generateYConstraints(const vector< Rectangle > &rs, Variable **vars, Constraint **&cs)
int generateXConstraints(const vector< Rectangle > &rs, Variable **vars, Constraint **&cs, const bool useNeighbourLists)
std::ostream & operator<<(std::ostream &os, const Rectangle &r)
A constraint determines a minimum or exact spacing required between two variables.
Event(EventType t, const std::shared_ptr< Node > &v_, double p)
std::shared_ptr< Node > v