Browse Source

Curve: Added copy() and clone()

Mugen87 7 years ago
parent
commit
351fa33c41

+ 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]

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

@@ -41,10 +41,12 @@ 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:Float tension] )</h3>
+		<div>
+			points – An array of [page:Vector3] points<br/>
+			closed – Whether the curve is closed. Default is *false*.
+			tension – Tension of the curve. Default is *false*.
+		</div>
 
 
 		<h2>Properties</h2>
@@ -58,10 +60,10 @@ 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 type]</h3>
 		<div>Possible values are `centripetal` (default), `chordal` and `catmullrom`.</div>

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

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

+ 25 - 8
src/extras/curves/CatmullRomCurve3.js

@@ -83,14 +83,13 @@ function CubicPoly() {
 var tmp = new Vector3();
 var px = new CubicPoly(), py = new CubicPoly(), pz = new CubicPoly();
 
-function CatmullRomCurve3( points ) {
+function CatmullRomCurve3( points, closed, tension ) {
 
 	Curve.call( this );
 
-	if ( points.length < 2 ) console.warn( 'THREE.CatmullRomCurve3: Points array needs at least two entries.' );
-
 	this.points = points || [];
-	this.closed = false;
+	this.closed = closed || false;
+	this.tension = tension || 0.5;
 
 }
 
@@ -169,10 +168,9 @@ CatmullRomCurve3.prototype.getPoint = function ( t, optionalTarget ) {
 
 	} else if ( this.type === '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 );
 
 	}
 
@@ -186,5 +184,24 @@ CatmullRomCurve3.prototype.getPoint = function ( t, optionalTarget ) {
 
 };
 
+CatmullRomCurve3.prototype.copy = function ( source ) {
+
+	Curve.prototype.copy.call( this, source );
+
+	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.tension = source.tension;
+
+	return this;
+
+};
+
 
 export { CatmullRomCurve3 };

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

@@ -7,10 +7,10 @@ function CubicBezierCurve( v0, v1, v2, v3 ) {
 
 	Curve.call( this );
 
-	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();
 
 }
 
@@ -34,5 +34,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

@@ -7,10 +7,10 @@ function CubicBezierCurve3( v0, v1, v2, v3 ) {
 
 	Curve.call( this );
 
-	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();
 
 }
 
@@ -35,5 +35,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 };

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

@@ -88,5 +88,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

@@ -6,8 +6,8 @@ function LineCurve( v1, v2 ) {
 
 	Curve.call( this );
 
-	this.v1 = v1;
-	this.v2 = v2;
+	this.v1 = v1 || new Vector2();
+	this.v2 = v2 || new Vector2();
 
 }
 
@@ -51,5 +51,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

@@ -6,8 +6,8 @@ function LineCurve3( v1, v2 ) {
 
 	Curve.call( this );
 
-	this.v1 = v1;
-	this.v2 = v2;
+	this.v1 = v1 || new Vector3();
+	this.v2 = v2 || new Vector3();
 
 }
 
@@ -43,5 +43,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

@@ -7,9 +7,9 @@ function QuadraticBezierCurve( v0, v1, v2 ) {
 
 	Curve.call( this );
 
-	this.v0 = v0;
-	this.v1 = v1;
-	this.v2 = v2;
+	this.v0 = v0 || new Vector2();
+	this.v1 = v1 || new Vector2();
+	this.v2 = v2 || new Vector2();
 
 }
 
@@ -33,5 +33,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

@@ -7,9 +7,9 @@ function QuadraticBezierCurve3( v0, v1, v2 ) {
 
 	Curve.call( this );
 
-	this.v0 = v0;
-	this.v1 = v1;
-	this.v2 = v2;
+	this.v0 = v0 || new Vector3();
+	this.v1 = v1 || new Vector3();
+	this.v2 = v2 || new Vector3();
 
 }
 
@@ -34,5 +34,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 };

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

@@ -7,7 +7,7 @@ function SplineCurve( points /* array of Vector2 */ ) {
 
 	Curve.call( this );
 
-	this.points = ( points === undefined ) ? [] : points;
+	this.points = points || [];
 
 }
 
@@ -40,4 +40,21 @@ SplineCurve.prototype.getPoint = function ( t, optionalTarget ) {
 
 };
 
+SplineCurve.prototype.copy = function ( source ) {
+
+	Curve.prototype.copy.call( this, source );
+
+	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 };