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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
|
/* <copyright>
This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/>
(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved.
</copyright> */
// Todo: This entire class should be converted to a module
var VecUtils = require("js/helper-classes/3D/vec-utils").VecUtils;
function SubpathOffsetPoint(pos, mapPos) {
this.Pos = Vector.create([pos[0],pos[1],pos[2]]);
this.CurveMapPos = Vector.create([mapPos[0], mapPos[1], mapPos[2]]);
}
function SubpathOffsetTriangle(v0, v1, v2) {
this.v0 = v0;
this.v1 = v1;
this.v2 = v2;
this.n = Vector.create([0,0,1]); //replace with the actual cross product later
}
function sortNumberAscending(a,b){
return a-b;
}
function sortNumberDescending(a,b){
return b-a;
}
function SegmentIntersections(){
this.paramArray = [];
}
///////////////////////////////////////////////////////////////////////
// Class GLSubpath
// representation a sequence of cubic bezier curves.
// Derived from class GLGeomObj
///////////////////////////////////////////////////////////////////////
function GLSubpath() {
///////////////////////////////////////////////////
// Instance variables
///////////////////////////////////////////////////
this._Anchors = [];
this._BBoxMin = [0, 0, 0];
this._BBoxMax = [0, 0, 0];
this._isClosed = false;
this._samples = []; //polyline representation of this curve
this._sampleParam = []; //parametric distance of samples, within [0, N], where N is # of Bezier curves (=# of anchor points if closed, =#anchor pts -1 if open)
this._anchorSampleIndex = []; //index within _samples corresponding to anchor points
this._UnprojectedAnchors = [];
//initially set the _dirty bit so we will construct samples
this._dirty = true;
//whether or not to use the canvas drawing to stroke/fill
this._useCanvasDrawing = true;
//the X and Y location of this subpath's canvas in stage world space of Ninja
this._canvasX = 0;
this._canvasY = 0;
//stroke information
this._strokeWidth = 0.0;
this._strokeColor = [0.4, 0.4, 0.4, 1.0];
this._strokeMaterial;
this._strokeStyle = "Solid";
this._materialAmbient = [0.2, 0.2, 0.2, 1.0];
this._materialDiffuse = [0.4, 0.4, 0.4, 1.0];
this._materialSpecular = [0.4, 0.4, 0.4, 1.0];
this._fillColor = [0.4, 0.4, 0.4, 1.0];
this._fillMaterial;
this._DISPLAY_ANCHOR_RADIUS = 5;
//drawing context
this._world = null;
//tool that owns this subpath
this._drawingTool = null;
this._planeMat = null;
this._planeMatInv = null;
this._planeCenter = null;
this.inheritedFrom = GLGeomObj;
this.inheritedFrom();
//used to query what the user selected, OR-able for future extensions
this.SEL_NONE = 0; //nothing was selected
this.SEL_ANCHOR = 1; //anchor point was selected
this.SEL_PREV = 2; //previous handle of anchor point was selected
this.SEL_NEXT = 4; //next handle of anchor point was selected
this.SEL_PATH = 8; //the path itself was selected
this._selectMode = this.SEL_NONE;
this._selectedAnchorIndex = -1;
this._SAMPLING_EPSILON = 0.5; //epsilon used for sampling the curve
this._DEFAULT_STROKE_WIDTH = 20; //use only if stroke width not specified
this._MAX_OFFSET_ANGLE = 10; //max angle (in degrees) between consecutive vectors from curve to offset path
// (current GLGeomObj complains if buildBuffers/render is added to GLSubpath prototype)
//buildBuffers
// Build the stroke vertices, normals, textures and colors
// Add that array data to the GPU using OpenGL data binding
this.buildBuffers = function () {
return; //no need to do anything for now
}//buildBuffers()
//render
// specify how to render the subpath in Canvas2D
this.render = function () {
// get the world
var world = this.getWorld();
if (!world) throw( "null world in subpath render" );
// get the context
var ctx = world.get2DContext();
if (!ctx) throw ("null context in subpath render")
var numAnchors = this.getNumAnchors();
if (numAnchors === 0)
return; //nothing to do for empty paths
ctx.save();
this.createSamples(); //dirty bit checked in this function...will generate a polyline representation
var bboxMin = this.getBBoxMin();
var bboxMax = this.getBBoxMax();
var bboxWidth = bboxMax[0] - bboxMin[0];
var bboxHeight = bboxMax[1] - bboxMin[1];
var bboxMid = Vector.create([0.5 * (bboxMax[0] + bboxMin[0]), 0.5 * (bboxMax[1] + bboxMin[1]), 0.5 * (bboxMax[2] + bboxMin[2])]);
ctx.clearRect(0, 0, bboxWidth, bboxHeight);
ctx.lineWidth = this._strokeWidth;
ctx.strokeStyle = "black";
if (this._strokeColor)
ctx.strokeStyle = MathUtils.colorToHex( this._strokeColor );
ctx.fillStyle = "white";
if (this._fillColor){
//ctx.fillStyle = MathUtils.colorToHex( this._fillColor );
var fillColorStr = "rgba("+parseInt(255*this._fillColor[0])+","+parseInt(255*this._fillColor[1])+","+parseInt(255*this._fillColor[2])+","+this._fillColor[3]+")";
ctx.fillStyle = fillColorStr;
}
var lineCap = ['butt','round','square'];
ctx.lineCap = lineCap[1];
ctx.beginPath();
/*
commenting this out for now because of Chrome bug where coincident endpoints of bezier curve cause the curve to not be rendered
var prevAnchor = this.getAnchor(0);
ctx.moveTo(prevAnchor.getPosX()-bboxMin[0],prevAnchor.getPosY()-bboxMin[1]);
for (var i = 1; i < numAnchors; i++) {
var currAnchor = this.getAnchor(i);
ctx.bezierCurveTo(prevAnchor.getNextX()-bboxMin[0],prevAnchor.getNextY()-bboxMin[1], currAnchor.getPrevX()-bboxMin[0], currAnchor.getPrevY()-bboxMin[1], currAnchor.getPosX()-bboxMin[0], currAnchor.getPosY()-bboxMin[1]);
prevAnchor = currAnchor;
}
if (this._isClosed === true) {
var currAnchor = this.getAnchor(0);
ctx.bezierCurveTo(prevAnchor.getNextX()-bboxMin[0],prevAnchor.getNextY()-bboxMin[1], currAnchor.getPrevX()-bboxMin[0], currAnchor.getPrevY()-bboxMin[1], currAnchor.getPosX()-bboxMin[0], currAnchor.getPosY()-bboxMin[1]);
prevAnchor = currAnchor;
ctx.fill();
}
*/
var numPoints = this._samples.length/3;
ctx.moveTo(this._samples[0]-bboxMin[0],this._samples[1]-bboxMin[1]);
for (var i=0;i<numPoints;i++){
ctx.lineTo(this._samples[3*i]-bboxMin[0],this._samples[3*i + 1]-bboxMin[1]);
}
if (this._isClosed === true) {
ctx.lineTo(this._samples[0]-bboxMin[0],this._samples[1]-bboxMin[1]);
}
ctx.fill();
ctx.stroke();
ctx.restore();
} //render()
this.geomType = function () { return this.GEOM_TYPE_CUBIC_BEZIER; }
} //function GLSubpath ...class definition
/////////////////////////////////////////////////////////
// Property Accessors/Setters
/////////////////////////////////////////////////////////
GLSubpath.prototype.setWorld = function (world) { this._world = world; }
GLSubpath.prototype.getWorld = function () { return this._world; }
GLSubpath.prototype.makeDirty = function () {this._dirty = true;}
GLSubpath.prototype.setDrawingTool = function (tool) {this._drawingTool = tool;}
GLSubpath.prototype.getDrawingTool = function () {return this._drawingTool;}
GLSubpath.prototype.setPlaneMatrix = function(planeMat){this._planeMat = planeMat;}
GLSubpath.prototype.setPlaneMatrixInverse = function(planeMatInv){this._planeMatInv = planeMatInv;}
GLSubpath.prototype.setPlaneCenter = function(pc){this._planeCenter = pc;}
GLSubpath.prototype.getCanvasX = function(){return this._canvasX;}
GLSubpath.prototype.getCanvasY = function(){return this._canvasY;}
GLSubpath.prototype.setCanvasX = function(cx){this._canvasX=cx;}
GLSubpath.prototype.setCanvasY = function(cy){this._canvasY=cy;}
GLSubpath.prototype.getIsClosed = function () {return this._isClosed;}
GLSubpath.prototype.setIsClosed = function (isClosed) {
if (this._isClosed !== isClosed) {
this._isClosed = isClosed;
this._dirty = true;
}
}
GLSubpath.prototype.getNumAnchors = function () { return this._Anchors.length; }
GLSubpath.prototype.getAnchor = function (index) { return this._Anchors[index]; }
GLSubpath.prototype.addAnchor = function (anchorPt) {
this._Anchors.push(anchorPt);
this._selectedAnchorIndex = this._Anchors.length-1;
this._dirty = true;
}
GLSubpath.prototype.insertAnchor = function(anchorPt, index){
this._Anchors.splice(index, 0, anchorPt);
}
//remove and return anchor at specified index, return null on error
GLSubpath.prototype.removeAnchor = function (index) {
var retAnchor = null;
if (index < this._Anchors.length) {
retAnchor = this._Anchors.splice(index, 1);
this._dirty = true;
}
//deselect the removed anchor
this._selectedAnchorIndex = -1;
return retAnchor;
}
GLSubpath.prototype.deselectAnchorPoint = function(){
this._selectedAnchorIndex = -1;
}
GLSubpath.prototype.reversePath = function() {
var revAnchors = [];
var numAnchors = this._Anchors.length;
var lastIndex = numAnchors-1;
if (lastIndex<0){
return; //cannot reverse empty path
}
for (var i=lastIndex;i>=0;i--) {
var newAnchor = new GLAnchorPoint();
var oldAnchor = this._Anchors[i];
newAnchor.setPos(oldAnchor.getPosX(),oldAnchor.getPosY(),oldAnchor.getPosZ());
newAnchor.setPrevPos(oldAnchor.getNextX(),oldAnchor.getNextY(),oldAnchor.getNextZ());
newAnchor.setNextPos(oldAnchor.getPrevX(),oldAnchor.getPrevY(),oldAnchor.getPrevZ());
revAnchors.push(newAnchor);
}
if (this._selectedAnchorIndex >= 0){
this._selectedAnchorIndex = (numAnchors-1) - this._selectedAnchorIndex;
}
this._Anchors = revAnchors;
this._dirty=true;
}
//remove all the anchor points
GLSubpath.prototype.clearAllAnchors = function () {
this._Anchors = [];
this._isClosed = false;
this._dirty = true;
}
GLSubpath.prototype.insertAnchorAtParameter = function(index, param) {
if (index+1 >= this._Anchors.length && !this._isClosed) {
return;
}
//insert an anchor after the specified index using the parameter, using De Casteljau subdivision
var nextIndex = (index+1)%this._Anchors.length;
//build the De Casteljau points
var P0P1 = VecUtils.vecInterpolate(3, this._Anchors[index].getPos(), this._Anchors[index].getNext(), param);
var P1P2 = VecUtils.vecInterpolate(3, this._Anchors[index].getNext(), this._Anchors[nextIndex].getPrev(), param);
var P2P3 = VecUtils.vecInterpolate(3, this._Anchors[nextIndex].getPrev(), this._Anchors[nextIndex].getPos(), param);
var P0P1P2 = VecUtils.vecInterpolate(3, P0P1, P1P2, param);
var P1P2P3 = VecUtils.vecInterpolate(3, P1P2, P2P3, param);
var anchorPos = VecUtils.vecInterpolate(3, P0P1P2, P1P2P3, param);
//update the next of the anchor at index and prev of anchor at nextIndex
var isPrevCoincident = false;
var isNextCoincident = false;
if (VecUtils.vecDist( 3, P0P1, this._Anchors[index].getNext()) < this._SAMPLING_EPSILON) {
//no change to the next point
isPrevCoincident = true;
} else {
this._Anchors[index].setNextPos(P0P1[0], P0P1[1], P0P1[2]);
}
if (VecUtils.vecDist( 3, P2P3, this._Anchors[nextIndex].getPrev()) < this._SAMPLING_EPSILON) {
//no change to the prev point
isNextCoincident = true;
} else {
this._Anchors[nextIndex].setPrevPos(P2P3[0], P2P3[1], P2P3[2]);
}
//create a new anchor point
var newAnchor = new GLAnchorPoint();
if (isPrevCoincident && isNextCoincident){
anchorPos[0]=P1P2[0];anchorPos[1]=P1P2[1];anchorPos[2]=P1P2[2];
newAnchor.setPos(anchorPos[0],anchorPos[1],anchorPos[2]);
newAnchor.setPrevPos(anchorPos[0],anchorPos[1],anchorPos[2]);
newAnchor.setNextPos(anchorPos[0],anchorPos[1],anchorPos[2]);
} else {
newAnchor.setPrevPos(P0P1P2[0], P0P1P2[1], P0P1P2[2]);
newAnchor.setNextPos(P1P2P3[0], P1P2P3[1], P1P2P3[2]);
newAnchor.setPos(anchorPos[0], anchorPos[1], anchorPos[2]);
}
//insert the new anchor point at the correct index and set it as the selected anchor
this._Anchors.splice(nextIndex, 0, newAnchor);
this._selectedAnchorIndex = nextIndex;
this._dirty = true;
}
GLSubpath.prototype._checkIntersectionWithSamples = function(startIndex, endIndex, point, radius){
//check whether the point is within the radius distance from the curve represented as a polyline in _samples
//return the parametric distance along the curve if there is an intersection, else return null
//will assume that the BBox test is performed outside this function
if (endIndex<startIndex){
//go from startIndex to the end of the samples
endIndex = this._samples.length/3;
}
for (var i=startIndex; i<endIndex; i++){
var seg0 = Vector.create([this._samples[3*i], this._samples[3*i + 1], this._samples[3*i + 2]]);
var j=i+1;
var seg1 = Vector.create([this._samples[3*j], this._samples[3*j + 1], this._samples[3*j + 2]]);
var distToSegment = MathUtils.distPointToSegment(point, seg0, seg1);
if (distToSegment<=radius){
var paramDistance = MathUtils.paramPointProjectionOnSegment(point, seg0, seg1); //TODO Optimize! this function was called in distPointToSegment above
var retParam = this._sampleParam[i] + (this._sampleParam[j] - this._sampleParam[i])*paramDistance;
return retParam;
}
}
return null;
}
GLSubpath.prototype._checkIntersection = function(controlPts, beginParam, endParam, point, radius) {
//check whether the point is within radius distance from the curve
// if there is an intersection, return the parameter value (between beginParam and endParam) of the intersection point, else return null
var bboxMin = Vector.create([Infinity, Infinity, Infinity]);
var bboxMax = Vector.create([-Infinity,-Infinity,-Infinity]);
for (var i=0;i<controlPts.length;i++) {
for (var d=0;d<3;d++){
if (controlPts[i][d] < bboxMin[d]){
bboxMin[d] = controlPts[i][d];
}
if (controlPts[i][d] > bboxMax[d]){
bboxMax[d] = controlPts[i][d];
}
}
}
//check whether the bbox of the control points contains the point within the specified radius
for (var d=0;d<3;d++){
if (point[d] < (bboxMin[d]-radius)){
return null;
}
if (point[d] > (bboxMax[d]+radius)){
return null;
}
}
//check if the curve is already flat, and if so, check the distance from the segment C0C3 to the point
//measure distance of C1 and C2 to segment C0-C3
var distC1 = MathUtils.distPointToSegment(controlPts[1], controlPts[0], controlPts[3]);
var distC2 = MathUtils.distPointToSegment(controlPts[2], controlPts[0], controlPts[3]);
var maxDist = Math.max(distC1, distC2);
var threshold = this._SAMPLING_EPSILON; //this should be set outside this function //TODO
if (maxDist < threshold) { //if the curve is flat
var distP = MathUtils.distPointToSegment(point, controlPts[0], controlPts[3]); //TODO we may need to neglect cases where the non-perpendicular distance is used...
if (distP>radius)
return null;
else {
var param = MathUtils.paramPointProjectionOnSegment(point, controlPts[0], controlPts[3]); //TODO this function is already called in distPointToSegment...optimize by removing redundant call
//var param = VecUtils.vecDist(3, point, controlPts[0])/VecUtils.vecDist(3, controlPts[3], controlPts[0]);
if (param<0)
param=0;
if (param>1)
param=1;
return beginParam + (endParam-beginParam)*param;
}
}
//subdivide this curve using De Casteljau interpolation
var C0_ = VecUtils.vecInterpolate(3, controlPts[0], controlPts[1], 0.5);
var C1_ = VecUtils.vecInterpolate(3, controlPts[1], controlPts[2], 0.5);
var C2_ = VecUtils.vecInterpolate(3, controlPts[2], controlPts[3], 0.5);
var C0__ = VecUtils.vecInterpolate(3, C0_, C1_, 0.5);
var C1__ = VecUtils.vecInterpolate(3, C1_, C2_, 0.5);
var C0___ = VecUtils.vecInterpolate(3, C0__, C1__, 0.5);
//recursively sample the first half of the curve
var midParam = (endParam+beginParam)*0.5;
var param1 = this._checkIntersection(Vector.create([controlPts[0],C0_,C0__,C0___]), beginParam, midParam, point, radius);
if (param1!==null){
return param1;
}
//recursively sample the second half of the curve
var param2 = this._checkIntersection(Vector.create([C0___,C1__,C2_,controlPts[3]]), midParam, endParam, point, radius);
if (param2!==null){
return param2;
}
//no intersection, so return null
return null;
}
//whether the point lies within the bbox given by the four control points
GLSubpath.prototype._isWithinBoundingBox = function(point, ctrlPts, radius) {
var bboxMin = Vector.create([Infinity, Infinity, Infinity]);
var bboxMax = Vector.create([-Infinity,-Infinity,-Infinity]);
for (var i=0;i<ctrlPts.length;i++) {
for (var d=0;d<3;d++){
if (ctrlPts[i][d] < bboxMin[d]){
bboxMin[d] = ctrlPts[i][d];
}
if (ctrlPts[i][d] > bboxMax[d]){
bboxMax[d] = ctrlPts[i][d];
}
}
}
//check whether the bbox of the control points contains the point within the specified radius
for (var d=0;d<3;d++){
if (point[d] < (bboxMin[d]-radius)){
return false;
}
if (point[d] > (bboxMax[d]+radius)){
return false;
}
}
return true;
}
GLSubpath.prototype.pickAnchor = function (pickX, pickY, pickZ, radius) {
var numAnchors = this._Anchors.length;
var selAnchorIndex = -1;
var retCode = this.SEL_NONE;
var radSq = radius * radius;
var minDistance = Infinity;
//check if the clicked location is close to the currently selected anchor position
if (this._selectedAnchorIndex>=0 && this._selectedAnchorIndex<this._Anchors.length){
var distSq = this._Anchors[this._selectedAnchorIndex].getDistanceSq(pickX, pickY, pickZ);
//check the anchor point
if (distSq < minDistance && distSq < radSq) {
selAnchorIndex = this._selectedAnchorIndex;
minDistance = distSq;
retCode = retCode | this.SEL_ANCHOR;
}
}
//now check if the click location is close to any anchor position
if (selAnchorIndex===-1) {
for (var i = 0; i < numAnchors; i++) {
var distSq = this._Anchors[i].getDistanceSq(pickX, pickY, pickZ);
//check the anchor point
if (distSq < minDistance && distSq < radSq) {
selAnchorIndex = i;
minDistance = distSq;
}
}//for every anchor i
}
return selAnchorIndex;
}
//pick the path point closest to the specified location, return null if some anchor point (or its handles) is within radius, else return the parameter distance
GLSubpath.prototype.pickPath = function (pickX, pickY, pickZ, radius) {
var numAnchors = this._Anchors.length;
var selAnchorIndex = -1;
var retCode = this.SEL_NONE;
var radSq = radius * radius;
var minDistance = Infinity;
//check if the clicked location is close to the currently selected anchor position
if (this._selectedAnchorIndex>=0 && this._selectedAnchorIndex<this._Anchors.length){
var distSq = this._Anchors[this._selectedAnchorIndex].getDistanceSq(pickX, pickY, pickZ);
//check the anchor point
if (distSq < minDistance && distSq < radSq) {
selAnchorIndex = this._selectedAnchorIndex;
minDistance = distSq;
retCode = retCode | this.SEL_ANCHOR;
}
}
//now check if the click location is close to any anchor position
if (selAnchorIndex===-1) {
for (var i = 0; i < numAnchors; i++) {
var distSq = this._Anchors[i].getDistanceSq(pickX, pickY, pickZ);
//check the anchor point
if (distSq < minDistance && distSq < radSq) {
selAnchorIndex = i;
minDistance = distSq;
retCode = retCode | this.SEL_ANCHOR;
}
}//for every anchor i
}
//check the prev and next of the selected anchor if the above did not register a hit
if (this._selectedAnchorIndex>=0 && selAnchorIndex === -1) {
var distSq = this._Anchors[this._selectedAnchorIndex].getPrevDistanceSq(pickX, pickY, pickZ);
if (distSq < minDistance && distSq < radSq){
selAnchorIndex = this._selectedAnchorIndex;
minDistance = distSq;
retCode = retCode | this.SEL_PREV;
} else {
//check the next for this anchor point
distSq = this._Anchors[this._selectedAnchorIndex].getNextDistanceSq(pickX, pickY, pickZ);
if (distSq<minDistance && distSq<radSq){
selAnchorIndex = this._selectedAnchorIndex;
minDistance = distSq;
retCode = retCode | this.SEL_NEXT;
}
}
}
var retParam = null;
if (retCode !== this.SEL_NONE) {
retCode = retCode | this.SEL_PATH; //ensure that path is also selected if anything else is selected
this._selectedAnchorIndex = selAnchorIndex;
} else {
this._selectedAnchorIndex = -1;
var numSegments = this._isClosed ? numAnchors : numAnchors-1;
for (var i = 0; i < numSegments; i++) {
var nextIndex = (i+1)%numAnchors;
//check if the point is close to the bezier segment between anchor i and anchor nextIndex
var controlPoints = Vector.create([Vector.create([this._Anchors[i].getPosX(),this._Anchors[i].getPosY(),this._Anchors[i].getPosZ()]),
Vector.create([this._Anchors[i].getNextX(),this._Anchors[i].getNextY(),this._Anchors[i].getNextZ()]),
Vector.create([this._Anchors[nextIndex].getPrevX(),this._Anchors[nextIndex].getPrevY(),this._Anchors[nextIndex].getPrevZ()]),
Vector.create([this._Anchors[nextIndex].getPosX(),this._Anchors[nextIndex].getPosY(),this._Anchors[nextIndex].getPosZ()])]);
var point = Vector.create([pickX, pickY, pickZ]);
if (this._isWithinBoundingBox(point, controlPoints, radius)) {
//var intersectParam = this._checkIntersection(controlPoints, 0.0, 1.0, point, radius);
var intersectParam = this._checkIntersectionWithSamples(this._anchorSampleIndex[i], this._anchorSampleIndex[nextIndex]
|