-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathlibtess.cat.js
5778 lines (5016 loc) · 167 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
/**
* Copyright 2000, Silicon Graphics, Inc. All Rights Reserved.
* Copyright 2012, 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 Eric Veach, July 1994
* @author Brendan Kenny
*/
/**
* Base namespace.
*/
var libtess = libtess || {};
/**
* @define {boolean} [DEBUG description]
*/
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 : ''));
}
};
/**
* [sweepDebugEvent description]
* @param {libtess.GluTesselator} tess
*/
libtess.sweepDebugEvent = function(tess) {
// TODO(bckenny)
// sweep event updated
};
/**
* [GLU_TESS_MAX_COORD description]
* @type {number}
* @const
*/
libtess.GLU_TESS_MAX_COORD = 1e150;
// NOTE(bckenny): from glu.pl generator
/**
* [TRUE_PROJECT description]
* TODO(bckenny): see alg-outline for description
*
* @type {boolean}
*/
libtess.TRUE_PROJECT = false;
/**
* We cache vertex data for single-contour polygons so that we can
* try a quick-and-dirty decomposition first.
* @type {number}
* @const
*/
libtess.TESS_MAX_CACHE = 100;
/**
* [GLU_TESS_DEFAULT_TOLERANCE description]
* @type {number}
* @const
*/
libtess.GLU_TESS_DEFAULT_TOLERANCE = 0.0;
/**
* The begin/end calls must be properly nested. We keep track of
* the current state to enforce the ordering.
*
* @enum {number}
*/
libtess.tessState = {
// TODO(bckenny): only used in GluTesselator, probably move there
T_DORMANT: 0,
T_IN_POLYGON: 1,
T_IN_CONTOUR: 2
};
/**
* 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
// TODO(bckenny): need to export when compiled
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. Values of enum match WebGL
* constants.
*
* @enum {number}
*/
libtess.primitiveType = {
// TODO(bckenny): doc types
// TODO(bckenny): need to export when compiled, but can just use webgl constants when available
GL_LINE_LOOP: 2,
GL_TRIANGLES: 4,
GL_TRIANGLE_STRIP: 5,
GL_TRIANGLE_FAN: 6
};
/**
* The types of errors provided to 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
};
/**
* GLU enums necessary for this project.
* see enumglu.spec
* TODO(bckenny): better source for these?
*
* @enum {number}
*/
libtess.gluEnum = {
// NOTE(bckenny): values from enumglu.spec
// TODO(bckenny): most enums under here? drop GLU? or rename in other ways
GLU_TESS_MESH: 100112, // from tess.c
GLU_TESS_TOLERANCE: 100142,
GLU_TESS_WINDING_RULE: 100140,
GLU_TESS_BOUNDARY_ONLY: 100141,
// TODO(bckenny): should this live in errorType?
GLU_INVALID_ENUM: 100900,
GLU_INVALID_VALUE: 100901,
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
};
/** @typedef {number} */
libtess.PQHandle;
// TODO(bckenny): better typing on key?
/** @typedef {Object} */
libtess.PQKey;
// require libtess
/*global libtess */
/**
* Cached vertex data for single-countour polygons for quick-and-dirty
* decomposition.
* @constructor
*/
libtess.CachedVertex = function() {
/**
* [coords description]
* @type {Array.<number>}
*/
this.coords = [0, 0, 0];
// TODO(bckenny): better way to init?
/**
* [data description]
* @type {Object}
*/
this.data = null;
};
// require libtess
// require libtess.mesh
// require libtess.tessmono
// require libtess.render
// require libtess.normal
// require libtess.sweep
/*global libtess */
// TODO(bckenny): options for just triangles, just tristrips, single tristrip w/ resets
// other primitives with index buffer? would have to add a better tristrip extractor
// monotone poly -> tristrip seems possible...
// TODO(bckenny): create more javascript-y API, e.g. make gluTessEndPolygon async,
// don't require so many temp objects created
/**
* [GluTesselator description]
*
* @constructor
*/
libtess.GluTesselator = function() {
// Only initialize fields which can be changed by the api. Other fields
// are initialized where they are used.
// TODO(bckenny): many of these can be made private
// TODO(bckenny): can we combine call* and call*Data functions?
/*** state needed for collecting the input data ***/
/**
* what begin/end calls have we seen?
* @type {libtess.tessState}
*/
this.state = libtess.tessState.T_DORMANT;
/**
* lastEdge_.org is the most recent vertex
* @private
* @type {libtess.GluHalfEdge}
*/
this.lastEdge_ = null;
/**
* stores the input contours, and eventually the tessellation itself
* @type {libtess.GluMesh}
*/
this.mesh = null;
// NOTE(bckenny): initialized in this.emptyCache_
/**
* Error callback.
* @private
* @type {?function((libtess.errorType|libtess.gluEnum))}
*/
this.callError_ = null;
/*** state needed for projecting onto the sweep plane ***/
/**
* user-specified normal (if provided)
* @type {Array.<number>}
*/
this.normal = [0, 0, 0];
// TODO(bckenny): better way to init these arrays?
/**
* unit vector in s-direction (debugging)
* @type {Array.<number>}
*/
this.sUnit = [0, 0, 0];
/**
* unit vector in t-direction (debugging)
* @type {Array.<number>}
*/
this.tUnit = [0, 0, 0];
/*** state needed for the line sweep ***/
// TODO(bckenny): this could be moved to a sweep state object of some sort
/**
* tolerance for merging features
* @type {number}
*/
this.relTolerance = libtess.GLU_TESS_DEFAULT_TOLERANCE;
/**
* rule for determining polygon interior
* @type {libtess.windingRule}
*/
this.windingRule = libtess.windingRule.GLU_TESS_WINDING_ODD;
/**
* fatal error: needed combine callback
* @type {boolean}
*/
this.fatalError = false;
/**
* edge dictionary for sweep line
* @type {libtess.Dict}
*/
this.dict = null;
// NOTE(bckenny): dict initialized in sweep.initEdgeDict_, removed in sweep.doneEdgeDict_
/**
* priority queue of vertex events
* @type {libtess.PriorityQ}
*/
this.pq = null;
// NOTE(bckenny): pq initialized in sweep.initPriorityQ
/**
* current sweep event being processed
* @type {libtess.GluVertex}
*/
this.event = null;
/**
* Combine callback.
* @private
* @type {?function(Array.<number>, Array.<Object>, Array.<number>): Object}
*/
this.callCombine_ = null;
/*** state needed for rendering callbacks (see render.js) ***/
/**
* mark boundary edges (use EdgeFlag)
* @type {boolean}
*/
this.flagBoundary = false;
/**
* Extract contours, not triangles
* @type {boolean}
*/
this.boundaryOnly = false;
/**
* list of triangles which could not be rendered as strips or fans
* @type {libtess.GluFace}
*/
this.lonelyTriList = null;
/**
* Begin callback.
* @private
* @type {?function(libtess.primitiveType)}
*/
this.callBegin_ = null;
/**
* Edge flag callback.
* @private
* @type {?function(boolean)}
*/
this.callEdgeFlag_ = null;
/**
* Vertex callback.
* @private
* @type {?function(Object)}
*/
this.callVertex_ = null;
/**
* End callback.
* @private
* @type {?function()}
*/
this.callEnd_ = null;
/**
* Mesh callback.
* @private
* @type {?function(libtess.GluMesh)}
*/
this.callMesh_ = null;
/*** rendering callbacks that also pass polygon data ***/
/**
* BeginData callback.
* @private
* @type {?function(libtess.primitiveType, Object)}
*/
this.callBeginData_ = null;
/**
* EdgeFlagData callback.
* @private
* @type {?function(boolean, Object)}
*/
this.callEdgeFlagData_ = null;
/**
* VertexData callback.
* @private
* @type {?function(Object, Object)}
*/
this.callVertexData_ = null;
/**
* EndData callback.
* @private
* @type {?function(Object)}
*/
this.callEndData_ = null;
/**
* ErrorData callback.
* @private
* @type {?function((libtess.errorType|libtess.gluEnum), Object)}
*/
this.callErrorData_ = null;
/**
* CombineData callback.
* @private
* @type {?function(Array.<number>, Array.<Object>, Array.<number>, Object): Object}
*/
this.callCombineData_ = null;
/**
* client data for current polygon
* @private
* @type {Object}
*/
this.polygonData_ = null;
/*** state needed to cache single-contour polygons for renderCache() ***/
/**
* empty cache on next vertex() call
* @type {boolean}
*/
this.emptyCache = false;
// TODO(bckenny): possibly rename to be clear it's a boolean
/**
* number of cached vertices
* @type {number}
*/
this.cacheCount = 0;
/**
* the vertex data
* @type {Array.<libtess.CachedVertex>}
*/
this.cache = new Array(libtess.TESS_MAX_CACHE);
// TODO(bckenny): fill now? or init on demand
for (var i = 0; i < libtess.TESS_MAX_CACHE; i++) {
this.cache[i] = new libtess.CachedVertex();
}
};
/**
* Destory the tesselator object. See README.
*/
libtess.GluTesselator.prototype.gluDeleteTess = function() {
// TODO(bckenny): do we need a public API for this still?
this.requireState_(libtess.tessState.T_DORMANT);
// memFree(tess); TODO(bckenny)
};
/**
* Set properties for control over tesselation. See README.
* @param {libtess.gluEnum} which [description]
* @param {number|boolean} value [description]
*/
libtess.GluTesselator.prototype.gluTessProperty = function(which, value) {
// TODO(bckenny): split into more setters?
// TODO(bckenny): in any case, we can do better than this switch statement
switch (which) {
case libtess.gluEnum.GLU_TESS_TOLERANCE:
if (value < 0 || value > 1) {
break;
}
this.relTolerance = /** @type {number} */(value);
return;
case libtess.gluEnum.GLU_TESS_WINDING_RULE:
var windingRule = /** @type {libtess.windingRule} */(value);
switch (windingRule) {
case libtess.windingRule.GLU_TESS_WINDING_ODD:
case libtess.windingRule.GLU_TESS_WINDING_NONZERO:
case libtess.windingRule.GLU_TESS_WINDING_POSITIVE:
case libtess.windingRule.GLU_TESS_WINDING_NEGATIVE:
case libtess.windingRule.GLU_TESS_WINDING_ABS_GEQ_TWO:
this.windingRule = windingRule;
return;
default:
}
break;
case libtess.gluEnum.GLU_TESS_BOUNDARY_ONLY:
// TODO(bckenny): added boolean param type. make sure ok.
this.boundaryOnly = !!value;
return;
default:
this.callErrorOrErrorData(libtess.gluEnum.GLU_INVALID_ENUM);
return;
}
this.callErrorOrErrorData(libtess.gluEnum.GLU_INVALID_VALUE);
};
/**
* Returns tessellator property
* @param {libtess.gluEnum} which [description]
* @return {number|boolean} [description]
*/
libtess.GluTesselator.prototype.gluGetTessProperty = function(which) {
// TODO(bckenny): as above, split into more getters? and improve on switch statement
// why are these being asserted in getter but not setter?
switch (which) {
case libtess.gluEnum.GLU_TESS_TOLERANCE:
// tolerance should be in range [0..1]
libtess.assert(0 <= this.relTolerance && this.relTolerance <= 1);
return this.relTolerance;
case libtess.gluEnum.GLU_TESS_WINDING_RULE:
var rule = this.windingRule;
libtess.assert(rule === libtess.windingRule.GLU_TESS_WINDING_ODD ||
rule === libtess.windingRule.GLU_TESS_WINDING_NONZERO ||
rule === libtess.windingRule.GLU_TESS_WINDING_POSITIVE ||
rule === libtess.windingRule.GLU_TESS_WINDING_NEGATIVE ||
rule === libtess.windingRule.GLU_TESS_WINDING_ABS_GEQ_TWO);
return rule;
case libtess.gluEnum.GLU_TESS_BOUNDARY_ONLY:
libtess.assert(this.boundaryOnly === true || this.boundaryOnly === false);
return this.boundaryOnly;
default:
this.callErrorOrErrorData(libtess.gluEnum.GLU_INVALID_ENUM);
break;
}
return false;
};
/**
* Lets the user supply the polygon normal, if known. All input data
* is projected into a plane perpendicular to the normal before
* tesselation. All output triangles are oriented CCW with
* respect to the normal (CW orientation can be obtained by
* reversing the sign of the supplied normal). For example, if
* you know that all polygons lie in the x-y plane, call
* "tess.gluTessNormal(0.0, 0.0, 1.0)" before rendering any polygons.
*
* @param {number} x [description]
* @param {number} y [description]
* @param {number} z [description]
*/
libtess.GluTesselator.prototype.gluTessNormal = function(x, y, z) {
this.normal[0] = x;
this.normal[1] = y;
this.normal[2] = z;
};
/**
* Specify callbacks. See README. A null or undefined opt_fn removes current callback.
*
* @param {libtess.gluEnum} which [description]
* @param {?function()=} opt_fn [description]
*/
libtess.GluTesselator.prototype.gluTessCallback = function(which, opt_fn) {
var fn = !opt_fn ? null : opt_fn;
// TODO(bckenny): better opt_fn typing?
switch(which) {
case libtess.gluEnum.GLU_TESS_BEGIN:
this.callBegin_ = /** @type {function(libtess.primitiveType)} */ (fn);
return;
case libtess.gluEnum.GLU_TESS_BEGIN_DATA:
this.callBeginData_ =
/** @type {function(libtess.primitiveType, Object)} */ (fn);
return;
case libtess.gluEnum.GLU_TESS_EDGE_FLAG:
this.callEdgeFlag_ = /** @type {function(boolean)} */ (fn);
// If the client wants boundary edges to be flagged,
// we render everything as separate triangles (no strips or fans).
this.flagBoundary = (!!fn);
return;
case libtess.gluEnum.GLU_TESS_EDGE_FLAG_DATA:
this.callEdgeFlagData_ = /** @type {function(boolean, Object)} */ (fn);
// If the client wants boundary edges to be flagged,
// we render everything as separate triangles (no strips or fans).
this.flagBoundary = (!!fn);
return;
case libtess.gluEnum.GLU_TESS_VERTEX:
this.callVertex_ = /** @type {function(Object)} */ (fn);
return;
case libtess.gluEnum.GLU_TESS_VERTEX_DATA:
this.callVertexData_ = /** @type {function(Object, Object)} */ (fn);
return;
case libtess.gluEnum.GLU_TESS_END:
this.callEnd_ = /** @type {function()} */ (fn);
return;
case libtess.gluEnum.GLU_TESS_END_DATA:
this.callEndData_ = /** @type {function(Object)} */ (fn);
return;
case libtess.gluEnum.GLU_TESS_ERROR:
this.callError_ = /** @type {function((libtess.errorType|libtess.gluEnum))} */ (fn);
return;
case libtess.gluEnum.GLU_TESS_ERROR_DATA:
this.callErrorData_ =
/** @type {function((libtess.errorType|libtess.gluEnum), Object)} */ (fn);
return;
case libtess.gluEnum.GLU_TESS_COMBINE:
this.callCombine_ = /** @type {function(Array.<number>, Array.<Object>, Array.<number>): Object} */ (fn);
return;
case libtess.gluEnum.GLU_TESS_COMBINE_DATA:
this.callCombineData_ = /** @type {function(Array.<number>, Array.<Object>, Array.<number>, Object): Object} */ (fn);
return;
case libtess.gluEnum.GLU_TESS_MESH:
this.callMesh_ = /** @type {function(libtess.GluMesh)} */ (fn);
return;
default:
this.callErrorOrErrorData(libtess.gluEnum.GLU_INVALID_ENUM);
return;
}
};
/**
* Specify a vertex and associated data. Must be within calls to
* beginContour/endContour. See README.
*
* @param {Array.<number>} coords [description]
* @param {Object} data [description]
*/
libtess.GluTesselator.prototype.gluTessVertex = function(coords, data) {
var tooLarge = false;
// TODO(bckenny): pool allocation?
var clamped = [0, 0, 0];
this.requireState_(libtess.tessState.T_IN_CONTOUR);
if (this.emptyCache) {
this.emptyCache_();
this.lastEdge_ = null;
}
for (var i = 0; i < 3; ++i) {
var x = coords[i];
if (x < -libtess.GLU_TESS_MAX_COORD) {
x = -libtess.GLU_TESS_MAX_COORD;
tooLarge = true;
}
if (x > libtess.GLU_TESS_MAX_COORD) {
x = libtess.GLU_TESS_MAX_COORD;
tooLarge = true;
}
clamped[i] = x;
}
if (tooLarge) {
this.callErrorOrErrorData(libtess.errorType.GLU_TESS_COORD_TOO_LARGE);
}
if (this.mesh === null) {
if (this.cacheCount < libtess.TESS_MAX_CACHE) {
this.cacheVertex_(clamped, data);
return;
}
// cache is full, create mesh and add cached verts to it
this.emptyCache_();
}
this.addVertex_(clamped, data);
};
/**
* [gluTessBeginPolygon description]
* @param {Object} data Client data for current polygon
*/
libtess.GluTesselator.prototype.gluTessBeginPolygon = function(data) {
this.requireState_(libtess.tessState.T_DORMANT);
this.state = libtess.tessState.T_IN_POLYGON;
this.cacheCount = 0;
this.emptyCache = false;
this.mesh = null;
this.polygonData_ = data;
};
/**
* [gluTessBeginContour description]
*/
libtess.GluTesselator.prototype.gluTessBeginContour = function() {
this.requireState_(libtess.tessState.T_IN_POLYGON);
this.state = libtess.tessState.T_IN_CONTOUR;
this.lastEdge_ = null;
if (this.cacheCount > 0) {
// Just set a flag so we don't get confused by empty contours
// -- these can be generated accidentally with the obsolete
// NextContour() interface.
// TODO(bckenny): we aren't implementing NextContour() interface.
this.emptyCache = true;
}
};
/**
* [gluTessEndContour description]
*/
libtess.GluTesselator.prototype.gluTessEndContour = function() {
this.requireState_(libtess.tessState.T_IN_CONTOUR);
this.state = libtess.tessState.T_IN_POLYGON;
};
/**
* [gluTessEndPolygon description]
*/
libtess.GluTesselator.prototype.gluTessEndPolygon = function() {
this.requireState_(libtess.tessState.T_IN_POLYGON);
this.state = libtess.tessState.T_DORMANT;
if (this.mesh === null) {
if (!this.flagBoundary && !this.callMesh_) {
// Try some special code to make the easy cases go quickly
// (eg. convex polygons). This code does NOT handle multiple contours,
// intersections, edge flags, and of course it does not generate
// an explicit mesh either.
if (libtess.render.renderCache(this)) {
// TODO(bckenny): why only clear polygonData? does more need to be cleared?
this.polygonData_ = null;
return;
}
}
this.emptyCache_();
}
// Determine the polygon normal and project vertices onto the plane
// of the polygon.
libtess.normal.projectPolygon(this);
// computeInterior(tess) computes the planar arrangement specified
// by the given contours, and further subdivides this arrangement
// into regions. Each region is marked "inside" if it belongs
// to the polygon, according to the rule given by this.windingRule.
// Each interior region is guaranteed be monotone.
libtess.sweep.computeInterior(this);
if (!this.fatalError) {
// If the user wants only the boundary contours, we throw away all edges
// except those which separate the interior from the exterior.
// Otherwise we tessellate all the regions marked "inside".
if (this.boundaryOnly) {
libtess.tessmono.setWindingNumber(this.mesh, 1, true);
} else {
libtess.tessmono.tessellateInterior(this.mesh);
}
this.mesh.checkMesh();
if (this.callBegin_ || this.callEnd_ || this.callVertex_ ||
this.callEdgeFlag_ || this.callBeginData_ || this.callEndData_ ||
this.callVertexData_ || this.callEdgeFlagData_) {
if (this.boundaryOnly) {
// output boundary contours
libtess.render.renderBoundary(this, this.mesh);
} else {
// output strips and fans
libtess.render.renderMesh(this, this.mesh);
}
}
if (this.callMesh_) {
// Throw away the exterior faces, so that all faces are interior.
// This way the user doesn't have to check the "inside" flag,
// and we don't need to even reveal its existence. It also leaves
// the freedom for an implementation to not generate the exterior
// faces in the first place.
libtess.tessmono.discardExterior(this.mesh);
// user wants the mesh itself
this.callMesh_(this.mesh);
this.mesh = null;
this.polygonData_ = null;
return;
}
}
libtess.mesh.deleteMesh(this.mesh);
this.polygonData_ = null;
this.mesh = null;
};
/**
* Return the tessellator to its original dormant state.
* @private
*/
libtess.GluTesselator.prototype.makeDormant_ = function() {
if (this.mesh) {
libtess.mesh.deleteMesh(this.mesh);
}
this.state = libtess.tessState.T_DORMANT;
this.lastEdge_ = null;
this.mesh = null;
};
/**
* [requireState_ description]
* @private
* @param {libtess.tessState} state [description]
*/
libtess.GluTesselator.prototype.requireState_ = function(state) {
if (this.state !== state) {
this.gotoState_(state);
}
};
/**
* [gotoState_ description]
* @private
* @param {libtess.tessState} newState [description]
*/
libtess.GluTesselator.prototype.gotoState_ = function(newState) {
while (this.state !== newState) {
// We change the current state one level at a time, to get to the desired
// state.
if (this.state < newState) {
switch (this.state) {
case libtess.tessState.T_DORMANT:
this.callErrorOrErrorData(
libtess.errorType.GLU_TESS_MISSING_BEGIN_POLYGON);
this.gluTessBeginPolygon(null);
break;
case libtess.tessState.T_IN_POLYGON:
this.callErrorOrErrorData(
libtess.errorType.GLU_TESS_MISSING_BEGIN_CONTOUR);
this.gluTessBeginContour();
break;
}
} else {
switch (this.state) {
case libtess.tessState.T_IN_CONTOUR:
this.callErrorOrErrorData(
libtess.errorType.GLU_TESS_MISSING_END_CONTOUR);
this.gluTessEndContour();
break;
case libtess.tessState.T_IN_POLYGON:
this.callErrorOrErrorData(
libtess.errorType.GLU_TESS_MISSING_END_POLYGON);
// this.gluTessEndPolygon() is too much work!
this.makeDormant_();
break;
}
}
}
};
/**
* [addVertex_ description]
* @private
* @param {Array.<number>} coords [description]
* @param {Object} data [description]
*/
libtess.GluTesselator.prototype.addVertex_ = function(coords, data) {
var e = this.lastEdge_;
if (e === null) {
// Make a self-loop (one vertex, one edge).
e = libtess.mesh.makeEdge(this.mesh);
libtess.mesh.meshSplice(e, e.sym);
} else {
// Create a new vertex and edge which immediately follow e
// in the ordering around the left face.
libtess.mesh.splitEdge(e);
e = e.lNext;
}
// The new vertex is now e.org.
e.org.data = data;
e.org.coords[0] = coords[0];
e.org.coords[1] = coords[1];
e.org.coords[2] = coords[2];
// The winding of an edge says how the winding number changes as we
// cross from the edge''s right face to its left face. We add the
// vertices in such an order that a CCW contour will add +1 to
// the winding number of the region inside the contour.
e.winding = 1;
e.sym.winding = -1;
this.lastEdge_ = e;
};
/**
* [cacheVertex_ description]
* @private
* @param {Array.<number>} coords [description]
* @param {Object} data [description]
*/
libtess.GluTesselator.prototype.cacheVertex_ = function(coords, data) {
var v = this.cache[this.cacheCount];
v.data = data;
v.coords[0] = coords[0];
v.coords[1] = coords[1];
v.coords[2] = coords[2];
++this.cacheCount;
};
/**
* [emptyCache_ description]
* @private
*/
libtess.GluTesselator.prototype.emptyCache_ = function() {
// NOTE(bckenny): surprise!
this.mesh = new libtess.GluMesh();
for (var i = 0; i < this.cacheCount; i++) {
var v = this.cache[i];
this.addVertex_(v.coords, v.data);
}
this.cacheCount = 0;
this.emptyCache = false;
};
// TODO(bckenny): all following conditional callbacks could be simplified
// TODO(bckenny): using null for now, but may rework
// TODO(bckenny): should add documentation that references in callback are volatile (or make a copy)
// see README callback descriptions
/**
* [callBeginOrBeginData description]
* @param {libtess.primitiveType} type [description]
*/
libtess.GluTesselator.prototype.callBeginOrBeginData = function(type) {
if (this.callBeginData_) {
this.callBeginData_(type, this.polygonData_);
} else if (this.callBegin_) {
this.callBegin_(type);
}
};