Browse Source

Merge pull request #12514 from Mugen87/dev5

Curves: Added .copy() and .clone() methods
Mr.doob 7 years ago
parent
commit
b5cc08daa9

+ 6 - 0
docs/api/extras/core/Curve.html

@@ -97,6 +97,12 @@
 		Generates the Frenet Frames. Used in geometries like [page:TubeGeometry] or [page:ExtrudeGeometry].
 		</div>
 
+		<h3>[method:Curve clone]()</h3>
+		<div>Creates a clone of this curve.</div>
+
+		<h3>[method:Curve copy]( [page:Curve source] )</h3>
+		<div>Copies another curve to this instance.</div>
+
 		<h2>Source</h2>
 
 		[link:https://github.com/mrdoob/three.js/blob/master/src/[path].js src/[path].js]

+ 11 - 8
docs/api/extras/curves/CatmullRomCurve3.html

@@ -41,10 +41,13 @@ var curveObject = new THREE.Line( geometry, material );
 
 		<h2>Constructor</h2>
 
-		<h3>[name]( [page:Array points] )</h3>
-		<div>points – An array of [page:Vector3] points</div>
-
-
+		<h3>[name]( [page:Array points], [page:Boolean closed], [page:String curveType], [page:Float tension] )</h3>
+		<div>
+			points – An array of [page:Vector3] points<br/>
+			closed – Whether the curve is closed. Default is *false*.<br/>
+			curveType – Type of the curve. Default is *centripetal*.<br/>
+			tension – Tension of the curve. Default is *0.5*.
+		</div>
 
 
 		<h2>Properties</h2>
@@ -58,16 +61,16 @@ var curveObject = new THREE.Line( geometry, material );
 		</div>
 
 		<h3>[property:Array points]</h3>
-		<div>The array of array of [page:Vector3] points that define the curve.</div>
+		<div>The array of [page:Vector3] points that define the curve. It needs at least two entries.</div>
 
 		<h3>[property:Boolean closed]</h3>
-		<div>The curve will loop back onto itself when this is true. False by default</div>
+		<div>The curve will loop back onto itself when this is true.</div>
 
 		<h3>[property:String curveType]</h3>
-		<div>Possible values are `centripetal` (default), `chordal` and `catmullrom`.</div>
+		<div>Possible values are *centripetal*, *chordal* and *catmullrom*.</div>
 
 		<h3>[property:float tension]</h3>
-		<div>When [page:.type] is `catmullrom`, defines catmullrom's tension. Defaults is *0.5*.</div>
+		<div>When [page:.type] is *catmullrom*, defines catmullrom's tension.</div>
 
 
 		<h2>Methods</h2>

+ 7 - 7
docs/api/extras/curves/EllipseCurve.html

@@ -42,13 +42,13 @@ var ellipse = new THREE.Line( geometry, material );
 
 		<h3>[name]( [page:Float aX], [page:Float aY], [page:Float xRadius], [page:Float yRadius], [page:Radians aStartAngle], [page:Radians aEndAngle], [page:Boolean aClockwise], [page:Radians aRotation] )</h3>
 		<div>
-			[page:Float aX] – The X center of the ellipse.<br/>
-			[page:Float aY] – The Y center of the ellipse.<br/>
-			[page:Float xRadius] – The radius of the ellipse in the x direction.<br/>
-			[page:Float yRadius] – The radius of the ellipse in the y direction.<br/>
-			[page:Radians aStartAngle] – The start angle of the curve in radians starting from the middle right side.<br/>
-			[page:Radians aEndAngle] – The end angle of the curve in radians starting from the middle right side.<br/>
-			[page:Boolean aClockwise] – Whether the ellipse is drawn clockwise.<br/>
+			[page:Float aX] – The X center of the ellipse. Default is *0*.<br/>
+			[page:Float aY] – The Y center of the ellipse. Default is *0*.<br/>
+			[page:Float xRadius] – The radius of the ellipse in the x direction. Default is *1*.<br/>
+			[page:Float yRadius] – The radius of the ellipse in the y direction. Default is *1*.<br/>
+			[page:Radians aStartAngle] – The start angle of the curve in radians starting from the middle right side.  Default is *0*.<br/>
+			[page:Radians aEndAngle] – The end angle of the curve in radians starting from the middle right side. Default is *2 x Math.PI*.<br/>
+			[page:Boolean aClockwise] – Whether the ellipse is drawn clockwise. Default is *false*.<br/>
 			[page:Radians aRotation]  – The rotation angle of the ellipse in radians, counterclockwise from the positive X axis (optional). Default is *0*.<br/><br/>
 
 			<em>Note:</em> When going clockwise it's best to set the start angle to (Math.PI * 2) and then work towards lower numbers.

+ 14 - 0
src/extras/core/Curve.js

@@ -378,6 +378,20 @@ Object.assign( Curve.prototype, {
 			binormals: binormals
 		};
 
+	},
+
+	clone: function () {
+
+		return new this.constructor().copy( this );
+
+	},
+
+	copy: function ( source ) {
+
+		this.arcLengthDivisions = source.arcLengthDivisions;
+
+		return this;
+
 	}
 
 } );

