1+ // Based on the three.js Curve classes created by [zz85](http://www.lab4games.net/zz85/blog)
2+
13var Clamp = require ( '../../math/Clamp' ) ;
4+ var Vector2 = require ( '../../math/Vector2' ) ;
25var Vector3 = require ( '../../math/Vector3' ) ;
36var Matrix4 = require ( '../../math/Matrix4' ) ;
47var Class = require ( '../../utils/Class' ) ;
58
6- /**
7- * @author zz85 / http://www.lab4games.net/zz85/blog
8- * Extensible curve object
9- *
10- * Some common of Curve methods
11- * .getPoint(t), getTangent(t)
12- * .getPointAt(u), getTangentAt(u)
13- * .getPoints(), .getSpacedPoints()
14- * .getLength()
15- * .updateArcLengths()
16- *
17- * This following classes subclasses THREE.Curve:
18- *
19- * -- 2d classes --
20- * THREE.LineCurve
21- * THREE.QuadraticBezierCurve
22- * THREE.CubicBezierCurve
23- * THREE.SplineCurve
24- * THREE.ArcCurve
25- * THREE.EllipseCurve
26- *
27- * -- 3d classes --
28- * THREE.LineCurve3
29- * THREE.QuadraticBezierCurve3
30- * THREE.CubicBezierCurve3
31- * THREE.SplineCurve3
32- *
33- * A series of curves can be represented as a THREE.CurvePath
34- *
35- **/
36-
37- /**************************************************************
38- * Abstract Curve base class
39- **************************************************************/
9+ // Local cache vars
10+
11+ var tmpVec2A = new Vector2 ( ) ;
12+ var tmpVec2B = new Vector2 ( ) ;
13+
14+ // Our Base Curve which all other curves extend
4015
4116var Curve = new Class ( {
4217
4318 initialize :
4419
4520 function Curve ( )
4621 {
22+ this . arcLengthDivisions = 200 ;
4723 } ,
4824
4925 // Get point at relative position in curve according to arc length
5026 // - u [0 .. 1]
5127
52- getPointAt : function ( u )
28+ getPointAt : function ( u , out )
5329 {
5430 var t = this . getUtoTmapping ( u ) ;
5531
56- return this . getPoint ( t ) ;
32+ return this . getPoint ( t , out ) ;
5733 } ,
5834
5935 // Get sequence of points using getPoint( t )
6036
6137 getPoints : function ( divisions )
6238 {
63- if ( ! divisions )
64- {
65- divisions = 5 ;
66- }
39+ if ( divisions === undefined ) { divisions = 5 ; }
6740
6841 var points = [ ] ;
6942
@@ -79,10 +52,7 @@ var Curve = new Class({
7952
8053 getSpacedPoints : function ( divisions )
8154 {
82- if ( ! divisions )
83- {
84- divisions = 5 ;
85- }
55+ if ( divisions === undefined ) { divisions = 5 ; }
8656
8757 var points = [ ] ;
8858
@@ -107,10 +77,7 @@ var Curve = new Class({
10777
10878 getLengths : function ( divisions )
10979 {
110- if ( ! divisions )
111- {
112- divisions = ( this . __arcLengthDivisions ) ? ( this . __arcLengthDivisions ) : 200 ;
113- }
80+ if ( divisions === undefined ) { divisions = this . arcLengthDivisions ; }
11481
11582 if ( this . cacheArcLengths &&
11683 ( this . cacheArcLengths . length === divisions + 1 ) &&
@@ -123,15 +90,15 @@ var Curve = new Class({
12390
12491 var cache = [ ] ;
12592 var current ;
126- var last = this . getPoint ( 0 ) ;
93+ var last = this . getPoint ( 0 , tmpVec2A ) ;
12794 var sum = 0 ;
12895
12996 cache . push ( 0 ) ;
13097
13198 for ( var p = 1 ; p <= divisions ; p ++ )
13299 {
133- current = this . getPoint ( p / divisions ) ;
134- sum += current . distanceTo ( last ) ;
100+ current = this . getPoint ( p / divisions , tmpVec2B ) ;
101+ sum += current . distance ( last ) ;
135102 cache . push ( sum ) ;
136103 last = current ;
137104 }
@@ -199,9 +166,7 @@ var Curve = new Class({
199166
200167 if ( arcLengths [ i ] === targetArcLength )
201168 {
202- var t = i / ( il - 1 ) ;
203-
204- return t ;
169+ return i / ( il - 1 ) ;
205170 }
206171
207172 // we could get finer grain at lengths, or use simple interpolation between two points
@@ -225,8 +190,10 @@ var Curve = new Class({
225190 // 2 points a small delta apart will be used to find its gradient
226191 // which seems to give a reasonable approximation
227192
228- getTangent : function ( t )
193+ getTangent : function ( t , out )
229194 {
195+ if ( out === undefined ) { out = new Vector2 ( ) ; }
196+
230197 var delta = 0.0001 ;
231198 var t1 = t - delta ;
232199 var t2 = t + delta ;
@@ -243,19 +210,22 @@ var Curve = new Class({
243210 t2 = 1 ;
244211 }
245212
246- var pt1 = this . getPoint ( t1 ) ;
247- var pt2 = this . getPoint ( t2 ) ;
213+ // var pt1 = this.getPoint(t1);
214+ // var pt2 = this.getPoint(t2);
215+ // var vec = pt2.clone().sub(pt1);
216+ // return vec.normalize();
248217
249- var vec = pt2 . clone ( ) . sub ( pt1 ) ;
218+ this . getPoint ( t1 , tmpVec2A ) ;
219+ this . getPoint ( t2 , out ) ;
250220
251- return vec . normalize ( ) ;
221+ return out . sub ( tmpVec2A ) . normalize ( ) ;
252222 } ,
253223
254- getTangentAt : function ( u )
224+ getTangentAt : function ( u , out )
255225 {
256226 var t = this . getUtoTmapping ( u ) ;
257227
258- return this . getTangent ( t ) ;
228+ return this . getTangent ( t , out ) ;
259229 } ,
260230
261231 computeFrenetFrames : function ( segments , closed )
0 commit comments