-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMondrian2dsubdivisiongenerator.py
More file actions
1139 lines (1112 loc) · 42.1 KB
/
Mondrian2dsubdivisiongenerator.py
File metadata and controls
1139 lines (1112 loc) · 42.1 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
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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#Mondrian like subdivision generator
##import bpy
import random
global dimx
global dimy
dimx = 1000
dimy = 1300
totalnodes = 10
meshName = "Mondrian"
obName = "MondrianObj"
me = bpy.data.meshes.new(meshName)
ob = bpy.data.objects.new(obName, me)
ob.location = bpy.context.scene.cursor_location
bpy.context.scene.objects.link(ob)
##track edges on mondrian generator and vertices
nodes = {}
nodepostoi = {}
randomxlist = []
randomylist = []
def getrand(minv, maxv, randlist):
val = random.randint(minv,maxv)
if val not in randlist:
return val
else:
return getrand(minv,maxv,randlist)
for i in range(0,totalnodes):
attr = {}
randposx = getrand(1,dimx-1,randomxlist)
randposy = getrand(1,dimy-1,randomylist)
## randposx = random.randint(0,dimx)
## randposy = random.randint(0,dimy)
attr['position'] = (randposx,randposy)
attr['left'] = None
attr['right'] = None
attr['up'] = None
attr['down'] = None
attr['crossing'] = None
randomxlist.append(randposx)
randomylist.append(randposy)
nodes[i+1] = attr
nodepostoi[(randposx,randposy)] = i+1
nodeposlist = list(nodepostoi.keys())
#node ordering
nodexrank = nodeposlist[0:len(nodeposlist)]
nodeyrank = nodeposlist[0:len(nodeposlist)]
nodexrank.sort(key=lambda tup: tup[0])
nodeyrank.sort(key=lambda tup: tup[1])
#nodes keyed by ypos
ypnodes = {}
for node in nodes:
ypnodes[nodes[node]['position'][1]] = node
#nodes keyed by xpos
xpnodes = {}
for node in nodes:
xpnodes[nodes[node]['position'][0]] = node
#finished keying nodes by x and y pos
nodexirank = []
for pos in nodexrank:
nodexirank.append(nodepostoi[pos])
nodeyirank = []
for pos in nodeyrank:
nodeyirank.append(nodepostoi[pos])
##set node crossings
crossingnodes = {}
crossingnodesrev = {}
factor = 10
if len(nodeyirank) > 9:
factor = 100
elif len(nodeyirank) > 99:
factor = 1000
for node in nodes:
yrank = nodeyirank.index(node)
posx = nodes[node]['position'][0]
copynranks = nodeyirank[0:len(nodeyirank)]
del copynranks[yrank]
yrank += 1
for cr in range(0,totalnodes-1):
attr = {}
nodeval = yrank*factor+copynranks[cr]
##print(yrank)
##print("nodeval: ", nodeval)
posy = nodes[copynranks[cr]]['position'][1]
attr['position'] = (posx,posy)
attr['left'] = None
attr['right'] = None
nodevald = None
crnodepos = nodeyirank.index(copynranks[cr])
if crnodepos > 0:
if nodeyirank[crnodepos-1] == nodeyirank[yrank-1]:
nodevald = nodeyirank[yrank-1]
nodes[node]['up'] = nodeval##copynranks[cr]
else:
nodevald = yrank*factor + nodeyirank[crnodepos-1]
attr['down'] = nodevald
nodevald = None
if crnodepos < len(nodeyirank)-1:
if nodeyirank[crnodepos+1] == nodeyirank[yrank-1]:
nodevald = nodeyirank[yrank-1]
nodes[node]['down'] = nodeval ##copynranks[cr]
else:
nodevald = yrank*factor + nodeyirank[crnodepos+1]
attr['up'] = nodevald
crossingnodes[nodeval] = attr
crossingnodesrev[attr['position']] = nodeval
#2nd pass working on nodexirank indexing
for node in nodes:
xrank = nodexirank.index(node)
posy = nodes[node]['position'][1]
copynranks = nodexirank[0:len(nodexirank)]
del copynranks[xrank]
for cr in range(0,totalnodes-1):
posx = nodes[copynranks[cr]]['position'][0]
nodeval = crossingnodesrev[(posx,posy)]
attr = crossingnodes[nodeval]
nodevald = None
crnodepos = nodexirank.index(copynranks[cr])
if crnodepos > 0:
if nodexirank[crnodepos-1] == nodexirank[xrank]:
nodevald = nodexirank[xrank]
nodes[node]['right'] = nodeval##copynranks[cr]
else:
nposx = nodes[nodexirank[crnodepos-1]]['position'][0]
nodevald = crossingnodesrev[(nposx,posy)]
attr['left'] = nodevald
nodevald = None
if crnodepos < len(nodexirank)-1:
if nodexirank[crnodepos+1] == nodexirank[xrank]:
nodevald = nodexirank[xrank]
nodes[node]['left'] = nodeval ##copynranks[cr]
else:
nposx = nodes[nodexirank[crnodepos+1]]['position'][0]
####print(crossingnodesrev)
nodevald = crossingnodesrev[(nposx,posy)]
attr['right'] = nodevald
crossingnodes[nodeval] = attr
## randomize crossings
## crossing = 1 vertical crossing = 0 lateral
##print("nodes: ", nodes)
for node in crossingnodes:
randval = random.random()
if randval >= .5:
crossingnodes[node]['crossing'] = 1
else:
crossingnodes[node]['crossing'] = 0
##build boundary nodes
xboundaries = [0,dimx]
yboundaries = [0,dimy]
boundarydict = {}
boundarydictrev = {}
i = -1
checki = 0
for xboundary in xboundaries:
attr = {}
attr['left'] = None
attr['right'] = None
attr['up'] = None
attr['down'] = None
attr['crossing'] = None
ypos = 0
attr['position'] = (xboundary, ypos)
if xboundary != 0:
xpos = nodes[nodexirank[len(nodexirank)-1]]['position'][0]
##print('xpos: ',xpos)
bnnode = boundarydictrev[(xpos,ypos)]
##print('bnnode', bnnode)
attr['left'] = bnnode
boundarydict[bnnode]['right'] = i
boundarydictrev[(xboundary,ypos)] = i
boundarydict[i] = attr
prevnode = i
i -= 1
if i == -2:
nprevnode = prevnode
for node in nodexirank:
attr = {}
attr['left'] = None
attr['right'] = None
attr['up'] = None
attr['down'] = None
attr['crossing'] = None
xpos = nodes[node]['position'][0]
yminpos = nodes[nodeyirank[0]]['position'][1]
coord = (xpos,yminpos)
upnode = None
if coord in crossingnodesrev:
upnode = crossingnodesrev[coord]
if coord in nodepostoi:
upnode = nodepostoi[coord]
attr['up'] = upnode
attr['position'] = (xpos,0)
attr['left'] = nprevnode
boundarydict[nprevnode]['right'] = i
boundarydictrev[(xpos,0)] = i
boundarydict[i] = attr
nprevnode = i
i -= 1
for node in nodeyirank:
attr = {}
attr['left'] = None
attr['right'] = None
attr['up'] = None
attr['down'] = None
attr['crossing'] = None
ypos = nodes[node]['position'][1]
if checki == 0:
xminpos = nodes[nodexirank[0]]['position'][0]
coord = (xminpos, ypos)
rightnode = None
if coord in crossingnodesrev:
rightnode = crossingnodesrev[coord]
if coord in nodepostoi:
rightnode = nodepostoi[coord]
attr['right'] = rightnode
else:
xmaxpos = nodes[nodexirank[len(nodexirank)-1]]['position'][0]
coord = (xmaxpos, ypos)
leftnode = None
if coord in crossingnodesrev:
leftnode = crossingnodesrev[coord]
if coord in nodepostoi:
leftnode = nodepostoi[coord]
attr['left'] = leftnode
attr['position'] = (xboundary,ypos)
attr['down'] = prevnode
boundarydict[prevnode]['up'] = i
boundarydictrev[(xboundary,ypos)] = i
boundarydict[i] = attr
prevnode = i
i -= 1
attr = {}
attr['left'] = None
attr['right'] = None
attr['up'] = None
attr['down'] = None
attr['crossing'] = None
ypos = dimy
attr['position'] = (xboundary, ypos)
attr['down'] = prevnode
if xboundary != 0:
xpos = nodes[nodexirank[len(nodexirank)-1]]['position'][0]
bnnode = boundarydictrev[(xpos,ypos)]
attr['left'] = bnnode
boundarydict[bnnode]['right'] = i
boundarydictrev[(xboundary,ypos)] = i
boundarydict[i] = attr
if checki == 0:
nprevnode = i
i -= 1
for node in nodexirank:
attr = {}
attr['left'] = None
attr['right'] = None
attr['up'] = None
attr['down'] = None
attr['crossing'] = None
xpos = nodes[node]['position'][0]
ymaxpos = nodes[nodeyirank[len(nodeyirank)-1]]['position'][1]
coord = (xpos,ymaxpos)
downnode = None
if coord in crossingnodesrev:
downnode = crossingnodesrev[coord]
if coord in nodepostoi:
downnode = nodepostoi[coord]
attr['down'] = downnode
attr['position'] = (xpos,dimy)
attr['left'] = nprevnode
boundarydict[nprevnode]['right'] = i
boundarydictrev[(xpos,dimy)] = i
boundarydict[i] = attr
nprevnode = i
i -= 1
checki = i
## finished building boundary nodes
## compile all nodes into a position lookup dictionary
completerefrev = {}
for pos in nodepostoi:
completerefrev[pos] = nodepostoi[pos]
for pos in crossingnodesrev:
completerefrev[pos] = crossingnodesrev[pos]
for pos in boundarydictrev:
completerefrev[pos] = boundarydictrev[pos]
## finished compiling all nodes into a complete position lookup
##compile all nodes in a lookup directory
completeref = {}
for node in nodes:
completeref[node] = nodes[node]
for node in crossingnodes:
completeref[node] = crossingnodes[node]
for node in boundarydict:
completeref[node] = boundarydict[node]
##finished compiling all nodes
##reindex nodes to vertex list and position to index vert index
##a bit goofy doing this now, but saves work, and I don't feel like
##re writing the above.
vertices = []
vertpostoindex = {}
i = 0
for nodepos in completerefrev:
##vertices.append(nodepos)
npx, npy = nodepos
vertices.append((float(npx)/dimy,float(npy)/dimy,0.0))
vertpostoindex[nodepos] = i
i += 1
##finished reindexing vertices
##pass to complete connections between boundary and non boundary nodes
##one way passes have been completed above, but I hadn't ensured
##that bijection relations were fully done.
for node in boundarydict:
if completeref[node]['up'] != None:
neignode = completeref[node]['up']
if completeref[neignode]['down'] == None:
completeref[neignode]['down'] = node
boundarydict[node]['crossing'] = completeref[neignode]['crossing']
completeref[node]['crossing'] = completeref[neignode]['crossing']
if neignode in crossingnodes:
crossingnodes[neignode]['down'] = node
if neignode in nodes:
nodes[neignode]['down'] = node
if completeref[node]['down'] != None:
neignode = completeref[node]['down']
if completeref[neignode]['up'] == None:
completeref[neignode]['up'] = node
boundarydict[node]['crossing'] = completeref[neignode]['crossing']
completeref[node]['crossing'] = completeref[neignode]['crossing']
if neignode in crossingnodes:
crossingnodes[neignode]['up'] = node
if neignode in nodes:
nodes[neignode]['up'] = node
if completeref[node]['left'] != None:
neignode = completeref[node]['left']
if completeref[neignode]['right'] == None:
completeref[neignode]['right'] = node
boundarydict[node]['crossing'] = completeref[neignode]['crossing']
completeref[node]['crossing'] = completeref[neignode]['crossing']
if neignode in crossingnodes:
crossingnodes[neignode]['right'] = node
if neignode in nodes:
nodes[neignode]['right'] = node
if completeref[node]['right'] != None:
neignode = completeref[node]['right']
if completeref[neignode]['left'] == None:
completeref[neignode]['left'] = node
boundarydict[node]['crossing'] = completeref[neignode]['crossing']
completeref[node]['crossing'] = completeref[neignode]['crossing']
if neignode in crossingnodes:
crossingnodes[neignode]['left'] = node
if neignode in nodes:
nodes[neignode]['left'] = node
## finished completion on boundary pass
##indexing simple polygons over the completeref set
stopcheck = False
startposition = (0,0)
currentnode = completerefrev[startposition]
currentposition = startposition
nextrowval = completeref[currentnode]['right']
polynodesrev = {}
polynodes = {}
i = 0
while not stopcheck:
columnstepcheck = False
while not columnstepcheck:
####print(completeref[currentnode]['position'])
polypos = []##[completeref[currentnode]['position']]
for j in range(0,4):
currentposition = completeref[currentnode]['position']
if j == 0:
nextpos = completeref[currentnode]['up']
if nextpos == None:
columnstepcheck = True
break
elif j == 1:
nextpos = completeref[currentnode]['right']
if nextpos == None:
columnstepcheck = True
stopcheck = True
break
elif j == 2:
nextpos = completeref[currentnode]['down']
elif j == 3:
nextpos = completeref[currentnode]['left']
if nextpos == None:
print('missed assignment: ', currentnode)
nextpos = completeref[nextpos]['up']
##if j != 3:
polypos.append(currentposition)
currentnode = nextpos
if columnstepcheck:
continue
polynodesrev[tuple(polypos)] = i
polynodes[i] = tuple(polypos)
i += 1
if stopcheck:
continue
currentnode = nextrowval
nextrowval = completeref[currentnode]['right']
##finished indexing simple polygons
##recursive function to check crossing
def checkcross(crossval, node, crossingnodes, dirswitch, crosslist):
check = False
if crossval:
if dirswitch:
topnode = crossingnodes[node]['up']
## if topnode in nopasscrosslist:
## return crosslist
crosslist.append(topnode)
if topnode in crossingnodes:
ncrossval = crossingnodes[topnode]['crossing']
crossingnone = ncrossval == None ##node setting
if ncrossval and not crossingnone:
return checkcross(crossval,topnode,crossingnodes,dirswitch,
crosslist)
else:
return crosslist
else:
return crosslist
else:
downnode = crossingnodes[node]['down']
## if downnode in nopasscrosslist:
## return crosslist
crosslist.append(downnode)
if downnode in crossingnodes:
ncrossval = crossingnodes[downnode]['crossing']
crossingnone = ncrossval == None ##node setting
if ncrossval and not crossingnone:
return checkcross(crossval,downnode,crossingnodes,dirswitch,
crosslist)
else:
return crosslist
else:
return crosslist
else:
if dirswitch:
rightnode = crossingnodes[node]['right']
## if rightnode in nopasscrosslist:
## return crosslist
crosslist.append(rightnode)
if rightnode in crossingnodes:
ncrossval = crossingnodes[rightnode]['crossing']
crossingnone = ncrossval == None ##node setting
if not ncrossval and not crossingnone:
return checkcross(crossval,rightnode,crossingnodes,dirswitch,
crosslist)
else:
return crosslist
else:
return crosslist
else:
leftnode = crossingnodes[node]['left']
## if leftnode in nopasscrosslist:
## return crosslist
crosslist.append(leftnode)
if leftnode in crossingnodes:
ncrossval = crossingnodes[leftnode]['crossing']
crossingnone = ncrossval == None ##node setting
if not ncrossval and not crossingnone:
return checkcross(crossval,leftnode,crossingnodes,dirswitch,
crosslist)
else:
return crosslist
else:
return crosslist
##to check crossing nodes list, if a node is found it marked
## marked at the crossing node in the given direction.
## this is used in identifying the vertices for face construction
def checkneighbornode(crosslist, crossingnodes, nodes, direction):
cdirection = None
if direction == 'up':
cdirection = 'right'
elif direction == 'left':
cdirection = 'down'
elif direction == 'down':
cdirection = 'right'
elif direction == 'right':
cdirection = 'up'
i = 0
for cnode in crosslist:
neighnode = crossingnodes[cnode][cdirection]
if neighnode in nodes:
return neighnode,i
i += 1
return None, i
##function to check within face subdivision area
def nodecolumncheck(minpos, maxpos, pnodes, crosslist, direction,
completeref, selfnode):
check = True
eqcheck = False
i = 0
##print('crosslist nodecolumncheck: ', crosslist)
##print('minpos: ', minpos)
##print('maxpos: ', maxpos)
for node in crosslist:
pos = completeref[node]['position']
xpost = (pos[0] == 0) or (pos[0] == dimx)
ypost = (pos[1] == 0) or (pos[1] == dimy)
if xpost and ypost:
continue
npos = None
if direction:
ypos = pos[1]
if ypost:
continue
nodepos = pnodes[ypos]
if nodepos == selfnode:
continue
npos = nodes[nodepos]['position'][0]
else:
xpos = pos[0]
if xpost:
continue
nodepos = pnodes[xpos]
if nodepos == selfnode:
continue
npos = nodes[nodepos]['position'][1]
check1 = npos >= minpos
check2 = npos <= maxpos
check3 = npos == minpos
check4 = npos == maxpos
if check1 and check2:
check = False
if check3 or check4:
eqcheck = True
if not check:
break
i += 1
return check, eqcheck, i
##function used
def buildnodesedge(axispos, crosslist, completeref, direction):
rcrosslist = []
for node in crosslist:
pos = (None,None)
posx = None
posy = None
if direction:
posx = completeref[node]['position'][0]
posy = axispos
else:
posy = completeref[node]['position'][1]
posx = axispos
pos = (posx, posy)
rcrosslist.append(pos)
return rcrosslist
def updatecrossings(completeref, rcrossinglist, pcrossinglist):
if len(rcrossinglist) >= 3:
i = 0
for node in pcrossinglist:
if (i != 0) and (i != len(pcrossinglist)-1):
rnode = rcrossinglist[i]
crossingval = completeref[rnode]['crossing']
completeref[node]['crossing'] = crossingval
i += 1
return completeref
##function to build simple polys for intersection testing on a given
##aggregate face
def buildfacesimplepolys(minpos, maxpos, polynodesrev, completerefrev,
completeref):
stopcheck = False
startposition = minpos
maxposx = maxpos[0]
maxposy = maxpos[1]
currentnode = completerefrev[startposition]
currentposition = startposition
nextrowval = completeref[currentnode]['right']
polynodeslist = []
postopolynodes = {}
##print('buildfacesimplepolys minpos: ', minpos)
##print('buildfacesimplepolys maxpos: ', maxpos)
##print('nextrowval start: ',nextrowval)
while not stopcheck:
columnstepcheck = False
while not columnstepcheck:
polypos = []##[completeref[currentnode]['position']]
for j in range(0,4):
currentposition = completeref[currentnode]['position']
if j == 0:
nextpos = completeref[currentnode]['up']
##print('j==0 nextpos: ', nextpos)
nextposy = None
if nextpos != None:
nextposy = completeref[nextpos]['position'][1]
t2 = nextpos == None
if t2:
columnstepcheck = True
if currentposition[0] >= maxposx:
stopcheck = True
break
if nextposy > maxposy:
columnstepcheck = True
if currentposition[0] >= maxposx:
stopcheck = True
break
elif j == 1:
nextpos = completeref[currentnode]['right']
nextposx = None
if nextpos != None:
nextposx = completeref[nextpos]['position'][0]
t2 = nextpos == None
if t2:
columnstepcheck = True
stopcheck = True
break
if nextposx > maxposx:
columnstepcheck = True
stopcheck = True
break
elif j == 2:
nextpos = completeref[currentnode]['down']
elif j == 3:
nextpos = completeref[currentnode]['left']
nextpos = completeref[nextpos]['up']
##if j != 3:
polypos.append(currentposition)
currentnode = nextpos
if columnstepcheck:
continue
polynodeslist.append(polypos)
for pos in polypos:
if pos in postopolynodes:
postopolynodes[pos].append(polypos)
else:
postopolynodes[pos] = [polypos]
if stopcheck:
continue
##print('polynodeslist: ',polynodeslist)
currentnode = nextrowval
##print('currentnode: ', currentnode)
nextrowval = completeref[currentnode]['right']
return polynodeslist, postopolynodes
## end function simple polynode build
##function to reaggregate simple polys returns 4 nodes coordinates
def rebuildfacesimplepolys(rnodepos, rnodepoly, passlist, spostopolys,
completeref, completerefrev):
rnode = completerefrev[rnodepos]
directions = ['up','down','left','right']
orthogdirections = []
rnodeposneighbors = []
nnodepositiontoindex = {}
i = 0
##assumed that the position indexing for a neighbor node
## is always mirror symmetric relative to the previous iteration
## for a given direction
for direction in directions:
nnode = completeref[rnode][direction]
nnodepos = None
if nnode != None:
nnodepos = completeref[nnode]['position']
if nnodepos in rnodepoly[0]:
orthogdirections.append(direction)
rnodeposneighbors.append(nnodepos)
nnodepositiontoindex[nnodepos] = rnodepoly[0].index(nnodepos)
i += 1
crosslists = []
##print('rnodeposneighbors: ', rnodeposneighbors)
##print('rnodepoly: ', rnodepoly)
##print('spostopolys: ', spostopolys)
##print('passlist: ', passlist)
for nnodepos in rnodeposneighbors:
nextindex = nnodepositiontoindex[nnodepos]
##print('nextindex: ', nextindex)
inpasslist = True
neighbornodepos = nnodepos
processedpolys = rnodepoly
procpositions = [rnodepos,neighbornodepos]
prevneighnodepos = neighbornodepos
i = 0
while inpasslist and i < 100:
polyslist = spostopolys[neighbornodepos]
for poly in polyslist:
t1 = poly not in processedpolys
t2 = poly in passlist
if t1 and t2:
##print('processedpoly add: ', poly)
processedpolys.append(poly)
neighbornodepos = poly[nextindex]
procpositions.append(neighbornodepos)
elif t1 and not t2:
inpasslist = False
if prevneighnodepos == neighbornodepos:
inpasslist = False
prevneighnodepos = neighbornodepos
i += 1
crosslists.append(procpositions)
##print ("Crosslists: ", crosslists)
list1 = crosslists[0]
xdist = abs(list1[0][0] -list1[len(list1)-1][0])
zerox = False
if xdist == 0:
zerox = True
crosslist1 = crosslists[0]
crosslist2 = crosslists[1]
node2pos = None
if zerox:
node2pos = (crosslist2[len(crosslist2)-1][0],
crosslist1[len(crosslist1)-1][1])
else:
node2pos = (crosslist1[len(crosslist1)-1][0],
crosslist2[len(crosslist2)-1][1])
##print('crosslist1[0]: ', crosslist1[0])
##print('node2pos',node2pos)
vertposlist = [crosslist1[0],crosslist1[len(crosslist1)-1],
node2pos, crosslist2[len(crosslist2)-1]]
## in order to ensure proper keying on this poly we might want to
## to ensure that the face is ordered similarly to the simple
## poly method where minx, miny is the starting vertex, and
## maxx,maxy vert is in the 3rd vertex position, thus I sort
## the vertices first by x positions from smallest to largest,
##and then different x groups (remember two x's are the same on the
##rectangle for each group), sorting by the y's. I reverse the
##the sort order on the 2nd group to ensure the maxx, maxy is
##in the 1rst order of the 2nd group or when lists are concatenated
##in the 3rd position.
vertposlist.sort(key=lambda tup: tup[0])
vertpos1, vertpos2, vertpos3, vertpos4 = vertposlist
vertposlist1 = [vertpos1,vertpos2]
vertposlist2 = [vertpos3,vertpos4]
vertposlist1.sort(key=lambda tup: tup[1])
vertposlist2.sort(key=lambda tup: tup[1], reverse = True)
vertposlist = vertposlist1 + vertposlist2
return tuple(vertposlist)
## construct vertices faces crossings
facecnt = 0
vertcnt = 0
##rules for crossing
##Node have crossing precedent.
##An edge starting from a crossingnode, does not
##have an edge terminating on a non passing neighbor
##crossing node, but may pass into a crossing node
##if positive for crossing, or in other words, all edges
##starting from a crossing node may not have a 4 direction
##radial pattern but instead contingent on the crossing
##positives assigned.
##Crossingnodes and nodes alike have boundary clearance
##direction leeway. Meaning we can draw a subdivision edge
##in the direction of the nearest boundary, as long as such
##boundary is on a neighboring.
##All edges starting from a node have 4 direction
##radial pattern.
i = 0
faces = []
nopasscrosslist = []
for node in nodes:
#top crossval = 1, dirswitch = 1
switches1 = [0,1]
switches2 = [0,1]
for switch1 in switches1:
for switch2 in switches2:
crosslist = [node]
crosslist = checkcross(switch1, node, completeref,
switch2, crosslist)
##print('crosslist',crosslist)
crossendnode = crosslist[len(crosslist)-1]
if crossendnode == None:
del crosslist[len(crosslist)-1]
if len(crosslist) <= 1:
continue
if switch1 and switch2:
direction = 'up'
elif switch1 and not switch2:
direction = 'down'
elif not switch1 and switch2:
direction = 'right'
else:
direction = 'left'
ncheck, nodeindex = checkneighbornode(crosslist, completeref,
nodes, direction)
if ncheck != None:
crosslist = crosslist[0:nodeindex+1]
if switch1 and switch2:
maxpos = completeref[crosslist[len(crosslist)-1]]['position'][1]
minpos = completeref[crosslist[0]]['position'][1]
elif switch1 and not switch2:
minpos = completeref[crosslist[len(crosslist)-1]]['position'][1]
maxpos = completeref[crosslist[0]]['position'][1]
elif not switch1 and switch2:
maxpos = completeref[crosslist[len(crosslist)-1]]['position'][0]
minpos = completeref[crosslist[0]]['position'][0]
else:
minpos = completeref[crosslist[len(crosslist)-1]]['position'][0]
maxpos = completeref[crosslist[0]]['position'][0]
switch3 = None
switch4 = None
direction3 = None
direction4 = None
if direction == 'up':
#right
switch3 = False
switch4 = True
direction3 = 0
direction4 = 1
elif direction == 'right':
#down
switch3 = True
swithc4 = False
direction3 = 1
direction4 = 0
elif direction == 'down':
#left
switch3 = False
switch4 = False
direction3 = 0
direction4 = 1
elif direction == 'left':
#up
switch3 = True
switch4 = True
direction3 = 1
direction4 = 0
pnodes = None
if not direction3:
pnodes = xpnodes
else:
pnodes = ypnodes
cnode = crosslist[len(crosslist)-1]
crosslist2 = [cnode]
crosslist2 = checkcross(switch3, cnode, completeref,
switch4, crosslist2)
crossendnode = crosslist2[len(crosslist2)-1]
if crossendnode == None:
del crosslist2[len(crosslist2)-1]
##print('crosslist2: ', crosslist2)
##print('pnodes: ',pnodes)
ncolchk, eqcheck, c2nindex = nodecolumncheck(minpos, maxpos,
pnodes, crosslist2,
direction3, completeref,
node)
c2index = None
if not ncolchk:
if not eqcheck:
c2index = c2nindex
else:
c2index = c2nindex+1
else:
c2index = len(crosslist2)
crosslist2 = crosslist2[0:c2index]
if len(crosslist2) <= 1:
continue
##print('crosslist2: ', crosslist2)
cedgepos3 = None
cedgepos4 = None
if direction4:
cedgepos3 = completeref[crosslist2[len(crosslist2)-1]]['position'][0]
cedgepos4 = completeref[crosslist[0]]['position'][1]
else:
cedgepos3 = completeref[crosslist2[len(crosslist2)-1]]['position'][1]
cedgepos4 = completeref[crosslist[0]]['position'][0]
crosslist3 = buildnodesedge(cedgepos3, crosslist, completeref, direction4)
crosslist4 = buildnodesedge(cedgepos4, crosslist2, completeref, direction3)
allnodescrosslist = crosslist+crosslist2+crosslist3+crosslist4
completeref = updatecrossings(completeref, crosslist, crosslist3)
completeref = updatecrossings(completeref, crosslist2, crosslist4)
if direction == 'up':
maxpos = completeref[crosslist2[len(crosslist2)-1]]['position']
minpos = completeref[crosslist[0]]['position']
elif direction == 'down':
minpos = completeref[crosslist2[len(crosslist2)-1]]['position']
maxpos = completeref[crosslist[0]]['position']
elif direction == 'right':
maxpos = completeref[crosslist[len(crosslist)-1]]['position']
minpos = (completeref[crosslist[0]]['position'][0],
completeref[crosslist2[len(crosslist2)-1]]['position'][1])
elif direction == 'left':
maxpos = (completeref[crosslist[0]]['position'][0],
completeref[crosslist2[len(crosslist2)-1]]['position'][1])
minpos = completeref[crosslist[len(crosslist)-1]]['position']
spolys,spostopolys = buildfacesimplepolys(minpos, maxpos,
polynodesrev,
completerefrev, completeref)
##perform simple poly intersection testing, eliminating
##subdivision overlap
ydiff = maxpos[1] - minpos[1]
xdiff = maxpos[0] - minpos[0]
ccontinue = False
passlist = []
rootpoly = spostopolys[completeref[node]['position']]
rnodepos = completeref[node]['position']
##print('spolys: ', spolys)
##print('nopasscrosslist: ', nopasscrosslist)
for poly in spolys:
if poly in nopasscrosslist:
print('rejected poly: ', poly)
if poly == rootpoly:
ccontinue = True
break
else:
passlist.append(poly)
##print('passlist: ', passlist)
if ccontinue:
continue
vposlist = rebuildfacesimplepolys(rnodepos, rootpoly,
passlist, spostopolys,
completeref, completerefrev)
for poly in spolys:
if not poly in nopasscrosslist:
nopasscrosslist.append(poly)
## for cnode in allnodescrosslist:
## if not cnode in nopasscrosslist:
## nopasscrosslist.append(cnode)
# Two edges of face determined.
# Build vertices
## vpos1 = completeref[crosslist[0]]['position']
## vpos2 = completeref[crosslist2[0]]['position']
## vpos3 = completeref[crosslist2[len(crosslist2)-1]]['position']
## vpos4 = completeref[crosslist3[0]]['position']
##print('vposlist: ', vposlist)
vpos1,vpos2,vpos3,vpos4 = vposlist
vi1 = vertpostoindex[vpos1]
vi2 = vertpostoindex[vpos2]
vi3 = vertpostoindex[vpos3]
vi4 = vertpostoindex[vpos4]
facegroup = (vi1,vi2,vi3,vi4)
faces.append(facegroup)
## work remainders reiterates the same procedure much as given above.
## I've opted to skip this for now, overlap issues still occuring despite
## checks. Honestly I've opted to go for a simpler construction method
## since a fairly decent percentage of the map is likely filled by use
## of random crossing assignments, so the remainder are regularity filler.
##for polypositionsface in polynodesrev:
## if list(polypositionsface) in nopasscrosslist:
## continue
## else:
## nodepos = polypositionsface[0] ## we could randomly pick on this simple
## ## face
## node = completerefrev[nodepos]
## switches1 = [0,1]
## switches2 = [0,1]
## for switch1 in switches1:
## for switch2 in switches2:
## crosslist = [node]
## crosslist = checkcross(switch1, node, completeref,
## switch2, crosslist)
## ##print('crosslist',crosslist)
## crossendnode = crosslist[len(crosslist)-1]
## if crossendnode == None:
## del crosslist[len(crosslist)-1]
##
## if switch1 and switch2:
## direction = 'up'
## elif switch1 and not switch2:
## direction = 'down'
## elif not switch1 and switch2:
## direction = 'right'
## else:
## direction = 'left'
## ncheck, nodeindex = checkneighbornode(crosslist, completeref,
## nodes, direction)
## if ncheck != None:
## crosslist = crosslist[0:nodeindex+1]
## if len(crosslist) <= 1:
## continue
## if switch1 and switch2:
## maxpos = completeref[crosslist[len(crosslist)-1]]['position'][1]
## minpos = completeref[crosslist[0]]['position'][1]
## elif switch1 and not switch2:
## minpos = completeref[crosslist[len(crosslist)-1]]['position'][1]
## maxpos = completeref[crosslist[0]]['position'][1]
## elif not switch1 and switch2:
## maxpos = completeref[crosslist[len(crosslist)-1]]['position'][0]
## minpos = completeref[crosslist[0]]['position'][0]
## else:
## minpos = completeref[crosslist[len(crosslist)-1]]['position'][0]
## maxpos = completeref[crosslist[0]]['position'][0]
##
## switch3 = None
## switch4 = None
## direction3 = None
## direction4 = None
## if direction == 'up':
## #right
## switch3 = False
## switch4 = True
## direction3 = 0
## direction4 = 1
## elif direction == 'right':
## #down
## switch3 = True
## swithc4 = False