File: BuildReverse.cpp

package info (click to toggle)
kseg 0.4.0.3-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny
  • size: 3,160 kB
  • ctags: 2,052
  • sloc: cpp: 14,632; makefile: 10
file content (529 lines) | stat: -rw-r--r-- 13,878 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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
/*
 *  KSeg
 *  Copyright (C) 1999-2006 Ilya Baran
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *  Send comments and/or bug reports to:
 *                 ibaran@mit.edu
 */

#include <qapplication.h>

#include "defs.H"
#include "G_matrix.H"
#include "G_ref.H"
#include "G_refs.H"
#include "G_object.H"
#include "my_hash_map.H"
#include <vector>


class edge {
public:
  static G_matrix identity;

  G_ref *ref;

  edge(G_ref *inRef) {
    ref = inRef;
  }

  virtual const G_matrix &getMatrix() const { return identity; }
};

G_matrix edge::identity = G_matrix();


class matrixEdge : public edge {
public:
  G_matrix m;

  matrixEdge(G_ref *inRef, G_matrix inM) : edge(inRef), m(inM) {}

  const G_matrix &getMatrix() const { return m; }

};


// This class stores the graph of dependencies between objects.
// A constraint like:
// "If Point P moves by vector V, Segment S must move by the vector
//  M * V" would be represented by two matrixEdge's of M and M^-1
// between P and S.  If a bunch of edges form a cycle such that
// the product of matrices around that cycle is not the identity,
// that graph is inconsistent and needs to be repaired.  Since fixed
// edges are always associated with the identity matrix, a graph
// only containing fixed edges is always consistent.

class mygraph {
private:
  hash_map<G_ref *, vector<edge *> > map1;
  vector<vector<pair<G_ref *, edge *> > > changeableEdges;

public:
  void createFixedEdge(G_ref *r1, G_ref *r2) {
    map1[r1].push_back(new edge(r2));
    map1[r2].push_back(new edge(r1));
  }


  hash_map<G_ref *, G_matrix> getOutput(G_ref *initial) {
    hash_map<G_ref *, G_matrix> m; //will be returned
    //walks through the graph via bfs and multiplies the matrices through

    hash_set<G_ref *> visited;
    vector<pair<G_ref *, G_matrix> > toVisit;

    toVisit.push_back(make_pair(initial, G_matrix()));
    visited.insert(initial);

    int i = 0;
    while(i < (int)toVisit.size()) {

      int j = 0;
      if(toVisit[i].first->getParents().size() == 0 &&
	 toVisit[i].second * G_point(0, 0) == G_point(0, 0)) {
	if((toVisit[i].first->getType() & G_TEXT) == 0) m[toVisit[i].first] = toVisit[i].second;
      }

      while(j < (int)(map1[toVisit[i].first].size())) {
	edge *e = (map1[toVisit[i].first])[j];

	if(visited.count(e->ref) == 0) {
	  visited.insert(e->ref);

	  toVisit.push_back(make_pair(e->ref, e->getMatrix() * toVisit[i].second));
	}

	j++;
      }

      i++;
    }

    return m;
    
  }

  G_matrix getPath(G_ref *r1, G_ref *r2) {
    hash_set<G_ref *> visited;
    vector<pair<G_ref *, G_matrix> > toVisit;

    toVisit.push_back(make_pair(r1, G_matrix()));
    visited.insert(r1);

    if(r1 == r2) return G_matrix();

    int i = 0;
    while(i < (int)toVisit.size()) {
      int j = 0;

      while(j < (int)map1[toVisit[i].first].size()) {
	edge *e = (map1[toVisit[i].first])[j];

	if(e->ref == r2) return e->getMatrix() * toVisit[i].second;

	if(visited.count(e->ref) == 0) {
	  visited.insert(e->ref);

	  toVisit.push_back(make_pair(e->ref, e->getMatrix() * toVisit[i].second));
	}

	j++;
      }

      i++;
    }

    return G_matrix::getNullMatrix();
  }


  bool createChangeableEdge(G_ref *r1, G_ref *r2, G_matrix forw, G_matrix rev, int level) {
    G_matrix m = getPath(r1, r2);
    if(!(m == G_matrix::getNullMatrix())) { //check validity
      if(m.almostEqual(forw) == false) return false;
    }

    if(r1 == r2) return true;

    edge *e1 = new matrixEdge(r2, forw);
    edge *e2 = new matrixEdge(r1, rev);

    map1[r1].push_back(e1);
    map1[r2].push_back(e2);

    if(level >= (int)changeableEdges.size()) changeableEdges.resize(level + 1);
    changeableEdges[level].push_back(make_pair(r1, e1));
    changeableEdges[level].push_back(make_pair(r2, e2));

    return true;
  }

  void clear() { //resets everything:
    map1.clear();
    changeableEdges.clear();
  }

  void clearLevel(int level) {
    int i;

    if(level >= (int)changeableEdges.size()) return;

    for(i = 0; i < (int)changeableEdges[level].size(); ++i) {
      G_ref *r = changeableEdges[level][i].first;
      edge *e = changeableEdges[level][i].second;

      map1[r].erase(find(map1[r].begin(), map1[r].end(), e));
    }

    changeableEdges[level].clear();
  }

};