+ 29 - 9
src/extras/curves/CatmullRomCurve3.js

@@ -83,17 +83,16 @@ function CubicPoly() {
 var tmp = new Vector3();
 var px = new CubicPoly(), py = new CubicPoly(), pz = new CubicPoly();
 
-function CatmullRomCurve3( points ) {
+function CatmullRomCurve3( points, closed, curveType, tension ) {
 
 	Curve.call( this );
 
 	this.type = 'CatmullRomCurve3';
 
-	if ( points.length < 2 ) console.warn( 'THREE.CatmullRomCurve3: Points array needs at least two entries.' );
-
 	this.points = points || [];
-	this.closed = false;
-	this.curveType = 'centripetal';
+	this.closed = closed || false;
+	this.curveType = curveType || 'centripetal';
+	this.tension = tension || 0.5;
 
 }
 
@@ -172,10 +171,9 @@ CatmullRomCurve3.prototype.getPoint = function ( t, optionalTarget ) {
 
 	} else if ( this.curveType === 'catmullrom' ) {
 
-		var tension = this.tension !== undefined ? this.tension : 0.5;
-		px.initCatmullRom( p0.x, p1.x, p2.x, p3.x, tension );
-		py.initCatmullRom( p0.y, p1.y, p2.y, p3.y, tension );
-		pz.initCatmullRom( p0.z, p1.z, p2.z, p3.z, tension );
+		px.initCatmullRom( p0.x, p1.x, p2.x, p3.x, this.tension );
+		py.initCatmullRom( p0.y, p1.y, p2.y, p3.y, this.tension );
+		pz.initCatmullRom( p0.z, p1.z, p2.z, p3.z, this.tension );
 
 	}
 
@@ -189,5 +187,27 @@ CatmullRomCurve3.prototype.getPoint = function ( t, optionalTarget ) {
 
 };
 
+CatmullRomCurve3.prototype.copy = function ( source ) {
+
+	Curve.prototype.copy.call( this, source );
+
+	this.points = [];
+
+	for ( var i = 0, l = source.points.length; i < l; i ++ ) {
+
+		var point = source.points[ i ];
+
+		this.points.push( point.clone() );
+
+	}
+
+	this.closed = source.closed;
+	this.curveType = source.curveType;
+	this.tension = source.tension;
+
+	return this;
+
+};
+
 
 export { CatmullRomCurve3 };

+ 17 - 4
src/extras/curves/CubicBezierCurve.js

@@ -9,10 +9,10 @@ function CubicBezierCurve( v0, v1, v2, v3 ) {
 
 	this.type = 'CubicBezierCurve';
 
-	this.v0 = v0;
-	this.v1 = v1;
-	this.v2 = v2;
-	this.v3 = v3;
+	this.v0 = v0 || new Vector2();
+	this.v1 = v1 || new Vector2();
+	this.v2 = v2 || new Vector2();
+	this.v3 = v3 || new Vector2();
 
 }
 
@@ -36,5 +36,18 @@ CubicBezierCurve.prototype.getPoint = function ( t, optionalTarget ) {
 
 };
 
+CubicBezierCurve.prototype.copy = function ( source ) {
+
+	Curve.prototype.copy.call( this, source );
+
+	this.v0.copy( source.v0 );
+	this.v1.copy( source.v1 );
+	this.v2.copy( source.v2 );
+	this.v3.copy( source.v3 );
+
+	return this;
+
+};
+
 
 export { CubicBezierCurve };

+ 17 - 4
src/extras/curves/CubicBezierCurve3.js

@@ -9,10 +9,10 @@ function CubicBezierCurve3( v0, v1, v2, v3 ) {
 
 	this.type = 'CubicBezierCurve3';
 
-	this.v0 = v0;
-	this.v1 = v1;
-	this.v2 = v2;
-	this.v3 = v3;
+	this.v0 = v0 || new Vector3();
+	this.v1 = v1 || new Vector3();
+	this.v2 = v2 || new Vector3();
+	this.v3 = v3 || new Vector3();
 
 }
 
@@ -37,5 +37,18 @@ CubicBezierCurve3.prototype.getPoint = function ( t, optionalTarget ) {
 
 };
 
+CubicBezierCurve3.prototype.copy = function ( source ) {
+
+	Curve.prototype.copy.call( this, source );
+
+	this.v0.copy( source.v0 );
+	this.v1.copy( source.v1 );
+	this.v2.copy( source.v2 );
+	this.v3.copy( source.v3 );
+
+	return this;
+
+};
+
 
 export { CubicBezierCurve3 };

+ 28 - 7
src/extras/curves/EllipseCurve.js

@@ -8,16 +8,16 @@ function EllipseCurve( aX, aY, xRadius, yRadius, aStartAngle, aEndAngle, aClockw
 
 	this.type = 'EllipseCurve';
 
-	this.aX = aX;
-	this.aY = aY;
+	this.aX = aX || 0;
+	this.aY = aY || 0;
 
-	this.xRadius = xRadius;
-	this.yRadius = yRadius;
+	this.xRadius = xRadius || 1;
+	this.yRadius = yRadius || 1;
 
-	this.aStartAngle = aStartAngle;
-	this.aEndAngle = aEndAngle;
+	this.aStartAngle = aStartAngle || 0;
+	this.aEndAngle = aEndAngle || 2 * Math.PI;
 
-	this.aClockwise = aClockwise;
+	this.aClockwise = aClockwise || false;
 
 	this.aRotation = aRotation || 0;
 
@@ -90,5 +90,26 @@ EllipseCurve.prototype.getPoint = function ( t, optionalTarget ) {
 
 };
 
+EllipseCurve.prototype.copy = function ( source ) {
+
+	Curve.prototype.copy.call( this, source );
+
+	this.aX = source.aX;
+	this.aY = source.aY;
+
+	this.xRadius = source.xRadius;
+	this.yRadius = source.yRadius;
+
+	this.aStartAngle = source.aStartAngle;
+	this.aEndAngle = source.aEndAngle;
+
+	this.aClockwise = source.aClockwise;
+
+	this.aRotation = source.aRotation;
+
+	return this;
+
+};
+
 
 export { EllipseCurve };

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

@@ -8,8 +8,8 @@ function LineCurve( v1, v2 ) {
 
 	this.type = 'LineCurve';
 
-	this.v1 = v1;
-	this.v2 = v2;
+	this.v1 = v1 || new Vector2();
+	this.v2 = v2 || new Vector2();
 
 }
 
@@ -53,5 +53,16 @@ LineCurve.prototype.getTangent = function ( /* t */ ) {
 
 };
 
+LineCurve.prototype.copy = function ( source ) {
+
+	Curve.prototype.copy.call( this, source );
+
+	this.v1.copy( source.v1 );
+	this.v2.copy( source.v2 );
+
+	return this;
+
+};
+
 
 export { LineCurve };

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

@@ -8,8 +8,8 @@ function LineCurve3( v1, v2 ) {
 
 	this.type = 'LineCurve3';
 
-	this.v1 = v1;
-	this.v2 = v2;
+	this.v1 = v1 || new Vector3();
+	this.v2 = v2 || new Vector3();
 
 }
 
@@ -45,5 +45,16 @@ LineCurve3.prototype.getPointAt = function ( u, optionalTarget ) {
 
 };
 
+LineCurve3.prototype.copy = function ( source ) {
+
+	Curve.prototype.copy.call( this, source );
+
+	this.v1.copy( source.v1 );
+	this.v2.copy( source.v2 );
+
+	return this;
+
+};
+
 
 export { LineCurve3 };

+ 15 - 3
src/extras/curves/QuadraticBezierCurve.js

@@ -9,9 +9,9 @@ function QuadraticBezierCurve( v0, v1, v2 ) {
 
 	this.type = 'QuadraticBezierCurve';
 
-	this.v0 = v0;
-	this.v1 = v1;
-	this.v2 = v2;
+	this.v0 = v0 || new Vector2();
+	this.v1 = v1 || new Vector2();
+	this.v2 = v2 || new Vector2();
 
 }
 
@@ -35,5 +35,17 @@ QuadraticBezierCurve.prototype.getPoint = function ( t, optionalTarget ) {
 
 };
 
+QuadraticBezierCurve.prototype.copy = function ( source ) {
+
+	Curve.prototype.copy.call( this, source );
+
+	this.v0.copy( source.v0 );
+	this.v1.copy( source.v1 );
+	this.v2.copy( source.v2 );
+
+	return this;
+
+};
+
 
 export { QuadraticBezierCurve };

+ 15 - 3
src/extras/curves/QuadraticBezierCurve3.js

@@ -9,9 +9,9 @@ function QuadraticBezierCurve3( v0, v1, v2 ) {
 
 	this.type = 'QuadraticBezierCurve3';
 
-	this.v0 = v0;
-	this.v1 = v1;
-	this.v2 = v2;
+	this.v0 = v0 || new Vector3();
+	this.v1 = v1 || new Vector3();
+	this.v2 = v2 || new Vector3();
 
 }
 
@@ -36,5 +36,17 @@ QuadraticBezierCurve3.prototype.getPoint = function ( t, optionalTarget ) {
 
 };
 
+QuadraticBezierCurve3.prototype.copy = function ( source ) {
+
+	Curve.prototype.copy.call( this, source );
+
+	this.v0.copy( source.v0 );
+	this.v1.copy( source.v1 );
+	this.v2.copy( source.v2 );
+
+	return this;
+
+};
+
 
 export { QuadraticBezierCurve3 };

+ 20 - 1
src/extras/curves/SplineCurve.js

@@ -9,7 +9,7 @@ function SplineCurve( points /* array of Vector2 */ ) {
 
 	this.type = 'SplineCurve';
 
-	this.points = ( points === undefined ) ? [] : points;
+	this.points = points || [];
 
 }
 
@@ -42,4 +42,23 @@ SplineCurve.prototype.getPoint = function ( t, optionalTarget ) {
 
 };
 
+SplineCurve.prototype.copy = function ( source ) {
+
+	Curve.prototype.copy.call( this, source );
+
+	this.points = [];
+
+	for ( var i = 0, l = source.points.length; i < l; i ++ ) {
+
+		var point = source.points[ i ];
+
+		this.points.push( point.clone() );
+
+	}
+
+	return this;
+
+};
+
+
 export { SplineCurve };