Browse Source

Curve: Refactoring

Mugen87 8 years ago
parent
commit
19bf96a627

+ 21 - 20
docs/api/extras/core/Curve.html

@@ -11,14 +11,10 @@
 		<h1>[name]</h1>
 		<h1>[name]</h1>
 
 
 		<div class="desc">
 		<div class="desc">
-		An abstract base class for creating a curve object that contains methods for interpolation.
-		For an array of Curves see [page:CurvePath].
+		An abstract base class for creating a [name] object that contains methods for interpolation.
+		For an array of [name]s see [page:CurvePath].
 		</div>
 		</div>
 
 
-		<h2>Examples</h2>
-
-		<h3>[example:webgl_geometry_extrude_splines geometry / extrude / splines ]</h3>
-
 		<h2>Constructor</h2>
 		<h2>Constructor</h2>
 
 
 
 
@@ -27,26 +23,31 @@
 		This constructor creates a new [name].
 		This constructor creates a new [name].
 		</div>
 		</div>
 
 
+		<h2>Properties</h2>
+
+		<h3>[property:Integer arcLengthDivisions]</h3>
+		<div>This value determines the amount of divisions when calculating the cumulative segment lengths of a curve via [page:.getLengths].
+			To ensure precision when using methods like [page:.getSpacedPoints], it is recommended to increase [page:.arcLengthDivisions] if the curve is very large. Default is 200.</div>
 
 
 		<h2>Methods</h2>
 		<h2>Methods</h2>
 
 
-		<h3>[method:Vector getPoint]( [page:Number t] )</h3>
-		<div>Returns a vector for point t of the curve where t is between 0 and 1. Must be implemented in the extending class.</div>
+		<h3>[method:Vector getPoint]( [page:Float t] )</h3>
+		<div>Returns a vector for point t of the curve where t is between 0 and 1. Must be implemented in the extending curve.</div>
 
 
-		<h3>[method:Vector getPointAt]( [page:Number u] )</h3>
+		<h3>[method:Vector getPointAt]( [page:Float u] )</h3>
 		<div>
 		<div>
 			Returns a vector for point at a relative position in curve according to arc length.
 			Returns a vector for point at a relative position in curve according to arc length.
 			u is in the range [0, 1].
 			u is in the range [0, 1].
 		</div>
 		</div>
 
 
-		<h3>[method:Array getPoints]( [page:integer divisions] )</h3>
+		<h3>[method:Array getPoints]( [page:Integer divisions] )</h3>
 		<div>
 		<div>
 			divisions -- number of pieces to divide the curve into. Default is *5*.<br /><br />
 			divisions -- number of pieces to divide the curve into. Default is *5*.<br /><br />
 
 
 			Returns a set of divisions + 1 points using getPoint( t ).
 			Returns a set of divisions + 1 points using getPoint( t ).
 		</div>
 		</div>
 
 
-		<h3>[method:Array getSpacedPoints]( divisions )</h3>
+		<h3>[method:Array getSpacedPoints]( [page:Integer divisions] )</h3>
 		<div>
 		<div>
 			divisions -- number of pieces to divide the curve into. Default is *5*.<br /><br />
 			divisions -- number of pieces to divide the curve into. Default is *5*.<br /><br />
 
 
@@ -56,33 +57,33 @@
 		<h3>[method:Float getLength]()</h3>
 		<h3>[method:Float getLength]()</h3>
 		<div>Get total curve arc length.</div>
 		<div>Get total curve arc length.</div>
 
 
-		<h3>[method:Array getLengths]( divisions )</h3>
+		<h3>[method:Array getLengths]( [page:Integer divisions] )</h3>
 		<div>Get list of cumulative segment lengths.</div>
 		<div>Get list of cumulative segment lengths.</div>
 
 
 		<h3>[method:null updateArcLengths]()</h3>
 		<h3>[method:null updateArcLengths]()</h3>
 		<div>Update the cumlative segment distance cache.</div>
 		<div>Update the cumlative segment distance cache.</div>
 
 
-		<h3>[method:Float getUtoTmapping]( [page:Number u], distance )</h3>
+		<h3>[method:Float getUtoTmapping]( [page:Float u], [page:Float distance] )</h3>
 		<div>
 		<div>
-			Given u in the range ( 0 .. 1 ), returns [page:Number t] also in the range ( 0 .. 1 ).
+			Given u in the range ( 0 .. 1 ), returns [page:Float t] also in the range ( 0 .. 1 ).
 			u and t can then be used to give you points which are equidistant from the ends of the curve,
 			u and t can then be used to give you points which are equidistant from the ends of the curve,
 			using [page:.getPoint].
 			using [page:.getPoint].
 		 </div>
 		 </div>
 
 
