-
Notifications
You must be signed in to change notification settings - Fork 55
/
IP.py
1895 lines (1599 loc) · 71 KB
/
IP.py
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
#from gurobipy import *
from pulp import *
import cv2
import numpy as np
import sys
import csv
import copy
from utils import *
from skimage import measure
GAPS = {'wall_extraction': 5, 'door_extraction': 5, 'icon_extraction': 5, 'wall_neighbor': 5, 'door_neighbor': 5, 'icon_neighbor': 5, 'wall_conflict': 5, 'door_conflict': 5, 'icon_conflict': 5, 'wall_icon_neighbor': 5, 'wall_icon_conflict': 5, 'wall_door_neighbor': 5, 'door_point_conflict': 5}
DISTANCES = {'wall_icon': 5, 'point': 5, 'wall': 10, 'door': 5, 'icon': 5}
LENGTH_THRESHOLDS = {'wall': 5, 'door': 5, 'icon': 5}
junctionWeight = 100
augmentedJunctionWeight = 50
labelWeight = 1
wallWeight = 10
doorWeight = 10
iconWeight = 10
#wallTypeWeight = 10
#doorTypeWeight = 10
iconTypeWeight = 10
wallLineWidth = 3
doorLineWidth = 2
#doorExposureWeight = 0
NUM_WALL_TYPES = 1
NUM_DOOR_TYPES = 2
#NUM_LABELS = NUM_WALL_TYPES + NUM_DOOR_TYPES + NUM_ICONS + NUM_ROOMS + 1
NUM_LABELS = NUM_ICONS + NUM_ROOMS
WALL_LABEL_OFFSET = NUM_ROOMS + 1
DOOR_LABEL_OFFSET = NUM_ICONS + 1
ICON_LABEL_OFFSET = 0
ROOM_LABEL_OFFSET = NUM_ICONS
colorMap = ColorPalette(NUM_CORNERS).getColorMap()
width = 256
height = 256
maxDim = max(width, height)
sizes = np.array([width, height])
ORIENTATION_RANGES = getOrientationRanges(width, height)
iconNames = getIconNames()
iconNameNumberMap = dict(zip(iconNames, range(len(iconNames))))
iconNumberNameMap = dict(zip(range(len(iconNames)), iconNames))
## Extract corners from corner heatmp predictions
def extractCorners(heatmaps, threshold, gap, cornerType = 'wall', augment=False, gt=False):
if gt:
orientationPoints = heatmaps
else:
orientationPoints = extractCornersFromHeatmaps(heatmaps, threshold)
pass
if cornerType == 'wall':
cornerOrientations = []
for orientations in POINT_ORIENTATIONS:
cornerOrientations += orientations
continue
elif cornerType == 'door':
cornerOrientations = POINT_ORIENTATIONS[0]
else:
cornerOrientations = POINT_ORIENTATIONS[1]
pass
#print(orientationPoints)
if augment:
orientationMap = {}
for pointType, orientationOrientations in enumerate(POINT_ORIENTATIONS):
for orientation, orientations in enumerate(orientationOrientations):
orientationMap[orientations] = orientation
continue
continue
for orientationIndex, corners in enumerate(orientationPoints):
if len(corners) > 3:
continue #skip aug
pointType = orientationIndex // 4
if pointType in [2]:
orientation = orientationIndex % 4
orientations = POINT_ORIENTATIONS[pointType][orientation]
for i in range(len(orientations)):
newOrientations = list(orientations)
newOrientations.remove(orientations[i])
newOrientations = tuple(newOrientations)
if not newOrientations in orientationMap:
continue
newOrientation = orientationMap[newOrientations]
for corner in corners:
orientationPoints[(pointType - 1) * 4 + newOrientation].append(corner + (True, ))
continue
continue
elif pointType in [1]:
orientation = orientationIndex % 4
orientations = POINT_ORIENTATIONS[pointType][orientation]
for orientation in range(4):
if orientation in orientations:
continue
newOrientations = list(orientations)
newOrientations.append(orientation)
newOrientations = tuple(newOrientations)
if not newOrientations in orientationMap:
continue
newOrientation = orientationMap[newOrientations]
for corner in corners:
orientationPoints[(pointType + 1) * 4 + newOrientation].append(corner + (True, ))
continue
continue
pass
continue
pass
#print(orientationPoints)
pointOffset = 0
pointOffsets = []
points = []
pointOrientationLinesMap = []
for orientationIndex, corners in enumerate(orientationPoints):
pointOffsets.append(pointOffset)
orientations = cornerOrientations[orientationIndex]
for point in corners:
orientationLines = {}
for orientation in orientations:
orientationLines[orientation] = []
continue
pointOrientationLinesMap.append(orientationLines)
continue
pointOffset += len(corners)
if cornerType == 'wall':
points += [[corner[0][0], corner[0][1], orientationIndex // 4, orientationIndex % 4] for corner in corners]
elif cornerType == 'door':
points += [[corner[0][0], corner[0][1], 0, orientationIndex] for corner in corners]
else:
points += [[corner[0][0], corner[0][1], 1, orientationIndex] for corner in corners]
pass
continue
augmentedPointMask = {}
lines = []
pointNeighbors = [[] for point in points]
for orientationIndex, corners in enumerate(orientationPoints):
orientations = cornerOrientations[orientationIndex]
for orientation in orientations:
if orientation not in [1, 2]:
continue
oppositeOrientation = (orientation + 2) % 4
lineDim = -1
if orientation == 0 or orientation == 2:
lineDim = 1
else:
lineDim = 0
pass
for cornerIndex, corner in enumerate(corners):
pointIndex = pointOffsets[orientationIndex] + cornerIndex
#print(corner)
if len(corner) > 3:
augmentedPointMask[pointIndex] = True
pass
ranges = copy.deepcopy(ORIENTATION_RANGES[orientation])
ranges[lineDim] = min(ranges[lineDim], corner[0][lineDim])
ranges[lineDim + 2] = max(ranges[lineDim + 2], corner[0][lineDim])
ranges[1 - lineDim] = min(ranges[1 - lineDim], corner[1][1 - lineDim] - gap)
ranges[1 - lineDim + 2] = max(ranges[1 - lineDim + 2], corner[2][1 - lineDim] + gap)
for oppositeOrientationIndex, oppositeCorners in enumerate(orientationPoints):
if oppositeOrientation not in cornerOrientations[oppositeOrientationIndex]:
continue
for oppositeCornerIndex, oppositeCorner in enumerate(oppositeCorners):
if orientationIndex == oppositeOrientationIndex and oppositeCornerIndex == cornerIndex:
continue
oppositePointIndex = pointOffsets[oppositeOrientationIndex] + oppositeCornerIndex
if oppositeCorner[0][lineDim] < ranges[lineDim] or oppositeCorner[0][lineDim] > ranges[lineDim + 2] or ranges[1 - lineDim] > oppositeCorner[2][1 - lineDim] or ranges[1 - lineDim + 2] < oppositeCorner[1][1 - lineDim]:
continue
if abs(oppositeCorner[0][lineDim] - corner[0][lineDim]) < LENGTH_THRESHOLDS[cornerType]:
continue
lineIndex = len(lines)
pointOrientationLinesMap[pointIndex][orientation].append(lineIndex)
pointOrientationLinesMap[oppositePointIndex][oppositeOrientation].append(lineIndex)
pointNeighbors[pointIndex].append(oppositePointIndex)
pointNeighbors[oppositePointIndex].append(pointIndex)
lines.append((pointIndex, oppositePointIndex))
continue
continue
continue
continue
continue
return points, lines, pointOrientationLinesMap, pointNeighbors, augmentedPointMask
## Corner type augmentation to enrich the candidate set (e.g., a T-shape corner can be treated as a L-shape corner)
def augmentPoints(points, decreasingTypes = [2], increasingTypes = [1]):
orientationMap = {}
for pointType, orientationOrientations in enumerate(POINT_ORIENTATIONS):
for orientation, orientations in enumerate(orientationOrientations):
orientationMap[orientations] = orientation
continue
continue
newPoints = []
for pointIndex, point in enumerate(points):
if point[2] not in decreasingTypes:
continue
orientations = POINT_ORIENTATIONS[point[2]][point[3]]
for i in range(len(orientations)):
newOrientations = list(orientations)
newOrientations.remove(orientations[i])
newOrientations = tuple(newOrientations)
if not newOrientations in orientationMap:
continue
newOrientation = orientationMap[newOrientations]
newPoints.append([point[0], point[1], point[2] - 1, newOrientation])
continue
continue
for pointIndex, point in enumerate(points):
if point[2] not in increasingTypes:
continue
orientations = POINT_ORIENTATIONS[point[2]][point[3]]
for orientation in range(4):
if orientation in orientations:
continue
oppositeOrientation = (orientation + 2) % 4
ranges = copy.deepcopy(ORIENTATION_RANGES[orientation])
lineDim = -1
if orientation == 0 or orientation == 2:
lineDim = 1
else:
lineDim = 0
pass
deltas = [0, 0]
if lineDim == 1:
deltas[0] = gap
else:
deltas[1] = gap
pass
for c in range(2):
ranges[c] = min(ranges[c], point[c] - deltas[c])
ranges[c + 2] = max(ranges[c + 2], point[c] + deltas[c])
continue
hasNeighbor = False
for neighborPointIndex, neighborPoint in enumerate(points):
if neighborPointIndex == pointIndex:
continue
neighborOrientations = POINT_ORIENTATIONS[neighborPoint[2]][neighborPoint[3]]
if oppositeOrientation not in neighborOrientations:
continue
inRange = True
for c in range(2):
if neighborPoint[c] < ranges[c] or neighborPoint[c] > ranges[c + 2]:
inRange = False
break
continue
if not inRange or abs(neighborPoint[lineDim] - point[lineDim]) < max(abs(neighborPoint[1 - lineDim] - point[1 - lineDim]), 1):
continue
hasNeighbor = True
break
if not hasNeighbor:
continue
newOrientations = list(orientations)
newOrientations.append(orientation)
newOrientations = tuple(newOrientations)
if not newOrientations in orientationMap:
continue
newOrientation = orientationMap[newOrientations]
newPoints.append([point[0], point[1], point[2] + 1, newOrientation])
continue
continue
return points + newPoints
## Remove invalid walls as preprocessing
def filterWalls(wallPoints, wallLines):
orientationMap = {}
for pointType, orientationOrientations in enumerate(POINT_ORIENTATIONS):
for orientation, orientations in enumerate(orientationOrientations):
orientationMap[orientations] = orientation
continue
continue
#print(POINT_ORIENTATIONS)
while True:
pointOrientationNeighborsMap = {}
for line in wallLines:
lineDim = calcLineDim(wallPoints, line)
for c, pointIndex in enumerate(line):
if lineDim == 0:
if c == 0:
orientation = 1
else:
orientation = 3
else:
if c == 0:
orientation = 2
else:
orientation = 0
pass
pass
if pointIndex not in pointOrientationNeighborsMap:
pointOrientationNeighborsMap[pointIndex] = {}
pass
if orientation not in pointOrientationNeighborsMap[pointIndex]:
pointOrientationNeighborsMap[pointIndex][orientation] = []
pass
pointOrientationNeighborsMap[pointIndex][orientation].append(line[1 - c])
continue
continue
invalidPointMask = {}
for pointIndex, point in enumerate(wallPoints):
if pointIndex not in pointOrientationNeighborsMap:
invalidPointMask[pointIndex] = True
continue
orientationNeighborMap = pointOrientationNeighborsMap[pointIndex]
orientations = POINT_ORIENTATIONS[point[2]][point[3]]
if len(orientationNeighborMap) < len(orientations):
if len(orientationNeighborMap) >= 2 and tuple(orientationNeighborMap.keys()) in orientationMap:
newOrientation = orientationMap[tuple(orientationNeighborMap.keys())]
wallPoints[pointIndex][2] = len(orientationNeighborMap) - 1
wallPoints[pointIndex][3] = newOrientation
#print(orientationNeighborMap)
#print('new', len(orientationNeighborMap), newOrientation)
continue
invalidPointMask[pointIndex] = True
pass
continue
if len(invalidPointMask) == 0:
break
newWallPoints = []
pointIndexMap = {}
for pointIndex, point in enumerate(wallPoints):
if pointIndex not in invalidPointMask:
pointIndexMap[pointIndex] = len(newWallPoints)
newWallPoints.append(point)
pass
continue
wallPoints = newWallPoints
newWallLines = []
for lineIndex, line in enumerate(wallLines):
if line[0] in pointIndexMap and line[1] in pointIndexMap:
newLine = (pointIndexMap[line[0]], pointIndexMap[line[1]])
newWallLines.append(newLine)
pass
continue
wallLines = newWallLines
continue
pointOrientationLinesMap = [{} for _ in range(len(wallPoints))]
pointNeighbors = [[] for _ in range(len(wallPoints))]
for lineIndex, line in enumerate(wallLines):
lineDim = calcLineDim(wallPoints, line)
for c, pointIndex in enumerate(line):
if lineDim == 0:
if wallPoints[pointIndex][lineDim] < wallPoints[line[1 - c]][lineDim]:
orientation = 1
else:
orientation = 3
pass
else:
if wallPoints[pointIndex][lineDim] < wallPoints[line[1 - c]][lineDim]:
orientation = 2
else:
orientation = 0
pass
pass
if orientation not in pointOrientationLinesMap[pointIndex]:
pointOrientationLinesMap[pointIndex][orientation] = []
pass
pointOrientationLinesMap[pointIndex][orientation].append(lineIndex)
pointNeighbors[pointIndex].append(line[1 - c])
continue
continue
return wallPoints, wallLines, pointOrientationLinesMap, pointNeighbors
## Write wall points to result file
def writePoints(points, pointLabels, output_prefix='test/'):
with open(output_prefix + 'points_out.txt', 'w') as points_file:
for point in points:
points_file.write(str(point[0] + 1) + '\t' + str(point[1] + 1) + '\t')
points_file.write(str(point[0] + 1) + '\t' + str(point[1] + 1) + '\t')
points_file.write('point\t')
points_file.write(str(point[2] + 1) + '\t' + str(point[3] + 1) + '\n')
points_file.close()
with open(output_prefix + 'point_labels.txt', 'w') as point_label_file:
for point in pointLabels:
point_label_file.write(str(point[0]) + '\t' + str(point[1]) + '\t' + str(point[2]) + '\t' + str(point[3]) + '\n')
point_label_file.close()
## Write doors to result file
def writeDoors(points, lines, doorTypes, output_prefix='test/'):
with open(output_prefix + 'doors_out.txt', 'w') as doors_file:
for lineIndex, line in enumerate(lines):
point_1 = points[line[0]]
point_2 = points[line[1]]
doors_file.write(str(point_1[0] + 1) + '\t' + str(point_1[1] + 1) + '\t')
doors_file.write(str(point_2[0] + 1) + '\t' + str(point_2[1] + 1) + '\t')
doors_file.write('door\t')
doors_file.write(str(doorTypes[lineIndex] + 1) + '\t1\n')
doors_file.close()
## Write icons to result file
def writeIcons(points, icons, iconTypes, output_prefix='test/'):
with open(output_prefix + 'icons_out.txt', 'w') as icons_file:
for iconIndex, icon in enumerate(icons):
point_1 = points[icon[0]]
point_2 = points[icon[1]]
point_3 = points[icon[2]]
point_4 = points[icon[3]]
x_1 = int(round((point_1[0] + point_3[0]) // 2)) + 1
x_2 = int(round((point_2[0] + point_4[0]) // 2)) + 1
y_1 = int(round((point_1[1] + point_2[1]) // 2)) + 1
y_2 = int(round((point_3[1] + point_4[1]) // 2)) + 1
icons_file.write(str(x_1) + '\t' + str(y_1) + '\t')
icons_file.write(str(x_2) + '\t' + str(y_2) + '\t')
icons_file.write(iconNumberNameMap[iconTypes[iconIndex]] + '\t')
#icons_file.write(str(iconNumberStyleMap[iconTypes[iconIndex]]) + '\t')
icons_file.write('1\t')
icons_file.write('1\n')
icons_file.close()
## Adjust wall corner locations to align with each other after optimization
def adjustPoints(points, lines):
lineNeighbors = []
for lineIndex, line in enumerate(lines):
lineDim = calcLineDim(points, line)
neighbors = []
for neighborLineIndex, neighborLine in enumerate(lines):
if neighborLineIndex <= lineIndex:
continue
neighborLineDim = calcLineDim(points, neighborLine)
point_1 = points[neighborLine[0]]
point_2 = points[neighborLine[1]]
lineDimNeighbor = calcLineDim(points, neighborLine)
if lineDimNeighbor != lineDim:
continue
if neighborLine[0] != line[0] and neighborLine[0] != line[1] and neighborLine[1] != line[0] and neighborLine[1] != line[1]:
continue
neighbors.append(neighborLineIndex)
continue
lineNeighbors.append(neighbors)
continue
visitedLines = {}
for lineIndex in range(len(lines)):
if lineIndex in visitedLines:
continue
lineGroup = [lineIndex]
while True:
newLineGroup = lineGroup
hasChange = False
for line in lineGroup:
neighbors = lineNeighbors[line]
for neighbor in neighbors:
if neighbor not in newLineGroup:
newLineGroup.append(neighbor)
hasChange = True
pass
continue
continue
if not hasChange:
break
lineGroup = newLineGroup
continue
for line in lineGroup:
visitedLines[line] = True
continue
#print([[points[pointIndex] for pointIndex in lines[lineIndex]] for lineIndex in lineGroup], calcLineDim(points, lines[lineGroup[0]]))
pointGroup = []
for line in lineGroup:
for index in range(2):
pointIndex = lines[line][index]
if pointIndex not in pointGroup:
pointGroup.append(pointIndex)
pass
continue
continue
#lineDim = calcLineDim(points, lines[lineGroup[0]])
xy = np.concatenate([np.array([points[pointIndex][:2] for pointIndex in lines[lineIndex]]) for lineIndex in lineGroup], axis=0)
mins = xy.min(0)
maxs = xy.max(0)
if maxs[0] - mins[0] > maxs[1] - mins[1]:
lineDim = 0
else:
lineDim = 1
pass
fixedValue = 0
for point in pointGroup:
fixedValue += points[point][1 - lineDim]
continue
fixedValue /= len(pointGroup)
for point in pointGroup:
points[point][1 - lineDim] = fixedValue
continue
continue
return
## Merge two close points after optimization
def mergePoints(points, lines):
validPointMask = {}
for line in lines:
validPointMask[line[0]] = True
validPointMask[line[1]] = True
continue
orientationMap = {}
for pointType, orientationOrientations in enumerate(POINT_ORIENTATIONS):
for orientation, orientations in enumerate(orientationOrientations):
orientationMap[orientations] = (pointType, orientation)
continue
continue
for pointIndex_1, point_1 in enumerate(points):
if pointIndex_1 not in validPointMask:
continue
for pointIndex_2, point_2 in enumerate(points):
if pointIndex_2 <= pointIndex_1:
continue
if pointIndex_2 not in validPointMask:
continue
if pointDistance(point_1[:2], point_2[:2]) <= DISTANCES['point']:
orientations = list(POINT_ORIENTATIONS[point_1[2]][point_1[3]] + POINT_ORIENTATIONS[point_2[2]][point_2[3]])
if len([line for line in lines if pointIndex_1 in line and pointIndex_2 in line]) > 0:
if abs(point_1[0] - point_2[0]) > abs(point_1[1] - point_2[1]):
orientations.remove(1)
orientations.remove(3)
else:
orientations.remove(0)
orientations.remove(2)
pass
pass
orientations = tuple(set(orientations))
if orientations not in orientationMap:
for lineIndex, line in enumerate(lines):
if pointIndex_1 in line and pointIndex_2 in line:
lines[lineIndex] = (-1, -1)
pass
continue
lineIndices_1 = [(lineIndex, tuple(set(line) - set((pointIndex_1, )))[0]) for lineIndex, line in enumerate(lines) if pointIndex_1 in line and pointIndex_2 not in line]
lineIndices_2 = [(lineIndex, tuple(set(line) - set((pointIndex_2, )))[0]) for lineIndex, line in enumerate(lines) if pointIndex_2 in line and pointIndex_1 not in line]
if len(lineIndices_1) == 1 and len(lineIndices_2) == 1:
lineIndex_1, index_1 = lineIndices_1[0]
lineIndex_2, index_2 = lineIndices_2[0]
lines[lineIndex_1] = (index_1, index_2)
lines[lineIndex_2] = (-1, -1)
pass
continue
pointInfo = orientationMap[orientations]
newPoint = [(point_1[0] + point_2[0]) // 2, (point_1[1] + point_2[1]) // 2, pointInfo[0], pointInfo[1]]
points[pointIndex_1] = newPoint
for lineIndex, line in enumerate(lines):
if pointIndex_2 == line[0]:
lines[lineIndex] = (pointIndex_1, line[1])
pass
if pointIndex_2 == line[1]:
lines[lineIndex] = (line[0], pointIndex_1)
pass
continue
pass
continue
continue
return
## Adjust door corner locations to align with each other after optimization
def adjustDoorPoints(doorPoints, doorLines, wallPoints, wallLines, doorWallMap):
for doorLineIndex, doorLine in enumerate(doorLines):
lineDim = calcLineDim(doorPoints, doorLine)
wallLine = wallLines[doorWallMap[doorLineIndex]]
wallPoint_1 = wallPoints[wallLine[0]]
wallPoint_2 = wallPoints[wallLine[1]]
fixedValue = (wallPoint_1[1 - lineDim] + wallPoint_2[1 - lineDim]) // 2
for endPointIndex in range(2):
doorPoints[doorLine[endPointIndex]][1 - lineDim] = fixedValue
continue
continue
## Generate icon candidates
def findIconsFromLines(iconPoints, iconLines):
icons = []
pointOrientationNeighborsMap = {}
for line in iconLines:
lineDim = calcLineDim(iconPoints, line)
for c, pointIndex in enumerate(line):
if lineDim == 0:
if c == 0:
orientation = 1
else:
orientation = 3
else:
if c == 0:
orientation = 2
else:
orientation = 0
pass
pass
if pointIndex not in pointOrientationNeighborsMap:
pointOrientationNeighborsMap[pointIndex] = {}
pass
if orientation not in pointOrientationNeighborsMap[pointIndex]:
pointOrientationNeighborsMap[pointIndex][orientation] = []
pass
pointOrientationNeighborsMap[pointIndex][orientation].append(line[1 - c])
continue
continue
for pointIndex, orientationNeighborMap in pointOrientationNeighborsMap.items():
if 1 not in orientationNeighborMap or 2 not in orientationNeighborMap:
continue
for neighborIndex_1 in orientationNeighborMap[1]:
if 2 not in pointOrientationNeighborsMap[neighborIndex_1]:
continue
lastCornerCandiates = pointOrientationNeighborsMap[neighborIndex_1][2]
for neighborIndex_2 in orientationNeighborMap[2]:
if 1 not in pointOrientationNeighborsMap[neighborIndex_2]:
continue
for lastCornerIndex in pointOrientationNeighborsMap[neighborIndex_2][1]:
if lastCornerIndex not in lastCornerCandiates:
continue
point_1 = iconPoints[pointIndex]
point_2 = iconPoints[neighborIndex_1]
point_3 = iconPoints[neighborIndex_2]
point_4 = iconPoints[lastCornerIndex]
x_1 = int((point_1[0] + point_3[0]) // 2)
x_2 = int((point_2[0] + point_4[0]) // 2)
y_1 = int((point_1[1] + point_2[1]) // 2)
y_2 = int((point_3[1] + point_4[1]) // 2)
#if x_2 <= x_1 or y_2 <= y_1:
#continue
if (x_2 - x_1 + 1) * (y_2 - y_1 + 1) <= LENGTH_THRESHOLDS['icon'] * LENGTH_THRESHOLDS['icon']:
continue
icons.append((pointIndex, neighborIndex_1, neighborIndex_2, lastCornerIndex))
continue
continue
continue
continue
return icons
## Find two wall lines facing each other and accumuate semantic information in between
def findLineNeighbors(points, lines, labelVotesMap, gap):
lineNeighbors = [[{}, {}] for lineIndex in range(len(lines))]
for lineIndex, line in enumerate(lines):
lineDim = calcLineDim(points, line)
for neighborLineIndex, neighborLine in enumerate(lines):
if neighborLineIndex <= lineIndex:
continue
neighborLineDim = calcLineDim(points, neighborLine)
if lineDim != neighborLineDim:
continue
minValue = max(points[line[0]][lineDim], points[neighborLine[0]][lineDim])
maxValue = min(points[line[1]][lineDim], points[neighborLine[1]][lineDim])
if maxValue - minValue < gap:
continue
fixedValue_1 = points[line[0]][1 - lineDim]
fixedValue_2 = points[neighborLine[0]][1 - lineDim]
minValue = int(minValue)
maxValue = int(maxValue)
fixedValue_1 = int(fixedValue_1)
fixedValue_2 = int(fixedValue_2)
if abs(fixedValue_2 - fixedValue_1) < gap:
continue
if lineDim == 0:
if fixedValue_1 < fixedValue_2:
region = ((minValue, fixedValue_1), (maxValue, fixedValue_2))
lineNeighbors[lineIndex][1][neighborLineIndex] = region
lineNeighbors[neighborLineIndex][0][lineIndex] = region
else:
region = ((minValue, fixedValue_2), (maxValue, fixedValue_1))
lineNeighbors[lineIndex][0][neighborLineIndex] = region
lineNeighbors[neighborLineIndex][1][lineIndex] = region
else:
if fixedValue_1 < fixedValue_2:
region = ((fixedValue_1, minValue), (fixedValue_2, maxValue))
lineNeighbors[lineIndex][0][neighborLineIndex] = region
lineNeighbors[neighborLineIndex][1][lineIndex] = region
else:
region = ((fixedValue_2, minValue), (fixedValue_1, maxValue))
lineNeighbors[lineIndex][1][neighborLineIndex] = region
lineNeighbors[neighborLineIndex][0][lineIndex] = region
pass
pass
continue
continue
# remove neighbor pairs which are separated by another line
while True:
hasChange = False
for lineIndex, neighbors in enumerate(lineNeighbors):
lineDim = calcLineDim(points, lines[lineIndex])
for neighbor_1, region_1 in neighbors[1].items():
for neighbor_2, _ in neighbors[0].items():
if neighbor_2 not in lineNeighbors[neighbor_1][0]:
continue
region_2 = lineNeighbors[neighbor_1][0][neighbor_2]
if region_1[0][lineDim] < region_2[0][lineDim] + gap and region_1[1][lineDim] > region_2[1][lineDim] - gap:
lineNeighbors[neighbor_1][0].pop(neighbor_2)
lineNeighbors[neighbor_2][1].pop(neighbor_1)
hasChange = True
pass
continue
continue
continue
if not hasChange:
break
for lineIndex, directionNeighbors in enumerate(lineNeighbors):
for direction, neighbors in enumerate(directionNeighbors):
for neighbor, region in neighbors.items():
labelVotes = labelVotesMap[:, region[1][1], region[1][0]] + labelVotesMap[:, region[0][1], region[0][0]] - labelVotesMap[:, region[0][1], region[1][0]] - labelVotesMap[:, region[1][1], region[0][0]]
neighbors[neighbor] = labelVotes
continue
continue
continue
return lineNeighbors
## Find neighboring wall line/icon pairs
def findRectangleLineNeighbors(rectanglePoints, rectangles, linePoints, lines, lineNeighbors, gap, distanceThreshold):
rectangleLineNeighbors = [{} for rectangleIndex in range(len(rectangles))]
minDistanceLineNeighbors = {}
for rectangleIndex, rectangle in enumerate(rectangles):
for lineIndex, line in enumerate(lines):
lineDim = calcLineDim(linePoints, line)
minValue = max(rectanglePoints[rectangle[0]][lineDim], rectanglePoints[rectangle[2 - lineDim]][lineDim], linePoints[line[0]][lineDim])
maxValue = min(rectanglePoints[rectangle[1 + lineDim]][lineDim], rectanglePoints[rectangle[3]][lineDim], linePoints[line[1]][lineDim])
if maxValue - minValue < gap:
continue
rectangleFixedValue_1 = (rectanglePoints[rectangle[0]][1 - lineDim] + rectanglePoints[rectangle[1 + lineDim]][1 - lineDim]) // 2
rectangleFixedValue_2 = (rectanglePoints[rectangle[2 - lineDim]][1 - lineDim] + rectanglePoints[rectangle[3]][1 - lineDim]) // 2
lineFixedValue = (linePoints[line[0]][1 - lineDim] + linePoints[line[1]][1 - lineDim]) // 2
if lineFixedValue < rectangleFixedValue_2 - gap and lineFixedValue > rectangleFixedValue_1 + gap:
continue
if lineFixedValue <= rectangleFixedValue_1 + gap:
index = lineDim * 2 + 0
distance = rectangleFixedValue_1 - lineFixedValue
if index not in minDistanceLineNeighbors or distance < minDistanceLineNeighbors[index][1]:
minDistanceLineNeighbors[index] = (lineIndex, distance, 1 - lineDim)
else:
index = lineDim * 2 + 1
distance = lineFixedValue - rectangleFixedValue_2
if index not in minDistanceLineNeighbors or distance < minDistanceLineNeighbors[index][1]:
minDistanceLineNeighbors[index] = (lineIndex, distance, lineDim)
if lineFixedValue < rectangleFixedValue_1 - distanceThreshold or lineFixedValue > rectangleFixedValue_2 + distanceThreshold:
continue
if lineFixedValue <= rectangleFixedValue_1 + gap:
if lineDim == 0:
rectangleLineNeighbors[rectangleIndex][lineIndex] = 1
else:
rectangleLineNeighbors[rectangleIndex][lineIndex] = 0
pass
pass
else:
if lineDim == 0:
rectangleLineNeighbors[rectangleIndex][lineIndex] = 0
else:
rectangleLineNeighbors[rectangleIndex][lineIndex] = 1
pass
pass
continue
if len(rectangleLineNeighbors[rectangleIndex]) == 0 or True:
for index, lineNeighbor in minDistanceLineNeighbors.items():
rectangleLineNeighbors[rectangleIndex][lineNeighbor[0]] = lineNeighbor[2]
continue
pass
continue
return rectangleLineNeighbors
## Find the door line to wall line map
def findLineMap(points, lines, points_2, lines_2, gap):
lineMap = [{} for lineIndex in range(len(lines))]
for lineIndex, line in enumerate(lines):
lineDim = calcLineDim(points, line)
for neighborLineIndex, neighborLine in enumerate(lines_2):
neighborLineDim = calcLineDim(points_2, neighborLine)
if lineDim != neighborLineDim:
continue
minValue = max(points[line[0]][lineDim], points_2[neighborLine[0]][lineDim])
maxValue = min(points[line[1]][lineDim], points_2[neighborLine[1]][lineDim])
if maxValue - minValue < gap:
continue
fixedValue_1 = (points[line[0]][1 - lineDim] + points[line[1]][1 - lineDim]) // 2
fixedValue_2 = (points_2[neighborLine[0]][1 - lineDim] + points_2[neighborLine[1]][1 - lineDim]) // 2
if abs(fixedValue_2 - fixedValue_1) > gap:
continue
lineMinValue = points[line[0]][lineDim]
lineMaxValue = points[line[1]][lineDim]
ratio = float(maxValue - minValue + 1) / (lineMaxValue - lineMinValue + 1)
lineMap[lineIndex][neighborLineIndex] = ratio
continue
continue
return lineMap
## Find the one-to-one door line to wall line map after optimization
def findLineMapSingle(points, lines, points_2, lines_2, gap):
lineMap = []
for lineIndex, line in enumerate(lines):
lineDim = calcLineDim(points, line)
minDistance = max(width, height)
minDistanceLineIndex = -1
for neighborLineIndex, neighborLine in enumerate(lines_2):
neighborLineDim = calcLineDim(points_2, neighborLine)
if lineDim != neighborLineDim:
continue
minValue = max(points[line[0]][lineDim], points_2[neighborLine[0]][lineDim])
maxValue = min(points[line[1]][lineDim], points_2[neighborLine[1]][lineDim])
if maxValue - minValue < gap:
continue
fixedValue_1 = (points[line[0]][1 - lineDim] + points[line[1]][1 - lineDim]) // 2
fixedValue_2 = (points_2[neighborLine[0]][1 - lineDim] + points_2[neighborLine[1]][1 - lineDim]) // 2
distance = abs(fixedValue_2 - fixedValue_1)
if distance < minDistance:
minDistance = distance
minDistanceLineIndex = neighborLineIndex
pass
continue
#if abs(fixedValue_2 - fixedValue_1) > gap:
#continue
#print((lineIndex, minDistance, minDistanceLineIndex))
lineMap.append(minDistanceLineIndex)
continue
return lineMap
## Find conflicting line pairs
def findConflictLinePairs(points, lines, gap, distanceThreshold, considerEndPoints=False):
conflictLinePairs = []
for lineIndex_1, line_1 in enumerate(lines):
lineDim_1 = calcLineDim(points, line_1)
point_1 = points[line_1[0]]
point_2 = points[line_1[1]]
fixedValue_1 = int(round((point_1[1 - lineDim_1] + point_2[1 - lineDim_1]) // 2))
minValue_1 = int(min(point_1[lineDim_1], point_2[lineDim_1]))
maxValue_1 = int(max(point_1[lineDim_1], point_2[lineDim_1]))
for lineIndex_2, line_2 in enumerate(lines):
if lineIndex_2 <= lineIndex_1:
continue
lineDim_2 = calcLineDim(points, line_2)
point_1 = points[line_2[0]]
point_2 = points[line_2[1]]
if lineDim_2 == lineDim_1:
if line_1[0] == line_2[0] or line_1[1] == line_2[1]:
conflictLinePairs.append((lineIndex_1, lineIndex_2))
continue
elif line_1[0] == line_2[1] or line_1[1] == line_2[0]:
continue
pass
else:
if (line_1[0] in line_2 or line_1[1] in line_2):
continue
pass
if considerEndPoints:
if min([pointDistance(points[line_1[0]], points[line_2[0]]), pointDistance(points[line_1[0]], points[line_2[1]]), pointDistance(points[line_1[1]], points[line_2[0]]), pointDistance(points[line_1[1]], points[line_2[1]])]) <= gap:
conflictLinePairs.append((lineIndex_1, lineIndex_2))
continue
pass
fixedValue_2 = int(round((point_1[1 - lineDim_2] + point_2[1 - lineDim_2]) // 2))
minValue_2 = int(min(point_1[lineDim_2], point_2[lineDim_2]))
maxValue_2 = int(max(point_1[lineDim_2], point_2[lineDim_2]))
if lineDim_1 == lineDim_2:
if abs(fixedValue_2 - fixedValue_1) >= distanceThreshold or minValue_1 > maxValue_2 - gap or minValue_2 > maxValue_1 - gap:
continue
conflictLinePairs.append((lineIndex_1, lineIndex_2))
#drawLines(output_prefix + 'lines_' + str(lineIndex_1) + "_" + str(lineIndex_2) + '.png', width, height, points, [line_1, line_2])
else:
if minValue_1 > fixedValue_2 - gap or maxValue_1 < fixedValue_2 + gap or minValue_2 > fixedValue_1 - gap or maxValue_2 < fixedValue_1 + gap:
continue
conflictLinePairs.append((lineIndex_1, lineIndex_2))
pass
continue
continue
return conflictLinePairs
## Find conflicting line/icon pairs
def findConflictRectanglePairs(points, rectangles, gap):
conflictRectanglePairs = []
for rectangleIndex_1, rectangle_1 in enumerate(rectangles):
for rectangleIndex_2, rectangle_2 in enumerate(rectangles):
if rectangleIndex_2 <= rectangleIndex_1:
continue
conflict = False
for cornerIndex in range(4):
if rectangle_1[cornerIndex] == rectangle_2[cornerIndex]:
conflictRectanglePairs.append((rectangleIndex_1, rectangleIndex_2))
conflict = True
break
continue
if conflict:
continue
minX = max((points[rectangle_1[0]][0] + points[rectangle_1[2]][0]) // 2, (points[rectangle_2[0]][0] + points[rectangle_2[2]][0]) // 2)
maxX = min((points[rectangle_1[1]][0] + points[rectangle_1[3]][0]) // 2, (points[rectangle_2[1]][0] + points[rectangle_2[3]][0]) // 2)
if minX > maxX - gap:
continue
minY = max((points[rectangle_1[0]][1] + points[rectangle_1[1]][1]) // 2, (points[rectangle_2[0]][1] + points[rectangle_2[1]][1]) // 2)
maxY = min((points[rectangle_1[2]][1] + points[rectangle_1[3]][1]) // 2, (points[rectangle_2[2]][1] + points[rectangle_2[3]][1]) // 2)
if minY > maxY - gap:
continue
conflictRectanglePairs.append((rectangleIndex_1, rectangleIndex_2))
continue
continue
return conflictRectanglePairs