-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathKSegConstruction.cpp
More file actions
477 lines (355 loc) · 11.9 KB
/
Copy pathKSegConstruction.cpp
File metadata and controls
477 lines (355 loc) · 11.9 KB
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
/*
* 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 "KSegConstruction.H"
#include "KSegView.H"
#include "G_refs.H"
#include "G_ref.H"
#include "G_object.H"
#include "my_hash_map.H"
#include "G_refSearcher.H"
std::vector<KSegConstruction *> KSegConstruction::constructions;
KSegConstruction::KSegConstruction()
{
constructions.push_back(this);
}
KSegConstruction::~KSegConstruction()
{
constructions.erase(std::find(constructions.begin(),
constructions.end(), this));
}
void KSegConstruction::addRef(G_ref *ref)
{
KSegDocument::addRef(ref);
//givens will be added into the right place by themselves
if(ref->getFinal()) final.append(ref);
if(ref->getInitial()) initial.append(ref);
if(ref->getType() == G_LOOP) loops.append(ref);
}
void KSegConstruction::delRef(G_ref *ref)
{
KSegDocument::delRef(ref);
if(ref->getGiven()) given.removeRef(ref);
if(ref->getFinal()) final.removeRef(ref);
if(ref->getInitial()) initial.removeRef(ref);
if(ref->getType() == G_LOOP) loops.removeRef(ref);
}
void KSegConstruction::play(G_refs givens, KSegDocument *doc, int iter, bool first)
{
static hash_map<G_ref *, G_ref *> initialMap; //map of the initial objects
static NotInConstructionSearcher nicSearcher;
static ImplicitInitialSearcher iiSearcher;
if(first) {
initialMap.clear();
nicSearcher.reInit();
iiSearcher.reInit();
}
ViewTransform trans = KSegView::constructionTransform;
ViewTransform invTrans(-trans.offsetX * trans.zoom, -trans.offsetY * trans.zoom, 1. / trans.zoom);
hash_map<G_ref *, G_ref *> currentMap = initialMap;
currentMap.resize(allRefs.size());
int i, j;
//match the givens:
vector<int> givenPerm;
bool tmp = match(givens, given, givenPerm);
Q_ASSERT(tmp);
for(i = 0; i < (int)given.count(); i++) {
currentMap[given[i]] = givens[givenPerm[i]];
}
QRect win = KSegView::constructionPainter->window();
if(win.width() > 100 && win.height() > 100) {
win.setSize(QSize(win.width() - 60, win.height() - 60));
win.moveBy(30, 30);
}
//main loop over all the refs in this construction
for(i = 0; i < (int)allRefs.count(); i++) {
//if loop
if(allRefs[i]->getType() == G_LOOP) {
if(iter) {
G_refs newGivens;
for(j = 0; j < int(allRefs[i]->getParents().count()); j++) {
newGivens.append(currentMap[allRefs[i]->getParents()[j]]);
}
play(newGivens, doc, iter - 1, false);
currentMap.insert(initialMap.begin(), initialMap.end());
}
continue;
}
//should it be constructed?
if(allRefs[i]->getGiven()) continue;
if(allRefs[i]->getFinal() && iter != 0) continue;
if(currentMap.count(allRefs[i]) != 0) continue;
if(nicSearcher.search(allRefs[i]) == false) continue;
//construct it!
G_refs newParents;
//set up the parents
bool construct = true; //will be marked false if any parents are missing
// like because they are final
for(j = 0; j < int(allRefs[i]->getParents().count()); j++) {
if(currentMap.count(allRefs[i]->getParents()[j]) == 0) {
construct = false;
break;
}
newParents.append(currentMap[allRefs[i]->getParents()[j]]);
}
if(!construct) continue;
G_ref *r = new G_ref();
//creation!
//check if it's a transform becuase then the type depends on the parent:
if((allRefs[i]->getType() & G_CURVE) && IS_TRANSFORM(allRefs[i]->getDescendType())) {
r->create(newParents[0]->getType(), allRefs[i]->getDescendType(), newParents, doc, false);
}
else { //normal creation
r->create(allRefs[i]->getType(), allRefs[i]->getDescendType(), newParents, doc, false);
}
if(r->getType() == G_CALCULATE) { //we must transfer the formula string
((G_calculateObject *)(r->getObject()))->
setInitialFormulaString(((G_calculateObject *)(allRefs[i]->getObject()))->getFormulaString());
r->update();
}
if(r->getType() == G_LOCUS) { //we need to keep the number of samples the same
((G_locusObject *)(r->getObject()))->
setMaxSamples(((G_locusObject *)(allRefs[i]->getObject()))->getMaxSamples());
}
//set the right draw style and attributes
allRefs[i]->getDrawstyle()->addReference();
r->setDrawstyle(allRefs[i]->getDrawstyle());
r->setVisible(allRefs[i]->getVisible());
r->setLabelVisible(allRefs[i]->getLabelVisible());
r->getLabel().setText(allRefs[i]->getLabel().getText(), false);
r->setLabelPos(allRefs[i]->getLabelPos());
//randomize positions of points and measurements:
if(r->getType() == G_POINT) {
if(r->getDescendType() == G_CONSTRAINED_POINT) {
((G_pointObject *)(r->getObject()))->setP(INTRAND(0, 1000) / 1000.);
r->update();
}
if(r->getDescendType() == G_FREE_POINT) {
r->getObject()->translate(G_point(INTRAND(win.left(), win.right()),
INTRAND(win.top(), win.bottom())));
}
}
if(r->getType() & G_TEXT) {
r->getObject()->translate(G_point(INTRAND(win.left(), win.right()),
INTRAND(win.top(), win.bottom())));
}
if(r->isDrawn()) {
r->getObject()->tempTransform(trans);
r->getObject()->draw(*(KSegView::constructionPainter));
r->getObject()->tempTransform(invTrans);
}
//put it in the maps.
currentMap[allRefs[i]] = r;
if(allRefs[i]->getInitial() || iiSearcher.search(allRefs[i]) == false) {
initialMap[allRefs[i]] = r;
}
}
if(first) { //conserve memory
initialMap.clear();
nicSearcher.reInit();
}
}
bool KSegConstruction::doesMatch(const G_refs& which)
{
vector<int> v; //dummy
return match(which, given, v);
}
bool KSegConstruction::canMakeNormal(G_ref *which)
{
if(which->getType() == G_LOOP) return false;
if(which->getNormal() || which->getFinal() || which->getInitial()) return true;
//otherwise the ref is a given. Check if there are loops in the document
if(loops.size() > 0) return false;
return true;
}
bool KSegConstruction::canMakeGiven(G_ref *which)
{
if(which->getType() == G_LOOP) return false;
if(which->getGiven()) return true;
//if there are loops, return false
if(loops.size() > 0) return false;
int i;
//if it's the driven point of a locus, can't make it given
for(i = 0; i < int(which->getChildrenConst().size()); ++i) {
if(which->getChildrenConst()[i]->getType() != G_LOCUS) continue;
if(which->getChildrenConst()[i]->getDescendType() != G_OBJECT_LOCUS) continue;
if(which->getChildrenConst()[i]->getParentsConst()[1] != which) continue;
return false;
}
//now check if making it given would disrupt a locus chain
for(i = 0; i < (int)allRefs.count(); ++i) {
if(allRefs[i]->getType() != G_LOCUS ||
allRefs[i]->getDescendType() != G_OBJECT_LOCUS) continue;
BrokenLocusChainSearcher blcs(allRefs[i]->getParentsConst()[1], which);
if(blcs.search(allRefs[i]->getParentsConst()[0]) == false) return false;
}
return true;
}
bool KSegConstruction::canMakeFinal(G_ref * /*which*/)
{
return true;
}
bool KSegConstruction::canMakeInitial(G_ref * /*which*/)
{
return true;
}
void KSegConstruction::makeNormal(G_ref *which)
{
if(which->getNormal()) return;
//insert undo here
addUndo(new G_undoChangeConstructMode(which));
if(which->getGiven()) given.removeRef(which);
if(which->getFinal()) final.removeRef(which);
if(which->getInitial()) initial.removeRef(which);
which->setNormal();
}
void KSegConstruction::makeGiven(G_ref *which, int where)
{
if(which->getGiven()) return;
//insert undo here
addUndo(new G_undoChangeConstructMode(which));
if(which->getFinal()) final.removeRef(which);
if(which->getInitial()) initial.removeRef(which);
which->setGiven();
if(where == -1) given.append(which);
else given.insert(where, which);
}
void KSegConstruction::makeFinal(G_ref *which)
{
if(which->getFinal()) return;
//insert undo here
addUndo(new G_undoChangeConstructMode(which));
if(which->getGiven()) given.removeRef(which);
if(which->getInitial()) initial.removeRef(which);
which->setFinal();
final.append(which);
}
void KSegConstruction::makeInitial(G_ref *which)
{
if(which->getInitial()) return;
//insert undo here
addUndo(new G_undoChangeConstructMode(which));
if(which->getGiven()) given.removeRef(which);
if(which->getFinal()) final.removeRef(which);
which->setInitial();
initial.append(which);
}
ScanType KSegConstruction::scanConstructionFile(QFile &f, const G_refs &selected)
{
vector<int> givens;
//check to make sure the file won't crash us.
if(!f.open(QIODevice::ReadOnly) || f.size() < 5)
return INVALID;
char block[4];
f.readBlock(block, 4);
if(block[0] || block[1] || block[2] || block[3] + 5 > (int)f.size()) {
f.close();
return INVALID;
}
f.ungetch(block[3]);
f.ungetch(block[2]);
f.ungetch(block[1]);
f.ungetch(block[0]);
QDataStream stream(&f);
QString tmp;
stream >> tmp;
int majVer, minVer;
bool isConstr, ok;
ok = KSegDocument::parseVersionString(tmp, &isConstr, &majVer, &minVer);
if(!ok || !isConstr || (majVer == 0 && minVer == 0)) { //constructions version 0.0 have no given info at start
f.close();
return INVALID;
}
int num;
stream >> num;
while(num--) {
int tmp;
stream >> tmp;
givens.push_back(tmp);
}
vector<int> v; //dummy
if(match(selected, givens, v))
return MATCHED;
else return UNMATCHED;
}
//find a permutation of sel which matches given.
//set perm to that permutation or return false
//if it doesn't exist.
bool KSegConstruction::match(const G_refs& sel, const vector<int>& given, vector<int> &perm) {
if(sel.size() != given.size()) return false;
vector<int> revperm(sel.size());
perm.resize(sel.size());
int i, j, k;
for(i = 0; i < (int)sel.size(); ++i) {
perm[i] = -1;
revperm[i] = -1;
}
int unmatched = 0;
//start with a simple greedy match pass
for(i = 0; i < (int)sel.size(); ++i) {
bool atLeastOne = false;
for(j = 0; j < (int)sel.size(); ++j) {
if((sel[j]->getType() & given[i])) {
atLeastOne = true;
if(revperm[j] != -1) continue;
perm[i] = j;
revperm[j] = i;
break;
}
}
if(!atLeastOne) return false; //there was something that doesn't fit to anything
if(j == (int)sel.size()) unmatched++; //if reached end of loop, didn't match
}
if(unmatched == 0) return true;
//length 3 augmenting path search
for(i = 0; i < (int)sel.size(); ++i) {
if(perm[i] != -1) continue;
for(j = 0; j < (int)sel.size(); ++j) {
if(!(sel[j]->getType() & given[i])) continue;
Q_ASSERT(revperm[j] != -1);
int x = revperm[j];
Q_ASSERT(x != i);
//try to match it with something
for(k = 0; k < (int)sel.size(); ++k) {
if(j == k || revperm[k] != -1) continue;
if(!(sel[k]->getType() & given[x])) continue;
//we have a match:
perm[i] = j;
revperm[j] = i;
perm[x] = k;
revperm[k] = x;
unmatched--;
if(unmatched == 0) return true;
k = j = sel.size(); //this breaks out of the loops
i = -1; //reset the outer loop
}
}
}
return unmatched == 0;
}
bool KSegConstruction::match(const G_refs& sel, const G_refs &given, vector<int> &perm) {
vector<int> g;
int i;
for(i = 0; i < (int)given.size(); ++i) {
g.push_back(given[i]->whatCanItBe());
}
return match(sel, g, perm);
}