-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathccc4.html
2019 lines (1705 loc) · 47.6 KB
/
ccc4.html
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
<!DOCTYPE html>
<html theme=dark>
<body>
<base href="www2/">
<link rel="preload" href="fa-regular-400.woff2" as="font" type="font/woff2" crossorigin>
<link rel="preload" href="fa-solid-900.woff2" as="font" type="font/woff2" crossorigin>
<script src=glue.js global extend></script>
<script src=ui.js></script>
<script src=ui_validation.js></script>
<script src=ui_nav.js></script>
<script src=ui_grid.js></script>
<script>
(function () {
"use strict"
const G = window
DEBUG('DEBUG_PLAN')
let clone = structuredClone
let EPSILON = 1e-5
function near(a, b) { return abs(a - b) < EPSILON }
// debugging -----------------------------------------------------------------
let errs = []
let log_stack = []
function log(err, ...args) {
errs.push(isstr(err) ? subst(err, ...args) : err)
}
function push_log() {
log_stack.push(errs)
errs = []
errs.t0 = clock()
}
function pop_log(err, ...args) {
let errs1 = errs
let dt = (clock() - errs1.t0) * 1000
errs1.dt = dt
errs = log_stack.pop()
if (err !== undefined && errs1.length) {
log(err, ...args)
log(errs1)
if (dt > 3)
log('{0} TIME {1} ms', ('*').repeat(dt), dec(dt))
} else {
return errs1
}
}
function check(v, ...args) {
if (!v) log(...args)
return v
}
function pr_errs(es, level) {
let indent = (' ').repeat(level)
for (let s of es)
if (isarray(s))
pr_errs(s, level+1)
else
pr(indent + s)
}
function pr_log() {
assert(!log_stack.length)
pr_errs(errs, 0)
errs.length = 0
}
let next_id = {}
function gen_id(k) {
if (!next_id[k])
next_id[k] = 1
else
next_id[k]++
return next_id[k]
}
// bbox ----------------------------------------------------------------------
function bbox() {
return [inf, inf, -inf, -inf] // x1, y1, x2, y2
}
function bbox_add_bbox(bb, x1, y1, x2, y2) {
bb[0] = min(bb[0], x1, x2)
bb[1] = min(bb[1], y1, y2)
bb[2] = max(bb[2], x1, x2)
bb[3] = max(bb[3], y1, y2)
}
function bbox_add_point(bb, x, y) {
bb[0] = min(bb[0], x)
bb[1] = min(bb[1], y)
bb[2] = max(bb[2], x)
bb[3] = max(bb[3], y)
}
// polygon offset algorithm --------------------------------------------------
// hypotenuse function: computes sqrt(a^2 + b^2) without underflow / overflow problems.
function hypot(a, b) {
if (a == 0 && b == 0) return 0
a = abs(a)
b = abs(b)
let M = max(a, b)
let m = min(a, b)
return M * sqrt(1 + (m / M)**2)
}
// distance between two points. avoids underflow and overflow.
function distance(x1, y1, x2, y2) {
return hypot(x2-x1, y2-y1)
}
// parallel line segment at a distance on the right side of a segment.
// use a negative distance for the left side, or reflect the returned points
// against their respective initial points.
function line_offset(d, x1, y1, x2, y2, out) {
// normal vector of the same length as original segment.
let dx = -(y2-y1)
let dy = x2-x1
let k = d / distance(x1, y1, x2, y2) // normal vector scale factor
// normal vector scaled and translated to (x1,y1) and (x2,y2)
out[0] = x1 + dx * k
out[1] = y1 + dy * k
out[2] = x2 + dx * k
out[3] = y2 + dy * k
return out
}
// evaluate a line at time t using linear interpolation.
// the time between 0..1 covers the segment interval.
function line_point(t, x1, y1, x2, y2, out) {
out[0] = x1 + t * (x2 - x1)
out[1] = y1 + t * (y2 - y1)
return out
}
// intersect line segment (x1, y1, x2, y2) with line segment (x3, y3, x4, y4).
// returns the time on the first line where intersection occurs.
// if the intersection occurs outside the segments themselves, then t is
// outside the 0..1 range. if the lines are parallel then t is +/-inf.
// if they are coincidental, t is NaN.
function line_line_intersection(x1, y1, x2, y2, x3, y3, x4, y4) {
let d = (y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1)
return ((x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3)) / d
}
function find_seg(p1, p2) {
for (let seg of p1.segs)
if (seg[0] == p2)
return seg
else if (seg[1] == p2)
return seg
assert(false)
}
function offset_corner(p0, p1, p2, d, out) {
let seg1 = find_seg(p0, p1)
let seg2 = find_seg(p1, p2)
let [x1, y1, x2, y2] = line_offset( d, p0[0], p0[1], p1[0], p1[1], out)
let [x3, y3, x4, y4] = line_offset(-d, p2[0], p2[1], p1[0], p1[1], out)
let t1 = line_line_intersection(x1, y1, x2, y2, x3, y3, x4, y4)
if (abs(t1) == inf) { // 0-degree corner: make a line cap of 2 points, 1*d thick
let dx = x2 == x4 ? d * sign(x1 - x2) : 0
let dy = y2 == y4 ? d * sign(y1 - y2) : 0
out[0] = x2 + dx
out[1] = y2 + dy
out[2] = x4 + dx
out[3] = y4 + dy
} else if (t1 != t1) { // 180-degree corner: use the offset point on the first line
out[0] = x2
out[1] = y2
out[2] = null
out[3] = null
} else { // bent corner
out[2] = null
out[3] = null
line_point(t1, x1, y1, x2, y2, out)
}
return out
}
function offset_poly(ps, d) {
let ops = []
let out = []
if (ps.length == 2 && points_equal(ps[0], ps[1])) { // single null seg: make a square
let i = 0
for (let op of [[-d, -d], [d, -d], [d, d], [-d, d]]) {
op.p = ps[0]
op.ci = i++
ops.push(op)
}
} else {
for (let i = 0, n = ps.length; i < n; i++) {
let p1 = ps[i]
// skip over null segments that can't project a _|_.
let i0 = i-1; let p0; do { p0 = ps[mod(i0--, n)] } while (points_equal(p0, p1))
let i2 = i+1; let p2; do { p2 = ps[mod(i2++, n)] } while (points_equal(p2, p1))
let [x1, y1, x2, y2] = offset_corner(p0, p1, p2, d, out)
let [x0, y0] = p1
let op1 = [x1 - x0, y1 - y0]
op1.p = p1
op1.ci = i
ops.push(op1)
if (x2 != null) {
let op2 = [x2 - x0, y2 - y0]
op2.p = p1
op2.ci = i
ops.push(op2)
}
}
}
return ops
}
/* ---------------------------------------------------------------------------
Algorithm for extracting all base cycles from an unidirected planar graph.
- input: no null segs, no isolated points, no overlapping or intersecting segs allowed.
- points can be: end-points (#adj=1), joints (#adj=2) or branch points (#adj > 2).
- output: cycles and filaments.
Paper : https://www.geometrictools.com/Documentation/MinimalCycleBasis.pdf
Code : https://github.com/vbichkovsky/min-cycles/blob/master/src/cycles.js
*/
function rebuild_adj_refs(ps) {
for (let p of ps)
p.adj.length = 0
for (let p of ps)
for (let seg of p.segs)
p.adj.push(seg[1-seg_pi(seg, p)])
}
function rem_edge(p1, p2, ps) {
remove_value(p1.adj, p2)
remove_value(p2.adj, p1)
if (p1.adj == 0) remove_value(ps, p1)
if (p2.adj == 0) remove_value(ps, p2)
}
function rem_filament(p, ps) {
while (p && p.adj.length < 2) {
remove_value(ps, p)
let pa = p.adj[0]
if (pa)
rem_edge(p, pa, ps)
p = pa
}
}
function is_cw(ps) {
let s = 0
for (let i = 0, n = ps.length; i < n; i++) {
let [x1, y1] = ps[mod(i-1, n)]
let [x2, y2] = ps[i]
s += (x2-x1)*(y2+y1)
}
return s < 0
}
function left_bottom_point(ps) {
return ps.reduce((p0, p1) => {
let x1 = p1[0]
let x0 = p0[0]
if (x1 < x0) return p1
if (x0 < x1) return p0
let y1 = p1[1]
let y0 = p0[1]
if (y1 > y0) return p1
return p0
})
}
// return a number from the range [0..4) which is monotonically increasing
// with the clockwise angle that the input vector makes against the x axis.
function pseudo_angle(dx, dy) {
let p = dx / (abs(dx) + abs(dy)) // -1..0 (x <= 0) or 0..1 (x >= 0)
return dy < 0 ? 3 + p : 1 - p // 2..4 (y <= 0) or 0..2 (y >= 0)
}
// return the angle sweep from angle a1 to a2 in cw (+) or ccw (-) dir.
function angle_sweep(a1, a2, clockwise, circle_sweep) {
circle_sweep ??= 4
let d = a2 - a1
if (d < 0 && clockwise)
d += circle_sweep
else if (d > 0 && !clockwise)
d -= circle_sweep
return d
}
// return p1 from p.adj where the angle at p on (p0,p,p1) is smallest in cw or ccw direction.
function next_adj(p0, p, clockwise, max_a) {
max_a ??= inf
if (p.adj.length == 1 && max_a >= 2) // end-point, go back (or forward if first)
return p.adj[0]
let x0 = p0 ? p0[0] - p[0] : 0
let y0 = p0 ? p0[1] - p[1] : 1
let a0 = pseudo_angle(x0, y0)
let min_a = inf // min angle
let min_p // point with min angle to (p0,p)
for (let p1 of p.adj) {
if (p1 == p0)
continue
let x1 = p1[0] - p[0]
let y1 = p1[1] - p[1]
let a1 = pseudo_angle(x1, y1)
let a = abs(angle_sweep(a1, a0, clockwise))
if (a < min_a && a <= max_a) {
min_a = a
min_p = p1
}
}
return min_p
}
function closed_walk(first, outer_cycle) {
let walk = []
let curr = first
let prev
do {
walk.push(curr)
let next = next_adj(prev, curr, !prev || outer_cycle)
prev = curr
curr = next
} while (curr != first) // TODO: fix |_ bug
return walk
}
// NOTE: do `ps.push(ps[0], ps[1])` before you call this!
function polygon_area(ps) {
let area = 0
for (let i = 1, n = ps.length; i <= n; i++)
area += ps[mod(i, n)][0] * (ps[mod(i+1, n)][1] - ps[i-1][1])
return area / 2
}
function extract_cycles_for(comp) {
let ps = [...comp.ps]
while (ps.length > 0) {
let p = left_bottom_point(ps)
let c = closed_walk(p)
if (c[1] != c[c.length-1]) { // not started with a filament
c.i = gen_id('cycle')
c.comp = comp
c.area = abs(polygon_area(c))
comp.cycles.push(c)
}
// the first edge is always safe to remove because starting from the leftmost
// point means that there cannot be a cycle to the right of that first edge
// so that edge is part of at most one cycle: our cycle.
rem_edge(c[0], c[1], ps)
// the removed edge's end-points are now possibly end-points of filaments
// that we must remove too.
rem_filament(c[0], ps)
rem_filament(c[1], ps)
}
}
function extract_outer_cycle_for(comp) {
let p = left_bottom_point(comp.ps)
let c = closed_walk(p, true)
c.i = gen_id('cycle')
c.outer = true
c.comp = comp
c.area = abs(polygon_area(c))
c.reverse()
comp.cycles.push(c)
comp.outer_cycle = c
}
// house plan model ----------------------------------------------------------
function is_h(seg) { return seg[0][1] == seg[1][1] }
function is_v(seg) { return seg[0][0] == seg[1][0] }
function is_null(seg) { return is_h(seg) && is_v(seg) }
function seg_dir(seg) { return is_h(seg) ? '-' : is_v(seg) ? '|' : '/' }
function seg_x1(seg) { return min(seg[0][0], seg[1][0]) }
function seg_y1(seg) { return min(seg[0][1], seg[1][1]) }
function seg_x2(seg) { return max(seg[0][0], seg[1][0]) }
function seg_y2(seg) { return max(seg[0][1], seg[1][1]) }
function set_seg_x1(seg, x) { let x1i = seg[0][0] < seg[1][0] ? 0 : 1; seg[x1i][0] = x }
function set_seg_y1(seg, y) { let y1i = seg[0][1] < seg[1][1] ? 0 : 1; seg[y1i][1] = y }
function set_seg_x2(seg, x) { let x2i = seg[0][0] < seg[1][0] ? 1 : 0; seg[x2i][0] = x }
function set_seg_y2(seg, y) { let y2i = seg[0][1] < seg[1][1] ? 1 : 0; seg[y2i][1] = y }
function seg_axis(seg) { return is_v(seg) ? seg_x1(seg) : seg_y1(seg) }
function seg_m1 (seg) { return is_v(seg) ? seg_y1(seg) : seg_x1(seg) }
function seg_m2 (seg) { return is_v(seg) ? seg_y2(seg) : seg_x2(seg) }
function set_seg_m1 (seg, m) { if (is_v(seg)) set_seg_y1(seg, m); else set_seg_x1(seg, m) }
function set_seg_m2 (seg, m) { if (is_v(seg)) set_seg_y2(seg, m); else set_seg_x2(seg, m) }
function set_seg_axis(seg, a) {
if (is_v(seg)) {
seg[0][0] = a
seg[1][0] = a
} else {
seg[0][1] = a
seg[1][1] = a
}
}
function points_equal(p1, p2) { return p1[0] == p2[0] && p1[1] == p2[1] }
function seg_pi(seg, p) {
assert(seg[0] == p || seg[1] == p)
return seg[0] == p ? 0 : 1
}
function seg_i1(seg) { let mi = is_v(seg) ? 1 : 0; return seg[0][mi] < seg[1][mi] ? 0 : 1 }
function seg_i2(seg) { let mi = is_v(seg) ? 1 : 0; return seg[0][mi] < seg[1][mi] ? 1 : 0 }
function seg_p1(seg) { return seg[seg_i1(seg)] }
function seg_p2(seg) { return seg[seg_i2(seg)] }
function seg_center(seg) {
let [p1, p2] = seg
let [x1, y1] = p1
let [x2, y2] = p2
return [
(x2 + x1) / 2,
(y2 + y1) / 2,
]
}
function poly_center(ps) {
let [x0, y0] = ps[0]
let twicearea = 0
let x = 0
let y = 0
for (let i = 0, n = ps.length, j = n-1; i < n; j = i++) {
let [x1, y1] = ps[i]
let [x2, y2] = ps[j]
let f = (x1 - x0) * (y2 - y0) - (x2 - x0) * (y1 - y0)
twicearea += f
x += (x1 + x2 - 2 * x0) * f
y += (y1 + y2 - 2 * y0) * f
}
let f = twicearea * 3
return [
x / f + x0,
y / f + y0
]
}
function house_plan(t) {
let levels = t.levels
let house_plan = {levels: levels}
function recompute_house_plan_bbox() {
let bb = bbox()
for (let level of house_plan.levels) {
let level_plan = house_plan.level_plan(level)
bbox_add_bbox(bb, ...level_plan.bb)
}
return bb
}
function level_plan(level) {
if (level.level_plan)
return level.level_plan
let ps = level.ps; if (!ps) { ps = []; level.ps = ps; }
let comps = []
let level_plan = {ps: ps, comps: comps, house_plan: house_plan}
level.level_plan = level_plan
// model editing -------------------------------------------------------
function init_point(p, i) {
p.adj = []
p.i = gen_id('point')
}
function add_point(x, y) {
let p = [x, y]
init_point(p)
ps.push(p)
log('point added: {0}', p.i)
return p
}
level_plan.add_point = add_point
function rem_point(p) {
remove_value(ps, p)
log('point removed: {0}', p.i)
}
function rem_points(cond, msg) {
let a = []
remove_values(ps, function(p) {
if (!cond(p)) return
a.push(p.i)
return true
})
if (a.length)
log('{0} removed: {1}', msg ?? 'points', a.join(' '))
}
function add_seg_ref(p, p2) {
p.adj.push(p2)
}
function rem_seg_ref(p, p2) {
remove_value(p.adj, p2)
}
function add_seg_refs(p1, p2) {
add_seg_ref(p1, p2)
add_seg_ref(p2, p1)
}
function rem_seg_refs(p1, p2) {
rem_seg_ref(p1, p2)
rem_seg_ref(p2, p1)
}
function add_seg(p1, p2) {
add_seg_refs(p1, p2)
log('seg added: {0}-{1}', p1.i, p2.i)
}
level_plan.add_seg = add_seg
function rem_seg(p1, p2) {
rem_seg_refs(p1, p2)
log('seg removed: {0}-{1}', p1.i, p2.i)
}
function rem_segs(cond, msg) {
let a = []
for (let p1 of ps)
for (let p2 of p.adj)
if (cond(p1, p2)) {
rem_seg_refs(p1, p2)
a.push(p1.i+'-'+p2.i)
}
if (log && a.length)
log('{0} removed: {1}', msg ?? 'segs', a.join(','))
}
function set_seg_point(p1, p2, old_p, new_p) {
let other_p = old_p == p1 ? p2 : p1
rem_seg_refs(old_p, other_p)
add_seg_refs(new_p, other_p)
log('seg end moved: {0}-{1} -> {2}-{3}', p1.i, p2.i, other_p.i, new_p.i)
return old_p
}
// model fixing --------------------------------------------------------
function remove_isolated_points() {
rem_points(p => p.segs.length == 0, 'isolated points')
}
function remove_null_segs() {
rem_segs(is_null, 'null segs')
}
function remove_angled_segs() {
rem_segs((p1, p2) => seg_dir(p1, p2) == '/', 'angled segs')
}
// NOTE: assumes segs are sorted, null segs removed, points deduplicated
function merge_colinear_segs() {
for (let p of ps) {
if (p.segs.length == 2) {
let s1 = p.segs[0]
let s2 = p.segs[1]
if (is_v(s1) && is_v(s2) || is_h(s1) && is_h(s2)) { // colinear
let p1i = 1-seg_pi(s2, p)
let p2 = s2[p1i]
push_log()
set_seg_point(s1, p1i, p2)
rem_seg(s2)
pop_log('segs merged: {0} + {1} => {0}', s1.i, s2.i, s1.i)
}
}
}
}
// shortens seg at m with new point and adds new seg without detaching the original seg's end-points.
function split_seg_at(p1, p2, m) {
push_log()
let m1 = seg_m1(p1, p2)
let m2 = seg_m2(p1, p2)
let x = is_v(p1, p2) ? seg_x1(p1, p2) : m
let y = is_v(p1, p2) ? m : seg_y1(p1, p2)
let new_p = add_point(x, y)
let old_p = set_seg_point(p1, p2, seg_p2(p1, p2), new_p)
add_seg(new_p, old_p)
pop_log('seg split: {0} {4}>{1}<{5} => {2} {3}', p1.i+'-'+p2.i, m, p1.i+'-'+p2.i, new_p.i+'-'+old_p.i, m1, m2)
}
// NOTE: only works for h and v segs.
function split_intersecting_segs_on(v) {
push_log()
let segs = []
for (let p1 of ps) {
for (let p2 of p1.adj) {
if (is_v(p1, p2) == v) {
for (let p3 of ps) {
for (let p4 of p3.adj) {
if (is_v(p3, p4) != v) {
let m1 = seg_m1(p1, p2)
let m2 = seg_m2(p1, p2)
let bm1 = seg_m1(p3, p4)
let bm2 = seg_m2(p3, p4)
let a = seg_axis(p1, p2)
let ba = seg_axis(p1, p2)
if (bm1 <= a && bm2 >= a && ba > m1 && ba < m2) {
// splitting adds a seg at the end of segs array which will
// be also tested in the outer loop and possibly split further.
// the shortened seg is potentially split multiple times
// in this inner loop.
split_seg_at(p1, p2, ba)
}
}
}
}
}
}
}
pop_log('split all intersecting segs')
}
function split_intersecting_segs() {
split_intersecting_segs_on(0)
split_intersecting_segs_on(1)
}
// NOTE: leaves isolated points behind.
function points_cmp(p1, p2) {
if (p1[0] < p2[0]) return -1
if (p1[0] > p2[0]) return 1
if (p1[1] < p2[1]) return -1
if (p1[1] > p2[1]) return 1
if (p1.i < p2.i) return -1
if (p1.i > p2.i) return 1
return 0
}
function deduplicate_points() {
push_log()
ps.sort(points_cmp)
let p0
for (let i = 0; i < ps.length; i++) {
let p = ps[i]
if (p0 && points_equal(p, p0)) { // replace p with p0
for (let j = 0; j < p.adj.length; j++) { // each connected seg
let p2 = p.adj[j]
for (let i = 0; i < 2; i++) // each seg end
if (seg[i] == p) {
set_seg_point(seg, i, p0, 'seg end point dedup')
j-- // because seg was just removed from p.segs
}
}
continue
}
p0 = p
}
pop_log('deduplicate all points')
}
// NOTE: requires no intersecting segments.
function break_overlapping_segs() {
push_log()
let segs = []
for (let p1 of ps)
for (let p2 of p1.adj)
segs.push(p1, p2)
segs.sort(function(s1, s2) {
let v1 = is_v(s1)
let v2 = is_v(s2)
// level 1 grouping by direction
if (v1 < v2) return -1
if (v1 > v2) return 1
let m1 = seg_axis(s1)
let m2 = seg_axis(s2)
// level 2 grouping by axis
if (m1 < m2) return -1
if (m1 > m2) return 1
// level 3 grouping by starting point because most segments are
// non-overlapping and we want to skip those quickly.
let i = v1 ? 1 : 0
let c1 = min(s1[0][i], s1[1][i])
let c2 = min(s2[0][i], s2[1][i])
if (c1 < c2) return -1
if (c1 > c2) return 1
return 0
})
let i0, v0, m0
for (let i = 0, n = segs.length; i <= n; i++) {
let seg = segs[i]
let v = seg ? is_v(seg) : null
let m = seg ? seg_axis(seg) : null
if (v0 == null) {
i0 = i
v0 = v
m0 = m
} else if (v != v0 || m != m0) {
if (i >= i0 + 2) { // there's at least 2 segments on this axis
for (let j = i0+1; j < i; j++) {
let seg1 = segs[j]
let seg0 = segs[j-1]
let m1_1 = seg_m1(seg1)
let m2_0 = seg_m2(seg0)
if (m1_1 < m2_0) {
// seg points that are overlapping segs have no _|_ joints
// or they wouldn't be overlapping the seg, so it's safe
// to remove the overlapping seg as long as we elongate
// the overlapped seg.
log('segs overlap: {0} {1}: {2}<={3}: seg {1} removed, seg {0}.m2 set to {4}',
seg0.i, seg1.i, m1_1, m2_0, seg_m2(seg1))
seg1.removed = true
set_seg_m2(seg0, max(seg_m2(seg1), seg_m2(seg0)))
}
}
}
i0 = i
v0 = v
m0 = m
}
}
rem_marked_segs()
pop_log('breaking overlapping colinear segs')
}
// finding graph components --------------------------------------------
function add_comp() {
let comp = {}
comp.i = comps.length
comp.ps = []
comp.cycles = []
comp.segs = []
comps.push(comp)
return comp
}
// NOTE: needs adj refs
function find_comps() {
for (let p of ps)
p.visited = false
comps.length = 0
function dfs(p, ps, segs) {
p.visited = true
ps.push(p)
for (let seg of p.segs)
segs.add(seg)
for (p of p.adj)
if (!p.visited)
dfs(p, ps, segs)
}
for (let p of ps) {
if (!p.visited) {
let comp = add_comp()
let segs = set()
dfs(p, comp.ps, segs)
comp.segs = set_toarray(segs)
log('comp {0}: {1}', comp.i, comp.ps.map(p=>p.i).join(' '))
}
}
}
// finding which components are inside islands -------------------------
function segs_bb(comp) {
let bb = bbox()
for (let seg of comp.segs) {
let [x1, y1] = seg[0]
let [x2, y2] = seg[1]
bbox_add_bbox(bb, x1, y1, x2, y2)
}
return bb
}
function skin_bb(comp) {
let bb = bbox()
for (let dp of comp.outer_cycle.edges) {
let [dx, dy] = dp
let [x, y] = dp.p
bbox_add_point(bb, x + dx, y + dy)
}
return bb
}
function plan_bb() {
let bb = bbox()
for (let c of comps) {
bbox_add_bbox(bb, ...c.bb)
}
return bb
}
function bb_inside(cbb, pbb) {
let [px1, py1, px2, py2] = pbb
let [cx1, cy1, cx2, cy2] = cbb
return cx1 >= px1 && cx2 <= px2 && cy1 >= py1 && cy2 <= py2
}
function point_inside(p, ps) {
let [x, y] = p
let inside = false
for (let i = 0, j = ps.length - 1; i < ps.length; j = i++) {
let xi = ps[i][0]
let yi = ps[i][1]
let xj = ps[j][0]
let yj = ps[j][1]
let intersect = ((yi > y) != (yj > y)) && (x < (xj - xi) * (y - yi) / (yj - yi) + xi)
if (intersect)
inside = !inside
}
return inside
}
function comp_inside(c, p) {
if (!bb_inside(c.bb, p.bb))
return false
return point_inside(c.ps[0], p.outer_cycle)
}
function find_inside_comps() {
// seg-based bbox: enough for computing inside flag.
for (let c of comps) {
c.inside = false
c.bb = segs_bb(c)
}
level_plan.bb = plan_bb()
for (let c of comps) {
if (c.inside)
continue
for (let p of comps) {
if (p == c)
continue
if (comp_inside(c, p)) {
c.inside = true
log('comp {0} is inside', c.i)
}
}
}
}
// plan loading & validation -------------------------------------------
function extract_cycles() {
for (let c of comps)
extract_outer_cycle_for(c)
rebuild_adj_refs(ps)
for (let c of comps)
extract_cycles_for(c)
rebuild_adj_refs(ps)
if (0)
for (let co of comps)
for (let c of comp.cycles)
log('cycle {0}/{1} {2} {3} {4}: {5}', co.i, c.i,
is_cw(c) ? 'cw' : 'ccw',
c.outer ? 'outer' : '',
c.inside ? 'inside' : '',
c.map(p=>p.i).join(' ')
)
}
function create_edges_for(c) {
c.edges = offset_poly(c, (c.outer ? (c.comp.inside ? -4 : -12) : -4))
}
function create_edges() {
for (let comp of comps) {
for (let c of comp.cycles) {
create_edges_for(c)
create_edges_for(comp.outer_cycle)
}
}
// recompunte bbox now that we have the skin.
for (let c of comps)
c.bb = skin_bb(c)
level_plan.bb = plan_bb()
// recompute house plan bbox.
if (house_plan.bb)
house_plan.bb = recompute_house_plan_bbox()
}
level_plan.snap_lines = []
function fix_level() {
split_intersecting_segs()
break_overlapping_segs()
remove_null_segs()
deduplicate_points()
merge_colinear_segs()
remove_isolated_points()
rebuild_adj_refs(ps)
find_comps()
extract_cycles()
find_inside_comps()
create_edges()
level_plan.snap_lines[0] = snap_lines_for(1)
level_plan.snap_lines[1] = snap_lines_for(0)
}
// plan view UI ops ----------------------------------------------------
// Find and fix the the cycles that contain the sequence (p0,p1,p2) or (p2,p1,p0).
// If (p0,p1,p2) is found then the cycle is to the left of the sequence if it's an inner cycle.
// If (p2,p1,p0) is found then the cycle is to the right of the sequence if it's an inner cycle.
// It's the opposite if it's an outer cycle.
// The same cycle will contain the sequence twice (once as is once in reverse)
// if the sequence is (part of) a filament.
function fix_cycle(c, i, fw, action, new_p, p0, p1, p2) {
if (action == 'replace') {
log('cycle point replaced: {0}/{1}: {2}->{3}', c.i, i, c[i].i, new_p.i)
c[i] = new_p
} else if (action == 'insert') {
i = fw ? i : i+1 // insert point in the cycle array
log('cycle point inserted: {0}/{1} (before {2}): {3}', c.i, i, c[i] ? c[i].i : 'end', new_p.i)
insert(c, i, new_p)
}
}
function fix_cycles_containing(p0, p1, p2, action, new_p) {
push_log()
for (let comp of comps)
for (let c of comp.cycles) {
let i0 = 0
while (1) {
let i = c.indexOf(p1, i0)
if (i == -1)
break
let fp0 = c[mod(i-1, c.length)]
let fp2 = c[mod(i+1, c.length)]
if (fp0 == p0 && fp2 == p2) fix_cycle(c, i, 1, action, new_p, p0, p1, p2)
if (fp0 == p2 && fp2 == p0) fix_cycle(c, i, 0, action, new_p, p2, p1, p0)
i0 = i+1
}
}
pop_log('all cycles containing ({0},{1},{2}): {3} with {4}', p0.i, p1.i, p2.i, action, new_p.i)
}
// colinear segs directly end-to-end tied to the segment we want to move
// must be separated by addidng a _|_ seg in between so they're not dragged along.
// NOTE: do not deduplicate points after this!
function detach_seg_at(seg, i, p00) {
push_log()
let p = seg[i]
let p0 = seg[1-i]
let new_p = add_point(p[0], p[1])
// each side of (p0,p) needs a different kind of fixing depending on
// whether there's a _|_ seg at the separation point on that side or not.
if (p.adj.length > 2) {
for (let cw = 0; cw <= 1; cw++) {
let p1 = next_adj(p0, p, cw, 1)
fix_cycles_containing(p0, p, p1 ?? p00, p1 ? 'replace' : 'insert', new_p)
}
} else { // no _|_ segs on the sides.
fix_cycles_containing(p0, p, p00, 'insert', new_p)
}
set_seg_point(seg, i, new_p, 1)
let new_seg = add_seg(p, new_p)
pop_log('seg detached: {0}', seg.i)
}
function opposite_seg(seg, i) {
let v = is_v(seg)
let p = seg[i]
for (let seg1 of p.segs) { // each segment connected to that end-point
if (seg1 == seg) // itself
continue
if (is_v(seg1) != v) // not colinear
continue
if (is_null(seg1)) // just added
continue
return seg1
}
}
function detach_opposite_seg(seg, i) {
let seg1 = opposite_seg(seg, i)
if (!seg1) return
let p = seg[i]
if (seg1[0] == p) detach_seg_at(seg1, 0, seg[1-i])
if (seg1[1] == p) detach_seg_at(seg1, 1, seg[1-i])
}