From 668c88e1f66505cf89654e4004956d8e8ee93665 Mon Sep 17 00:00:00 2001 From: Mark Nguyen <1138440+canxerian@users.noreply.github.com> Date: Mon, 30 Sep 2024 14:28:40 +0100 Subject: [PATCH] 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 --- examples/jsm/curves/NURBSCurve.js | 37 ++++++++++- test/unit/addons/curves/NURBSCurve.tests.js | 70 +++++++++++++++++++++ test/unit/three.addons.unit.js | 1 + 3 files changed, 105 insertions(+), 3 deletions(-) create mode 100644 test/unit/addons/curves/NURBSCurve.tests.js diff --git a/examples/jsm/curves/NURBSCurve.js b/examples/jsm/curves/NURBSCurve.js index 8be8dde2d61b4b..f0ee02b398a52d 100644 --- a/examples/jsm/curves/NURBSCurve.js +++ b/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 }; \ No newline at end of file diff --git a/test/unit/addons/curves/NURBSCurve.tests.js b/test/unit/addons/curves/NURBSCurve.tests.js new file mode 100644 index 00000000000000..06150bf07fcafa --- /dev/null +++ b/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" ); + + } ); + + } ); + + } ); + +} ); diff --git a/test/unit/three.addons.unit.js b/test/unit/three.addons.unit.js index 049188251191fe..3b1862431ab7e0 100644 --- a/test/unit/three.addons.unit.js +++ b/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';