-		<h3>[method:Vector getTangent]( [page:Number t] )</h3>
+		<h3>[method:Vector getTangent]( [page:Float t] )</h3>
 		<div>
 		<div>
-			Returns a unit vector tangent at t. If the subclassed curve do not implement its
-			tangent derivation, 2 points a small delta apart will be used to find its gradient
-			which seems to give a reasonable approximation
+			Returns a unit vector tangent at t. If the derived curve does not implement its
+			tangent derivation, two points a small delta apart will be used to find its gradient
+			which seems to give a reasonable approximation.
 		</div>
 		</div>
 
 
-		<h3>[method:Vector getTangentAt]( [page:Number u] )</h3>
+		<h3>[method:Vector getTangentAt]( [page:Float u] )</h3>
 		<div>
 		<div>
 			Returns tangent at a point which is equidistant to the ends of the curve from the
 			Returns tangent at a point which is equidistant to the ends of the curve from the
 			point given in [page:.getTangent].
 			point given in [page:.getTangent].
 		</div>
 		</div>
 
 
-		<h3>[method:Object computeFrenetFrames]( segments, closed )</h3>
+		<h3>[method:Object computeFrenetFrames]( [page:Integer segments], [page:Boolean closed] )</h3>
 		<div>
 		<div>
 		Generates the Frenet Frames. Used in geometries like [page:TubeGeometry] or [page:ExtrudeGeometry].
 		Generates the Frenet Frames. Used in geometries like [page:TubeGeometry] or [page:ExtrudeGeometry].
 		</div>
 		</div>

+ 37 - 3
examples/js/CurveExtras.js

@@ -15,7 +15,11 @@
 
 
 	// GrannyKnot
 	// GrannyKnot
 
 
-	function GrannyKnot() {}
+	function GrannyKnot() {
+
+		THREE.Curve.call( this );
+
+	}
 
 
 	GrannyKnot.prototype = Object.create( THREE.Curve.prototype );
 	GrannyKnot.prototype = Object.create( THREE.Curve.prototype );
 	GrannyKnot.prototype.constructor = GrannyKnot;
 	GrannyKnot.prototype.constructor = GrannyKnot;
@@ -36,6 +40,8 @@
 
 
 	function HeartCurve( s ) {
 	function HeartCurve( s ) {
 
 
+		THREE.Curve.call( this );
+
 		this.scale = ( s === undefined ) ? 5 : s;
 		this.scale = ( s === undefined ) ? 5 : s;
 
 
 	}
 	}
@@ -59,6 +65,8 @@
 
 
 	function VivianiCurve( radius ) {
 	function VivianiCurve( radius ) {
 
 
+		THREE.Curve.call( this );
+
 		this.radius = radius;
 		this.radius = radius;
 
 
 	}
 	}
@@ -81,7 +89,11 @@
 
 
 	// KnotCurve
 	// KnotCurve
 
 
-	function KnotCurve() {}
+	function KnotCurve() {
+
+		THREE.Curve.call( this );
+
+	}
 
 
 	KnotCurve.prototype = Object.create( THREE.Curve.prototype );
 	KnotCurve.prototype = Object.create( THREE.Curve.prototype );
 	KnotCurve.prototype.constructor = KnotCurve;
 	KnotCurve.prototype.constructor = KnotCurve;
@@ -103,7 +115,11 @@
 
 
 	// HelixCurve
 	// HelixCurve
 
 
-	function HelixCurve() {}
+	function HelixCurve() {
+
+		THREE.Curve.call( this );
+
+	}
 
 
 	HelixCurve.prototype = Object.create( THREE.Curve.prototype );
 	HelixCurve.prototype = Object.create( THREE.Curve.prototype );
 	HelixCurve.prototype.constructor = HelixCurve;
 	HelixCurve.prototype.constructor = HelixCurve;
@@ -127,6 +143,8 @@
 
 
 	function TrefoilKnot( s ) {
 	function TrefoilKnot( s ) {
 
 
+		THREE.Curve.call( this );
+
 		this.scale = ( s === undefined ) ? 10 : s;
 		this.scale = ( s === undefined ) ? 10 : s;
 
 
 	}
 	}
@@ -150,6 +168,8 @@
 
 
 	function TorusKnot( s ) {
 	function TorusKnot( s ) {
 
 
+		THREE.Curve.call( this );
+
 		this.scale = ( s === undefined ) ? 10 : s;
 		this.scale = ( s === undefined ) ? 10 : s;
 
 
 	}
 	}
