34 os <<
"{"<<r.minX<<
","<<r.maxX<<
","<<r.minY<<
","<<r.maxY<<
"},";
38: minX(x),maxX(
X),minY(y),maxY(
Y) {
45struct CmpNodePos {
bool operator()(
const Node* u,
const Node* v)
const; };
47typedef set<Node*,CmpNodePos> NodeSet;
53 Node *firstAbove, *firstBelow;
54 NodeSet leftNeighbours, rightNeighbours;
56 firstAbove=firstBelow=
nullptr;
57 assert(r.
width()<1e40);
59 void addLeftNeighbour(
Node *u) {
60 leftNeighbours.insert(u);
62 void addRightNeighbour(
Node *u) {
63 rightNeighbours.insert(u);
65 void setNeighbours(
const NodeSet &
left,
const NodeSet &
right) {
67 rightNeighbours=
right;
69 n->addRightNeighbour(
this);
72 n->addLeftNeighbour(
this);
76bool CmpNodePos::operator() (
const Node* u,
const Node* v)
const {
77 if (u->pos < v->pos) {
80 if (v->pos < u->pos) {
89 NodeSet::iterator i=scanline.find(v);
90 while(i!=scanline.begin()) {
92 if(u->r.overlapX(v->r)<=0) {
96 if(u->r.overlapX(v->r)<=u->r.overlapY(v->r)) {
104 NodeSet::iterator i=scanline.find(v);
105 for(i++;i!=scanline.end(); i++) {
107 if(u->r.overlapX(v->r)<=0) {
111 if(u->r.overlapX(v->r)<=u->r.overlapY(v->r)) {
121 std::shared_ptr<Node>
v;
128 if(&ea.
v->r==&eb.
v->r) {
133 }
else if(ea.
pos > eb.
pos) {
135 }
else if(ea.
pos < eb.
pos) {
147 Constraint** &cs,
const bool useNeighbourLists) {
149 vector<Event> events;
150 events.reserve(2 * rs.size());
151 for(
size_t i=0;i<rs.size();i++) {
153 auto v = std::make_shared<Node>(vars[i],rs[i],rs[i].getCentreX());
154 events.emplace_back(
Open,v,rs[i].getMinY());
155 events.emplace_back(
Close,v,rs[i].getMaxY());
160 vector<Constraint*> constraints;
161 for(
Event &e : events) {
165 if(useNeighbourLists) {
171 NodeSet::iterator it=scanline.find(v);
172 if(it!=scanline.begin()) {
178 if(++it!=scanline.end()) {
186 if(useNeighbourLists) {
187 for(
Node *u : v->leftNeighbours) {
188 double sep = (v->r.width()+u->r.width())/2.0;
189 constraints.push_back(
new Constraint(u->v,v->v,sep));
190 u->rightNeighbours.erase(v);
193 for(
Node *u : v->rightNeighbours) {
194 double sep = (v->r.width()+u->r.width())/2.0;
195 constraints.push_back(
new Constraint(v->v,u->v,sep));
196 u->leftNeighbours.erase(v);
199 Node *l=v->firstAbove, *r=v->firstBelow;
201 double sep = (v->r.width()+l->r.width())/2.0;
202 constraints.push_back(
new Constraint(l->v,v->v,sep));
203 l->firstBelow=v->firstBelow;
206 double sep = (v->r.width()+r->r.
width())/2.0;
207 constraints.push_back(
new Constraint(v->v,r->v,sep));
208 r->firstAbove=v->firstAbove;
214 int m =constraints.size();
216 for(
int i=0;i<m;i++) cs[i]=constraints[i];
226 vector<Event> events;
227 events.reserve(2 * rs.size());
228 for(
size_t i=0;i<rs.size();i++) {
230 auto v = std::make_shared<Node>(vars[i],rs[i],rs[i].getCentreY());
231 events.emplace_back(
Open,v,rs[i].getMinX());
232 events.emplace_back(
Close,v,rs[i].getMaxX());
236 vector<Constraint*> constraints;
237 for(
Event &e : events) {
241 NodeSet::iterator i=scanline.find(v);
242 if(i!=scanline.begin()) {
248 if(++i!=scanline.end()) {
255 Node *l=v->firstAbove, *r=v->firstBelow;
257 double sep = (v->r.height()+l->r.height())/2.0;
258 constraints.push_back(
new Constraint(l->v,v->v,sep));
259 l->firstBelow=v->firstBelow;
262 double sep = (v->r.height()+r->r.
height())/2.0;
263 constraints.push_back(
new Constraint(v->v,r->v,sep));
264 r->firstAbove=v->firstAbove;
269 int m =constraints.size();
271 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