-
Notifications
You must be signed in to change notification settings - Fork 3
/
x-model3.js
1558 lines (1247 loc) · 37.5 KB
/
x-model3.js
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
/*
Polygon-based editable 3D model components.
Written by Cosmin Apreutesei. Public domain.
Components are made primarily of polygons enclosed and connected by
lines defined over a common point cloud. A component can contain instances
of other components at relative transforms. A component can also contain
standalone lines.
The editing API implements the direct manipulation UI and performs
automatic creation/removal/intersection of points/lines/polygons
while keeping the model numerically stable and clean. In particular:
- editing operations never leave duplicate points/lines/polygons.
- existing points are never moved when adding new geometry.
- when existing lines are cut, straightness is preserved to best accuracy.
*/
(function() {
model3_component = function(pe) {
assert(pe)
let e = {name: pe.name, editor: pe.editor}
let gl = assert(pe.gl)
let push_undo = pe.push_undo
function log(s, ...args) {
assert(LOG)
print(e.id, s, ...args)
}
// model (as in MVC and as in 3D model) -----------------------------------
let points = [] // [(x, y, z), ...]
let normals = [] // [(x, y, z), ...]; normals for smooth meshes.
let free_pis = [] // [p1i,...]; freelist of point indices.
let prc = [] // [rc1,...]; ref counts of points.
let lines = [] // [(p1i, p2i, rc, sm, op), ...]; rc=refcount, sm=smoothness, op=opacity.
let free_lis = [] // [l1i,...]; freelist of line indices.
let faces = [] // [poly3[p1i, p2i, ..., lis: [line1i,...], selected:, material:, ],...]
let free_fis = [] // [face1_index,...]
let meshes = set() // {{face},...}; meshes are sets of all faces connected by smooth lines.
let children = [] // [mat1,...]
// model-to-view info (as in MVC).
let used_points_changed // time to reload the used_pis buffer.
let used_lines_changed // time to reload the *_edge_lis buffers.
let faces_changed // time to rebuild faces geometry.
let edge_line_count = 0 // number of face edge lines.
let nonedge_line_count = 0 // number of standalone lines.
let bbox_changed // time to recompute the bbox.
let sel_child_count = 0
// low-level model editing API that:
// - records undo ops in the undo stack.
// - updates and/or invalidates any affected view buffers.
let point_count = () => prc.length
{
let _v0 = v3()
function get_point(pi, out) {
out = out || _v0
out[0] = points[3*pi+0]
out[1] = points[3*pi+1]
out[2] = points[3*pi+2]
out.i = pi
return out
}}
{
let _v0 = v3()
function add_point_xyz(x, y, z, expect_pi) {
return add_point(_v0.set(x, y, z), expect_pi)
}}
function add_point(p, expect_pi) {
assert(p.is_v3)
let pi = free_pis.pop()
if (pi == null) {
points.push(p[0], p[1], p[2])
normals.push(0, 0, 0)
pi = prc.length
} else {
p.to_v3_array(points, pi)
}
prc[pi] = 0
assert(expect_pi == null || pi == expect_pi)
update_point(pi, p)
if (LOG)
log('add_point', pi, p.x+','+p.y+','+p.z)
return pi
}
let unref_point = function(pi, expect_rc) {
let rc0 = prc[pi]--
assert(rc0 > 0)
assert(expect_rc == null || expect_rc == rc0)
if (rc0 == 1) {
free_pis.push(pi)
used_points_changed = true
let p = get_point(pi)
push_undo(add_point_xyz, p[0], p[1], p[2], pi)
}
push_undo(ref_point, pi, rc0 - 1)
// if (LOG) log('unref_point', pi, prc[pi])
}
let ref_point = function(pi, expect_rc) {
let rc0 = prc[pi]++
assert(expect_rc == null || expect_rc == rc0)
if (rc0 == 0)
used_points_changed = true
push_undo(unref_point, pi, rc0 + 1)
// if (LOG) log('ref_point', pi, prc[pi])
}
{
let _v0 = v3()
function move_point_xyz(pi, x, y, z, x0, y0, z0) {
return move_point(pi, _v0.set(x, y, z), x0, y0, z0)
}}
function move_point(pi, p, expect_x, expect_y, expect_z) {
let p0 = get_point(pi)
let x0 = p0[0]
let y0 = p0[1]
let z0 = p0[2]
if (expect_x != null) {
assert(expect_x == x0)
assert(expect_y == y0)
assert(expect_z == z0)
}
p.to_v3_array(points, pi)
update_point(pi, p)
push_undo(move_point_xyz, pi, x0, y0, z0, p[0], p[1], p[2])
}
let line_count = () => lines.length / 5
{
let _line = line3()
function get_line(li, out) {
out = out || _line
let p1i = lines[5*li+0]
let p2i = lines[5*li+1]
get_point(p1i, out[0])
get_point(p2i, out[1])
out.i = li
return out
}}
function line_rc (li) { return lines[5*li+2] }
function line_smoothness (li) { return lines[5*li+3] }
function line_opacity (li) { return lines[5*li+4] }
function each_line(f) {
for (let li = 0, n = line_count(); li < n; li++)
if (line_rc(li))
f(get_line(li))
}
function add_line(p1i, p2i, expect_li) {
let li = free_lis.pop()
if (li == null) {
li = lines.push(p1i, p2i, 1, 0, 1)
li = (lines.length / 5) - 1
} else {
lines[5*li+0] = p1i
lines[5*li+1] = p2i
lines[5*li+2] = 1 // ref. count
lines[5*li+3] = 0 // smoothness
lines[5*li+4] = 1 // opacity
}
assert(expect_li == null || expect_li == li)
nonedge_line_count++
used_lines_changed = true
bbox_changed = true
ref_point(p1i)
ref_point(p2i)
push_undo(unref_line, li, 1)
if (LOG)
log('add_line', li, p1i+','+p2i)
return li
}
function unref_line(li, expect_rc) {
let rc = --lines[5*li+2]
assert(rc >= 0)
assert(expect_rc == null || expect_rc == rc + 1)
if (rc == 0) {
nonedge_line_count--
used_lines_changed = true
bbox_changed = true
let p1i = lines[5*li+0]
let p2i = lines[5*li+1]
unref_point(p1i)
unref_point(p2i)
free_lis.push(li)
push_undo(add_line, p1i, p2i, li)
if (LOG)
log('remove_line', li)
} else {
if (rc == 1) {
nonedge_line_count++
edge_line_count--
used_lines_changed = true
} else {
edge_line_count--
}
push_undo(ref_line, li, rc)
}
// if (LOG)
// log('unref_line', li, rc)
}
function ref_line(li, expect_rc) {
let rc0 = lines[5*li+2]++
assert(expect_rc == null || expect_rc == rc0)
if (rc0 == 1) {
nonedge_line_count--
edge_line_count++
used_lines_changed = true
} else {
assert(rc0 > 1)
edge_line_count++
}
push_undo(unref_line, li, rc0 + 1)
// if (LOG)
// log('ref_line', li, rc0 + 1)
}
function merge_lines(li1, li2, expect_rc) {
let pi1 = lines[5*li2+0] // pi1
let pi2 = lines[5*li2+1] // pi2
if (LOG)
log('merge_lines', li1, li2)
assert(expect_rc == null || line_rc(li2) == expect_rc)
while (line_rc(li2))
unref_line(li2)
lines[5*li1+1] = pi2 // pi2
used_lines_changed = true
push_undo(cut_line, li1, pi1, li2)
}
function cut_line(li1, pi, expect_li) {
let pi2 = lines[5*li1+1] // pi2
let rc1 = lines[5*li1+2] // rc
let sm1 = lines[5*li1+3] // smoothness
let op1 = lines[5*li1+4] // opacity
if (LOG)
log('cut_line', li1, pi)
let li2 = add_line(pi, pi2)
lines[5*li1+1] = pi // pi2
while (line_rc(li2) < rc1)
ref_line(li2)
set_line_smoothness (li2, sm1)
set_line_opacity (li2, op1)
used_lines_changed = true
assert(expect_li == null || li2 == expect_li)
push_undo(merge_lines, li1, li2, rc1)
}
function replace_line_endpoint(li, new_pi, old_pi) {
let pi0 = lines[5*li+0]
let pi1 = lines[5*li+1]
let i = (old_pi == pi0) ? 0 : ((old_pi == pi1) ? 1 : assert(false))
lines[5*li+i] = new_pi
used_lines_changed = true
ref_point(new_pi)
unref_point(old_pi)
if (LOG)
log('replace_line_endpoint', li, '@'+i, old_pi, '->', new_pi)
push_undo(replace_line_endpoint, old_pi, new_pi)
}
let face = {is_face3: true}
face.get_point = function(ei, out) {
return get_point(this[ei], out)
}
face.get_normal = function(ei, out) {
if (this.mesh)
return out.from_v3_array(normals, this[ei])
else
return this.plane().normal
}
face.get_edge = function(ei, out) {
out = get_line(this.lis[ei], out)
out.ei = ei // edge index.
if (out[1].i == this[ei]) { // fix edge endpoints order.
let p1 = out[0]
let p2 = out[1]
out[0] = p2
out[1] = p1
}
return out
}
face.each_edge = function(f) {
for (let ei = 0, n = this.length; ei < n; ei++)
f(this.get_edge(ei))
}
face.is_flat = function() {
for (let li of face.lis)
if (lines[5*li+3])
return false
return true
}
let face3 = poly3.subclass(face)
let mat_faces_map = map() // {material -> [face1,...]}
function material_instance(mat) {
mat = mat || pe.default_material
let mat_insts = attr(mat_faces_map, mat, Array)
mat_insts.material = mat
return mat_insts
}
function add_face(pis, lis, material) {
let face
let fi = free_fis.pop()
if (fi == null) {
fi = faces.length
face = face3()
face.lis = []
face.points = points
face.i = fi
faces[fi] = face
} else {
face = faces[fi]
}
if (pis) {
face.extend(pis)
for (let pi of pis)
ref_point(pi)
}
if (lis) {
face.lis.extend(lis)
for (let li of lis)
ref_line(li)
} else
update_face_lis(face)
material = material || pe.default_material
face.mat_inst = material_instance(material)
face.mat_inst.push(face)
faces_changed = true
if (LOG)
log('add_face', face.i, face.join(','), face.lis.join(','), material.i)
return face
}
function remove_face(face) {
free_fis.push(face.i)
for (let li of face.lis)
unref_line(li)
for (let pi of face)
unref_point(pi)
face.length = 0
face.lis.length = 0
face.mat_inst.remove_value(face)
face.mat_inst = null
if (LOG)
log('remove_face', face.i)
}
function set_material(face, material) {
let m_inst0 = face.mat_inst
let m0 = m_inst0.material
let m_inst = material_instance(material)
if (m_inst == m_inst0)
return
m_inst0.remove_value(face)
face.mat_inst = m_inst
face.mat_inst.push(face)
faces_changed = true
if (LOG)
log('set_material', face.i, material.i)
push_undo(set_material, face, m0)
}
function ref_or_add_line(p1i, p2i) {
let found_li
for (let li = 0, n = line_count(); li < n; li++) {
let _p1i = lines[5*li+0]
let _p2i = lines[5*li+1]
if ((_p1i == p1i && _p2i == p2i) || (_p1i == p2i && _p2i == p1i)) {
found_li = li
break
}
}
if (found_li == null)
found_li = add_line(p1i, p2i)
ref_line(found_li)
return found_li
}
function update_face_lis(face) {
let lis = face.lis
lis.length = 0
let p1i = face[0]
for (let i = 1, n = face.length; i < n; i++) {
let p2i = face[i]
lis.push(assert(ref_or_add_line(p1i, p2i)))
p1i = p2i
}
lis.push(assert(ref_or_add_line(p1i, face[0])))
faces_changed = true
}
function insert_edge(face, ei, pi, line_before_point, li) {
let line_ei = ei - (line_before_point ? 1 : 0)
assert(line_ei >= 0) // can't use ei=0 and line_before_point=true with this function.
if (LOG)
log('insert_edge', face.i, '@'+ei, 'pi='+pi, '@'+line_ei, 'li='+li, 'before_pi='+face[ei])
face.insert(ei, pi)
face.lis.insert(line_ei, li)
if (face.mesh)
face.mesh.normals_valid = false
face.invalidate()
faces_changed = true
}
function replace_edge(face, ei, li, expect_li) {
let li0 = face.lis[ei]
assert(expect_li == null || expect_li == li0)
face.lis[ei] = li
if (LOG)
log('replace_edge', face.i, '@'+ei, li0, '->', li)
push_undo(replace_edge, face, ei, li0, li)
}
function replace_face_point(face, ei, pi) {
let pi0 = face[ei]
face[ei] = pi
if (LOG)
log('replace_face_point', face.i, '@'+ei, pi0, '->', pi)
push_undo(replace_face_point, face, ei, pi0)
}
function each_line_face(li, f) {
for (let face of faces)
if (face.lis.includes(li))
f(face)
}
{
let common_meshes = set()
let nomesh_faces = []
function set_line_smoothness(li, sm) {
let sm0 = lines[5*li+3]
if (sm == sm0)
return
push_undo(set_line_smoothness, li, sm0)
if (!sm0 == !sm) // smoothness category hasn't changed.
return
lines[5*li+3] = sm
if (sm > 0) { // line has gotten smooth.
each_line_face(li, function(face) {
if (face.mesh)
common_meshes.add(face.mesh)
else
nomesh_faces.push(face)
})
let target_mesh
if (common_meshes.size == 0) {
// none of the faces are part of a mesh, so make one.
let mesh = set()
meshes.add(mesh)
common_meshes.add(mesh)
target_mesh = mesh
} else {
// merge all meshes into the first one and remove the rest.
for (let mesh of common_meshes) {
if (!target_mesh) {
target_mesh = mesh
} else {
for (let face of mesh) {
target_mesh.add(face)
face.mesh = target_mesh
}
meshes.delete(mesh)
}
}
}
// add flat faces to the target mesh.
for (let face of nomesh_faces) {
target_mesh.add(face)
face.mesh = target_mesh
}
target_mesh.normals_valid = false
} else { // line has gotten non-smooth.
// remove faces containing `li` from their smooth mesh.
let target_mesh
each_line_face(li, function(face) {
assert(!target_mesh || target_mesh == mesh) // one mesh only.
target_mesh = face.mesh
if (face.is_flat())
face.mesh.delete(face)
face.mesh = null
})
// remove the mesh if it became empty.
if (target_mesh.size == 0)
meshes.delete(target_mesh)
}
common_meshes.clear()
nomesh_faces.length = 0
faces_changed = true
}}
function set_line_opacity(li, op) {
let op0 = lines[5*li+4]
if (op == op0)
return
push_undo(set_line_opacity, li, op0)
if (!op0 == !op) // opacity category hasn't changed.
return
lines[5*li+4] = op
used_lines_changed = true
}
// component children
function add_child(comp, mat, layer) {
assert(mat.is_mat4)
assert(comp.editor == pe.editor)
mat.comp = comp
mat.layer = layer || pe.default_layer
children.push(mat)
pe.child_added(e, mat)
if (LOG)
log('add_child', mat)
return mat
}
function remove_child(mat) {
assert(children.remove_value(mat))
pe.child_removed(e, mat)
}
function set_child_layer(node, layer) {
assert(node.comp == this)
node.layer = layer
pe.layer_changed(e, node)
return node
}
function set_child_matrix(nat, mat1) {
assert(mat.comp == this)
return mat.set(mat1)
}
// public API
e.point_count = point_count
e.get_point = get_point
e.line_count = line_count
e.get_line = get_line
e.each_line = each_line
e.add_line = add_line
e.unref_line = unref_line
e.set_line_smoothness = set_line_smoothness
e.set_line_opacity = set_line_opacity
e.faces = faces
e.add_face = add_face
e.remove_face = remove_face
e.set_material = set_material
e.each_line_face = each_line_face
e.children = children
e.add_child = add_child
e.remove_child = remove_child
e.set_child_layer = set_child_layer
e.set = function(t) {
if (t.points) {
let p = v3()
for (let i = 0, n = t.points.length; i < n; i += 3)
add_point(p.from_array(t.points, i))
}
if (t.faces)
for (let ft of t.faces)
add_face(ft, ft.lis, ft.material)
if (t.lines)
for (let i = 0, n = t.lines.length; i < n; i += 2)
add_line(t.lines[i], t.lines[i+1])
}
// hit testing & snapping -------------------------------------------------
// return the line from target line to its closest point
// with the point index in line[1].i.
function line_hit_points(target_line, max_d, p2p_distance2, f) {
let min_ds = 1/0
let int_line = line3()
let min_int_line
let p1 = int_line[0]
let p2 = int_line[1]
let i1 = target_line[0].i
let i2 = target_line[1].i
for (let i = 0, n = point_count(); i < n; i++) {
if (i == i1 || i == i2) // don't hit target line's endpoints
continue
get_point(i, p2)
target_line.closest_point_to_point(p2, true, p1)
let ds = p2p_distance2(p1, p2)
if (ds <= max_d ** 2) {
if (f && f(int_line) === false)
continue
if (ds < min_ds) {
min_ds = ds
min_int_line = min_int_line || line3()
min_int_line[0].set(p1)
min_int_line[1].set(p2)
min_int_line[1].i = i
}
}
}
return min_int_line
}
// return the point on closest line from target point.
function point_hit_lines(p, max_d, p2p_distance2, f, each_line_f) {
let min_ds = 1/0
let line = line3()
let int_p = v3()
let min_int_p
each_line_f = each_line_f || each_line
each_line_f(function(line) {
line.closest_point_to_point(p, true, int_p)
let ds = p2p_distance2(p, int_p)
if (ds <= max_d ** 2) {
if (!(f && f(int_p, line) === false)) {
if (ds < min_ds) {
min_ds = ds
min_int_p = assign(min_int_p || v3(), int_p)
}
}
}
})
return min_int_p
}
// return the point on closest face line from target point.
function point_hit_edge(p, face, max_d, p2p_distance2, f) {
return point_hit_lines(p, max_d, p2p_distance2, f, f => each_edge(face, f))
}
// return the projected point on closest line from target line.
{
let _l0 = line3()
let _l1 = line3()
let _l2 = line3()
let _v0 = v3()
function line_hit_lines(model, target_line, max_d, p2p_distance2, int_mode, is_line_valid, is_int_line_valid) {
let min_ds = 1/0
let min_int_p
is_line_valid = is_line_valid || return_true
is_int_line_valid = is_int_line_valid || return_true
for (let li = 0, n = line_count(); li < n; li++) {
if (lines[5*li+2]) { // ref count: used.
let line = get_line(li)
if (model)
line.transform(model)
if (is_line_valid(line)) {
let p1i = line[0].i
let p2i = line[1].i
let q1i = target_line[0].i
let q2i = target_line[1].i
let touch1 = p1i == q1i || p1i == q2i
let touch2 = p2i == q1i || p2i == q2i
if (touch1 != touch2) {
// skip lines with a single endpoint common with the target line.
} else if (touch1 && touch2) {
//
} else {
let int_line = target_line.intersect_line(line, _l0, int_mode)
if (int_line) {
let ds = p2p_distance2(int_line[0], int_line[1])
if (ds <= max_d ** 2) {
let int_p = int_line[1]
int_p.line = line
int_p.int_line = int_line
int_p.li = li
if (is_int_line_valid(int_p)) {
if (ds < min_ds) {
min_ds = ds
min_int_p = min_int_p || _v0
min_int_p.assign(int_p)
min_int_p.line = _l1.assign(line)
min_int_p.int_line = _l2.assign(int_line)
}
}
}
}
}
}
}
}
return min_int_p
}}
e.point_hit_lines = point_hit_lines
e.point_hit_edge = point_hit_edge
e.line_hit_lines = line_hit_lines
// selection --------------------------------------------------------------
{
let _bb0 = box3()
let _bb1 = box3()
e.bbox = function() {
let bb = _bb0
if (bbox_changed) {
bb.reset()
each_line(line => bb.add(line))
for (let child of children) {
let cbb = child.comp.bbox().to(_bb1).transform(child)
bb.add(cbb)
}
bbox_changed = false
}
return bb
}}
let sel_lines = set() // {l1i,...}
let sel_lines_changed
{
let _line = line3()
e.each_selected_line = function(f) {
for (let li of sel_lines) {
get_line(li, _line)
f(_line)
}
}}
function select_all_lines(sel) {
if (sel)
for (let i = 0, n = e.line_count(); i < n; i++)
sel_lines.add(i)
else
sel_lines.clear()
sel_lines_changed = true
}
let sel_face_count = 0
function face_set_selected(face, sel) {
if (!face.selected == !sel)
return
face.selected = sel
sel_face_count += (sel ? 1 : -1)
faces_changed = true
}
function select_all_faces(sel) {
for (let face of faces)
face_set_selected(face, sel)
}
function select_edges(face, mode) {
face.each_edge(function(line) {
e.select_line(line.i, mode)
})
}
function select_line_faces(li, mode) {
e.each_line_face(li, function(face) {
e.select_face(face, mode)
})
}
e.select_face = function(face, mode, with_lines) {
if (mode == 'add' || mode == 'remove') {
face_set_selected(face, mode == 'add')
if (mode == 'add' && with_lines) {
select_edges(face, mode)
sel_lines_changed = true
}
} else if (mode == 'toggle') {
e.select_face(face, face.selected ? 'remove' : 'add', with_lines)
} else
assert(false)
}
e.select_line = function(li, mode, with_faces) {
if (mode == 'add') {
sel_lines.add(li)
if (with_faces)
select_line_faces(li, 'add')
} else if (mode == 'remove') {
sel_lines.delete(li)
} else if (mode == 'toggle') {
e.select_line(li, sel_lines.has(li) ? 'remove' : 'add', with_faces)
} else
assert(false)
sel_lines_changed = true
}
e.is_line_selected = function(li) {
return sel_lines.has(li)
}
e.selected_child = null
e.select_child = function(child, mode) {
let sel = mode == 'toggle' ? !child.selected : mode == 'add'
if (sel == !!child.selected)
return
child.selected = sel
sel_child_count += (sel ? 1 : -1)
e.selected_child = sel_child_count == 1 ? child : null
sel_lines_changed = true
}
function select_all_children(sel) {
for (let child of children)
e.select_child(child, sel ? 'add' : 'remove')
}
e.select_all = function(sel, with_children) {
if (sel == null) sel = true
select_all_faces(sel)
select_all_lines(sel)
if (with_children !== false)
select_all_children(sel)
}
e.remove_selection = function() {
// remove all faces that selected lines are sides of.
for (let li of sel_lines)
e.each_line_face(li, remove_face)
// remove all selected faces.
for (let face of faces)
if (face.selected)
remove_face(face)
// remove all selected lines.
remove_lines(sel_lines)
// TODO: merge faces.
select_all_lines(false)
update_face_lis()
}
e.selected_face_count = () => sel_face_count
e.selected_line_count = () => sel_lines.size
e.selected_child_count = () => sel_child_count
// model editing ----------------------------------------------------------
let real_p2p_distance2 = (p1, p2) => p1.distance2(p2)
e.draw_line = function(line) {
let p1 = line[0]
let p2 = line[1]
// check for min. line length for lines with new endpoints.
if (p1.i == null || p2.i == null) {
if (p1.distance2(p2) <= NEAR ** 2)
return
} else if (p1.i == p2.i) {
// check if end point was snapped to start end point.
return
}
let line_ps = [p1, p2] // line's points as an open polygon.
// cut the line into segments at intersections with existing points.
line_hit_points(line, NEAR, real_p2p_distance2, null, null, function(int_line) {
let p = int_line[0]
let i = p.i
if (i !== p1.i && i !== p2.i) { // exclude end points.
p = p.clone()
p.i = i
line_ps.push(p)
}
})
// sort intersection points by their distance relative to p1
// so that adjacent points form line segments.
function sort_line_ps() {
if (line_ps.length)
line_ps.sort(function(sp1, sp2) {
let ds1 = p1.distance2(sp1)
let ds2 = p1.distance2(sp2)
return ds1 - ds2
})
}
sort_line_ps()
// check if any of the line segments intersect any existing lines.
// the ones that do must be broken down further, and so must the
// existing lines that are cut by them.