forked from hemant-singh/ifc-fis
/
poly2tri.js
1998 lines (1755 loc) · 64.4 KB
/
poly2tri.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
/*
* Poly2Tri Copyright (c) 2009-2013, Poly2Tri Contributors
* http://code.google.com/p/poly2tri/
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* * Neither the name of Poly2Tri nor the names of its contributors may be
* used to endorse or promote products derived from this software without specific
* prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* jshint browser:false, forin:true, noarg:true, noempty:true, eqeqeq:true, bitwise:true,
strict:true, undef:true, unused:true, curly:true, immed:true, latedef:true,
newcap:true, trailing:true, maxcomplexity:11, indent:4
*/
/*
* Note
* ====
* the structure of this JavaScript version of poly2tri intentionnaly follows
* as closely as possible the structure of the reference C++ version, to make it
* easier to keep the 2 versions in sync.
*/
/**
* Module encapsulation
* @param {Object} global a reference to the global object :
* window in the browser, global on the server
*/
(function(global) {
"use strict";
// --------------------------------------------------------------poly2tri module
// Save the previous value of the poly2tri variable,
// so that it can be restored later on, if noConflict is used.
var previousPoly2tri = global.poly2tri;
// The top-level namespace. All public poly2tri classes and functions will
// be attached to it. Exported for both the browser and the server (Node.js).
var poly2tri;
/* global exports */
if (typeof exports !== 'undefined') {
poly2tri = exports;
} else {
poly2tri = global.poly2tri = {};
}
// Runs the library in noConflict mode, returning the poly2tri variable
// to its previous owner. Returns a reference to this library object.
poly2tri.noConflict = function() {
global.poly2tri = previousPoly2tri;
return this;
};
// -------------------------------------------------------------------PointError
/**
* Custom exception class to indicate invalid Point values
* @param {String} message error message
* @param {array<Point>} points invalid points
*/
// Class added in the JavaScript version (was not present in the c++ version)
var PointError = function (message, points) {
this.name = "PointError";
this.points = points = points || [];
this.message = message || "Invalid Points!";
for (var i = 0; i < points.length; i++) {
this.message += " " + Point.toString(points[i]);
}
};
PointError.prototype = new Error();
PointError.prototype.constructor = PointError;
// ------------------------------------------------------------------------Point
/**
* Construct a point
* @param {Number} x coordinate (0 if undefined)
* @param {Number} y coordinate (0 if undefined)
*/
var Point = function(x, y) {
this.x = +x || 0;
this.y = +y || 0;
// All extra fields added to Point are prefixed with _p2t_
// to avoid collisions if custom Point class is used.
// The edges this point constitutes an upper ending point
this._p2t_edge_list = null;
};
/**
* For pretty printing ex. <i>"(5;42)"</i>)
*/
Point.prototype.toString = function() {
return ("(" + this.x + ";" + this.y + ")");
};
/**
* Creates a copy of this Point object.
* @returns Point
*/
Point.prototype.clone = function() {
return new Point(this.x, this.y);
};
/**
* Set this Point instance to the origo. <code>(0; 0)</code>
*/
Point.prototype.set_zero = function() {
this.x = 0.0;
this.y = 0.0;
return this; // for chaining
};
/**
* Set the coordinates of this instance.
* @param x number.
* @param y number;
*/
Point.prototype.set = function(x, y) {
this.x = +x || 0;
this.y = +y || 0;
return this; // for chaining
};
/**
* Negate this Point instance. (component-wise)
*/
Point.prototype.negate = function() {
this.x = -this.x;
this.y = -this.y;
return this; // for chaining
};
/**
* Add another Point object to this instance. (component-wise)
* @param n Point object.
*/
Point.prototype.add = function(n) {
this.x += n.x;
this.y += n.y;
return this; // for chaining
};
/**
* Subtract this Point instance with another point given. (component-wise)
* @param n Point object.
*/
Point.prototype.sub = function(n) {
this.x -= n.x;
this.y -= n.y;
return this; // for chaining
};
/**
* Multiply this Point instance by a scalar. (component-wise)
* @param s scalar.
*/
Point.prototype.mul = function(s) {
this.x *= s;
this.y *= s;
return this; // for chaining
};
/**
* Return the distance of this Point instance from the origo.
*/
Point.prototype.length = function() {
return Math.sqrt(this.x * this.x + this.y * this.y);
};
/**
* Normalize this Point instance (as a vector).
* @return The original distance of this instance from the origo.
*/
Point.prototype.normalize = function() {
var len = this.length();
this.x /= len;
this.y /= len;
return len;
};
/**
* Test this Point object with another for equality.
* @param p any "Point like" object with {x,y} (duck typing)
* @return <code>True</code> if <code>this == p</code>, <code>false</code> otherwise.
*/
Point.prototype.equals = function(p) {
return this.x === p.x && this.y === p.y;
};
// -----------------------------------------------------Point ("static" methods)
/**
* Negate a point component-wise and return the result as a new Point object.
* @param p Point object.
* @return the resulting Point object.
*/
Point.negate = function(p) {
return new Point(-p.x, -p.y);
};
/**
* Add two points component-wise and return the result as a new Point object.
* @param a Point object.
* @param b Point object.
* @return the resulting Point object.
*/
Point.add = function(a, b) {
return new Point(a.x + b.x, a.y + b.y);
};
/**
* Subtract two points component-wise and return the result as a new Point object.
* @param a Point object.
* @param b Point object.
* @return the resulting Point object.
*/
Point.sub = function(a, b) {
return new Point(a.x - b.x, a.y - b.y);
};
/**
* Multiply a point by a scalar and return the result as a new Point object.
* @param s the scalar (a number).
* @param p Point object.
* @return the resulting Point object.
*/
Point.mul = function(s, p) {
return new Point(s * p.x, s * p.y);
};
/**
* Perform the cross product on either two points (this produces a scalar)
* or a point and a scalar (this produces a point).
* This function requires two parameters, either may be a Point object or a
* number.
* @param a Point object or scalar.
* @param b Point object or scalar.
* @return a Point object or a number, depending on the parameters.
*/
Point.cross = function(a, b) {
if (typeof(a) === 'number') {
if (typeof(b) === 'number') {
return a * b;
} else {
return new Point(-a * b.y, a * b.x);
}
} else {
if (typeof(b) === 'number') {
return new Point(b * a.y, -b * a.x);
} else {
return a.x * b.y - a.y * b.x;
}
}
};
// -----------------------------------------------------------------"Point-Like"
/*
* The following functions operate on "Point" or any "Point like" object
* with {x,y} (duck typing).
*/
/**
* Point pretty printing ex. <i>"(5;42)"</i>)
* @param p any "Point like" object with {x,y}
* @returns {String}
*/
Point.toString = function(p) {
// Try a custom toString first, and fallback to Point.prototype.toString if none
var s = p.toString();
return (s === '[object Object]' ? Point.prototype.toString.call(p) : s);
};
/**
* Compare two points component-wise.
* @param a,b any "Point like" objects with {x,y}
* @return <code>< 0</code> if <code>a < b</code>,
* <code>> 0</code> if <code>a > b</code>,
* <code>0</code> otherwise.
*/
Point.compare = function(a, b) {
if (a.y === b.y) {
return a.x - b.x;
} else {
return a.y - b.y;
}
};
Point.cmp = Point.compare; // backward compatibility
/**
* Test two Point objects for equality.
* @param a,b any "Point like" objects with {x,y}
* @return <code>True</code> if <code>a == b</code>, <code>false</code> otherwise.
*/
Point.equals = function(a, b) {
return a.x === b.x && a.y === b.y;
};
/**
* Peform the dot product on two vectors.
* @param a,b any "Point like" objects with {x,y}
* @return The dot product (as a number).
*/
Point.dot = function(a, b) {
return a.x * b.x + a.y * b.y;
};
// -------------------------------------------------------------------------Edge
/**
* Represents a simple polygon's edge
* @param {Point} p1
* @param {Point} p2
*/
var Edge = function(p1, p2) {
this.p = p1;
this.q = p2;
if (p1.y > p2.y) {
this.q = p1;
this.p = p2;
} else if (p1.y === p2.y) {
if (p1.x > p2.x) {
this.q = p1;
this.p = p2;
} else if (p1.x === p2.x) {
throw new PointError('poly2tri Invalid Edge constructor: repeated points!', [p1]);
}
}
if (! this.q._p2t_edge_list) {
this.q._p2t_edge_list = [];
}
this.q._p2t_edge_list.push(this);
};
// ---------------------------------------------------------------------Triangle
/**
* Triangle class.<br>
* Triangle-based data structures are known to have better performance than
* quad-edge structures.
* See: J. Shewchuk, "Triangle: Engineering a 2D Quality Mesh Generator and
* Delaunay Triangulator", "Triangulations in CGAL"
*
* @param a,b,c any "Point like" objects with {x,y} (duck typing)
*/
var Triangle = function(a, b, c) {
// Triangle points
this.points_ = [a, b, c];
// Neighbor list
this.neighbors_ = [null, null, null];
// Has this triangle been marked as an interior triangle?
this.interior_ = false;
// Flags to determine if an edge is a Constrained edge
this.constrained_edge = [false, false, false];
// Flags to determine if an edge is a Delauney edge
this.delaunay_edge = [false, false, false];
};
/**
* For pretty printing ex. <i>"[(5;42)(10;20)(21;30)]"</i>)
*/
Triangle.prototype.toString = function() {
var p2s = Point.toString;
return ("[" + p2s(this.points_[0]) + p2s(this.points_[1]) + p2s(this.points_[2]) + "]");
};
Triangle.prototype.getPoint = function(index) {
return this.points_[index];
};
// for backward compatibility
Triangle.prototype.GetPoint = Triangle.prototype.getPoint;
Triangle.prototype.getNeighbor = function(index) {
return this.neighbors_[index];
};
/**
* Test if this Triangle contains the Point object given as parameters as its
* vertices. Only point references are compared, not values.
* @return <code>True</code> if the Point object is of the Triangle's vertices,
* <code>false</code> otherwise.
*/
Triangle.prototype.containsPoint = function(point) {
var points = this.points_;
// Here we are comparing point references, not values
return (point === points[0] || point === points[1] || point === points[2]);
};
/**
* Test if this Triangle contains the Edge object given as parameter as its
* bounding edges. Only point references are compared, not values.
* @return <code>True</code> if the Edge object is of the Triangle's bounding
* edges, <code>false</code> otherwise.
*/
Triangle.prototype.containsEdge = function(edge) {
return this.containsPoint(edge.p) && this.containsPoint(edge.q);
};
Triangle.prototype.containsPoints = function(p1, p2) {
return this.containsPoint(p1) && this.containsPoint(p2);
};
Triangle.prototype.isInterior = function() {
return this.interior_;
};
Triangle.prototype.setInterior = function(interior) {
this.interior_ = interior;
return this;
};
/**
* Update neighbor pointers.
* @param {Point} p1 Point object.
* @param {Point} p2 Point object.
* @param {Triangle} t Triangle object.
*/
Triangle.prototype.markNeighborPointers = function(p1, p2, t) {
var points = this.points_;
// Here we are comparing point references, not values
if ((p1 === points[2] && p2 === points[1]) || (p1 === points[1] && p2 === points[2])) {
this.neighbors_[0] = t;
} else if ((p1 === points[0] && p2 === points[2]) || (p1 === points[2] && p2 === points[0])) {
this.neighbors_[1] = t;
} else if ((p1 === points[0] && p2 === points[1]) || (p1 === points[1] && p2 === points[0])) {
this.neighbors_[2] = t;
} else {
throw new Error('poly2tri Invalid Triangle.markNeighborPointers() call');
}
};
/**
* Exhaustive search to update neighbor pointers
* @param {Triangle} t
*/
Triangle.prototype.markNeighbor = function(t) {
var points = this.points_;
if (t.containsPoints(points[1], points[2])) {
this.neighbors_[0] = t;
t.markNeighborPointers(points[1], points[2], this);
} else if (t.containsPoints(points[0], points[2])) {
this.neighbors_[1] = t;
t.markNeighborPointers(points[0], points[2], this);
} else if (t.containsPoints(points[0], points[1])) {
this.neighbors_[2] = t;
t.markNeighborPointers(points[0], points[1], this);
}
};
Triangle.prototype.clearNeigbors = function() {
this.neighbors_[0] = null;
this.neighbors_[1] = null;
this.neighbors_[2] = null;
};
Triangle.prototype.clearDelunayEdges = function() {
this.delaunay_edge[0] = false;
this.delaunay_edge[1] = false;
this.delaunay_edge[2] = false;
};
/**
* Returns the point clockwise to the given point.
*/
Triangle.prototype.pointCW = function(p) {
var points = this.points_;
// Here we are comparing point references, not values
if (p === points[0]) {
return points[2];
} else if (p === points[1]) {
return points[0];
} else if (p === points[2]) {
return points[1];
} else {
return null;
}
};
/**
* Returns the point counter-clockwise to the given point.
*/
Triangle.prototype.pointCCW = function(p) {
var points = this.points_;
// Here we are comparing point references, not values
if (p === points[0]) {
return points[1];
} else if (p === points[1]) {
return points[2];
} else if (p === points[2]) {
return points[0];
} else {
return null;
}
};
/**
* Returns the neighbor clockwise to given point.
*/
Triangle.prototype.neighborCW = function(p) {
// Here we are comparing point references, not values
if (p === this.points_[0]) {
return this.neighbors_[1];
} else if (p === this.points_[1]) {
return this.neighbors_[2];
} else {
return this.neighbors_[0];
}
};
/**
* Returns the neighbor counter-clockwise to given point.
*/
Triangle.prototype.neighborCCW = function(p) {
// Here we are comparing point references, not values
if (p === this.points_[0]) {
return this.neighbors_[2];
} else if (p === this.points_[1]) {
return this.neighbors_[0];
} else {
return this.neighbors_[1];
}
};
Triangle.prototype.getConstrainedEdgeCW = function(p) {
// Here we are comparing point references, not values
if (p === this.points_[0]) {
return this.constrained_edge[1];
} else if (p === this.points_[1]) {
return this.constrained_edge[2];
} else {
return this.constrained_edge[0];
}
};
Triangle.prototype.getConstrainedEdgeCCW = function(p) {
// Here we are comparing point references, not values
if (p === this.points_[0]) {
return this.constrained_edge[2];
} else if (p === this.points_[1]) {
return this.constrained_edge[0];
} else {
return this.constrained_edge[1];
}
};
Triangle.prototype.setConstrainedEdgeCW = function(p, ce) {
// Here we are comparing point references, not values
if (p === this.points_[0]) {
this.constrained_edge[1] = ce;
} else if (p === this.points_[1]) {
this.constrained_edge[2] = ce;
} else {
this.constrained_edge[0] = ce;
}
};
Triangle.prototype.setConstrainedEdgeCCW = function(p, ce) {
// Here we are comparing point references, not values
if (p === this.points_[0]) {
this.constrained_edge[2] = ce;
} else if (p === this.points_[1]) {
this.constrained_edge[0] = ce;
} else {
this.constrained_edge[1] = ce;
}
};
Triangle.prototype.getDelaunayEdgeCW = function(p) {
// Here we are comparing point references, not values
if (p === this.points_[0]) {
return this.delaunay_edge[1];
} else if (p === this.points_[1]) {
return this.delaunay_edge[2];
} else {
return this.delaunay_edge[0];
}
};
Triangle.prototype.getDelaunayEdgeCCW = function(p) {
// Here we are comparing point references, not values
if (p === this.points_[0]) {
return this.delaunay_edge[2];
} else if (p === this.points_[1]) {
return this.delaunay_edge[0];
} else {
return this.delaunay_edge[1];
}
};
Triangle.prototype.setDelaunayEdgeCW = function(p, e) {
// Here we are comparing point references, not values
if (p === this.points_[0]) {
this.delaunay_edge[1] = e;
} else if (p === this.points_[1]) {
this.delaunay_edge[2] = e;
} else {
this.delaunay_edge[0] = e;
}
};
Triangle.prototype.setDelaunayEdgeCCW = function(p, e) {
// Here we are comparing point references, not values
if (p === this.points_[0]) {
this.delaunay_edge[2] = e;
} else if (p === this.points_[1]) {
this.delaunay_edge[0] = e;
} else {
this.delaunay_edge[1] = e;
}
};
/**
* The neighbor across to given point.
*/
Triangle.prototype.neighborAcross = function(p) {
// Here we are comparing point references, not values
if (p === this.points_[0]) {
return this.neighbors_[0];
} else if (p === this.points_[1]) {
return this.neighbors_[1];
} else {
return this.neighbors_[2];
}
};
Triangle.prototype.oppositePoint = function(t, p) {
var cw = t.pointCW(p);
return this.pointCW(cw);
};
/**
* Legalize triangle by rotating clockwise around oPoint
* @param {Point} opoint
* @param {Point} npoint
*/
Triangle.prototype.legalize = function(opoint, npoint) {
var points = this.points_;
// Here we are comparing point references, not values
if (opoint === points[0]) {
points[1] = points[0];
points[0] = points[2];
points[2] = npoint;
} else if (opoint === points[1]) {
points[2] = points[1];
points[1] = points[0];
points[0] = npoint;
} else if (opoint === points[2]) {
points[0] = points[2];
points[2] = points[1];
points[1] = npoint;
} else {
throw new Error('poly2tri Invalid Triangle.legalize() call');
}
};
/**
* Returns the index of a point in the triangle.
* The point *must* be a reference to one of the triangle's vertices.
* @param {Point} p Point object
* @returns {Number} index 0, 1 or 2
*/
Triangle.prototype.index = function(p) {
var points = this.points_;
// Here we are comparing point references, not values
if (p === points[0]) {
return 0;
} else if (p === points[1]) {
return 1;
} else if (p === points[2]) {
return 2;
} else {
throw new Error('poly2tri Invalid Triangle.index() call');
}
};
Triangle.prototype.edgeIndex = function(p1, p2) {
var points = this.points_;
// Here we are comparing point references, not values
if (p1 === points[0]) {
if (p2 === points[1]) {
return 2;
} else if (p2 === points[2]) {
return 1;
}
} else if (p1 === points[1]) {
if (p2 === points[2]) {
return 0;
} else if (p2 === points[0]) {
return 2;
}
} else if (p1 === points[2]) {
if (p2 === points[0]) {
return 1;
} else if (p2 === points[1]) {
return 0;
}
}
return -1;
};
/**
* Mark an edge of this triangle as constrained.<br>
* This method takes either 1 parameter (an edge index or an Edge instance) or
* 2 parameters (two Point instances defining the edge of the triangle).
*/
Triangle.prototype.markConstrainedEdgeByIndex = function(index) {
this.constrained_edge[index] = true;
};
Triangle.prototype.markConstrainedEdgeByEdge = function(edge) {
this.markConstrainedEdgeByPoints(edge.p, edge.q);
};
Triangle.prototype.markConstrainedEdgeByPoints = function(p, q) {
var points = this.points_;
// Here we are comparing point references, not values
if ((q === points[0] && p === points[1]) || (q === points[1] && p === points[0])) {
this.constrained_edge[2] = true;
} else if ((q === points[0] && p === points[2]) || (q === points[2] && p === points[0])) {
this.constrained_edge[1] = true;
} else if ((q === points[1] && p === points[2]) || (q === points[2] && p === points[1])) {
this.constrained_edge[0] = true;
}
};
// ------------------------------------------------------------------------utils
var PI_3div4 = 3 * Math.PI / 4;
var PI_2 = Math.PI / 2;
var EPSILON = 1e-12;
/*
* Inital triangle factor, seed triangle will extend 30% of
* PointSet width to both left and right.
*/
var kAlpha = 0.3;
var Orientation = {
"CW": 1,
"CCW": -1,
"COLLINEAR": 0
};
/**
* Forumla to calculate signed area<br>
* Positive if CCW<br>
* Negative if CW<br>
* 0 if collinear<br>
* <pre>
* A[P1,P2,P3] = (x1*y2 - y1*x2) + (x2*y3 - y2*x3) + (x3*y1 - y3*x1)
* = (x1-x3)*(y2-y3) - (y1-y3)*(x2-x3)
* </pre>
*/
function orient2d(pa, pb, pc) {
var detleft = (pa.x - pc.x) * (pb.y - pc.y);
var detright = (pa.y - pc.y) * (pb.x - pc.x);
var val = detleft - detright;
if (val > -(EPSILON) && val < (EPSILON)) {
return Orientation.COLLINEAR;
} else if (val > 0) {
return Orientation.CCW;
} else {
return Orientation.CW;
}
}
function inScanArea(pa, pb, pc, pd) {
var pdx = pd.x;
var pdy = pd.y;
var adx = pa.x - pdx;
var ady = pa.y - pdy;
var bdx = pb.x - pdx;
var bdy = pb.y - pdy;
var adxbdy = adx * bdy;
var bdxady = bdx * ady;
var oabd = adxbdy - bdxady;
if (oabd <= (EPSILON)) {
return false;
}
var cdx = pc.x - pdx;
var cdy = pc.y - pdy;
var cdxady = cdx * ady;
var adxcdy = adx * cdy;
var ocad = cdxady - adxcdy;
if (ocad <= (EPSILON)) {
return false;
}
return true;
}
// ---------------------------------------------------------------AdvancingFront
/**
* Advancing front node
* @param {Point} p any "Point like" object with {x,y} (duck typing)
* @param {Triangle} t triangle (optionnal)
*/
var Node = function(p, t) {
this.point = p;
this.triangle = t || null;
this.next = null; // Node
this.prev = null; // Node
this.value = p.x;
};
var AdvancingFront = function(head, tail) {
this.head_ = head; // Node
this.tail_ = tail; // Node
this.search_node_ = head; // Node
};
AdvancingFront.prototype.head = function() {
return this.head_;
};
AdvancingFront.prototype.setHead = function(node) {
this.head_ = node;
};
AdvancingFront.prototype.tail = function() {
return this.tail_;
};
AdvancingFront.prototype.setTail = function(node) {
this.tail_ = node;
};
AdvancingFront.prototype.search = function() {
return this.search_node_;
};
AdvancingFront.prototype.setSearch = function(node) {
this.search_node_ = node;
};
AdvancingFront.prototype.findSearchNode = function(/*x*/) {
// TODO: implement BST index
return this.search_node_;
};
AdvancingFront.prototype.locateNode = function(x) {
var node = this.search_node_;
/* jshint boss:true */
if (x < node.value) {
while (node = node.prev) {
if (x >= node.value) {
this.search_node_ = node;
return node;
}
}
} else {
while (node = node.next) {
if (x < node.value) {
this.search_node_ = node.prev;
return node.prev;
}
}
}
return null;
};
AdvancingFront.prototype.locatePoint = function(point) {
var px = point.x;
var node = this.findSearchNode(px);
var nx = node.point.x;
if (px === nx) {
// Here we are comparing point references, not values
if (point !== node.point) {
// We might have two nodes with same x value for a short time
if (point === node.prev.point) {
node = node.prev;
} else if (point === node.next.point) {
node = node.next;
} else {
throw new Error('poly2tri Invalid AdvancingFront.locatePoint() call');
}
}
} else if (px < nx) {
/* jshint boss:true */
while (node = node.prev) {
if (point === node.point) {
break;
}
}
} else {
while (node = node.next) {
if (point === node.point) {
break;
}
}
}
if (node) {
this.search_node_ = node;
}
return node;
};
// ------------------------------------------------------------------------Basin
var Basin = function() {
this.left_node = null; // Node
this.bottom_node = null; // Node
this.right_node = null; // Node
this.width = 0.0; // number
this.left_highest = false;
};
Basin.prototype.clear = function() {
this.left_node = null;
this.bottom_node = null;
this.right_node = null;
this.width = 0.0;
this.left_highest = false;
};
// --------------------------------------------------------------------EdgeEvent
var EdgeEvent = function() {
this.constrained_edge = null; // Edge
this.right = false;
};
// ----------------------------------------------------SweepContext (public API)
/**
* Constructor for the triangulation context.
* It accepts a simple polyline, which defines the constrained edges.
* Possible options are:
* cloneArrays: if true, do a shallow copy of the Array parameters
* (contour, holes). Points inside arrays are never copied.
* Default is false : keep a reference to the array arguments,
* who will be modified in place.
* @param {Array} contour array of "Point like" objects with {x,y} (duck typing)
* @param {Object} options constructor options
*/
var SweepContext = function(contour, options) {
options = options || {};
this.triangles_ = [];
this.map_ = [];
this.points_ = (options.cloneArrays ? contour.slice(0) : contour);
this.edge_list = [];
// Bounding box of all points. Computed at the start of the triangulation,
// it is stored in case it is needed by the caller.
this.pmin_ = this.pmax_ = null;
// Advancing front
this.front_ = null; // AdvancingFront
// head point used with advancing front
this.head_ = null; // Point
// tail point used with advancing front
this.tail_ = null; // Point
this.af_head_ = null; // Node
this.af_middle_ = null; // Node
this.af_tail_ = null; // Node
this.basin = new Basin();
this.edge_event = new EdgeEvent();
this.initEdges(this.points_);
};
/**
* Add a hole to the constraints
* @param {Array} polyline array of "Point like" objects with {x,y} (duck typing)
*/