@@ -176,6 +196,8 @@
 
 
 	function CinquefoilKnot( s ) {
 	function CinquefoilKnot( s ) {
 
 
+		THREE.Curve.call( this );
+
 		this.scale = ( s === undefined ) ? 10 : s;
 		this.scale = ( s === undefined ) ? 10 : s;
 
 
 	}
 	}
@@ -202,6 +224,8 @@
 
 
 	function TrefoilPolynomialKnot( s ) {
 	function TrefoilPolynomialKnot( s ) {
 
 
+		THREE.Curve.call( this );
+
 		this.scale = ( s === undefined ) ? 10 : s;
 		this.scale = ( s === undefined ) ? 10 : s;
 
 
 	}
 	}
@@ -232,6 +256,8 @@
 
 
 	function FigureEightPolynomialKnot( s ) {
 	function FigureEightPolynomialKnot( s ) {
 
 
+		THREE.Curve.call( this );
+
 		this.scale = ( s === undefined ) ? 1 : s;
 		this.scale = ( s === undefined ) ? 1 : s;
 
 
 	}
 	}
@@ -255,6 +281,8 @@
 
 
 	function DecoratedTorusKnot4a( s ) {
 	function DecoratedTorusKnot4a( s ) {
 
 
+		THREE.Curve.call( this );
+
 		this.scale = ( s === undefined ) ? 40 : s;
 		this.scale = ( s === undefined ) ? 40 : s;
 
 
 	}
 	}
@@ -278,6 +306,8 @@
 
 
 	function DecoratedTorusKnot4b( s ) {
 	function DecoratedTorusKnot4b( s ) {
 
 
+		THREE.Curve.call( this );
+
 		this.scale = ( s === undefined ) ? 40 : s;
 		this.scale = ( s === undefined ) ? 40 : s;
 
 
 	}
 	}
@@ -301,6 +331,8 @@
 
 
 	function DecoratedTorusKnot5a( s ) {
 	function DecoratedTorusKnot5a( s ) {
 
 
+		THREE.Curve.call( this );
+
 		this.scale = ( s === undefined ) ? 40 : s;
 		this.scale = ( s === undefined ) ? 40 : s;
 
 
 	}
 	}
@@ -324,6 +356,8 @@
 
 
 	function DecoratedTorusKnot5c( s ) {
 	function DecoratedTorusKnot5c( s ) {
 
 
+		THREE.Curve.call( this );
+
 		this.scale = ( s === undefined ) ? 40 : s;
 		this.scale = ( s === undefined ) ? 40 : s;
 
 
 	}
 	}

+ 17 - 0
src/Three.Legacy.js

@@ -776,6 +776,23 @@ Object.defineProperty( Skeleton.prototype, 'useVertexTexture', {
 
 
 } );
 } );
 
 
