File: generate-constraints.cpp

package info (click to toggle)
tulip 4.8.0dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 179,264 kB
  • ctags: 64,517
  • sloc: cpp: 600,444; ansic: 36,311; makefile: 22,136; python: 1,304; sh: 946; yacc: 522; xml: 337; pascal: 157; php: 66; lex: 55
file content (349 lines) | stat: -rw-r--r-- 8,648 bytes parent folder | download | duplicates (2)
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/**
 * \brief Functions to automatically generate constraints for the
 * rectangular node overlap removal problem.
 *
 * Authors:
 *   Tim Dwyer <tgdwyer@gmail.com>
 *
 * Copyright (C) 2005 Authors
 *
 * Released under GNU LGPL.  Read the file 'COPYING' for more information.
 */

#include <set>
#include <cassert>
#include <stdlib.h>
#include "generate-constraints.h"
#include "constraint.h"

#include "isnan.h" /* Include last */

using std::set;
using std::vector;

namespace vpsc {
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,
                     const double& xb, const double& yb)
  : minX(x),maxX(X),minY(y),maxY(Y),xBorder(xb),yBorder(yb) {
  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;
  }

  if (isNaN(u->pos) != isNaN(v->pos)) {
    return isNaN(u->pos);
  }

  return u < v;

  /* I don't know how important it is to handle NaN correctly
   * (e.g. we probably handle it badly in other code anyway, and
   * in any case the best we can hope for is to reduce the
   * badness of other nodes).
   *
   * Nevertheless, we try to do the right thing here and in
   * event comparison.  The issue is that (on platforms with
   * ieee floating point comparison) NaN compares neither less
   * than nor greater than any other number, yet sort wants a
   * well-defined ordering.  In particular, we want to ensure
   * transitivity of equivalence, which normally wouldn't be
   * guaranteed if the "middle" item in the transitivity
   * involves a NaN.  (NaN is neither less than nor greater than
   * other numbers, so tends to be considered as equal to all
   * other numbers: even unequal numbers.)
   */
}

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;
}

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;
  }
  else if(isNaN(ea->pos) != isNaN(ea->pos)) {
    /* See comment in CmpNodePos. */
    return ( isNaN(ea->pos)
             ? -1
             : 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 ConstraintsGenerator::generateXConstraints(Rectangle** rs, Variable** vars, Constraint** &cs, const bool useNeighbourLists) {
  unsigned 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
      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));
          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));
          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;
        }
      }

      scanline.erase(v);
      delete v;
    }

    delete e;
  }

  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 ConstraintsGenerator::generateYConstraints(Rectangle** rs, Variable** vars, Constraint** &cs) {
  unsigned 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;
  }

  cs=new Constraint*[m=constraints.size()];

  for(i=0; i<m; i++) cs[i]=constraints[i];

  return m;
}
}