static mygraph graph1;

bool setupFixedRef(G_ref *ref);
int setupChangeableRef(G_ref *ref, int state, int level);
void setupFixedChangeableRef(G_ref *ref); // for going back in case of timeout

static hash_set<G_ref *> done;
static G_refs changeable;
static G_ref *startRef;

// This function sets up the parts of the graph that
// will definitely not change.  It is linear time.
void BuildReverseFixed(G_ref *ref)
{
  if(done.count(ref) > 0) return; //already done.
  done.insert(ref);

  if(setupFixedRef(ref)) changeable.push_back(ref);

  int i;
  for(i = 0; i < (int)ref->getParents().size(); ++i) {
    BuildReverseFixed(ref->getParents()[i]);
  }
}


// This function tries to set up a consistent set
// of edges among those that can change.  It does
// it with a simple exponential-time (in the
// number of changeable edges) brute-force
// algorithm, but if it finds it is taking too
// long, it reverts to a simpler linear-time
// algorithm which will potentially make more
// objects move than necessary.
void BuildReverseChangeable()
{
  int i;

  bool timeOut = false;

  vector<int> states;
  
  QTime t;
  
  t.start();

  i = 0;
  while(i < (int)changeable.size()) {
    if(t.elapsed() > 1000) { //TIMEOUT!  go back to fixed algorithm.
      timeOut = true;
      break;
    }

    if((int)states.size() <= i) states.push_back(0);
    
    int result = setupChangeableRef(changeable[i], states[i], i);
    
    if(result) {
      states[i] = result;
      i++;
    }
    else {
      graph1.clearLevel(i);
      states.pop_back();
      i--;
    }
  }

  if(!timeOut) return;

  while(i >= 0) {
    graph1.clearLevel(i);
    i--;
  }

  for(i = 0; i < (int)changeable.size(); ++i) {
    setupFixedChangeableRef(changeable[i]);
  }
}


// This is the main function.
// It first sets up the part of the graph
// that's fixed, then the part of the graph
// that's variable, taking care of special
// cases, such as constrained points and text
// objects in the process.
hash_map<G_ref *, G_matrix> BuildReverse(const G_refs &refs)
{
  int i;

  done.clear();
  graph1.clear();
  changeable.clear();

  if(refs.size() == 0) return hash_map<G_ref *, G_matrix>();

  //check if the input consists of a single constrained point
  if(refs.size() == 1 && refs[0]->getType() == G_POINT &&
     refs[0]->getDescendType() == G_CONSTRAINED_POINT) {
    hash_map<G_ref *, G_matrix> out;
    out[refs[0]] = G_matrix();
    return out;
  }

  G_refs texts, refs2;

  //separate the texts (which have no impact on the structure)
  //from the refs
  for(i = 0; i < (int)refs.size(); ++i) {
    if(refs[i]->getType() & G_TEXT) {
      texts.push_back(refs[i]);
    }
    else {
      refs2.push_back(refs[i]);
    }
  }

  if(refs2.size() > 0) {
    startRef = refs2[0];

    for(i = 0; i < (int)refs2.size() - 1; ++i) {
      graph1.createFixedEdge(refs2[i], refs2[i + 1]);
    }
    
    for(i = 0; i < (int)refs2.size(); ++i) {
      BuildReverseFixed(refs2[i]);
    }

    BuildReverseChangeable();
    
    hash_map<G_ref *, G_matrix> toBeReturned = graph1.getOutput(refs2[0]);
    
    for(i = 0; i < (int)texts.size(); ++i) {
      toBeReturned[texts[i]] = G_matrix();
    }
    
    return toBeReturned;
  }

  //else if only texts are present

  hash_map<G_ref *, G_matrix> toBeReturned;

  for(i = 0; i < (int)texts.size(); ++i) {
    toBeReturned[texts[i]] = G_matrix();
  }
    
  return toBeReturned;
}