+Object.defineProperty( Curve.prototype, '__arcLengthDivisions', {
+
+	get: function () {
+
+		console.warn( 'THREE.Curve: .__arcLengthDivisions is now .arcLengthDivisions.' );
+		return this.arcLengthDivisions;
+
+	},
+	set: function ( value ) {
+
+		console.warn( 'THREE.Curve: .__arcLengthDivisions is now .arcLengthDivisions.' );
+		this.arcLengthDivisions = value;
+
+	}
+
+} );
+
 //
 //
 
 
 PerspectiveCamera.prototype.setLens = function ( focalLength, filmGauge ) {
 PerspectiveCamera.prototype.setLens = function ( focalLength, filmGauge ) {

+ 22 - 24
src/extras/core/Curve.js

@@ -6,30 +6,30 @@ import { Matrix4 } from '../../math/Matrix4';
  * @author zz85 / http://www.lab4games.net/zz85/blog
  * @author zz85 / http://www.lab4games.net/zz85/blog
  * Extensible curve object
  * Extensible curve object
  *
  *
- * Some common of Curve methods
+ * Some common of curve methods:
  * .getPoint(t), getTangent(t)
  * .getPoint(t), getTangent(t)
  * .getPointAt(u), getTangentAt(u)
  * .getPointAt(u), getTangentAt(u)
  * .getPoints(), .getSpacedPoints()
  * .getPoints(), .getSpacedPoints()
  * .getLength()
  * .getLength()
  * .updateArcLengths()
  * .updateArcLengths()
  *
  *
- * This following classes subclasses THREE.Curve:
+ * This following curves inherit from THREE.Curve:
  *
  *
- * -- 2d classes --
+ * -- 2D curves --
+ * THREE.ArcCurve
+ * THREE.CubicBezierCurve
+ * THREE.EllipseCurve
  * THREE.LineCurve
  * THREE.LineCurve
  * THREE.QuadraticBezierCurve
  * THREE.QuadraticBezierCurve
- * THREE.CubicBezierCurve
  * THREE.SplineCurve
  * THREE.SplineCurve
- * THREE.ArcCurve
- * THREE.EllipseCurve
  *
  *
- * -- 3d classes --
+ * -- 3D curves --
+ * THREE.CatmullRomCurve3
+ * THREE.CubicBezierCurve3
  * THREE.LineCurve3
  * THREE.LineCurve3
  * THREE.QuadraticBezierCurve3
  * THREE.QuadraticBezierCurve3
- * THREE.CubicBezierCurve3
- * THREE.CatmullRomCurve3
  *
  *
- * A series of curves can be represented as a THREE.CurvePath
+ * A series of curves can be represented as a THREE.CurvePath.
  *
  *
  **/
  **/
 
 
@@ -37,7 +37,11 @@ import { Matrix4 } from '../../math/Matrix4';
  *	Abstract Curve base class
  *	Abstract Curve base class
  **************************************************************/
  **************************************************************/
 
 
-function Curve() {}
+function Curve() {
+
+	this.arcLengthDivisions = 200;
+
+}
 
 
 Object.assign( Curve.prototype, {
 Object.assign( Curve.prototype, {
 
 
@@ -46,7 +50,7 @@ Object.assign( Curve.prototype, {
 
 
 	getPoint: function () {
 	getPoint: function () {
 
 
-		console.warn( "THREE.Curve: Warning, getPoint() not implemented!" );
+		console.warn( 'THREE.Curve: .getPoint() not implemented.' );
 		return null;
 		return null;
 
 
 	},
 	},
@@ -110,13 +114,12 @@ Object.assign( Curve.prototype, {
 
 
 	getLengths: function ( divisions ) {
 	getLengths: function ( divisions ) {
 
 
-		if ( divisions === undefined ) divisions = ( this.__arcLengthDivisions ) ? ( this.__arcLengthDivisions ) : 200;
+		if ( divisions === undefined ) divisions = this.arcLengthDivisions;
 
 
-		if ( this.cacheArcLengths
-			&& ( this.cacheArcLengths.length === divisions + 1 )
-			&& ! this.needsUpdate ) {
+		if ( this.cacheArcLengths &&
+			( this.cacheArcLengths.length === divisions + 1 ) &&
+			! this.needsUpdate ) {
 
 
-			//console.log( "cached", this.cacheArcLengths );
 			return this.cacheArcLengths;
 			return this.cacheArcLengths;
 
 
 		}
 		}
@@ -140,7 +143,7 @@ Object.assign( Curve.prototype, {
 
 
 		this.cacheArcLengths = cache;
 		this.cacheArcLengths = cache;
 
 
-		return cache; // { sums: cache, sum:sum }; Sum is in the last element.
+		return cache; // { sums: cache, sum: sum }; Sum is in the last element.
 
 
 	},
 	},
 
 
@@ -171,8 +174,6 @@ Object.assign( Curve.prototype, {
 
 
 		}
 		}
 
 
-		//var time = Date.now();
-
 		// binary search for the index with largest value smaller than target u distance
 		// binary search for the index with largest value smaller than target u distance
 
 
 		var low = 0, high = il - 1, comparison;
 		var low = 0, high = il - 1, comparison;
@@ -204,12 +205,9 @@ Object.assign( Curve.prototype, {
 
 
 		i = high;
 		i = high;
 
 
-		//console.log('b' , i, low, high, Date.now()- time);
-
 		if ( arcLengths[ i ] === targetArcLength ) {
 		if ( arcLengths[ i ] === targetArcLength ) {
 
 
-			var t = i / ( il - 1 );
-			return t;
+			return i / ( il - 1 );
 
 
 		}
 		}
 
 

+ 3 - 1
src/extras/core/CurvePath.js

@@ -15,6 +15,8 @@ import { LineCurve } from '../curves/LineCurve';
 
 
 function CurvePath() {
 function CurvePath() {
 
 
+	Curve.call( this );
+
 	this.curves = [];
 	this.curves = [];
 
 
 	this.autoClose = false; // Automatically closes the path
 	this.autoClose = false; // Automatically closes the path
@@ -102,7 +104,7 @@ CurvePath.prototype = Object.assign( Object.create( Curve.prototype ), {
 
 
 		this.needsUpdate = true;
 		this.needsUpdate = true;
 		this.cacheLengths = null;
 		this.cacheLengths = null;
-		this.getLengths();
+		this.getCurveLengths();
 
 
 	},
 	},
 
 

+ 2 - 0
src/extras/curves/CatmullRomCurve3.js

@@ -85,6 +85,8 @@ var px = new CubicPoly(), py = new CubicPoly(), pz = new CubicPoly();
 
 
 function CatmullRomCurve3( p /* array of Vector3 */ ) {
 function CatmullRomCurve3( p /* array of Vector3 */ ) {
 
 
+	Curve.call( this );
+
 	this.points = p || [];
 	this.points = p || [];
 	this.closed = false;
 	this.closed = false;
 
 

+ 2 - 0
src/extras/curves/CubicBezierCurve.js

@@ -5,6 +5,8 @@ import { Vector2 } from '../../math/Vector2';
 
 
 function CubicBezierCurve( v0, v1, v2, v3 ) {
 function CubicBezierCurve( v0, v1, v2, v3 ) {
 
 
+	Curve.call( this );
+
 	this.v0 = v0;
 	this.v0 = v0;
 	this.v1 = v1;
 	this.v1 = v1;
 	this.v2 = v2;
 	this.v2 = v2;

+ 2 - 0
src/extras/curves/CubicBezierCurve3.js

@@ -5,6 +5,8 @@ import { Vector3 } from '../../math/Vector3';
 
 
 function CubicBezierCurve3( v0, v1, v2, v3 ) {
 function CubicBezierCurve3( v0, v1, v2, v3 ) {
 
 
+	Curve.call( this );
+
 	this.v0 = v0;
 	this.v0 = v0;
 	this.v1 = v1;
 	this.v1 = v1;
 	this.v2 = v2;
 	this.v2 = v2;

+ 2 - 0
src/extras/curves/EllipseCurve.js

@@ -4,6 +4,8 @@ import { Vector2 } from '../../math/Vector2';
 
 
 function EllipseCurve( aX, aY, xRadius, yRadius, aStartAngle, aEndAngle, aClockwise, aRotation ) {
 function EllipseCurve( aX, aY, xRadius, yRadius, aStartAngle, aEndAngle, aClockwise, aRotation ) {
 
 
+	Curve.call( this );
+
 	this.aX = aX;
 	this.aX = aX;
 	this.aY = aY;
 	this.aY = aY;
 
 

+ 2 - 0
src/extras/curves/LineCurve.js

@@ -3,6 +3,8 @@ import { Curve } from '../core/Curve';
 
 
 function LineCurve( v1, v2 ) {
 function LineCurve( v1, v2 ) {
 
 
+	Curve.call( this );
+
 	this.v1 = v1;
 	this.v1 = v1;
 	this.v2 = v2;
 	this.v2 = v2;
 
 

+ 2 - 0
src/extras/curves/LineCurve3.js

@@ -4,6 +4,8 @@ import { Curve } from '../core/Curve';
 
 
 function LineCurve3( v1, v2 ) {
 function LineCurve3( v1, v2 ) {
 
 
+	Curve.call( this );
+
 	this.v1 = v1;
 	this.v1 = v1;
 	this.v2 = v2;
 	this.v2 = v2;
 
 

+ 2 - 0
src/extras/curves/QuadraticBezierCurve.js

@@ -5,6 +5,8 @@ import { Vector2 } from '../../math/Vector2';
 
 
 function QuadraticBezierCurve( v0, v1, v2 ) {
 function QuadraticBezierCurve( v0, v1, v2 ) {
 
 
+	Curve.call( this );
+
 	this.v0 = v0;
 	this.v0 = v0;
 	this.v1 = v1;
 	this.v1 = v1;
 	this.v2 = v2;
 	this.v2 = v2;

+ 2 - 0
src/extras/curves/QuadraticBezierCurve3.js

@@ -5,6 +5,8 @@ import { Vector3 } from '../../math/Vector3';
 
 
 function QuadraticBezierCurve3( v0, v1, v2 ) {
 function QuadraticBezierCurve3( v0, v1, v2 ) {
 
 
+	Curve.call( this );
+
 	this.v0 = v0;
 	this.v0 = v0;
 	this.v1 = v1;
 	this.v1 = v1;
 	this.v2 = v2;
 	this.v2 = v2;

+ 2 - 0
src/extras/curves/SplineCurve.js

@@ -5,6 +5,8 @@ import { Vector2 } from '../../math/Vector2';
 
 
 function SplineCurve( points /* array of Vector2 */ ) {
 function SplineCurve( points /* array of Vector2 */ ) {
 
 
+	Curve.call( this );
+
 	this.points = ( points === undefined ) ? [] : points;
 	this.points = ( points === undefined ) ? [] : points;
 
 
 }
 }