Jelajahi Sumber

NURBSCurve: Add `toJSON()` and `fromJSON()`. (#29514)

* NURBSCurve.toJSON method

* NURBSCurve.fromJSON method

* Add NURBSCurve.tests.js to test runner

* Move NURBSCurve.tests.js to test/unit/addons
Mark Nguyen 1 tahun lalu
induk
melakukan
668c88e1f6

+ 34 - 3
examples/jsm/curves/NURBSCurve.js

@@ -26,14 +26,17 @@ class NURBSCurve extends Curve {
 
 		super();
 
+		const knotsLength = knots ? knots.length - 1 : 0;
+		const pointsLength = controlPoints ? controlPoints.length : 0;
+
 		this.degree = degree;
 		this.knots = knots;
 		this.controlPoints = [];
 		// Used by periodic NURBS to remove hidden spans
 		this.startKnot = startKnot || 0;
-		this.endKnot = endKnot || ( this.knots.length - 1 );
+		this.endKnot = endKnot || knotsLength;
 
-		for ( let i = 0; i < controlPoints.length; ++ i ) {
+		for ( let i = 0; i < pointsLength; ++ i ) {
 
 			// ensure Vector4 for control points
 			const point = controlPoints[ i ];
@@ -75,6 +78,34 @@ class NURBSCurve extends Curve {
 
 	}
 
+	toJSON() {
+
+		const data = super.toJSON();
+
+		data.degree = this.degree;
+		data.knots = [ ...this.knots ];
+		data.controlPoints = this.controlPoints.map( p => p.toArray() );
+		data.startKnot = this.startKnot;
+		data.endKnot = this.endKnot;
+
+		return data;
+
+	}
+
+	fromJSON( json ) {
+
+		super.fromJSON( json );
+
+		this.degree = json.degree;
+		this.knots = [ ...json.knots ];
+		this.controlPoints = json.controlPoints.map( p => new Vector4( p[ 0 ], p[ 1 ], p[ 2 ], p[ 3 ] ) );
+		this.startKnot = json.startKnot;
+		this.endKnot = json.endKnot;
+
+		return this;
+
+	}
+
 }
 
-export { NURBSCurve };
+export { NURBSCurve };

+ 70 - 0
test/unit/addons/curves/NURBSCurve.tests.js

@@ -0,0 +1,70 @@
+/* global QUnit */
+
+import { NURBSCurve } from '../../../../examples/jsm/curves/NURBSCurve.js';
+import { MathUtils } from '../../../../src/math/MathUtils.js';
+import { Vector4 } from '../../../../src/math/Vector4.js';
+
+export default QUnit.module( 'Extras', () => {
+
+	QUnit.module( 'Curves', () => {
+
+		QUnit.module( 'NURBSCurve', ( hooks ) => {
+			
+			let _nurbsCurve = undefined;
+
+			hooks.before( function () {
+
+				const nurbsControlPoints = [];
+				const nurbsKnots = [];
+				const nurbsDegree = 3;
+
+				for ( let i = 0; i <= nurbsDegree; i ++ ) {
+
+					nurbsKnots.push( 0 );
+
+				}
+
+				for ( let i = 0, j = 20; i < j; i ++ ) {
+
+					const point = new Vector4( Math.random(), Math.random(), Math.random(), 1 );
+					nurbsControlPoints.push( point );
+
+					const knot = ( i + 1 ) / ( j - nurbsDegree );
+					nurbsKnots.push( MathUtils.clamp( knot, 0, 1 ) );
+
+				}
+
+				 _nurbsCurve = new NURBSCurve( nurbsDegree, nurbsKnots, nurbsControlPoints );
+
+			} );
+
+			QUnit.test( 'toJSON', ( assert ) => {
+
+				const json = _nurbsCurve.toJSON();
+
+				assert.equal( json.degree, _nurbsCurve.degree, "json.degree ok" );
+				assert.deepEqual( json.knots, _nurbsCurve.knots, "json.knots ok" );
+				assert.deepEqual( json.controlPoints, _nurbsCurve.controlPoints.map( p => p.toArray() ), "json.controlPoints ok" );
+				assert.equal( json.startKnot, _nurbsCurve.startKnot, "json.startKnot ok" );
+				assert.equal( json.endKnot, _nurbsCurve.endKnot, "json.endKnot ok" );
+
+			} );
+
+			QUnit.test( 'fromJSON', ( assert ) => {
+
+				const json = _nurbsCurve.toJSON();
+				const fromJson = new NURBSCurve().fromJSON( json );
+
+				assert.equal( fromJson.degree, _nurbsCurve.degree, "json.degree ok" );
+				assert.deepEqual( fromJson.knots, _nurbsCurve.knots, "json.knots ok" );
+				assert.deepEqual( fromJson.controlPoints, _nurbsCurve.controlPoints, "json.controlPoints ok" );
+				assert.equal( fromJson.startKnot, _nurbsCurve.startKnot, "json.startKnot ok" );
+				assert.equal( fromJson.endKnot, _nurbsCurve.endKnot, "json.endKnot ok" );
+
+			} );
+
+		} );
+
+	} );
+
+} );

+ 1 - 0
test/unit/three.addons.unit.js

@@ -2,3 +2,4 @@
 //addons/utils
 import './addons/utils/BufferGeometryUtils.tests.js';
 import './addons/math/ColorSpaces.tests.js';
+import './addons/curves/NURBSCurve.tests.js';

粤ICP备19079148号