-
Notifications
You must be signed in to change notification settings - Fork 21
/
libtess.cat.js
4766 lines (4114 loc) · 143 KB
/
libtess.cat.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
/**
* @license
* Copyright 2000, Silicon Graphics, Inc. All Rights Reserved.
* Copyright 2015, Google Inc. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice including the dates of first publication and
* either this permission notice or a reference to http://oss.sgi.com/projects/FreeB/
* shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* SILICON GRAPHICS, INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
* IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* Original Code. The Original Code is: OpenGL Sample Implementation,
* Version 1.2.1, released January 26, 2000, developed by Silicon Graphics,
* Inc. The Original Code is Copyright (c) 1991-2000 Silicon Graphics, Inc.
* Copyright in any portions created by third parties is as indicated
* elsewhere herein. All Rights Reserved.
*/
/**
* @author ericv@cs.stanford.edu (Eric Veach)
* @author bckenny@google.com (Brendan Kenny)
*/
/**
* Base namespace.
* @const
*/
var libtess = {};
/**
* Whether to run asserts and extra debug checks.
* @define {boolean}
*/
libtess.DEBUG = false;
/**
* Checks if the condition evaluates to true if libtess.DEBUG is true.
* @param {*} condition The condition to check.
* @param {string=} opt_message Error message in case of failure.
* @throws {Error} Assertion failed, the condition evaluates to false.
*/
libtess.assert = function(condition, opt_message) {
if (libtess.DEBUG && !condition) {
throw new Error('Assertion failed' +
(opt_message ? ': ' + opt_message : ''));
}
};
/**
* The maximum vertex coordinate size, 1e150. Anything larger will trigger a
* GLU_TESS_COORD_TOO_LARGE error callback and the vertex will be clamped to
* this value for all tessellation calculations.
* @const {number}
*/
libtess.GLU_TESS_MAX_COORD = 1e150;
// NOTE(bckenny): value from glu.pl generator
/**
* Normally the polygon is projected to a plane perpendicular to one of the
* three coordinate axes before tessellating in 2d. This helps numerical
* accuracy by forgoing a transformation step by simply dropping one coordinate
* dimension.
*
* However, this can affect the placement of intersection points for non-axis-
* aligned polygons. Setting TRUE_PROJECT to true will instead project onto a
* plane actually perpendicular to the polygon's normal.
*
* NOTE(bckenny): I can find no instances on the internet in which this mode has
* been used, but it's difficult to search for. This was a compile-time setting
* in the original, so setting this as constant. If this is exposed in the
* public API, remove the ignore coverage directives on
* libtess.normal.projectPolygon and libtess.normal.normalize_.
* @const {boolean}
*/
libtess.TRUE_PROJECT = false;
/**
* The default tolerance for merging features, 0, meaning vertices are only
* merged if they are exactly coincident
* If a higher tolerance is needed, significant rewriting will need to occur.
* See libtess.sweep.TOLERANCE_NONZERO_ as a starting place.
* @const {number}
*/
libtess.GLU_TESS_DEFAULT_TOLERANCE = 0;
/**
* The input contours parition the plane into regions. A winding
* rule determines which of these regions are inside the polygon.
*
* For a single contour C, the winding number of a point x is simply
* the signed number of revolutions we make around x as we travel
* once around C (where CCW is positive). When there are several
* contours, the individual winding numbers are summed. This
* procedure associates a signed integer value with each point x in
* the plane. Note that the winding number is the same for all
* points in a single region.
*
* The winding rule classifies a region as "inside" if its winding
* number belongs to the chosen category (odd, nonzero, positive,
* negative, or absolute value of at least two). The current GLU
* tesselator implements the "odd" rule. The "nonzero" rule is another
* common way to define the interior. The other three rules are
* useful for polygon CSG operations.
* @enum {number}
*/
libtess.windingRule = {
// NOTE(bckenny): values from enumglu.spec
GLU_TESS_WINDING_ODD: 100130,
GLU_TESS_WINDING_NONZERO: 100131,
GLU_TESS_WINDING_POSITIVE: 100132,
GLU_TESS_WINDING_NEGATIVE: 100133,
GLU_TESS_WINDING_ABS_GEQ_TWO: 100134
};
/**
* The type of primitive return from a "begin" callback. GL_LINE_LOOP is only
* returned when GLU_TESS_BOUNDARY_ONLY is true. GL_TRIANGLE_STRIP and
* GL_TRIANGLE_FAN are no longer returned since 1.1.0 (see release notes).
* @enum {number}
*/
libtess.primitiveType = {
GL_LINE_LOOP: 2,
GL_TRIANGLES: 4,
GL_TRIANGLE_STRIP: 5,
GL_TRIANGLE_FAN: 6
};
/**
* The types of errors provided in the error callback.
* @enum {number}
*/
libtess.errorType = {
// TODO(bckenny) doc types
// NOTE(bckenny): values from enumglu.spec
GLU_TESS_MISSING_BEGIN_POLYGON: 100151,
GLU_TESS_MISSING_END_POLYGON: 100153,
GLU_TESS_MISSING_BEGIN_CONTOUR: 100152,
GLU_TESS_MISSING_END_CONTOUR: 100154,
GLU_TESS_COORD_TOO_LARGE: 100155,
GLU_TESS_NEED_COMBINE_CALLBACK: 100156
};
/**
* Enum values necessary for providing settings and callbacks. See the readme
* for details.
* @enum {number}
*/
libtess.gluEnum = {
// TODO(bckenny): rename so not always typing libtess.gluEnum.*?
// NOTE(bckenny): values from enumglu.spec
GLU_TESS_BEGIN: 100100,
GLU_TESS_VERTEX: 100101,
GLU_TESS_END: 100102,
GLU_TESS_ERROR: 100103,
GLU_TESS_EDGE_FLAG: 100104,
GLU_TESS_COMBINE: 100105,
GLU_TESS_BEGIN_DATA: 100106,
GLU_TESS_VERTEX_DATA: 100107,
GLU_TESS_END_DATA: 100108,
GLU_TESS_ERROR_DATA: 100109,
GLU_TESS_EDGE_FLAG_DATA: 100110,
GLU_TESS_COMBINE_DATA: 100111,
GLU_TESS_MESH: 100112, // NOTE(bckenny): from tess.c
GLU_TESS_TOLERANCE: 100142,
GLU_TESS_WINDING_RULE: 100140,
GLU_TESS_BOUNDARY_ONLY: 100141,
// TODO(bckenny): move this to libtess.errorType?
GLU_INVALID_ENUM: 100900,
GLU_INVALID_VALUE: 100901
};
/** @typedef {number} */
libtess.PQHandle;
/* global libtess */
/** @const */
libtess.geom = {};
/**
* Returns whether vertex u and vertex v are equal.
* @param {libtess.GluVertex} u
* @param {libtess.GluVertex} v
* @return {boolean}
*/
libtess.geom.vertEq = function(u, v) {
return u.s === v.s && u.t === v.t;
};
/**
* Returns whether vertex u is lexicographically less than or equal to vertex v.
* @param {libtess.GluVertex} u
* @param {libtess.GluVertex} v
* @return {boolean}
*/
libtess.geom.vertLeq = function(u, v) {
return (u.s < v.s) || (u.s === v.s && u.t <= v.t);
};
/**
* Given three vertices u,v,w such that geom.vertLeq(u,v) && geom.vertLeq(v,w),
* evaluates the t-coord of the edge uw at the s-coord of the vertex v.
* Returns v.t - (uw)(v.s), ie. the signed distance from uw to v.
* If uw is vertical (and thus passes thru v), the result is zero.
*
* The calculation is extremely accurate and stable, even when v
* is very close to u or w. In particular if we set v.t = 0 and
* let r be the negated result (this evaluates (uw)(v.s)), then
* r is guaranteed to satisfy MIN(u.t,w.t) <= r <= MAX(u.t,w.t).
* @param {libtess.GluVertex} u
* @param {libtess.GluVertex} v
* @param {libtess.GluVertex} w
* @return {number}
*/
libtess.geom.edgeEval = function(u, v, w) {
var gapL = v.s - u.s;
var gapR = w.s - v.s;
if (gapL + gapR > 0) {
if (gapL < gapR) {
return (v.t - u.t) + (u.t - w.t) * (gapL / (gapL + gapR));
} else {
return (v.t - w.t) + (w.t - u.t) * (gapR / (gapL + gapR));
}
}
// vertical line
return 0;
};
/**
* Returns a number whose sign matches geom.edgeEval(u,v,w) but which
* is cheaper to evaluate. Returns > 0, == 0 , or < 0
* as v is above, on, or below the edge uw.
* @param {libtess.GluVertex} u
* @param {libtess.GluVertex} v
* @param {libtess.GluVertex} w
* @return {number}
*/
libtess.geom.edgeSign = function(u, v, w) {
var gapL = v.s - u.s;
var gapR = w.s - v.s;
if (gapL + gapR > 0) {
return (v.t - w.t) * gapL + (v.t - u.t) * gapR;
}
// vertical line
return 0;
};
/**
* Version of VertLeq with s and t transposed.
* Returns whether vertex u is lexicographically less than or equal to vertex v.
* @param {libtess.GluVertex} u
* @param {libtess.GluVertex} v
* @return {boolean}
*/
libtess.geom.transLeq = function(u, v) {
return (u.t < v.t) || (u.t === v.t && u.s <= v.s);
};
/**
* Version of geom.edgeEval with s and t transposed.
* Given three vertices u,v,w such that geom.transLeq(u,v) &&
* geom.transLeq(v,w), evaluates the t-coord of the edge uw at the s-coord of
* the vertex v. Returns v.s - (uw)(v.t), ie. the signed distance from uw to v.
* If uw is vertical (and thus passes thru v), the result is zero.
*
* The calculation is extremely accurate and stable, even when v
* is very close to u or w. In particular if we set v.s = 0 and
* let r be the negated result (this evaluates (uw)(v.t)), then
* r is guaranteed to satisfy MIN(u.s,w.s) <= r <= MAX(u.s,w.s).
* @param {libtess.GluVertex} u
* @param {libtess.GluVertex} v
* @param {libtess.GluVertex} w
* @return {number}
*/
libtess.geom.transEval = function(u, v, w) {
var gapL = v.t - u.t;
var gapR = w.t - v.t;
if (gapL + gapR > 0) {
if (gapL < gapR) {
return (v.s - u.s) + (u.s - w.s) * (gapL / (gapL + gapR));
} else {
return (v.s - w.s) + (w.s - u.s) * (gapR / (gapL + gapR));
}
}
// vertical line
return 0;
};
/**
* Version of geom.edgeSign with s and t transposed.
* Returns a number whose sign matches geom.transEval(u,v,w) but which
* is cheaper to evaluate. Returns > 0, == 0 , or < 0
* as v is above, on, or below the edge uw.
* @param {libtess.GluVertex} u
* @param {libtess.GluVertex} v
* @param {libtess.GluVertex} w
* @return {number}
*/
libtess.geom.transSign = function(u, v, w) {
var gapL = v.t - u.t;
var gapR = w.t - v.t;
if (gapL + gapR > 0) {
return (v.s - w.s) * gapL + (v.s - u.s) * gapR;
}
// vertical line
return 0;
};
/**
* Returns whether edge is directed from right to left.
* @param {libtess.GluHalfEdge} e
* @return {boolean}
*/
libtess.geom.edgeGoesLeft = function(e) {
return libtess.geom.vertLeq(e.dst(), e.org);
};
/**
* Returns whether edge is directed from left to right.
* @param {libtess.GluHalfEdge} e
* @return {boolean}
*/
libtess.geom.edgeGoesRight = function(e) {
return libtess.geom.vertLeq(e.org, e.dst());
};
/**
* Calculates the L1 distance between vertices u and v.
* @param {libtess.GluVertex} u
* @param {libtess.GluVertex} v
* @return {number}
*/
libtess.geom.vertL1dist = function(u, v) {
return Math.abs(u.s - v.s) + Math.abs(u.t - v.t);
};
// NOTE(bckenny): vertCCW is called nowhere in libtess and isn't part of the
// public API.
/* istanbul ignore next */
/**
* For almost-degenerate situations, the results are not reliable.
* Unless the floating-point arithmetic can be performed without
* rounding errors, *any* implementation will give incorrect results
* on some degenerate inputs, so the client must have some way to
* handle this situation.
* @param {!libtess.GluVertex} u
* @param {!libtess.GluVertex} v
* @param {!libtess.GluVertex} w
* @return {boolean}
*/
libtess.geom.vertCCW = function(u, v, w) {
return (u.s * (v.t - w.t) + v.s * (w.t - u.t) + w.s * (u.t - v.t)) >= 0;
};
/**
* Given parameters a,x,b,y returns the value (b*x+a*y)/(a+b),
* or (x+y)/2 if a==b==0. It requires that a,b >= 0, and enforces
* this in the rare case that one argument is slightly negative.
* The implementation is extremely stable numerically.
* In particular it guarantees that the result r satisfies
* MIN(x,y) <= r <= MAX(x,y), and the results are very accurate
* even when a and b differ greatly in magnitude.
* @private
* @param {number} a
* @param {number} x
* @param {number} b
* @param {number} y
* @return {number}
*/
libtess.geom.interpolate_ = function(a, x, b, y) {
// from Macro RealInterpolate:
//(a = (a < 0) ? 0 : a, b = (b < 0) ? 0 : b, ((a <= b) ? ((b == 0) ? ((x+y) / 2) : (x + (y-x) * (a/(a+b)))) : (y + (x-y) * (b/(a+b)))))
a = (a < 0) ? 0 : a;
b = (b < 0) ? 0 : b;
if (a <= b) {
if (b === 0) {
return (x + y) / 2;
} else {
return x + (y - x) * (a / (a + b));
}
} else {
return y + (x - y) * (b / (a + b));
}
};
/**
* Given edges (o1,d1) and (o2,d2), compute their point of intersection.
* The computed point is guaranteed to lie in the intersection of the
* bounding rectangles defined by each edge.
* @param {!libtess.GluVertex} o1
* @param {!libtess.GluVertex} d1
* @param {!libtess.GluVertex} o2
* @param {!libtess.GluVertex} d2
* @param {!libtess.GluVertex} v
*/
libtess.geom.edgeIntersect = function(o1, d1, o2, d2, v) {
// This is certainly not the most efficient way to find the intersection
// of two line segments, but it is very numerically stable.
// Strategy: find the two middle vertices in the VertLeq ordering,
// and interpolate the intersection s-value from these. Then repeat
// using the TransLeq ordering to find the intersection t-value.
var z1;
var z2;
var tmp;
if (!libtess.geom.vertLeq(o1, d1)) {
// Swap(o1, d1);
tmp = o1;
o1 = d1;
d1 = tmp;
}
if (!libtess.geom.vertLeq(o2, d2)) {
// Swap(o2, d2);
tmp = o2;
o2 = d2;
d2 = tmp;
}
if (!libtess.geom.vertLeq(o1, o2)) {
// Swap(o1, o2);
tmp = o1;
o1 = o2;
o2 = tmp;
// Swap(d1, d2);
tmp = d1;
d1 = d2;
d2 = tmp;
}
if (!libtess.geom.vertLeq(o2, d1)) {
// Technically, no intersection -- do our best
v.s = (o2.s + d1.s) / 2;
} else if (libtess.geom.vertLeq(d1, d2)) {
// Interpolate between o2 and d1
z1 = libtess.geom.edgeEval(o1, o2, d1);
z2 = libtess.geom.edgeEval(o2, d1, d2);
if (z1 + z2 < 0) { z1 = -z1; z2 = -z2; }
v.s = libtess.geom.interpolate_(z1, o2.s, z2, d1.s);
} else {
// Interpolate between o2 and d2
z1 = libtess.geom.edgeSign(o1, o2, d1);
z2 = -libtess.geom.edgeSign(o1, d2, d1);
if (z1 + z2 < 0) { z1 = -z1; z2 = -z2; }
v.s = libtess.geom.interpolate_(z1, o2.s, z2, d2.s);
}
// Now repeat the process for t
if (!libtess.geom.transLeq(o1, d1)) {
// Swap(o1, d1);
tmp = o1;
o1 = d1;
d1 = tmp;
}
if (!libtess.geom.transLeq(o2, d2)) {
// Swap(o2, d2);
tmp = o2;
o2 = d2;
d2 = tmp;
}
if (!libtess.geom.transLeq(o1, o2)) {
// Swap(o1, o2);
tmp = o1;
o1 = o2;
o2 = tmp;
// Swap(d1, d2);
tmp = d1;
d1 = d2;
d2 = tmp;
}
if (!libtess.geom.transLeq(o2, d1)) {
// Technically, no intersection -- do our best
v.t = (o2.t + d1.t) / 2;
} else if (libtess.geom.transLeq(d1, d2)) {
// Interpolate between o2 and d1
z1 = libtess.geom.transEval(o1, o2, d1);
z2 = libtess.geom.transEval(o2, d1, d2);
if (z1 + z2 < 0) { z1 = -z1; z2 = -z2; }
v.t = libtess.geom.interpolate_(z1, o2.t, z2, d1.t);
} else {
// Interpolate between o2 and d2
z1 = libtess.geom.transSign(o1, o2, d1);
z2 = -libtess.geom.transSign(o1, d2, d1);
if (z1 + z2 < 0) { z1 = -z1; z2 = -z2; }
v.t = libtess.geom.interpolate_(z1, o2.t, z2, d2.t);
}
};
/* global libtess */
// TODO(bckenny): could maybe merge GluMesh and mesh.js since these are
// operations on the mesh
/** @const */
libtess.mesh = {};
/****************** Basic Edge Operations **********************/
/**
* makeEdge creates one edge, two vertices, and a loop (face).
* The loop consists of the two new half-edges.
*
* @param {libtess.GluMesh} mesh [description].
* @return {libtess.GluHalfEdge} [description].
*/
libtess.mesh.makeEdge = function(mesh) {
// TODO(bckenny): probably move to GluMesh, but needs Make* methods with it
var e = libtess.mesh.makeEdgePair_(mesh.eHead);
// complete edge with vertices and face (see mesh.makeEdgePair_)
libtess.mesh.makeVertex_(e, mesh.vHead);
libtess.mesh.makeVertex_(e.sym, mesh.vHead);
libtess.mesh.makeFace_(e, mesh.fHead);
return e;
};
/**
* meshSplice(eOrg, eDst) is the basic operation for changing the
* mesh connectivity and topology. It changes the mesh so that
* eOrg.oNext <- OLD( eDst.oNext )
* eDst.oNext <- OLD( eOrg.oNext )
* where OLD(...) means the value before the meshSplice operation.
*
* This can have two effects on the vertex structure:
* - if eOrg.org != eDst.org, the two vertices are merged together
* - if eOrg.org == eDst.org, the origin is split into two vertices
* In both cases, eDst.org is changed and eOrg.org is untouched.
*
* Similarly (and independently) for the face structure,
* - if eOrg.lFace == eDst.lFace, one loop is split into two
* - if eOrg.lFace != eDst.lFace, two distinct loops are joined into one
* In both cases, eDst.lFace is changed and eOrg.lFace is unaffected.
*
* Some special cases:
* If eDst == eOrg, the operation has no effect.
* If eDst == eOrg.lNext, the new face will have a single edge.
* If eDst == eOrg.lPrev(), the old face will have a single edge.
* If eDst == eOrg.oNext, the new vertex will have a single edge.
* If eDst == eOrg.oPrev(), the old vertex will have a single edge.
*
* @param {libtess.GluHalfEdge} eOrg [description].
* @param {libtess.GluHalfEdge} eDst [description].
*/
libtess.mesh.meshSplice = function(eOrg, eDst) {
// TODO: more descriptive name?
var joiningLoops = false;
var joiningVertices = false;
if (eOrg === eDst) {
return;
}
if (eDst.org !== eOrg.org) {
// We are merging two disjoint vertices -- destroy eDst.org
joiningVertices = true;
libtess.mesh.killVertex_(eDst.org, eOrg.org);
}
if (eDst.lFace !== eOrg.lFace) {
// We are connecting two disjoint loops -- destroy eDst.lFace
joiningLoops = true;
libtess.mesh.killFace_(eDst.lFace, eOrg.lFace);
}
// Change the edge structure
libtess.mesh.splice_(eDst, eOrg);
if (!joiningVertices) {
// We split one vertex into two -- the new vertex is eDst.org.
// Make sure the old vertex points to a valid half-edge.
libtess.mesh.makeVertex_(eDst, eOrg.org);
eOrg.org.anEdge = eOrg;
}
if (!joiningLoops) {
// We split one loop into two -- the new loop is eDst.lFace.
// Make sure the old face points to a valid half-edge.
libtess.mesh.makeFace_(eDst, eOrg.lFace);
eOrg.lFace.anEdge = eOrg;
}
};
/**
* deleteEdge(eDel) removes the edge eDel. There are several cases:
* if (eDel.lFace != eDel.rFace()), we join two loops into one; the loop
* eDel.lFace is deleted. Otherwise, we are splitting one loop into two;
* the newly created loop will contain eDel.dst(). If the deletion of eDel
* would create isolated vertices, those are deleted as well.
*
* This function could be implemented as two calls to __gl_meshSplice
* plus a few calls to memFree, but this would allocate and delete
* unnecessary vertices and faces.
*
* @param {libtess.GluHalfEdge} eDel [description].
*/
libtess.mesh.deleteEdge = function(eDel) {
var eDelSym = eDel.sym;
var joiningLoops = false;
// First step: disconnect the origin vertex eDel.org. We make all
// changes to get a consistent mesh in this "intermediate" state.
if (eDel.lFace !== eDel.rFace()) {
// We are joining two loops into one -- remove the left face
joiningLoops = true;
libtess.mesh.killFace_(eDel.lFace, eDel.rFace());
}
if (eDel.oNext === eDel) {
libtess.mesh.killVertex_(eDel.org, null);
} else {
// Make sure that eDel.org and eDel.rFace() point to valid half-edges
eDel.rFace().anEdge = eDel.oPrev();
eDel.org.anEdge = eDel.oNext;
libtess.mesh.splice_(eDel, eDel.oPrev());
if (!joiningLoops) {
// We are splitting one loop into two -- create a new loop for eDel.
libtess.mesh.makeFace_(eDel, eDel.lFace);
}
}
// Claim: the mesh is now in a consistent state, except that eDel.org
// may have been deleted. Now we disconnect eDel.dst().
if (eDelSym.oNext === eDelSym) {
libtess.mesh.killVertex_(eDelSym.org, null);
libtess.mesh.killFace_(eDelSym.lFace, null);
} else {
// Make sure that eDel.dst() and eDel.lFace point to valid half-edges
eDel.lFace.anEdge = eDelSym.oPrev();
eDelSym.org.anEdge = eDelSym.oNext;
libtess.mesh.splice_(eDelSym, eDelSym.oPrev());
}
// Any isolated vertices or faces have already been freed.
libtess.mesh.killEdge_(eDel);
};
/******************** Other Edge Operations **********************/
/* All these routines can be implemented with the basic edge
* operations above. They are provided for convenience and efficiency.
*/
/**
* addEdgeVertex(eOrg) creates a new edge eNew such that
* eNew == eOrg.lNext, and eNew.dst() is a newly created vertex.
* eOrg and eNew will have the same left face.
*
* @param {libtess.GluHalfEdge} eOrg [description].
* @return {libtess.GluHalfEdge} [description].
*/
libtess.mesh.addEdgeVertex = function(eOrg) {
// TODO(bckenny): why is it named this?
var eNew = libtess.mesh.makeEdgePair_(eOrg);
var eNewSym = eNew.sym;
// Connect the new edge appropriately
libtess.mesh.splice_(eNew, eOrg.lNext);
// Set the vertex and face information
eNew.org = eOrg.dst();
libtess.mesh.makeVertex_(eNewSym, eNew.org);
eNew.lFace = eNewSym.lFace = eOrg.lFace;
return eNew;
};
/**
* splitEdge(eOrg) splits eOrg into two edges eOrg and eNew,
* such that eNew == eOrg.lNext. The new vertex is eOrg.dst() == eNew.org.
* eOrg and eNew will have the same left face.
*
* @param {libtess.GluHalfEdge} eOrg [description].
* @return {!libtess.GluHalfEdge} [description].
*/
libtess.mesh.splitEdge = function(eOrg) {
var tempHalfEdge = libtess.mesh.addEdgeVertex(eOrg);
var eNew = tempHalfEdge.sym;
// Disconnect eOrg from eOrg.dst() and connect it to eNew.org
libtess.mesh.splice_(eOrg.sym, eOrg.sym.oPrev());
libtess.mesh.splice_(eOrg.sym, eNew);
// Set the vertex and face information
eOrg.sym.org = eNew.org; // NOTE(bckenny): assignment to dst
eNew.dst().anEdge = eNew.sym; // may have pointed to eOrg.sym
eNew.sym.lFace = eOrg.rFace(); // NOTE(bckenny): assignment to rFace
eNew.winding = eOrg.winding; // copy old winding information
eNew.sym.winding = eOrg.sym.winding;
return eNew;
};
/**
* connect(eOrg, eDst) creates a new edge from eOrg.dst()
* to eDst.org, and returns the corresponding half-edge eNew.
* If eOrg.lFace == eDst.lFace, this splits one loop into two,
* and the newly created loop is eNew.lFace. Otherwise, two disjoint
* loops are merged into one, and the loop eDst.lFace is destroyed.
*
* If (eOrg == eDst), the new face will have only two edges.
* If (eOrg.lNext == eDst), the old face is reduced to a single edge.
* If (eOrg.lNext.lNext == eDst), the old face is reduced to two edges.
*
* @param {libtess.GluHalfEdge} eOrg [description].
* @param {libtess.GluHalfEdge} eDst [description].
* @return {!libtess.GluHalfEdge} [description].
*/
libtess.mesh.connect = function(eOrg, eDst) {
var joiningLoops = false;
var eNew = libtess.mesh.makeEdgePair_(eOrg);
var eNewSym = eNew.sym;
if (eDst.lFace !== eOrg.lFace) {
// We are connecting two disjoint loops -- destroy eDst.lFace
joiningLoops = true;
libtess.mesh.killFace_(eDst.lFace, eOrg.lFace);
}
// Connect the new edge appropriately
libtess.mesh.splice_(eNew, eOrg.lNext);
libtess.mesh.splice_(eNewSym, eDst);
// Set the vertex and face information
eNew.org = eOrg.dst();
eNewSym.org = eDst.org;
eNew.lFace = eNewSym.lFace = eOrg.lFace;
// Make sure the old face points to a valid half-edge
eOrg.lFace.anEdge = eNewSym;
if (!joiningLoops) {
// We split one loop into two -- the new loop is eNew.lFace
libtess.mesh.makeFace_(eNew, eOrg.lFace);
}
return eNew;
};
/******************** Other Operations **********************/
/**
* zapFace(fZap) destroys a face and removes it from the
* global face list. All edges of fZap will have a null pointer as their
* left face. Any edges which also have a null pointer as their right face
* are deleted entirely (along with any isolated vertices this produces).
* An entire mesh can be deleted by zapping its faces, one at a time,
* in any order. Zapped faces cannot be used in further mesh operations!
*
* @param {libtess.GluFace} fZap [description].
*/
libtess.mesh.zapFace = function(fZap) {
var eStart = fZap.anEdge;
// walk around face, deleting edges whose right face is also NULL
var eNext = eStart.lNext;
var e;
do {
e = eNext;
eNext = e.lNext;
e.lFace = null;
if (e.rFace() === null) {
// delete the edge -- see mesh.deleteEdge above
if (e.oNext === e) {
libtess.mesh.killVertex_(e.org, null);
} else {
// Make sure that e.org points to a valid half-edge
e.org.anEdge = e.oNext;
libtess.mesh.splice_(e, e.oPrev());
}
var eSym = e.sym;
if (eSym.oNext === eSym) {
libtess.mesh.killVertex_(eSym.org, null);
} else {
// Make sure that eSym.org points to a valid half-edge
eSym.org.anEdge = eSym.oNext;
libtess.mesh.splice_(eSym, eSym.oPrev());
}
libtess.mesh.killEdge_(e);
}
} while (e !== eStart);
// delete from circular doubly-linked list
var fPrev = fZap.prev;
var fNext = fZap.next;
fNext.prev = fPrev;
fPrev.next = fNext;
// TODO(bckenny): memFree( fZap );
// TODO(bckenny): probably null at callsite
};
// TODO(bckenny): meshUnion isn't called within libtess and isn't part of the
// public API. Could be useful if more mesh manipulation functions are exposed.
/* istanbul ignore next */
/**
* meshUnion() forms the union of all structures in
* both meshes, and returns the new mesh (the old meshes are destroyed).
*
* @param {!libtess.GluMesh} mesh1
* @param {!libtess.GluMesh} mesh2
* @return {!libtess.GluMesh}
*/
libtess.mesh.meshUnion = function(mesh1, mesh2) {
// TODO(bceknny): probably move to GluMesh method
var f1 = mesh1.fHead;
var v1 = mesh1.vHead;
var e1 = mesh1.eHead;
var f2 = mesh2.fHead;
var v2 = mesh2.vHead;
var e2 = mesh2.eHead;
// Add the faces, vertices, and edges of mesh2 to those of mesh1
if (f2.next !== f2) {
f1.prev.next = f2.next;
f2.next.prev = f1.prev;
f2.prev.next = f1;
f1.prev = f2.prev;
}
if (v2.next !== v2) {
v1.prev.next = v2.next;
v2.next.prev = v1.prev;
v2.prev.next = v1;
v1.prev = v2.prev;
}
if (e2.next !== e2) {
e1.sym.next.sym.next = e2.next;
e2.next.sym.next = e1.sym.next;
e2.sym.next.sym.next = e1;
e1.sym.next = e2.sym.next;
}
// TODO(bckenny): memFree(mesh2);
// TODO(bckenny): If function is kept, remove mesh2's data to enforce.
return mesh1;
};
/**
* deleteMesh(mesh) will free all storage for any valid mesh.
* @param {libtess.GluMesh} mesh [description].
*/
libtess.mesh.deleteMesh = function(mesh) {
// TODO(bckenny): unnecessary, I think.
// TODO(bckenny): might want to explicitly null at callsite
// lots of memFrees. see also DELETE_BY_ZAPPING
};
/************************ Utility Routines ************************/
/**
* Creates a new pair of half-edges which form their own loop.
* No vertex or face structures are allocated, but these must be assigned
* before the current edge operation is completed.
*
* TODO(bckenny): warning about eNext strictly being first of pair? (see code)
*
* @private
* @param {libtess.GluHalfEdge} eNext [description].
* @return {libtess.GluHalfEdge} [description].
*/
libtess.mesh.makeEdgePair_ = function(eNext) {
var e = new libtess.GluHalfEdge();
var eSym = new libtess.GluHalfEdge();
// TODO(bckenny): how do we ensure this? see above comment in jsdoc
// Make sure eNext points to the first edge of the edge pair
// if (eNext->Sym < eNext ) { eNext = eNext->Sym; }
// NOTE(bckenny): check this for bugs in current implementation!
// Insert in circular doubly-linked list before eNext.
// Note that the prev pointer is stored in sym.next.
var ePrev = eNext.sym.next;
eSym.next = ePrev;
ePrev.sym.next = e;
e.next = eNext;
eNext.sym.next = eSym;
e.sym = eSym;
e.oNext = e;
e.lNext = eSym;
eSym.sym = e;
eSym.oNext = eSym;
eSym.lNext = e;
return e;
};
/**
* splice_ is best described by the Guibas/Stolfi paper or the
* CS348a notes. Basically, it modifies the mesh so that
* a.oNext and b.oNext are exchanged. This can have various effects
* depending on whether a and b belong to different face or vertex rings.
* For more explanation see mesh.meshSplice below.
*
* @private
* @param {libtess.GluHalfEdge} a [description].
* @param {libtess.GluHalfEdge} b [description].
*/
libtess.mesh.splice_ = function(a, b) {
var aONext = a.oNext;
var bONext = b.oNext;
aONext.sym.lNext = b;
bONext.sym.lNext = a;
a.oNext = bONext;
b.oNext = aONext;
};
/**
* makeVertex_(eOrig, vNext) attaches a new vertex and makes it the
* origin of all edges in the vertex loop to which eOrig belongs. "vNext" gives
* a place to insert the new vertex in the global vertex list. We insert
* the new vertex *before* vNext so that algorithms which walk the vertex
* list will not see the newly created vertices.
*
* NOTE: unlike original, acutally allocates new vertex.
*
* @private
* @param {libtess.GluHalfEdge} eOrig [description].
* @param {libtess.GluVertex} vNext [description].
*/
libtess.mesh.makeVertex_ = function(eOrig, vNext) {
// insert in circular doubly-linked list before vNext
var vPrev = vNext.prev;
var vNew = new libtess.GluVertex(vNext, vPrev);
vPrev.next = vNew;
vNext.prev = vNew;
vNew.anEdge = eOrig;
// leave coords, s, t undefined
// TODO(bckenny): does above line mean 0 specifically, or does it matter?
// fix other edges on this vertex loop
var e = eOrig;
do {
e.org = vNew;
e = e.oNext;
} while (e !== eOrig);
};