bool setupFixedRef(G_ref *ref) //returns whether there are changeable edges for it
{
  if(ref->getParents().size() == 0) return false;
  
  if(ref->getType() & G_GEOMETRIC && IS_TRANSFORM(ref->getDescendType())) {
    if(ref->getDescendType() == G_TRANSLATED) {
      graph1.createFixedEdge(ref, ref->getParents()[0]);
      graph1.createFixedEdge(ref->getParents()[1], ref->getParents()[2]);
    }
    else if(ref->getDescendType() == G_ROTATED) {
      if(ref->getParents().count() == 5) { //by three points
	graph1.createFixedEdge(ref->getParents()[2], ref->getParents()[3]);
	graph1.createFixedEdge(ref->getParents()[2], ref->getParents()[4]);
      }

      return true;
    }
    else if(ref->getDescendType() == G_SCALED) {
      return true;
    }
    else if(ref->getDescendType() == G_REFLECTED) {
      return true;
    }
  }
  else if(ref->getType() == G_CIRCLE && ref->getDescendType() == G_CENTERRADIUS_CIRCLE) {
    graph1.createFixedEdge(ref, ref->getParents()[0]);
  }
  else if(ref->getType() == G_LINE && (ref->getDescendType() == G_PARALLEL_LINE ||
				       ref->getDescendType() == G_PERPENDICULAR_LINE)) {
    graph1.createFixedEdge(ref, ref->getParents()[0]);
  }
  else if(ref->getType() == G_LOCUS) {
    graph1.createFixedEdge(ref, ref->getParents()[0]);
    graph1.createFixedEdge(ref, ref->getParents()[1]);
  }
  else if(ref->getType() & G_TEXT) {
    int i;

    if(ref->getType() == G_MEASURE &&
       (ref->getDescendType() == G_DISTANCE_MEASURE ||
	ref->getDescendType() == G_ANGLE_MEASURE)) { //these have dependent parents
      for(i = 0; i < (int)ref->getParents().size() - 1; ++i) {
	graph1.createFixedEdge(ref->getParents()[i + 1], ref->getParents()[i]);
      }
    }
  }
  else {
    int i;

    for(i = 0; i < (int)ref->getParents().size(); ++i) {
      graph1.createFixedEdge(ref, ref->getParents()[i]);
    }
  }

  return false;
}


void setupFixedChangeableRef(G_ref *ref)
{
  //this works for all--rotated, translated, scaled.
  graph1.createFixedEdge(ref, ref->getParents()[0]);
  graph1.createFixedEdge(ref, ref->getParents()[1]);
}


int setupChangeableRef(G_ref *ref, int state, int level)
{
  bool success;

  graph1.clearLevel(level);
  
  if(ref->getDescendType() == G_ROTATED) {

    if(state < 1) {
      double angle;

      if(ref->getParents().count() == 5) { //by three points
	angle = (ref->getParents()[4]->getObject()->getPoint() -
		 ref->getParents()[3]->getObject()->getPoint()).angle() -
	  (ref->getParents()[2]->getObject()->getPoint() -
	   ref->getParents()[3]->getObject()->getPoint()).angle();
      }
      else { //value-based
	angle = ref->getParents()[2]->getObject()->getNumValue() / -180. * M_PI;
      }
	
      success = graph1.createChangeableEdge(ref, ref->getParents()[0], G_matrix::rotate(angle),
					    G_matrix::rotate(-angle), level);
      success = success && graph1.createChangeableEdge(startRef, ref->getParents()[1],
						      G_matrix::translate(1,1), G_matrix::translate(-1,-1),
						      level);
      
      if(success) return 1;
      
      graph1.clearLevel(level);
    }

    if(state < 2) {
      success = graph1.createChangeableEdge(ref, ref->getParents()[0], G_matrix(), G_matrix(), level);
      success = success && graph1.createChangeableEdge(ref, ref->getParents()[1],
						       G_matrix(), G_matrix(), level);
      
      if(success) return 2;
      
      graph1.clearLevel(level);
    }
    
    return 0;
  }

  if(ref->getDescendType() == G_SCALED) {
    if(state < 1) {
      double scale;

      if(ref->getParents().count() == 4) { //by two segments
	scale = ref->getParents()[3]->getObject()->getSegment().length() /
	  ref->getParents()[2]->getObject()->getSegment().length();
      }
      else { //value based
	scale = ref->getParents()[2]->getObject()->getNumValue();
      }

      success = graph1.createChangeableEdge(ref, ref->getParents()[0], G_matrix::scale(1. / (scale + SMALL)),
					    G_matrix::rotate(scale), level);
      success = success && graph1.createChangeableEdge(startRef, ref->getParents()[1],
						      G_matrix::translate(1,1), G_matrix::translate(-1,-1),
						      level);
      
      if(success) return 1;
      
      graph1.clearLevel(level);
    }

    if(state < 2) {
      success = graph1.createChangeableEdge(ref, ref->getParents()[0], G_matrix(), G_matrix(), level);
      success = success && graph1.createChangeableEdge(ref, ref->getParents()[1],
						       G_matrix(), G_matrix(), level);
      
      if(success) return 2;
      
      graph1.clearLevel(level);
    }
    
    return 0;
  }

  if(ref->getDescendType() == G_REFLECTED) {
    if(state < 1) {
      G_straight *reflect = ref->getParents()[1]->getObject()->getStraightRef();
      G_matrix m = G_matrix::reflect(reflect->getDirection().getX(),
				     reflect->getDirection().getY());

      success = graph1.createChangeableEdge(ref, ref->getParents()[0], m, m, level);
      success = success && graph1.createChangeableEdge(startRef, ref->getParents()[1],
						      G_matrix::translate(1,1), G_matrix::translate(-1,-1),
						      level);
      
      if(success) return 1;
      
      graph1.clearLevel(level);
    }

    if(state < 2) {
      success = graph1.createChangeableEdge(ref, ref->getParents()[0], G_matrix(), G_matrix(), level);
      success = success && graph1.createChangeableEdge(ref, ref->getParents()[1],
						       G_matrix(), G_matrix(), level);
      
      if(success) return 2;
      
      graph1.clearLevel(level);
    }
    
    return 0;
  }

  return 0;
}