Skip to content

Commit

Permalink
NURBSCurve: Add toJSON() and fromJSON(). (#29514)
Browse files Browse the repository at this point in the history
* NURBSCurve.toJSON method

* NURBSCurve.fromJSON method

* Add NURBSCurve.tests.js to test runner

* Move NURBSCurve.tests.js to test/unit/addons
  • Loading branch information
canxerian authored Sep 30, 2024
1 parent c9ee853 commit 668c88e
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 3 deletions.
37 changes: 34 additions & 3 deletions examples/jsm/curves/NURBSCurve.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ];
Expand Down Expand Up @@ -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 changes: 70 additions & 0 deletions test/unit/addons/curves/NURBSCurve.tests.js
Original file line number Diff line number Diff line change
@@ -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 change: 1 addition & 0 deletions test/unit/three.addons.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
//addons/utils
import './addons/utils/BufferGeometryUtils.tests.js';
import './addons/math/ColorSpaces.tests.js';
import './addons/curves/NURBSCurve.tests.js';

0 comments on commit 668c88e

Please sign in to comment.