-
Notifications
You must be signed in to change notification settings - Fork 0
/
ccc3.html
2846 lines (2349 loc) · 65.9 KB
/
ccc3.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>
<head>
<base href="www2/">
<link rel="icon" href="../www/favicon256.svg">
<link rel="preload" href="icons/fa-regular-400.woff2" as="font" type="font/woff2" crossorigin>
<link rel="preload" href="icons/fa-solid-900.woff2" as="font" type="font/woff2" crossorigin>
</head>
<body>
<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 src=../www/earcut.js></script>
<script src=../www/3d.js global></script>
<script src=../www/plane-graph.js></script>
<script src=../www/gl.js></script>
<script src=../www/gl-renderer.js></script>
<script src=../www/model3.js></script>
<script src=../www/suncalc.js></script>
<script src=../www/modeleditor.js></script>
<script>
(function () {
"use strict"
const G = window
let out = []
G.DEBUG_PLAN = 0
G.DEBUG_PLAN_LOAD = 1
let floor_names = [
'GROUND FLOOR',
'1ˢᵗ FLOOR',
'2ⁿᵈ FLOOR',
'3ʳᵈ FLOOR',
]
function seg_is_v(p1, p2) { return near(p1[0], p2[0]) }
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 seg_is_v(seg[0], seg[1]) ? seg_x1(seg) : seg_y1(seg) }
function seg_m1 (seg) { return seg_is_v(seg[0], seg[1]) ? seg_y1(seg) : seg_x1(seg) }
function seg_m2 (seg) { return seg_is_v(seg[0], seg[1]) ? seg_y2(seg) : seg_x2(seg) }
function set_seg_axis(seg, a) {
if (seg_is_v(seg[0], seg[1])) {
seg[0][0] = a
seg[1][0] = a
} else {
seg[0][1] = a
seg[1][1] = a
}
}
function seg_pi(seg, p) {
assert(seg[0] == p || seg[1] == p)
return seg[0] == p ? 0 : 1
}
function seg_i1(seg) { let mi = seg_is_v(seg[0], seg[1]) ? 1 : 0; return seg[0][mi] < seg[1][mi] ? 0 : 1 }
function seg_i2(seg) { let mi = seg_is_v(seg[0], seg[1]) ? 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) {
return line_middle(seg[0], seg[1])
}
let NEAR = 1e-5
function near(a, b) { return abs(a - b) < NEAR }
// face plan model utils -----------------------------------------------------
// NOTE: face is 0,1,2,3 going clockwise from the bottom-side face.
let face_is_v = i => i & 1 // 0,1,2,3 -> 0,1,0,1
let face_sign = i => (i & 2) - 1 // 0,1,2,3 -> -1,-1,1,1
let face_axis_sign = i => ((i + 3) & 2) - 1 // 0,1,2,3 -> 1,-1,-1,1
// view utils ----------------------------------------------------------------
let format_length = d => format_kcount(d / 1e2, 2)+'m'
let format_area = a => format_kcount(abs(a) / 1e4, 2)+'m²'
function line_middle(p1, p2) {
let [x1, y1] = p1
let [x2, y2] = p2
return [
(x2 + x1) / 2,
(y2 + y1) / 2,
]
}
function seg_center(seg) {
return line_middle(seg[0], seg[1])
}
// house model ---------------------------------------------------------------
let defaults = {
floor_h: 250,
int_wall_thickness: 8,
ext_wall_thickness: 18,
}
function poly_get_point(i, out) {
out[0] = this[i][0]
out[1] = this[i][1]
return out
}
function house(house) {
for (let k in defaults)
if (!(k in house))
house[k] = defaults[k]
function house_bb() {
let bb = bbox2()
for (let floor of house.floors)
bb.add_bbox2(...floor.bb)
return bb
}
function create_floor(t, floor_i) {
let floor = plane_graph()
floor.orthogonal = true
floor.house = house
floor.roofs = t.roofs
floor.i = floor_i
floor.id = 'floor'+floor_i
floor.h ??= house.floor_h
floor.fixed_h = t.h
floor.bb = bbox2()
floor.snap_lines = []
floor._after_fix = function() {
create_edges()
fix_roofs()
floor.snap_lines[0] = snap_lines_for(1)
floor.snap_lines[1] = snap_lines_for(0)
for (let face of house.faces)
face.fix()
}
function fix_areas_for(comp) {
for (let c of comp.cycles) {
if (c.outer)
continue
let a0 = c.edges.area()
for (let icomp of c.islands) {
fix_areas_for(icomp)
c.edges._area -= icomp.outer_cycle.edges.area()
}
let a1 = c.edges.area()
if (0 && a1 != a0)
log('cycle', c.id, 'area fixed:', a1, '+', a0-a1, '=', a0)
}
}
function create_edges() {
for (let p of floor.ps)
p.max_offset = 0
let t_int = house.int_wall_thickness
let t_ext = house.ext_wall_thickness
let o_int = t_int / 2
let o_ext = t_ext - o_int
for (let comp of floor.comps) {
for (let c of comp.cycles) {
let edges = poly2()
edges.get_point = poly_get_point
c.edges = c.offset(c.outer ? (c.comp.inside ? -o_int : -o_ext) : -o_int, edges)
}
}
for (let c of floor)
fix_areas_for(c)
// compute the level plan bbox now that we have outer skins.
floor.bb.reset()
for (let co of floor.comps)
floor.bb.add_bbox2(...co.outer_cycle.edges.bbox())
// recompute house plan bbox.
if (house.bb) // not in init
house.bb = house_bb()
}
// roofs ---------------------------------------------------------------
function roof_ridge_is_v(roof) { return roof.axis == 'v' }
function gable_roof_ridge_axis(roof) {
let M1 = roof_ridge_is_v(roof) ? 0 : 1
let m1 = roof.bb[M1]
let m2 = roof.bb[M1+2]
return (m1 + m2) / 2
}
function gable_roof_height_at(m, roof) {
let a = roof.pitch * rad
let M1 = roof_ridge_is_v(roof) ? 0 : 1
let m1 = roof.bb[M1]
let m2 = roof.bb[M1+2]
let mc = (m2 + m1) / 2
return round(tan(a) * ((m2 - mc) - abs(m - mc)))
}
function gable_roof_height_at_point(px, py, roof) {
let m = roof_ridge_is_v(roof) ? px : py
return gable_roof_height_at(m, roof)
}
function roof_section() {
}
function init_roofs() {
if (!floor.roofs)
return
push_log('init roofs')
for (let roof of floor.roofs) {
roof.box = bbox2(...roof.box)
fix_roof(roof)
}
pop_log()
}
function fix_roof(roof) {
//
}
function fix_roofs() {
if (!floor.roofs)
return
for (let roof of floor.roofs)
fix_roof(roof)
}
// 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.
// NOTE: replacing a point in the cycle (instead of always adding one) makes the cycle
// technically invalid (as it's skipping a point) but we do it to keep the offset edge
// stable at that corner while dragging the segment.
function fix_cycle(c, i, fw, action, new_p, p0, p1, p2) {
if (action == 'replace') {
log('cycle point replaced:', c.id, '/', i, ':', c[i].id, '->', new_p.id)
c[i] = new_p
} else if (action == 'insert') {
i = fw ? i : i+1 // insert point in the cycle array
log('cycle point inserted:', c.id, '/', i, '(before ', c[i] ? c[i].id : 'end','):', new_p.id)
insert(c, i, new_p)
}
}
function fix_cycles_containing(p0, p1, p2, action, new_p) {
push_log('fix all cycles containing (', p0.id, p1.id, p2.id, ':', action, 'with', new_p.id)
for (let comp of floor.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()
}
// 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('detach seg:', seg.id)
let p = seg[i]
let p0 = seg[1-i]
let new_p = floor.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 = floor.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)
}
floor.set_seg_point(seg, i, new_p, 1)
let new_seg = floor.add_seg(p, new_p)
pop_log()
}
function points_equal(p1, p2) { return p1[0] == p2[0] && p1[1] == p2[1] }
function opposite_seg(seg, i) {
let v = seg_is_v(seg[0], seg[1])
let p = seg[i]
for (let seg1 of p.segs) { // each segment connected to that end-point
if (seg1 == seg) // itself
continue
if (seg_is_v(seg1[0], seg1[1]) != v) // not colinear
continue
if (points_equal(seg1[0], seg1[1])) // 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])
else
if (seg1[1] == p) detach_seg_at(seg1, 1, seg[1-i])
}
function detach_colinear_segs(seg) {
detach_opposite_seg(seg, 0)
detach_opposite_seg(seg, 1)
}
function segs_overlap(am1, am2, bm1, bm2) { // check if two segments overlap
return !(am2 <= bm1 || bm2 <= am1)
}
function snap_lines_for(v, exclude_seg) {
let mi = v ? 0 : 1
let ms = []
for (let floor of house.floors) {
for (let p of floor.ps) {
if (exclude_seg && (exclude_seg[0] == p || exclude_seg[1] == p))
continue
let m = p[mi]
ms.push(m)
}
}
ms.sort()
uniq_sorted(ms)
ms.snap = function(m, snap_d) {
let min_d = 1/0
let min_m
for (let m1 of ms) {
let d = abs(m - m1)
if (d <= snap_d && d < min_d) {
min_d = d
min_m = m1
}
}
return min_m // ?? snap(m, 10)
}
ms.v = v
return ms
}
function snap_x(x, nosnap) {
let snap_d = nosnap ? 2 : 20
return floor.snap_lines[0].snap(x, snap_d)
}
function snap_y(y, nosnap) {
let snap_d = nosnap ? 2 : 20
return floor.snap_lines[1].snap(y, snap_d)
}
floor.snap_x = snap_x
floor.snap_y = snap_y
function seg_move_bump_limit(seg, sign) {
let v = seg_is_v(seg[0], seg[1])
let a = seg_axis(seg)
let min_a1 = sign * 1/0
let s_m1 = seg_m1(seg)
let s_m2 = seg_m2(seg)
for (let seg1 of floor.segs) {
let s1_m1 = seg_m1(seg1)
let s1_m2 = seg_m2(seg1)
let a1 = seg_axis(seg1)
if (seg_is_v(seg1[0], seg1[1]) != v) {
let closest_m = sign < 0 ? max(s1_m1, s1_m2) : min(s1_m1, s1_m2)
s1_m1 = a1
s1_m2 = a1
a1 = closest_m
}
if (sign * a1 > sign * a && sign * a1 < sign * min_a1) {
if (segs_overlap(s_m1, s_m2, s1_m1, s1_m2)) {
min_a1 = a1
}
}
}
return min_a1
}
function seg_move_ranges(seg) {
let min_a = seg_move_bump_limit(seg, -1)
let max_a = seg_move_bump_limit(seg, 1)
let ranges = [
[min_a, max_a],
]
return ranges
}
function seg_resize_bump_limit(seg, p, sign) {
let v = seg_is_v(seg[0], seg[1])
let a = p[v ? 1 : 0]
let m = p[v ? 0 : 1]
let min_a1 = sign * 1/0
for (let seg1 of floor.segs) {
if (seg_is_v(seg1[0], seg1[1]) != v) {
let a1 = seg_axis(seg1)
if (sign * a1 > sign * a && sign * a1 < sign * min_a1) {
let m1 = seg_m1(seg1)
let m2 = seg_m2(seg1)
if (m >= m1 && m <= m2)
min_a1 = a1
}
}
}
return min_a1
}
function seg_resize_ranges(seg, p) {
let min_a = seg_resize_bump_limit(seg, p, -1)
let max_a = seg_resize_bump_limit(seg, p, 1)
let ranges = [
[min_a, max_a],
]
return ranges
}
function closest_range(a, ranges) {
let min_range
let min_d = 1/0
for (let range of ranges) {
let [a1, a2] = range
if (a >= a1 && a <= a2) { // in range
min_range = range
break
}
let d = max(0, min(abs(a1 - a), abs(a2 - a))) // distance to range's closest end-point
if (d < min_d) {
min_range = range
min_d = d
}
}
return min_range
}
function snap_seg(a, ranges, snap_lines, draw_state, snap_d) {
let snap_range = closest_range(a, ranges)
let [min_a, max_a] = snap_range
a = clamp(a, min_a, max_a)
let sa = snap_lines.snap(a, ui.key('shift') ? 2 : snap_d)
draw_state.snap_lines.length = 0
if (sa != null)
draw_state.snap_lines.push(sa, snap_lines.v)
return sa
}
// find the seg in the left, right, top, bottom side around p.
function seg_around_point(p, sdx, sdy) {
let p0 = [p[0] + sdx, p[1] + sdy]
let p1 = floor.next_adj(p0, p, 1, 0)
// find the seg of (p, p1)
for (let seg of p.segs)
if (seg[0] == p && seg[1] == p1 || seg[1] == p && seg[0] == p1)
return seg
}
function start_move_seg(seg, draw_state) {
push_log('START MOVE SEG:', seg.id)
detach_colinear_segs(seg)
pop_log()
let move_freely = seg[0].segs.length == 1 && seg[1].segs.length == 1
let seg0 = [[...seg[0]], [...seg[1]]]
let ranges = seg_move_ranges(seg)
let snap_lines = snap_lines_for(seg_is_v(seg[0], seg[1]), move_freely && seg)
let v = seg_is_v(seg0[0], seg0[1])
let m_snap_lines = move_freely && snap_lines_for(!seg_is_v(seg[0], seg[1]), seg)
// find all affected _|_ edges so we can show their lengths as we move the seg
{
let perp_edges = []
// find the farthest edge point from `c.edges[i0]` going in `dir` direction
// in the array, which has the same spatial direction `v` with the previous point.
function last_perp_edge_point(c, i, n, dir, v) {
let ep0 = c.edges[mod(i, n)]
while (1) {
let ep1 = c.edges[mod(i, n)]
let ep2 = c.edges[mod(i+dir, n)]
if (ep2.p == ep0.p) // end-cap, don't measure
return
if (seg_is_v(ep1, ep2) != v) // changed direction, return last edge point
return ep1
i += dir
}
}
let sp1 = seg[0]
let sp2 = seg[1]
let v = seg_is_v(seg[0], seg[1])
for (let co of floor.comps) {
for (let c of co.cycles) {
for (let i = 0, n = c.edges.length; i <= n; i++) {
let ep1 = c.edges[(i+0) % n]
let ep2 = c.edges[(i+1) % n]
if (!(ep1.p == sp1 && ep2.p == sp2 || ep1.p == sp2 && ep2.p == sp1)) // not seg's edge
continue
let ep0 = last_perp_edge_point(c, i+0, n, -1, !v)
let ep3 = last_perp_edge_point(c, i+1, n, 1, !v)
if (!(ep0 || ep3))
continue
if (0) {
// remove measurement of parallel wall of the same length
let A = v ? 0 : 1 // index of cross axis of perp segs
let dupe = ep0 && ep3 && ep0.p[A] == ep3.p[A]
if (dupe)
ep3 = null
}
if (ep0) perp_edges.push([ep0, ep1])
if (ep3) perp_edges.push([ep2, ep3])
}
}
}
draw_state.measure_edges = perp_edges
}
let s = {}
s.move = function(dx, dy, draw_state) {
let dm = v ? dy : dx
let da = v ? dx : dy
if (move_freely) {
let a = seg_axis(seg0) + da
let sa = snap_lines.snap(a) ?? a
a = sa ?? a
// snap both end-points on main-axis.
let m1 = seg_m1(seg0) + dm
let m2 = seg_m2(seg0) + dm
let m1s = m_snap_lines.snap(m1)
let m2s = m_snap_lines.snap(m2)
if (m1s != null && m2s != null) // both ends snapped
if (abs(m1s - m1) <= abs(m2s - m2)) // pick the one closer to snap line
m2s = null
else
m1s = null
if (m1s != null)
m2s = m1s + (seg_m2(seg0) - seg_m1(seg0))
else if (m2s != null)
m1s = m2s - (seg_m2(seg0) - seg_m1(seg0))
seg[0][0] = v ? a : m1s ?? m1
seg[1][0] = v ? a : m2s ?? m2
seg[0][1] = !v ? a : m1s ?? m1
seg[1][1] = !v ? a : m2s ?? m2
} else { // move on cross axis
let a = seg_axis(seg0) + (v ? dx : dy)
let sa = snap_seg(a, ranges, snap_lines, draw_state, 20)
a = sa ?? a
set_seg_axis(seg, a)
create_edges()
}
}
s.stop = function(draw_state, remove_seg) {
push_log('STOP MOVE SEG:', seg.id)
if (remove_seg)
rem_seg(seg)
floor.fix()
pop_log()
draw_state.snap_lines.length = 0
draw_state.measure_edges.length = 0
}
return s
}
floor.start_move_seg = start_move_seg
function start_resize_seg(p, mx0, my0, dx, dy, sdx, sdy, draw_state) {
// based on direction of drag, either detach a seg or add a seg, and start resizing it.
push_log('START RESIZE SEG')
let seg
if (p) {
// find seg in the drag direction around p.
if (p.segs.length == 1 && seg_is_v(p.segs[0][0], p.segs[0][1]) == !!sdy) {
// p is a free end-point and we're dragging along its main axis: resize it.
seg = p.segs[0]
} else {
// find the seg around p in the direction of drag: that's the segment we want to detach and resize.
seg = seg_around_point(p, sdx, sdy)
if (seg) {
// detach seg at branch point p so we can then resize it by its free end-point.
let x = p[0] + dx * abs(sdx)
let y = p[1] + dy * abs(sdy)
let new_p = floor.add_point(x, y)
floor.set_seg_point(seg, seg_pi(seg, p), new_p)
p = new_p
}
}
}
if (!seg) { // create new seg, anchored to "hovered point" or to "mouse pos when clicked"
let x1, y1
if (p) {
[x1, y1] = p
} else {
x1 = mx0
y1 = my0
}
let x2 = x1 + dx * abs(sdx)
let y2 = y1 + dy * abs(sdy)
let p1 = floor.add_point(x1, y1)
let p2 = floor.add_point(x2, y2)
seg = floor.add_seg(p1, p2)
p = p2
}
log('resizing seg:', seg.id)
let p0 = [...p]
floor.fix()
pop_log()
let ranges = seg_resize_ranges(seg, p)
let snap_lines = snap_lines_for(!seg_is_v(seg[0], seg[1]), seg)
let s = {seg: seg}
s.resize = function(dx, dy, draw_state, hs) {
let x1 = p0[0] + dx * abs(sdx)
let y1 = p0[1] + dy * abs(sdy)
let a = sdx ? x1 : y1
let sa = snap_seg(a, ranges, snap_lines, draw_state, 30)
if (sa != null)
a = sa
if (sdx)
x1 = a
else
y1 = a
p[0] = x1
p[1] = y1
hs.set('x', x1)
hs.set('y', y1)
create_edges()
}
s.stop = function(draw_state) {
push_log('STOP RESIZE SEG:', seg.id)
floor.fix()
pop_log()
draw_state.snap_lines.length = 0
draw_state.measure_edges.length = 0
}
return s
}
floor.start_resize_seg = start_resize_seg
// init ----------------------------------------------------------------
push_log('LOADING LEVEL', floor.i)
floor.load(t.points, t.lines)
init_roofs()
pop_log()
return floor
}
// face -------------------------------------------------------------------
// returns x1, y1, x2, y2, min_depth, max_depth
function face_bb(vert) {
let [x1, y1, x2, y2] = house.bb
let h = house.h
if (vert)
return [y1, 0, y2, h, x1, x2]
else
return [x1, 0, x2, h, y1, y2]
}
function p_x(p) { return p[0] }
function p_y(p) { return p[1] }
function create_face(face_i) {
let fv = face_is_v(face_i)
let fs = face_sign(face_i)
let fas = face_axis_sign(face_i)
let bb = bbox2()
let ep_m = fv ? p_y : p_x
let ep_axis = fv ? p_x : p_y
function ep_h(ep) {
return ep.p.h ?? 0
}
function edge_sign(ep1, ep2) {
return ep_m(ep1) < ep_m(ep2) ? 1 : -1
}
let walls = [] // exterior wall horizontal measurements
let planes = []
let face = {house: house, i: face_i, bb: bb, planes: planes}
function create_face_planes() {
return // TODO
face.bb = face_bb(fv) // TODO: reuse bb
walls.length = 0
planes.length = 0
// find outer edges that are projecting in the face's direction.
for (let floor of house.floors) {
for (let comp of floor.comps) {
if (comp.inside)
continue
let eps = comp.outer_cycle.edges
for (let i = 0, n = eps.length; i < n; i++) {
let ep1 = eps[(i+0) % n]
let ep2 = eps[(i+1) % n]
if (seg_is_v(ep1, ep2) != fv) // _|_ edge: invisible
continue
if (edge_sign(ep1, ep2) != fs) // back-face edge: obscured: cull it
continue
let last_wall = walls[walls.length-1]
let last_ep2 = last_wall && last_wall[1]
if (last_ep2 == ep1) // in-wall point: skip (i.e. merge segs)
last_wall[1] = ep2
else
walls.push([ep1, ep2, floor, comp])
}
// merge first and last segs if necessary.
let first_wall = walls[0]
let last_wall = walls[walls.length-1]
if (last_wall[1] == first_wall[0]) {
first_wall[0] = last_wall[0]
walls.pop()
}
}
}
// pr(face, walls.map(w => w[2].i+':'+w[0].p.i+'-'+w[1].p.id).join(' '))
// sort walls by depth.
walls.sort(function(w1, w2) {
// 1st level grouping: by depth (asc order).
let y1 = ep_axis(w1[0])
let y2 = ep_axis(w2[0])
let dy = fas * (y1 - y2)
if (dy) return dy
// 2nd level grouping: by floor (desc order).
let l1 = w1[2].i
let l2 = w2[2].i
return l2 - l1
})
let eq_depth = (w1, w2) => ep_axis(w1[0]) == ep_axis(w2[0])
let eq_floor = (w1, w2) => w1[2] == w2[2]
// create face polygons by merging the faces that are on the same vertical plane.
for (let [j1, j2] of group_sorted(walls, eq_depth)) {
// 1st level grouping: j1,j2 is all walls of a single depth plane.
push_log('FACE PLANE', 'face=', face_i, 'plane=', planes.length, 'depth=', ep_axis(walls[j1][0]), 'i=', j1, '..', j2)
let plan = plane_graph()
plan.depth = ep_axis(walls[j1][0])
function add_seg(x1, y1, x2, y2, ep1, ep2, ...log_args) {
push_log(...log_args, ':', x1, y1, '-', x2, y2)
let p1 = plan.add_point(x1, y1)
let p2 = plan.add_point(x2, y2)
// set back refs to origin eps for quick updating
p1.ep1 = ep1
p2.ep2 = ep2
plan.add_seg(p1, p2)
pop_log()
}
let last_i1, last_i2, last_floor_i
for (let [i1, i2] of group_sorted(walls, eq_floor, j1, j2)) {
// 2nd level grouping: i1,i2 is all walls of a single floor of this depth plane.
let floor = walls[i1][2]
let h = floor.h
let y = floor.y
let y1 = y
let y2 = y + h
for (let i = i1; i < i2; i++) {
// make vertical segments (these are just constructed, no merging).
let [ep1, ep2] = walls[i]
let x1 = ep_m(ep1)
let x2 = ep_m(ep2)
let y11 = y1 - ep_h(ep1)
let y12 = y1 - ep_h(ep2)
// pr(face_i, floor.i, x1, x2, ep_h(ep1), ep_h(ep2))
add_seg(x1, y11, x1, y2, ep1, ep2, 'vertical of floor', floor.i)
add_seg(x2, y12, x2, y2, ep1, ep2, 'vertical of floor', floor.i)
// for top and bottom floors whichever they are in this particular
// depth plane (might not be all floors), make top and bottom segments.
if (last_floor_i != floor.i+1) // top line
add_seg(x1, y11, x2, y12, ep1, ep2, 'top of floor', floor.i)
if (i2 == j2 || last_floor_i != null && last_floor_i != floor.i+1) // bottom line
add_seg(x1, y2, x2, y2, ep1, ep2, 'bottom of floor', floor.i)
}
// merge segments between this floor and the last floor.
// adding all end-points from the segments of both floors
// and sorting them gives us exactly the segments we want,
// no analyzing intersections necessary.
if (last_floor_i == floor.i+1) {
let a = []
for (let i = i1; i < i2; i++)
a.push(walls[i][0], walls[i][1])
for (let i = last_i1; i < last_i2; i++)
a.push(walls[i][0], walls[i][1])
a.sort((ep1, ep2) => ep_m(ep1) - ep_m(ep2))
for (let i = 0, n = a.length; i < n; i += 2) {
let ep1 = a[i]
let ep2 = a[i+1]
let x1 = ep_m(ep1)
let x2 = ep_m(ep2)
if (x1 != x2)
add_seg(x1, y1, x2, y1, ep1, ep2, 'between floors', last_floor_i, 'and', floor.i)
}
}
last_i1 = i1
last_i2 = i2
last_floor_i = floor.i
}
plan.fix()
planes.push(plan)
pop_log()
}
}
function wall_face_rect(plan, lv, wr, i, scale) {
let h = pix(lv.h, scale)
let v = wall_vert(wr, i)
let p1 = wall_p1(wr, i+0, true, null, scale)
let p2 = wall_p1(wr, i+1, true, null, scale)
let X = v ? 1 : 0
let Y = v ? 0 : 1
let x1 = p1[X]
let x2 = p2[X]
let x = min(x1, x2)
let w = max(x1, x2) - x
let y = 0
return [x, y, w, h, p1[Y]] // (x, y, w, h, z)
}
face.fix = create_face_planes
return face
}
// init -------------------------------------------------------------------
function init() {
let t0 = clock()
push_log_if(DEBUG_PLAN_LOAD, 'LOADING PLAN')
let floors = house.floors
house.floors = []
house.faces = []
for (let n = floors.length, i = n-1; i >= 0; i--) {
let floor = create_floor(floors[i], i)
house.floors[i] = floor
}
for (let floor of house.floors)
floor.fix()
let y = 0
for (let n = house.floors.length, i = n-1; i >= 0; i--) {
let floor = house.floors[i]
floor.y = y
y += floor.h
}
house.h = y
for (let i = 0; i <= 3; i++)
house.faces.push(create_face(i))
house.bb = house_bb()
for (let face of house.faces)
face.fix()
let dt = round((clock() - t0) * 1000)
log('TIME', ('*').repeat(dt), dt, 'ms')
pop_log()
}
init()
return house
}
// shared scale --------------------------------------------------------------
function scaler(name) {
let s = {}
let line_width = 2 // thickness of wall line stroke in pixels at 1:1 scale
let scale, margin
s.d = d => round(d * scale)
s.x = x => round(x * scale)
s.y = y => round(y * scale)
s.plan_x = x => x / scale
s.plan_y = y => y / scale
s.plan_d = d => d / scale
s.transform = function(cx, w, h) {
if (s.rotation) {
let xc = round(w / 2)
let yc = round(h / 2)
cx.translate(xc, yc)
cx.rotate(s.rotation)
cx.translate(-xc, -yc)
}
cx.translate(s.x0, s.y0)
}
function set_scale(sc) {
scale = sc
s.scale = sc
// snap line width to an even number for sharpness
s.line_width = snap(line_width * sc, 2)
}
let bb = bbox2()
s.scale_to_fit = function(bb1, w, h) {
if (s.zoomed) return
let margin = ceil(ui.em(2) + line_width)
w -= 2 * margin
h -= 2 * margin
array_set(bb, bb1)
bb.rotate(s.rotation)
let [x1, y1, x2, y2] = bb
let bw = x2 - x1