-
Notifications
You must be signed in to change notification settings - Fork 14
/
IKRS.BezierPath.js
794 lines (555 loc) · 22.2 KB
/
IKRS.BezierPath.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
/**
* @author Ikaros Kappler
* @date 2013-08-19
* @version 1.0.0
**/
IKRS.BezierPath = function( pathPoints ) {
//window.alert( "bezierCurves=" + bezierCurves );
//window.alert( "pathPoints=" + pathPoints );
IKRS.Object.call( this );
if( !pathPoints )
pathPoints = [];
this.totalArcLength = 0.0;
this.bezierCurves = [];
for( var i = 1; i < pathPoints.length; i++ ) {
var bounds = new THREE.Box2( pathPoints[i].x - pathPoints[i-1].x,
pathPoints[i].y - pathPoints[i-1].y
);
// window.alert( "pathSegment=(" + pathPoints[i-1].x + "," + pathPoints[i-1].y + ") to (" + pathPoints[i].x + "," + pathPoints[i].y + "); bounds=(" + bounds.max + ", " + bounds.min + ")" );
// Create a new Bezier curve inside the box
var bCurve = new IKRS.CubicBezierCurve( pathPoints[i-1],
pathPoints[i],
new THREE.Vector2( pathPoints[i-1].x, // + bounds.max/2,
pathPoints[i-1].y - bounds.min/2
),
// This control point will be auto-adjusted in the next step
new THREE.Vector2( pathPoints[i].x + bounds.max/2,
pathPoints[i].y // - bounds.min/2
)
);
this.bezierCurves.push( bCurve );
this.totalArcLength += bCurve.getLength();
// Auto adjust the second control point (should be on a linear sub-space)
if( this.bezierCurves.length >= 2 ) {
this.adjustSuccessorControlPoint( this.bezierCurves.length-2, // curveIndex,
true, // obtain handle length?
true // update arc lengths
);
}
}
}
IKRS.BezierPath.prototype = new IKRS.Object();
IKRS.BezierPath.prototype.constructor = IKRS.BezierPath;
// These variables equal the values from IKRS.CubicBezierCurve
IKRS.BezierPath.prototype.START_POINT = 0;
IKRS.BezierPath.prototype.START_CONTROL_POINT = 1;
IKRS.BezierPath.prototype.END_CONTROL_POINT = 2;
IKRS.BezierPath.prototype.END_POINT = 3;
IKRS.BezierPath.prototype.getLength = function() {
return this.totalArcLength;
};
IKRS.BezierPath.prototype.computeVerticalAreaSize = function( deltaSize, useAbsoluteValues ) {
var bounds = this.computeBoundingBox();
var relativeX = bounds.xMax;
//window.alert( "relativeX=" + relativeX );
var size = 0.0;
for( var i = 0; i < this.bezierCurves.length; i++ ) {
size += this.bezierCurves[i].computeVerticalAreaSize( relativeX, // An imaginary x-axis at the right bound
deltaSize,
useAbsoluteValues
);
}
return size;
};
IKRS.BezierPath.prototype.computeVerticalRevolutionVolumeSize = function( useAbsoluteValues ) {
var bounds = this.computeBoundingBox();
var relativeX = bounds.xMax;
//window.alert( "relativeX=" + relativeX );
var volume = 0.0;
for( var i = 0; i < this.bezierCurves.length; i++ ) {
volume += this.bezierCurves[i].computeVerticalRevolutionVolumeSize( relativeX, // An imaginary x-axis at the right bound
//deltaSize,
useAbsoluteValues
);
}
return volume;
};
IKRS.BezierPath.prototype.updateArcLengths = function() {
this.totalArcLength = 0.0;
for( var i = 0; i < this.bezierCurves.length; i++ ) {
this.bezierCurves[ i ].updateArcLengths();
this.totalArcLength += this.bezierCurves[ i ].getLength();
}
}
IKRS.BezierPath.prototype.getCurveCount = function() {
return this.bezierCurves.length;
}
IKRS.BezierPath.prototype.getCurveAt = function( curveIndex ) {
return this.bezierCurves[ curveIndex ];
}
IKRS.BezierPath.prototype.removeEndPoint = function() {
if( this.bezierCurves.length <= 1 )
return false;
var newArray = [ this.bezierCurves.length-1 ];
for( var i = 0; i < this.bezierCurves.length-1; i++ ) {
newArray[i] = this.bezierCurves[i];
}
// Update arc length
this.totalArcLength -= this.bezierCurves[ this.bezierCurves.length-1 ].getLength();
this.bezierCurves = newArray;
return true;
}
IKRS.BezierPath.prototype.removeStartPoint = function() {
if( this.bezierCurves.length <= 1 )
return false;
var newArray = [ this.bezierCurves.length-1 ];
for( var i = 1; i < this.bezierCurves.length; i++ ) {
newArray[i-1] = this.bezierCurves[i];
}
// Update arc length
this.totalArcLength -= this.bezierCurves[ 0 ].getLength();
this.bezierCurves = newArray;
return true;
}
/**
* This function joins the bezier curve at the given index with
* its predecessor.
**/
IKRS.BezierPath.prototype.joinAt = function( curveIndex ) {
//window.alert( "curveIndex=" + curveIndex );
if( curveIndex < 0 || curveIndex >= this.bezierCurves.length )
return false;
var leftCurve = this.bezierCurves[ curveIndex-1 ];
var rightCurve = this.bezierCurves[ curveIndex ];
// Make the length of the new handle double that long
var leftControlPoint = leftCurve.getStartControlPoint().clone();
leftControlPoint.sub( leftCurve.getStartPoint() );
leftControlPoint.multiplyScalar( 2.0 );
leftControlPoint.add( leftCurve.getStartPoint() );
var rightControlPoint = rightCurve.getEndControlPoint().clone();
rightControlPoint.sub( rightCurve.getEndPoint() );
rightControlPoint.multiplyScalar( 2.0 );
rightControlPoint.add( rightCurve.getEndPoint() );
var newCurve = new IKRS.CubicBezierCurve( leftCurve.getStartPoint(),
rightCurve.getEndPoint(),
leftControlPoint,
rightControlPoint
);
// Place into array
var newArray = [ this.bezierCurves.length - 1 ];
for( var i = 0; i < curveIndex-1; i++ ) {
newArray[ i ] = this.bezierCurves[i];
}
newArray[ curveIndex-1 ] = newCurve;
// Shift trailing curves left
for( var i = curveIndex; i+1 < this.bezierCurves.length; i++ ) {
newArray[ i ] = this.bezierCurves[ i+1 ];
}
this.bezierCurves = newArray;
this.updateArcLengths();
return true;
}
IKRS.BezierPath.prototype.splitAt = function( curveIndex,
segmentIndex
) {
// Must be a valid curve index
if( curveIndex < 0 || curveIndex >= this.bezierCurves.length )
return false;
var oldCurve = this.bezierCurves[ curveIndex ];
// Segment must be an INNER point!
// (the outer points are already bezier end/start points!)
if( segmentIndex < 1 || segmentIndex-1 >= oldCurve.segmentCache.length )
return false;
// Make room for a new curve
for( var c = this.bezierCurves.length; c > curveIndex; c-- ) {
// Move one position to the right
this.bezierCurves[ c ] = this.bezierCurves[ c-1 ];
}
// Accumulate segment lengths
var u = 0;
for( var i = 0; i < segmentIndex; i++ )
u += oldCurve.segmentLengths[i];
var tangent = oldCurve.getTangentAt( u );
//tangent = tangent.normalize();
//window.alert( JSON.stringify(tangent) );
tangent = tangent.multiplyScalar( 0.25 );
var leftEndControlPoint = oldCurve.segmentCache[ segmentIndex ].clone();
leftEndControlPoint.sub( tangent );
//tangent.negate();
var rightStartControlPoint = oldCurve.segmentCache[ segmentIndex ].clone();
rightStartControlPoint.add( tangent );
// Make the old existing handles a quarter that long
var leftStartControlPoint = oldCurve.getStartControlPoint().clone();
// move to (0,0)
leftStartControlPoint.sub( oldCurve.getStartPoint() );
leftStartControlPoint.multiplyScalar( 0.25 );
leftStartControlPoint.add( oldCurve.getStartPoint() );
var rightEndControlPoint = oldCurve.getEndControlPoint().clone(); // oldCurve.getStartControlPoint().clone();
// move to (0,0)
rightEndControlPoint.sub( oldCurve.getEndPoint() );
rightEndControlPoint.multiplyScalar( 0.25 );
rightEndControlPoint.add( oldCurve.getEndPoint() );
var newLeft = new IKRS.CubicBezierCurve( oldCurve.getStartPoint(), // old start point
oldCurve.segmentCache[ segmentIndex ], // new end point
leftStartControlPoint, // old start control point
leftEndControlPoint // new end control point
);
var newRight = new IKRS.CubicBezierCurve( oldCurve.segmentCache[ segmentIndex ], // new start point
oldCurve.getEndPoint(), // old end point
rightStartControlPoint, // new start control point
rightEndControlPoint // old end control point
);
// Insert split curve(s) at free index
this.bezierCurves[ curveIndex ] = newLeft;
this.bezierCurves[ curveIndex + 1 ] = newRight;
// Update total arc length, even if there is only a very little change!
this.totalArcLength -= oldCurve.getLength();
this.totalArcLength += newLeft.getLength();
this.totalArcLength += newRight.getLength();
return true;
}
IKRS.BezierPath.prototype.scale = function( anchor, // Vector2
scaling // Vector2
) {
for( var i = 0; i < this.bezierCurves.length; i++ ) {
var curve = this.bezierCurves[ i ];
IKRS.BezierPath._scalePoint( curve.getStartPoint(), anchor, scaling );
IKRS.BezierPath._scalePoint( curve.getStartControlPoint(), anchor, scaling );
IKRS.BezierPath._scalePoint( curve.getEndControlPoint(), anchor, scaling );
// Do NOT scale the end point here!
// Don't forget that the curves are connected and on curve's end point
// the the successor's start point (same instance)!
}
// Finally move the last end point (was not scaled yet)
if( this.bezierCurves.length > 0 ) {
IKRS.BezierPath._scalePoint( this.bezierCurves[ this.bezierCurves.length-1 ].getEndPoint(),
anchor,
scaling
);
}
this.updateArcLengths();
}
IKRS.BezierPath._scalePoint = function( point, // Vector2
anchor, // Vector2
scaling // Vector2
) {
// Move point to origin
point.sub( anchor );
// Apply scaling
point.setX( point.x * scaling.x );
point.setY( point.y * scaling.y );
// Move back to original position
point.add( anchor );
}
IKRS.BezierPath.prototype.getPointAt = function( u ) {
if( u < 0 || u > this.totalArcLength ) {
console.log( "[IKRS.BezierPath.getPointAt(u)] u is out of bounds: " + u + "." );
return null;
}
// Find the spline to extract the value from
var i = 0;
var uTemp = 0.0;
while( i < this.bezierCurves.length &&
(uTemp + this.bezierCurves[i].getLength()) < u
) {
uTemp += this.bezierCurves[ i ].getLength();
i++;
}
//window.alert( i );
// if u == arcLength
// -> i is max
if( i >= this.bezierCurves.length )
return this.bezierCurves[ this.bezierCurves.length-1 ].getEndPoint().clone();
var bCurve = this.bezierCurves[ i ];
var relativeU = u - uTemp;
//window.alert( "relativeU=" + relativeU );
return bCurve.getPointAt( relativeU );
}
IKRS.BezierPath.prototype.getPoint = function( t ) {
//window.alert( "IKRS.BezierPath.totalArcLength=" + this.totalArcLength );
return this.getPointAt( t * this.totalArcLength );
}
IKRS.BezierPath.prototype.getTangent = function( t ) {
return this.getTangentAt( t * this.totalArcLength );
}
IKRS.BezierPath.prototype.getTangentAt = function( u ) {
if( u < 0 || u > this.totalArcLength ) {
console.log( "[IKRS.BezierPath.getTangentAt(u)] u is out of bounds: " + u + "." );
return null;
}
// Find the spline to extract the value from
var i = 0;
var uTemp = 0.0;
while( i < this.bezierCurves.length &&
(uTemp + this.bezierCurves[i].getLength()) < u
) {
uTemp += this.bezierCurves[ i ].getLength();
i++;
}
//window.alert( i );
// if u == arcLength
// -> i is max
//if( i >= this.bezierCurves.length )
// return this.bezierCurves[ this.bezierCurves.length-1 ].getEndPoint().clone();
var bCurve = this.bezierCurves[ i ];
var relativeU = u - uTemp;
//window.alert( "relativeU["+i+"]=" + relativeU );
return bCurve.getTangentAt( relativeU );
// !!!
//return new THREE.Vector2( 0, 0 );
}
IKRS.BezierPath.prototype.getPerpendicular = function( t ) {
return this.getPerpendicularAt( t * this.totalArcLength );
}
IKRS.BezierPath.prototype.getPerpendicularAt = function( u ) {
if( u < 0 || u > this.totalArcLength ) {
console.log( "[IKRS.BezierPath.getPerpendicularAt(u)] u is out of bounds: " + u + "." );
return null;
}
// Find the spline to extract the value from
var i = 0;
var uTemp = 0.0;
while( i < this.bezierCurves.length &&
(uTemp + this.bezierCurves[i].getLength()) < u
) {
uTemp += this.bezierCurves[ i ].getLength();
i++;
}
//window.alert( i );
// if u == arcLength
// -> i is max
//if( i >= this.bezierCurves.length )
// return this.bezierCurves[ this.bezierCurves.length-1 ].getEndPoint().clone();
var bCurve = this.bezierCurves[ i ];
var relativeU = u - uTemp;
//window.alert( "relativeU["+i+"]=" + relativeU );
return bCurve.getPerpendicularAt( relativeU );
// !!!
//return new THREE.Vector2( 0, 0 );
}
IKRS.BezierPath.prototype.computeBoundingBox = function() {
if( this.bezierCurves.length == 0 ) {
// Empty box
return new IKRS.BoundingBox( 0, 0, 0, 0 );
}
var boundingBox = this.bezierCurves[ 0 ].computeBoundingBox();
for( var i = 1; i < this.bezierCurves.length; i++ ) {
var tmpBounds = this.bezierCurves[ i ].computeBoundingBox();
boundingBox.xMin = Math.min( boundingBox.xMin, tmpBounds.xMin );
boundingBox.xMax = Math.max( boundingBox.xMax, tmpBounds.xMax );
boundingBox.yMin = Math.min( boundingBox.yMin, tmpBounds.yMin );
boundingBox.yMax = Math.max( boundingBox.yMax, tmpBounds.yMax );
}
return boundingBox;
}
IKRS.BezierPath.prototype.moveCurvePoint = function( curveIndex, // int
pointID, // int
moveAmount // THREE.Vector2
) {
var bCurve = this.getCurveAt( curveIndex );
bCurve.moveCurvePoint( pointID,
moveAmount,
true, // move control point, too
true // updateArcLengths
);
//window.alert( "old.x=" + bCurve.getStartPoint().x + ", new.x=" + (this.getCurveAt( curveIndex ).getStartPoint()).x );
// If inner point and NOT control point
// --> move neightbour
if( pointID == this.START_POINT && curveIndex > 0 ) {
// Set predecessor's control point!
var predecessor = this.getCurveAt( curveIndex-1 );
predecessor.moveCurvePoint( this.END_CONTROL_POINT,
moveAmount,
true, // move control point, too
false // updateArcLengths
);
} else if( pointID == this.END_POINT && curveIndex+1 < this.bezierCurves.length ) {
// Set successcor
var successor = this.getCurveAt( curveIndex+1 );
successor.moveCurvePoint( this.START_CONTROL_POINT,
moveAmount,
true, // move control point, too
false // updateArcLengths
);
} else if( pointID == this.START_CONTROL_POINT && curveIndex > 0 ) {
this.adjustPredecessorControlPoint( curveIndex,
true, // obtain handle length?
false // update arc lengths
);
} else if( pointID == this.END_CONTROL_POINT && curveIndex+1 < this.getCurveCount() ) {
this.adjustSuccessorControlPoint( curveIndex,
true, // obtain handle length?
false // update arc lengths
);
}
// Don't forget to update the arc lengths!
// Note: this can be optimized as only two curves have changed their lengths!
this.updateArcLengths();
}
IKRS.BezierPath.prototype.adjustPredecessorControlPoint = function( curveIndex, // int
obtainHandleLength, // boolean
updateArcLengths // boolean
) {
if( curveIndex <= 0 )
return false;
/*
return this.adjustNeighbourControlPoint( this.getCurveAt( curveIndex ), // mainCurve,
this.getCurveAt( curveIndex-1 ), //neighbourCurve
obtainHandleLength,
updateArcLengths
);
*/
var mainCurve = this.getCurveAt( curveIndex );
var neighbourCurve = this.getCurveAt( curveIndex-1 );
return this.adjustNeighbourControlPoint( mainCurve,
neighbourCurve,
mainCurve.getStartPoint(), // the reference point
mainCurve.getStartControlPoint(), // the dragged control point
neighbourCurve.getEndPoint(), // the neighbour's point
neighbourCurve.getEndControlPoint(), // the neighbour's control point to adjust
obtainHandleLength,
updateArcLengths
);
}
IKRS.BezierPath.prototype.adjustSuccessorControlPoint = function( curveIndex, // int
obtainHandleLength, // boolean
updateArcLengths // boolean
) {
if( curveIndex+1 > this.getCurveCount() )
return false;
var mainCurve = this.getCurveAt( curveIndex );
var neighbourCurve = this.getCurveAt( curveIndex+1 );
return this.adjustNeighbourControlPoint( mainCurve,
neighbourCurve,
mainCurve.getEndPoint(), // the reference point
mainCurve.getEndControlPoint(), // the dragged control point
neighbourCurve.getStartPoint(), // the neighbour's point
neighbourCurve.getStartControlPoint(), // the neighbour's control point to adjust
obtainHandleLength,
updateArcLengths
);
}
// private
IKRS.BezierPath.prototype.adjustNeighbourControlPoint = function( mainCurve,
neighbourCurve,
mainPoint,
mainControlPoint,
neighbourPoint,
neighbourControlPoint,
obtainHandleLengths, // boolean
updateArcLengths
) {
/*
var neighbourPoint = neighbourCurve.getEndPoint();
var neighbourControlPoint = neighbourCurve.getEndControlPoint();
var mainPoint = mainCurve.getStartPoint();
var mainControlPoint = mainCurve.getStartControlPoint();
*/
// Calculate start handle length
var mainHandleBounds = new THREE.Vector2( mainControlPoint.x - mainPoint.x,
mainControlPoint.y - mainPoint.y
);
var neighbourHandleBounds = new THREE.Vector2( neighbourControlPoint.x - neighbourPoint.x,
neighbourControlPoint.y - neighbourPoint.y
);
var mainHandleLength = Math.sqrt( Math.pow(mainHandleBounds.x,2) + Math.pow(mainHandleBounds.y,2) );
var neighbourHandleLength = Math.sqrt( Math.pow(neighbourHandleBounds.x,2) + Math.pow(neighbourHandleBounds.y,2) );
if( mainHandleLength <= 0.1 )
return; // no secure length available for division
// Just invert the main handle
neighbourControlPoint.set( neighbourPoint.x - mainHandleBounds.x * (neighbourHandleLength/mainHandleLength),
neighbourPoint.y - mainHandleBounds.y * (neighbourHandleLength/mainHandleLength)
);
neighbourCurve.updateArcLengths();
}
IKRS.BezierPath.prototype.clone = function() {
var path = new IKRS.BezierPath( null );
for( var i = 0; i < this.bezierCurves.length; i++ ) {
path.bezierCurves.push( this.bezierCurves[i].clone() );
// Connect splines
if( i > 0 )
path.bezierCurves[i-1].endPoint = path.bezierCurves[i].startPoint;
}
path.updateArcLengths();
return path;
}
IKRS.BezierPath.prototype.equals = function( path ) {
if( !path )
return false;
// Check if path contains the credentials
if( !path.bezierCurves )
return false;
if( typeof path.bezierCurves.length == "undefined" )
return false;
if( path.bezierCurves.length != this.bezierCurves.length )
return false;
for( var i = 0; i < this.bezierCurves.length; i++ ) {
if( !this.bezierCurves[i].equals(path.bezierCurves[i]) )
return false;
}
return true;
}
IKRS.BezierPath.prototype.toJSON = function( prettyFormat ) {
//window.alert( "[IKRS.BezierPath.toJSON()] this.bezierCurves.length=" + this.bezierCurves.length );
var buffer = [];
buffer.push( "[" ); // array begin
for( var i = 0; i < this.bezierCurves.length; i++ ) {
if( i > 0 )
buffer.push( "," );
if( prettyFormat)
buffer.push( "\n\t" );
else
buffer.push( " " );
buffer.push( this.bezierCurves[i].toJSON( prettyFormat ) );
}
if( this.bezierCurves.length != 0 )
buffer.push( " " );
buffer.push( "]" ); // array end
return buffer.join( "" ); // Convert to string, with empty separator.
}
IKRS.BezierPath.fromJSON = function( jsonString ) {
var obj = JSON.parse( jsonString );
return IKRS.BezierPath.fromArray( obj );
}
IKRS.BezierPath.fromArray = function( arr ) {
//window.alert( (typeof arr) + ", " + JSON.stringify( arr ) );
if( !Array.isArray(arr) )
throw "[IKRS.BezierPath.fromArray] Passed object must be an array.";
if( arr.length < 2 )
throw "[IKRS.BezierPath.fromArray] Passed array must contain at least two bezier curves (has " + arr.length + ").";
// Create an empty bezier path
var bPath = new IKRS.BezierPath( null );
var lastCurve = null;
for( var i = 0; i < arr.length; i++ ) {
// Convert object to bezier curve
var bCurve = IKRS.CubicBezierCurve.fromObject( arr[i] );
// Set curve start point?
// (avoid duplicate point instances!)
if( lastCurve )
bCurve.startPoint = lastCurve.endPoint;
// Add to path's internal list
bPath.bezierCurves.push( bCurve );
bPath.totalArcLength += bCurve.getLength();
lastCurve = bCurve;
}
// Bezier segments added.
// Recalculate length?
//bPath.updateArcLengths();
// Done
return bPath;
}
/*
// Try JSON conversion
var tmpPoints = [];
tmpPoints.push( new THREE.Vector2( -200, 200 ) );
tmpPoints.push( new THREE.Vector2( 0, 40 ) );
tmpPoints.push( new THREE.Vector2( 200, -130 ) );
var tmpBPath = new IKRS.BezierPath( tmpPoints );
window.alert( "tmpBPath=" + tmpBPath );
var tmpJSON = tmpBPath.toJSON();
window.alert( "tmpJSON=" + tmpJSON );
// Re-construct path from JSON string
var tmpBPath2 = IKRS.BezierPath.fromJSON( tmpJSON );
window.alert( "tmpBPath2=" + tmpBPath2.toJSON() );
*/
//window.alert( "IKRS.BezierPath.prototype=" + IKRS.BezierPath.prototype );