From 73984054ca4bef0594aca0d4db87fa843b2cbe99 Mon Sep 17 00:00:00 2001 From: Mugen87 Date: Tue, 6 Apr 2021 23:36:32 +0200 Subject: [PATCH] Examples: Convert curves to ES6. --- examples/js/curves/CurveExtras.js | 410 +++++++++++++------------- examples/js/curves/NURBSCurve.js | 89 +++--- examples/js/curves/NURBSSurface.js | 51 ++-- examples/js/curves/NURBSUtils.js | 244 +++++++-------- examples/jsm/curves/CurveExtras.js | 441 ++++++++++++++-------------- examples/jsm/curves/NURBSCurve.js | 75 ++--- examples/jsm/curves/NURBSSurface.js | 47 ++- examples/jsm/curves/NURBSUtils.js | 218 +++++++------- 8 files changed, 790 insertions(+), 785 deletions(-) diff --git a/examples/js/curves/CurveExtras.js b/examples/js/curves/CurveExtras.js index bf64cccd0f0114..793fba841f9f2a 100644 --- a/examples/js/curves/CurveExtras.js +++ b/examples/js/curves/CurveExtras.js @@ -11,351 +11,343 @@ * http://www.mi.sanu.ac.rs/vismath/taylorapril2011/Taylor.pdf * https://prideout.net/blog/old/blog/index.html@p=44.html */ + // GrannyKnot - var Curves = function () { + class GrannyKnot extends THREE.Curve { - // GrannyKnot - function GrannyKnot() { + getPoint( t, optionalTarget = new THREE.Vector3() ) { - THREE.Curve.call( this ); + const point = optionalTarget; + t = 2 * Math.PI * t; + const x = - 0.22 * Math.cos( t ) - 1.28 * Math.sin( t ) - 0.44 * Math.cos( 3 * t ) - 0.78 * Math.sin( 3 * t ); + const y = - 0.1 * Math.cos( 2 * t ) - 0.27 * Math.sin( 2 * t ) + 0.38 * Math.cos( 4 * t ) + 0.46 * Math.sin( 4 * t ); + const z = 0.7 * Math.cos( 3 * t ) - 0.4 * Math.sin( 3 * t ); + return point.set( x, y, z ).multiplyScalar( 20 ); } - GrannyKnot.prototype = Object.create( THREE.Curve.prototype ); - GrannyKnot.prototype.constructor = GrannyKnot; + } // HeartCurve - GrannyKnot.prototype.getPoint = function ( t, optionalTarget ) { - var point = optionalTarget || new THREE.Vector3(); - t = 2 * Math.PI * t; - var x = - 0.22 * Math.cos( t ) - 1.28 * Math.sin( t ) - 0.44 * Math.cos( 3 * t ) - 0.78 * Math.sin( 3 * t ); - var y = - 0.1 * Math.cos( 2 * t ) - 0.27 * Math.sin( 2 * t ) + 0.38 * Math.cos( 4 * t ) + 0.46 * Math.sin( 4 * t ); - var z = 0.7 * Math.cos( 3 * t ) - 0.4 * Math.sin( 3 * t ); - return point.set( x, y, z ).multiplyScalar( 20 ); + class HeartCurve extends THREE.Curve { - }; // HeartCurve + constructor( scale = 5 ) { - - function HeartCurve( scale ) { - - THREE.Curve.call( this ); - this.scale = scale === undefined ? 5 : scale; + super(); + this.scale = scale; } - HeartCurve.prototype = Object.create( THREE.Curve.prototype ); - HeartCurve.prototype.constructor = HeartCurve; - - HeartCurve.prototype.getPoint = function ( t, optionalTarget ) { + getPoint( t, optionalTarget = new THREE.Vector3() ) { - var point = optionalTarget || new THREE.Vector3(); + const point = optionalTarget; t *= 2 * Math.PI; - var x = 16 * Math.pow( Math.sin( t ), 3 ); - var y = 13 * Math.cos( t ) - 5 * Math.cos( 2 * t ) - 2 * Math.cos( 3 * t ) - Math.cos( 4 * t ); - var z = 0; + const x = 16 * Math.pow( Math.sin( t ), 3 ); + const y = 13 * Math.cos( t ) - 5 * Math.cos( 2 * t ) - 2 * Math.cos( 3 * t ) - Math.cos( 4 * t ); + const z = 0; return point.set( x, y, z ).multiplyScalar( this.scale ); - }; // Viviani's THREE.Curve + } + } // Viviani's THREE.Curve - function VivianiCurve( scale ) { - THREE.Curve.call( this ); - this.scale = scale === undefined ? 70 : scale; + class VivianiCurve extends THREE.Curve { - } + constructor( scale = 70 ) { - VivianiCurve.prototype = Object.create( THREE.Curve.prototype ); - VivianiCurve.prototype.constructor = VivianiCurve; + super(); + this.scale = scale; + + } - VivianiCurve.prototype.getPoint = function ( t, optionalTarget ) { + getPoint( t, optionalTarget = new THREE.Vector3() ) { - var point = optionalTarget || new THREE.Vector3(); + const point = optionalTarget; t = t * 4 * Math.PI; // normalized to 0..1 - var a = this.scale / 2; - var x = a * ( 1 + Math.cos( t ) ); - var y = a * Math.sin( t ); - var z = 2 * a * Math.sin( t / 2 ); + const a = this.scale / 2; + const x = a * ( 1 + Math.cos( t ) ); + const y = a * Math.sin( t ); + const z = 2 * a * Math.sin( t / 2 ); return point.set( x, y, z ); - }; // KnotCurve - - - function KnotCurve() { + } - THREE.Curve.call( this ); + } // KnotCurve - } - KnotCurve.prototype = Object.create( THREE.Curve.prototype ); - KnotCurve.prototype.constructor = KnotCurve; + class KnotCurve extends THREE.Curve { - KnotCurve.prototype.getPoint = function ( t, optionalTarget ) { + getPoint( t, optionalTarget = new THREE.Vector3() ) { - var point = optionalTarget || new THREE.Vector3(); + const point = optionalTarget; t *= 2 * Math.PI; - var R = 10; - var s = 50; - var x = s * Math.sin( t ); - var y = Math.cos( t ) * ( R + s * Math.cos( t ) ); - var z = Math.sin( t ) * ( R + s * Math.cos( t ) ); + const R = 10; + const s = 50; + const x = s * Math.sin( t ); + const y = Math.cos( t ) * ( R + s * Math.cos( t ) ); + const z = Math.sin( t ) * ( R + s * Math.cos( t ) ); return point.set( x, y, z ); - }; // HelixCurve + } + } // HelixCurve - function HelixCurve() { - THREE.Curve.call( this ); + class HelixCurve extends THREE.Curve { - } + getPoint( t, optionalTarget = new THREE.Vector3() ) { - HelixCurve.prototype = Object.create( THREE.Curve.prototype ); - HelixCurve.prototype.constructor = HelixCurve; + const point = optionalTarget; + const a = 30; // radius - HelixCurve.prototype.getPoint = function ( t, optionalTarget ) { + const b = 150; // height - var point = optionalTarget || new THREE.Vector3(); - var a = 30; // radius + const t2 = 2 * Math.PI * t * b / 30; + const x = Math.cos( t2 ) * a; + const y = Math.sin( t2 ) * a; + const z = b * t; + return point.set( x, y, z ); - var b = 150; // height + } - var t2 = 2 * Math.PI * t * b / 30; - var x = Math.cos( t2 ) * a; - var y = Math.sin( t2 ) * a; - var z = b * t; - return point.set( x, y, z ); + } // TrefoilKnot - }; // TrefoilKnot + class TrefoilKnot extends THREE.Curve { - function TrefoilKnot( scale ) { + constructor( scale = 10 ) { - THREE.Curve.call( this ); - this.scale = scale === undefined ? 10 : scale; + super(); + this.scale = scale; } - TrefoilKnot.prototype = Object.create( THREE.Curve.prototype ); - TrefoilKnot.prototype.constructor = TrefoilKnot; - - TrefoilKnot.prototype.getPoint = function ( t, optionalTarget ) { + getPoint( t, optionalTarget = new THREE.Vector3() ) { - var point = optionalTarget || new THREE.Vector3(); + const point = optionalTarget; t *= Math.PI * 2; - var x = ( 2 + Math.cos( 3 * t ) ) * Math.cos( 2 * t ); - var y = ( 2 + Math.cos( 3 * t ) ) * Math.sin( 2 * t ); - var z = Math.sin( 3 * t ); + const x = ( 2 + Math.cos( 3 * t ) ) * Math.cos( 2 * t ); + const y = ( 2 + Math.cos( 3 * t ) ) * Math.sin( 2 * t ); + const z = Math.sin( 3 * t ); return point.set( x, y, z ).multiplyScalar( this.scale ); - }; // TorusKnot + } + } // TorusKnot - function TorusKnot( scale ) { - THREE.Curve.call( this ); - this.scale = scale === undefined ? 10 : scale; + class TorusKnot extends THREE.Curve { - } + constructor( scale = 10 ) { - TorusKnot.prototype = Object.create( THREE.Curve.prototype ); - TorusKnot.prototype.constructor = TorusKnot; + super(); + this.scale = scale; + + } - TorusKnot.prototype.getPoint = function ( t, optionalTarget ) { + getPoint( t, optionalTarget = new THREE.Vector3() ) { - var point = optionalTarget || new THREE.Vector3(); - var p = 3; - var q = 4; + const point = optionalTarget; + const p = 3; + const q = 4; t *= Math.PI * 2; - var x = ( 2 + Math.cos( q * t ) ) * Math.cos( p * t ); - var y = ( 2 + Math.cos( q * t ) ) * Math.sin( p * t ); - var z = Math.sin( q * t ); + const x = ( 2 + Math.cos( q * t ) ) * Math.cos( p * t ); + const y = ( 2 + Math.cos( q * t ) ) * Math.sin( p * t ); + const z = Math.sin( q * t ); return point.set( x, y, z ).multiplyScalar( this.scale ); - }; // CinquefoilKnot + } + } // CinquefoilKnot - function CinquefoilKnot( scale ) { - THREE.Curve.call( this ); - this.scale = scale === undefined ? 10 : scale; + class CinquefoilKnot extends THREE.Curve { - } + constructor( scale = 10 ) { - CinquefoilKnot.prototype = Object.create( THREE.Curve.prototype ); - CinquefoilKnot.prototype.constructor = CinquefoilKnot; + super(); + this.scale = scale; - CinquefoilKnot.prototype.getPoint = function ( t, optionalTarget ) { + } - var point = optionalTarget || new THREE.Vector3(); - var p = 2; - var q = 5; + getPoint( t, optionalTarget = new THREE.Vector3() ) { + + const point = optionalTarget; + const p = 2; + const q = 5; t *= Math.PI * 2; - var x = ( 2 + Math.cos( q * t ) ) * Math.cos( p * t ); - var y = ( 2 + Math.cos( q * t ) ) * Math.sin( p * t ); - var z = Math.sin( q * t ); + const x = ( 2 + Math.cos( q * t ) ) * Math.cos( p * t ); + const y = ( 2 + Math.cos( q * t ) ) * Math.sin( p * t ); + const z = Math.sin( q * t ); return point.set( x, y, z ).multiplyScalar( this.scale ); - }; // TrefoilPolynomialKnot + } + } // TrefoilPolynomialKnot - function TrefoilPolynomialKnot( scale ) { - THREE.Curve.call( this ); - this.scale = scale === undefined ? 10 : scale; + class TrefoilPolynomialKnot extends THREE.Curve { - } + constructor( scale = 10 ) { - TrefoilPolynomialKnot.prototype = Object.create( THREE.Curve.prototype ); - TrefoilPolynomialKnot.prototype.constructor = TrefoilPolynomialKnot; + super(); + this.scale = scale; - TrefoilPolynomialKnot.prototype.getPoint = function ( t, optionalTarget ) { + } - var point = optionalTarget || new THREE.Vector3(); + getPoint( t, optionalTarget = new THREE.Vector3() ) { + + const point = optionalTarget; t = t * 4 - 2; - var x = Math.pow( t, 3 ) - 3 * t; - var y = Math.pow( t, 4 ) - 4 * t * t; - var z = 1 / 5 * Math.pow( t, 5 ) - 2 * t; + const x = Math.pow( t, 3 ) - 3 * t; + const y = Math.pow( t, 4 ) - 4 * t * t; + const z = 1 / 5 * Math.pow( t, 5 ) - 2 * t; return point.set( x, y, z ).multiplyScalar( this.scale ); - }; + } - var scaleTo = function ( x, y, t ) { + } - var r = y - x; - return t * r + x; + function scaleTo( x, y, t ) { - }; // FigureEightPolynomialKnot + const r = y - x; + return t * r + x; + } // FigureEightPolynomialKnot - function FigureEightPolynomialKnot( scale ) { - THREE.Curve.call( this ); - this.scale = scale === undefined ? 1 : scale; + class FigureEightPolynomialKnot extends THREE.Curve { - } + constructor( scale = 1 ) { - FigureEightPolynomialKnot.prototype = Object.create( THREE.Curve.prototype ); - FigureEightPolynomialKnot.prototype.constructor = FigureEightPolynomialKnot; + super(); + this.scale = scale; - FigureEightPolynomialKnot.prototype.getPoint = function ( t, optionalTarget ) { + } + + getPoint( t, optionalTarget = new THREE.Vector3() ) { - var point = optionalTarget || new THREE.Vector3(); + const point = optionalTarget; t = scaleTo( - 4, 4, t ); - var x = 2 / 5 * t * ( t * t - 7 ) * ( t * t - 10 ); - var y = Math.pow( t, 4 ) - 13 * t * t; - var z = 1 / 10 * t * ( t * t - 4 ) * ( t * t - 9 ) * ( t * t - 12 ); + const x = 2 / 5 * t * ( t * t - 7 ) * ( t * t - 10 ); + const y = Math.pow( t, 4 ) - 13 * t * t; + const z = 1 / 10 * t * ( t * t - 4 ) * ( t * t - 9 ) * ( t * t - 12 ); return point.set( x, y, z ).multiplyScalar( this.scale ); - }; // DecoratedTorusKnot4a + } + } // DecoratedTorusKnot4a - function DecoratedTorusKnot4a( scale ) { - THREE.Curve.call( this ); - this.scale = scale === undefined ? 40 : scale; + class DecoratedTorusKnot4a extends THREE.Curve { - } + constructor( scale = 40 ) { - DecoratedTorusKnot4a.prototype = Object.create( THREE.Curve.prototype ); - DecoratedTorusKnot4a.prototype.constructor = DecoratedTorusKnot4a; + super(); + this.scale = scale; - DecoratedTorusKnot4a.prototype.getPoint = function ( t, optionalTarget ) { + } + + getPoint( t, optionalTarget = new THREE.Vector3() ) { - var point = optionalTarget || new THREE.Vector3(); + const point = optionalTarget; t *= Math.PI * 2; - var x = Math.cos( 2 * t ) * ( 1 + 0.6 * ( Math.cos( 5 * t ) + 0.75 * Math.cos( 10 * t ) ) ); - var y = Math.sin( 2 * t ) * ( 1 + 0.6 * ( Math.cos( 5 * t ) + 0.75 * Math.cos( 10 * t ) ) ); - var z = 0.35 * Math.sin( 5 * t ); + const x = Math.cos( 2 * t ) * ( 1 + 0.6 * ( Math.cos( 5 * t ) + 0.75 * Math.cos( 10 * t ) ) ); + const y = Math.sin( 2 * t ) * ( 1 + 0.6 * ( Math.cos( 5 * t ) + 0.75 * Math.cos( 10 * t ) ) ); + const z = 0.35 * Math.sin( 5 * t ); return point.set( x, y, z ).multiplyScalar( this.scale ); - }; // DecoratedTorusKnot4b + } + } // DecoratedTorusKnot4b - function DecoratedTorusKnot4b( scale ) { - THREE.Curve.call( this ); - this.scale = scale === undefined ? 40 : scale; + class DecoratedTorusKnot4b extends THREE.Curve { - } + constructor( scale = 40 ) { + + super(); + this.scale = scale; - DecoratedTorusKnot4b.prototype = Object.create( THREE.Curve.prototype ); - DecoratedTorusKnot4b.prototype.constructor = DecoratedTorusKnot4b; + } - DecoratedTorusKnot4b.prototype.getPoint = function ( t, optionalTarget ) { + getPoint( t, optionalTarget = new THREE.Vector3() ) { - var point = optionalTarget || new THREE.Vector3(); - var fi = t * Math.PI * 2; - var x = Math.cos( 2 * fi ) * ( 1 + 0.45 * Math.cos( 3 * fi ) + 0.4 * Math.cos( 9 * fi ) ); - var y = Math.sin( 2 * fi ) * ( 1 + 0.45 * Math.cos( 3 * fi ) + 0.4 * Math.cos( 9 * fi ) ); - var z = 0.2 * Math.sin( 9 * fi ); + const point = optionalTarget; + const fi = t * Math.PI * 2; + const x = Math.cos( 2 * fi ) * ( 1 + 0.45 * Math.cos( 3 * fi ) + 0.4 * Math.cos( 9 * fi ) ); + const y = Math.sin( 2 * fi ) * ( 1 + 0.45 * Math.cos( 3 * fi ) + 0.4 * Math.cos( 9 * fi ) ); + const z = 0.2 * Math.sin( 9 * fi ); return point.set( x, y, z ).multiplyScalar( this.scale ); - }; // DecoratedTorusKnot5a + } + } // DecoratedTorusKnot5a - function DecoratedTorusKnot5a( scale ) { - THREE.Curve.call( this ); - this.scale = scale === undefined ? 40 : scale; + class DecoratedTorusKnot5a extends THREE.Curve { - } + constructor( scale = 40 ) { + + super(); + this.scale = scale; - DecoratedTorusKnot5a.prototype = Object.create( THREE.Curve.prototype ); - DecoratedTorusKnot5a.prototype.constructor = DecoratedTorusKnot5a; + } - DecoratedTorusKnot5a.prototype.getPoint = function ( t, optionalTarget ) { + getPoint( t, optionalTarget = new THREE.Vector3() ) { - var point = optionalTarget || new THREE.Vector3(); - var fi = t * Math.PI * 2; - var x = Math.cos( 3 * fi ) * ( 1 + 0.3 * Math.cos( 5 * fi ) + 0.5 * Math.cos( 10 * fi ) ); - var y = Math.sin( 3 * fi ) * ( 1 + 0.3 * Math.cos( 5 * fi ) + 0.5 * Math.cos( 10 * fi ) ); - var z = 0.2 * Math.sin( 20 * fi ); + const point = optionalTarget; + const fi = t * Math.PI * 2; + const x = Math.cos( 3 * fi ) * ( 1 + 0.3 * Math.cos( 5 * fi ) + 0.5 * Math.cos( 10 * fi ) ); + const y = Math.sin( 3 * fi ) * ( 1 + 0.3 * Math.cos( 5 * fi ) + 0.5 * Math.cos( 10 * fi ) ); + const z = 0.2 * Math.sin( 20 * fi ); return point.set( x, y, z ).multiplyScalar( this.scale ); - }; // DecoratedTorusKnot5c + } + } // DecoratedTorusKnot5c - function DecoratedTorusKnot5c( scale ) { - THREE.Curve.call( this ); - this.scale = scale === undefined ? 40 : scale; + class DecoratedTorusKnot5c extends THREE.Curve { - } + constructor( scale = 40 ) { + + super(); + this.scale = scale; - DecoratedTorusKnot5c.prototype = Object.create( THREE.Curve.prototype ); - DecoratedTorusKnot5c.prototype.constructor = DecoratedTorusKnot5c; + } - DecoratedTorusKnot5c.prototype.getPoint = function ( t, optionalTarget ) { + getPoint( t, optionalTarget = new THREE.Vector3() ) { - var point = optionalTarget || new THREE.Vector3(); - var fi = t * Math.PI * 2; - var x = Math.cos( 4 * fi ) * ( 1 + 0.5 * ( Math.cos( 5 * fi ) + 0.4 * Math.cos( 20 * fi ) ) ); - var y = Math.sin( 4 * fi ) * ( 1 + 0.5 * ( Math.cos( 5 * fi ) + 0.4 * Math.cos( 20 * fi ) ) ); - var z = 0.35 * Math.sin( 15 * fi ); + const point = optionalTarget; + const fi = t * Math.PI * 2; + const x = Math.cos( 4 * fi ) * ( 1 + 0.5 * ( Math.cos( 5 * fi ) + 0.4 * Math.cos( 20 * fi ) ) ); + const y = Math.sin( 4 * fi ) * ( 1 + 0.5 * ( Math.cos( 5 * fi ) + 0.4 * Math.cos( 20 * fi ) ) ); + const z = 0.35 * Math.sin( 15 * fi ); return point.set( x, y, z ).multiplyScalar( this.scale ); - }; - - return { - GrannyKnot: GrannyKnot, - HeartCurve: HeartCurve, - VivianiCurve: VivianiCurve, - KnotCurve: KnotCurve, - HelixCurve: HelixCurve, - TrefoilKnot: TrefoilKnot, - TorusKnot: TorusKnot, - CinquefoilKnot: CinquefoilKnot, - TrefoilPolynomialKnot: TrefoilPolynomialKnot, - FigureEightPolynomialKnot: FigureEightPolynomialKnot, - DecoratedTorusKnot4a: DecoratedTorusKnot4a, - DecoratedTorusKnot4b: DecoratedTorusKnot4b, - DecoratedTorusKnot5a: DecoratedTorusKnot5a, - DecoratedTorusKnot5c: DecoratedTorusKnot5c - }; - - }(); + } + + } + + const Curves = { + GrannyKnot: GrannyKnot, + HeartCurve: HeartCurve, + VivianiCurve: VivianiCurve, + KnotCurve: KnotCurve, + HelixCurve: HelixCurve, + TrefoilKnot: TrefoilKnot, + TorusKnot: TorusKnot, + CinquefoilKnot: CinquefoilKnot, + TrefoilPolynomialKnot: TrefoilPolynomialKnot, + FigureEightPolynomialKnot: FigureEightPolynomialKnot, + DecoratedTorusKnot4a: DecoratedTorusKnot4a, + DecoratedTorusKnot4b: DecoratedTorusKnot4b, + DecoratedTorusKnot5a: DecoratedTorusKnot5a, + DecoratedTorusKnot5c: DecoratedTorusKnot5c + }; THREE.Curves = Curves; diff --git a/examples/js/curves/NURBSCurve.js b/examples/js/curves/NURBSCurve.js index ff8fa046f5d845..21d0ceecaa31a7 100644 --- a/examples/js/curves/NURBSCurve.js +++ b/examples/js/curves/NURBSCurve.js @@ -9,65 +9,66 @@ * **/ - var NURBSCurve = function ( degree, knots - /* array of reals */ - , controlPoints - /* array of Vector(2|3|4) */ - , startKnot - /* index in knots */ - , endKnot - /* index in knots */ - ) { - - THREE.Curve.call( this ); - 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; - - for ( var i = 0; i < controlPoints.length; ++ i ) { - - // ensure THREE.Vector4 for control points - var point = controlPoints[ i ]; - this.controlPoints[ i ] = new THREE.Vector4( point.x, point.y, point.z, point.w ); + class NURBSCurve extends THREE.Curve { + + constructor( degree, knots + /* array of reals */ + , controlPoints + /* array of Vector(2|3|4) */ + , startKnot + /* index in knots */ + , endKnot + /* index in knots */ + ) { + + super(); + 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; + + for ( let i = 0; i < controlPoints.length; ++ i ) { + + // ensure THREE.Vector4 for control points + const point = controlPoints[ i ]; + this.controlPoints[ i ] = new THREE.Vector4( point.x, point.y, point.z, point.w ); + + } } - }; + getPoint( t, optionalTarget = new THREE.Vector3() ) { - NURBSCurve.prototype = Object.create( THREE.Curve.prototype ); - NURBSCurve.prototype.constructor = NURBSCurve; + const point = optionalTarget; + const u = this.knots[ this.startKnot ] + t * ( this.knots[ this.endKnot ] - this.knots[ this.startKnot ] ); // linear mapping t->u + // following results in (wx, wy, wz, w) homogeneous point - NURBSCurve.prototype.getPoint = function ( t, optionalTarget ) { + const hpoint = THREE.NURBSUtils.calcBSplinePoint( this.degree, this.knots, this.controlPoints, u ); - var point = optionalTarget || new THREE.Vector3(); - var u = this.knots[ this.startKnot ] + t * ( this.knots[ this.endKnot ] - this.knots[ this.startKnot ] ); // linear mapping t->u - // following results in (wx, wy, wz, w) homogeneous point + if ( hpoint.w !== 1.0 ) { - var hpoint = THREE.NURBSUtils.calcBSplinePoint( this.degree, this.knots, this.controlPoints, u ); + // project to 3D space: (wx, wy, wz, w) -> (x, y, z, 1) + hpoint.divideScalar( hpoint.w ); - if ( hpoint.w != 1.0 ) { + } - // project to 3D space: (wx, wy, wz, w) -> (x, y, z, 1) - hpoint.divideScalar( hpoint.w ); + return point.set( hpoint.x, hpoint.y, hpoint.z ); } - return point.set( hpoint.x, hpoint.y, hpoint.z ); + getTangent( t, optionalTarget = new THREE.Vector3() ) { - }; + const tangent = optionalTarget; + const u = this.knots[ 0 ] + t * ( this.knots[ this.knots.length - 1 ] - this.knots[ 0 ] ); + const ders = THREE.NURBSUtils.calcNURBSDerivatives( this.degree, this.knots, this.controlPoints, u, 1 ); + tangent.copy( ders[ 1 ] ).normalize(); + return tangent; - NURBSCurve.prototype.getTangent = function ( t, optionalTarget ) { - - var tangent = optionalTarget || new THREE.Vector3(); - var u = this.knots[ 0 ] + t * ( this.knots[ this.knots.length - 1 ] - this.knots[ 0 ] ); - var ders = THREE.NURBSUtils.calcNURBSDerivatives( this.degree, this.knots, this.controlPoints, u, 1 ); - tangent.copy( ders[ 1 ] ).normalize(); - return tangent; + } - }; + } THREE.NURBSCurve = NURBSCurve; diff --git a/examples/js/curves/NURBSSurface.js b/examples/js/curves/NURBSSurface.js index cd0417ab54111d..7c67c0fd06e536 100644 --- a/examples/js/curves/NURBSSurface.js +++ b/examples/js/curves/NURBSSurface.js @@ -6,47 +6,48 @@ * Implementation is based on (x, y [, z=0 [, w=1]]) control points with w=weight. **/ - var NURBSSurface = function ( degree1, degree2, knots1, knots2 - /* arrays of reals */ - , controlPoints - /* array^2 of Vector(2|3|4) */ - ) { + class NURBSSurface { - this.degree1 = degree1; - this.degree2 = degree2; - this.knots1 = knots1; - this.knots2 = knots2; - this.controlPoints = []; - var len1 = knots1.length - degree1 - 1; - var len2 = knots2.length - degree2 - 1; // ensure THREE.Vector4 for control points + constructor( degree1, degree2, knots1, knots2 + /* arrays of reals */ + , controlPoints + /* array^2 of Vector(2|3|4) */ + ) { - for ( var i = 0; i < len1; ++ i ) { + this.degree1 = degree1; + this.degree2 = degree2; + this.knots1 = knots1; + this.knots2 = knots2; + this.controlPoints = []; + const len1 = knots1.length - degree1 - 1; + const len2 = knots2.length - degree2 - 1; // ensure THREE.Vector4 for control points - this.controlPoints[ i ] = []; + for ( let i = 0; i < len1; ++ i ) { - for ( var j = 0; j < len2; ++ j ) { + this.controlPoints[ i ] = []; - var point = controlPoints[ i ][ j ]; - this.controlPoints[ i ][ j ] = new THREE.Vector4( point.x, point.y, point.z, point.w ); + for ( let j = 0; j < len2; ++ j ) { + + const point = controlPoints[ i ][ j ]; + this.controlPoints[ i ][ j ] = new THREE.Vector4( point.x, point.y, point.z, point.w ); + + } } } - }; + getPoint( t1, t2, target ) { - NURBSSurface.prototype = { - constructor: NURBSSurface, - getPoint: function ( t1, t2, target ) { + const u = this.knots1[ 0 ] + t1 * ( this.knots1[ this.knots1.length - 1 ] - this.knots1[ 0 ] ); // linear mapping t1->u - var u = this.knots1[ 0 ] + t1 * ( this.knots1[ this.knots1.length - 1 ] - this.knots1[ 0 ] ); // linear mapping t1->u - - var v = this.knots2[ 0 ] + t2 * ( this.knots2[ this.knots2.length - 1 ] - this.knots2[ 0 ] ); // linear mapping t2->u + const v = this.knots2[ 0 ] + t2 * ( this.knots2[ this.knots2.length - 1 ] - this.knots2[ 0 ] ); // linear mapping t2->u THREE.NURBSUtils.calcSurfacePoint( this.degree1, this.degree2, this.knots1, this.knots2, this.controlPoints, u, v, target ); } - }; + + } THREE.NURBSSurface = NURBSSurface; diff --git a/examples/js/curves/NURBSUtils.js b/examples/js/curves/NURBSUtils.js index 3af2eb9e8c6652..039179a3eaeb6f 100644 --- a/examples/js/curves/NURBSUtils.js +++ b/examples/js/curves/NURBSUtils.js @@ -10,17 +10,18 @@ * NURBS Utils **************************************************************/ - var NURBSUtils = { - /* + class NURBSUtils { + + /* Finds knot vector span. p : degree u : parametric value U : knot vector returns the span */ - findSpan: function ( p, u, U ) { + static findSpan( p, u, U ) { - var n = U.length - p - 1; + const n = U.length - p - 1; if ( u >= U[ n ] ) { @@ -34,9 +35,9 @@ } - var low = p; - var high = n; - var mid = Math.floor( ( low + high ) / 2 ); + let low = p; + let high = n; + let mid = Math.floor( ( low + high ) / 2 ); while ( u < U[ mid ] || u >= U[ mid + 1 ] ) { @@ -56,8 +57,7 @@ return mid; - }, - + } /* Calculate basis functions. See The NURBS Book, page 70, algorithm A2.2 span : span in which u lies @@ -66,24 +66,26 @@ U : knot vector returns array[p+1] with basis functions values. */ - calcBasisFunctions: function ( span, u, p, U ) { - var N = []; - var left = []; - var right = []; + + static calcBasisFunctions( span, u, p, U ) { + + const N = []; + const left = []; + const right = []; N[ 0 ] = 1.0; - for ( var j = 1; j <= p; ++ j ) { + for ( let j = 1; j <= p; ++ j ) { left[ j ] = u - U[ span + 1 - j ]; right[ j ] = U[ span + j ] - u; - var saved = 0.0; + let saved = 0.0; - for ( var r = 0; r < j; ++ r ) { + for ( let r = 0; r < j; ++ r ) { - var rv = right[ r + 1 ]; - var lv = left[ j - r ]; - var temp = N[ r ] / ( rv + lv ); + const rv = right[ r + 1 ]; + const lv = left[ j - r ]; + const temp = N[ r ] / ( rv + lv ); N[ r ] = saved + rv * temp; saved = lv * temp; @@ -95,8 +97,7 @@ return N; - }, - + } /* Calculate B-Spline curve points. See The NURBS Book, page 82, algorithm A3.1. p : degree of B-Spline @@ -105,17 +106,19 @@ u : parametric point returns point for given u */ - calcBSplinePoint: function ( p, U, P, u ) { - var span = this.findSpan( p, u, U ); - var N = this.calcBasisFunctions( span, u, p, U ); - var C = new THREE.Vector4( 0, 0, 0, 0 ); - for ( var j = 0; j <= p; ++ j ) { + static calcBSplinePoint( p, U, P, u ) { + + const span = this.findSpan( p, u, U ); + const N = this.calcBasisFunctions( span, u, p, U ); + const C = new THREE.Vector4( 0, 0, 0, 0 ); - var point = P[ span - p + j ]; - var Nj = N[ j ]; - var wNj = point.w * Nj; + for ( let j = 0; j <= p; ++ j ) { + + const point = P[ span - p + j ]; + const Nj = N[ j ]; + const wNj = point.w * Nj; C.x += point.x * wNj; C.y += point.y * wNj; C.z += point.z * wNj; @@ -125,8 +128,7 @@ return C; - }, - + } /* Calculate basis functions derivatives. See The NURBS Book, page 72, algorithm A2.3. span : span in which u lies @@ -136,36 +138,38 @@ U : knot vector returns array[n+1][p+1] with basis functions derivatives */ - calcBasisFunctionDerivatives: function ( span, u, p, n, U ) { - var zeroArr = []; - for ( var i = 0; i <= p; ++ i ) zeroArr[ i ] = 0.0; + static calcBasisFunctionDerivatives( span, u, p, n, U ) { + + const zeroArr = []; + + for ( let i = 0; i <= p; ++ i ) zeroArr[ i ] = 0.0; - var ders = []; + const ders = []; - for ( var i = 0; i <= n; ++ i ) ders[ i ] = zeroArr.slice( 0 ); + for ( let i = 0; i <= n; ++ i ) ders[ i ] = zeroArr.slice( 0 ); - var ndu = []; + const ndu = []; - for ( var i = 0; i <= p; ++ i ) ndu[ i ] = zeroArr.slice( 0 ); + for ( let i = 0; i <= p; ++ i ) ndu[ i ] = zeroArr.slice( 0 ); ndu[ 0 ][ 0 ] = 1.0; - var left = zeroArr.slice( 0 ); - var right = zeroArr.slice( 0 ); + const left = zeroArr.slice( 0 ); + const right = zeroArr.slice( 0 ); - for ( var j = 1; j <= p; ++ j ) { + for ( let j = 1; j <= p; ++ j ) { left[ j ] = u - U[ span + 1 - j ]; right[ j ] = U[ span + j ] - u; - var saved = 0.0; + let saved = 0.0; - for ( var r = 0; r < j; ++ r ) { + for ( let r = 0; r < j; ++ r ) { - var rv = right[ r + 1 ]; - var lv = left[ j - r ]; + const rv = right[ r + 1 ]; + const lv = left[ j - r ]; ndu[ j ][ r ] = rv + lv; - var temp = ndu[ r ][ j - 1 ] / ndu[ j ][ r ]; + const temp = ndu[ r ][ j - 1 ] / ndu[ j ][ r ]; ndu[ r ][ j ] = saved + rv * temp; saved = lv * temp; @@ -175,19 +179,19 @@ } - for ( var j = 0; j <= p; ++ j ) { + for ( let j = 0; j <= p; ++ j ) { ders[ 0 ][ j ] = ndu[ j ][ p ]; } - for ( var r = 0; r <= p; ++ r ) { + for ( let r = 0; r <= p; ++ r ) { - var s1 = 0; - var s2 = 1; - var a = []; + let s1 = 0; + let s2 = 1; + const a = []; - for ( var i = 0; i <= p; ++ i ) { + for ( let i = 0; i <= p; ++ i ) { a[ i ] = zeroArr.slice( 0 ); @@ -195,11 +199,11 @@ a[ 0 ][ 0 ] = 1.0; - for ( var k = 1; k <= n; ++ k ) { + for ( let k = 1; k <= n; ++ k ) { - var d = 0.0; - var rk = r - k; - var pk = p - k; + let d = 0.0; + const rk = r - k; + const pk = p - k; if ( r >= k ) { @@ -208,10 +212,10 @@ } - var j1 = rk >= - 1 ? 1 : - rk; - var j2 = r - 1 <= pk ? k - 1 : p - r; + const j1 = rk >= - 1 ? 1 : - rk; + const j2 = r - 1 <= pk ? k - 1 : p - r; - for ( var j = j1; j <= j2; ++ j ) { + for ( let j = j1; j <= j2; ++ j ) { a[ s2 ][ j ] = ( a[ s1 ][ j ] - a[ s1 ][ j - 1 ] ) / ndu[ pk + 1 ][ rk + j ]; d += a[ s2 ][ j ] * ndu[ rk + j ][ pk ]; @@ -226,7 +230,7 @@ } ders[ k ][ r ] = d; - var j = s1; + const j = s1; s1 = s2; s2 = j; @@ -234,11 +238,11 @@ } - var r = p; + const r = p; - for ( var k = 1; k <= n; ++ k ) { + for ( let k = 1; k <= n; ++ k ) { - for ( var j = 0; j <= p; ++ j ) { + for ( let j = 0; j <= p; ++ j ) { ders[ k ][ j ] *= r; @@ -250,8 +254,7 @@ return ders; - }, - + } /* Calculate derivatives of a B-Spline. See The NURBS Book, page 93, algorithm A3.2. p : degree @@ -261,18 +264,20 @@ nd : number of derivatives returns array[d+1] with derivatives */ - calcBSplineDerivatives: function ( p, U, P, u, nd ) { - var du = nd < p ? nd : p; - var CK = []; - var span = this.findSpan( p, u, U ); - var nders = this.calcBasisFunctionDerivatives( span, u, p, du, U ); - var Pw = []; - for ( var i = 0; i < P.length; ++ i ) { + static calcBSplineDerivatives( p, U, P, u, nd ) { + + const du = nd < p ? nd : p; + const CK = []; + const span = this.findSpan( p, u, U ); + const nders = this.calcBasisFunctionDerivatives( span, u, p, du, U ); + const Pw = []; + + for ( let i = 0; i < P.length; ++ i ) { - var point = P[ i ].clone(); - var w = point.w; + const point = P[ i ].clone(); + const w = point.w; point.x *= w; point.y *= w; point.z *= w; @@ -280,11 +285,11 @@ } - for ( var k = 0; k <= du; ++ k ) { + for ( let k = 0; k <= du; ++ k ) { - var point = Pw[ span - p ].clone().multiplyScalar( nders[ k ][ 0 ] ); + const point = Pw[ span - p ].clone().multiplyScalar( nders[ k ][ 0 ] ); - for ( var j = 1; j <= p; ++ j ) { + for ( let j = 1; j <= p; ++ j ) { point.add( Pw[ span - p + j ].clone().multiplyScalar( nders[ k ][ j ] ) ); @@ -294,7 +299,7 @@ } - for ( var k = du + 1; k <= nd + 1; ++ k ) { + for ( let k = du + 1; k <= nd + 1; ++ k ) { CK[ k ] = new THREE.Vector4( 0, 0, 0 ); @@ -302,31 +307,32 @@ return CK; - }, - + } /* Calculate "K over I" returns k!/(i!(k-i)!) */ - calcKoverI: function ( k, i ) { - var nom = 1; - for ( var j = 2; j <= k; ++ j ) { + static calcKoverI( k, i ) { + + let nom = 1; + + for ( let j = 2; j <= k; ++ j ) { nom *= j; } - var denom = 1; + let denom = 1; - for ( var j = 2; j <= i; ++ j ) { + for ( let j = 2; j <= i; ++ j ) { denom *= j; } - for ( var j = 2; j <= k - i; ++ j ) { + for ( let j = 2; j <= k - i; ++ j ) { denom *= j; @@ -334,34 +340,35 @@ return nom / denom; - }, - + } /* Calculate derivatives (0-nd) of rational curve. See The NURBS Book, page 127, algorithm A4.2. Pders : result of function calcBSplineDerivatives returns array with derivatives for rational curve. */ - calcRationalCurveDerivatives: function ( Pders ) { - var nd = Pders.length; - var Aders = []; - var wders = []; - for ( var i = 0; i < nd; ++ i ) { + static calcRationalCurveDerivatives( Pders ) { + + const nd = Pders.length; + const Aders = []; + const wders = []; - var point = Pders[ i ]; + for ( let i = 0; i < nd; ++ i ) { + + const point = Pders[ i ]; Aders[ i ] = new THREE.Vector3( point.x, point.y, point.z ); wders[ i ] = point.w; } - var CK = []; + const CK = []; - for ( var k = 0; k < nd; ++ k ) { + for ( let k = 0; k < nd; ++ k ) { - var v = Aders[ k ].clone(); + const v = Aders[ k ].clone(); - for ( var i = 1; i <= k; ++ i ) { + for ( let i = 1; i <= k; ++ i ) { v.sub( CK[ k - i ].clone().multiplyScalar( this.calcKoverI( k, i ) * wders[ i ] ) ); @@ -373,8 +380,7 @@ return CK; - }, - + } /* Calculate NURBS curve derivatives. See The NURBS Book, page 127, algorithm A4.2. p : degree @@ -384,13 +390,14 @@ nd : number of derivatives returns array with derivatives. */ - calcNURBSDerivatives: function ( p, U, P, u, nd ) { - var Pders = this.calcBSplineDerivatives( p, U, P, u, nd ); - return this.calcRationalCurveDerivatives( Pders ); - }, + static calcNURBSDerivatives( p, U, P, u, nd ) { + + const Pders = this.calcBSplineDerivatives( p, U, P, u, nd ); + return this.calcRationalCurveDerivatives( Pders ); + } /* Calculate rational B-Spline surface point. See The NURBS Book, page 134, algorithm A4.3. p1, p2 : degrees of B-Spline surface @@ -399,22 +406,24 @@ u, v : parametric values returns point for given (u, v) */ - calcSurfacePoint: function ( p, q, U, V, P, u, v, target ) { - var uspan = this.findSpan( p, u, U ); - var vspan = this.findSpan( q, v, V ); - var Nu = this.calcBasisFunctions( uspan, u, p, U ); - var Nv = this.calcBasisFunctions( vspan, v, q, V ); - var temp = []; - for ( var l = 0; l <= q; ++ l ) { + static calcSurfacePoint( p, q, U, V, P, u, v, target ) { + + const uspan = this.findSpan( p, u, U ); + const vspan = this.findSpan( q, v, V ); + const Nu = this.calcBasisFunctions( uspan, u, p, U ); + const Nv = this.calcBasisFunctions( vspan, v, q, V ); + const temp = []; + + for ( let l = 0; l <= q; ++ l ) { temp[ l ] = new THREE.Vector4( 0, 0, 0, 0 ); - for ( var k = 0; k <= p; ++ k ) { + for ( let k = 0; k <= p; ++ k ) { - var point = P[ uspan - p + k ][ vspan - q + l ].clone(); - var w = point.w; + const point = P[ uspan - p + k ][ vspan - q + l ].clone(); + const w = point.w; point.x *= w; point.y *= w; point.z *= w; @@ -424,9 +433,9 @@ } - var Sw = new THREE.Vector4( 0, 0, 0, 0 ); + const Sw = new THREE.Vector4( 0, 0, 0, 0 ); - for ( var l = 0; l <= q; ++ l ) { + for ( let l = 0; l <= q; ++ l ) { Sw.add( temp[ l ].multiplyScalar( Nv[ l ] ) ); @@ -436,7 +445,8 @@ target.set( Sw.x, Sw.y, Sw.z ); } - }; + + } THREE.NURBSUtils = NURBSUtils; diff --git a/examples/jsm/curves/CurveExtras.js b/examples/jsm/curves/CurveExtras.js index 6ee7f1664faa88..ba41152fce1188 100644 --- a/examples/jsm/curves/CurveExtras.js +++ b/examples/jsm/curves/CurveExtras.js @@ -15,417 +15,412 @@ import { * https://prideout.net/blog/old/blog/index.html@p=44.html */ -var Curves = ( function () { +// GrannyKnot - // GrannyKnot +class GrannyKnot extends Curve { - function GrannyKnot() { + getPoint( t, optionalTarget = new Vector3() ) { - Curve.call( this ); - - } - - GrannyKnot.prototype = Object.create( Curve.prototype ); - GrannyKnot.prototype.constructor = GrannyKnot; - - GrannyKnot.prototype.getPoint = function ( t, optionalTarget ) { - - var point = optionalTarget || new Vector3(); + const point = optionalTarget; t = 2 * Math.PI * t; - var x = - 0.22 * Math.cos( t ) - 1.28 * Math.sin( t ) - 0.44 * Math.cos( 3 * t ) - 0.78 * Math.sin( 3 * t ); - var y = - 0.1 * Math.cos( 2 * t ) - 0.27 * Math.sin( 2 * t ) + 0.38 * Math.cos( 4 * t ) + 0.46 * Math.sin( 4 * t ); - var z = 0.7 * Math.cos( 3 * t ) - 0.4 * Math.sin( 3 * t ); + const x = - 0.22 * Math.cos( t ) - 1.28 * Math.sin( t ) - 0.44 * Math.cos( 3 * t ) - 0.78 * Math.sin( 3 * t ); + const y = - 0.1 * Math.cos( 2 * t ) - 0.27 * Math.sin( 2 * t ) + 0.38 * Math.cos( 4 * t ) + 0.46 * Math.sin( 4 * t ); + const z = 0.7 * Math.cos( 3 * t ) - 0.4 * Math.sin( 3 * t ); return point.set( x, y, z ).multiplyScalar( 20 ); - }; + } - // HeartCurve +} - function HeartCurve( scale ) { +// HeartCurve - Curve.call( this ); +class HeartCurve extends Curve { - this.scale = ( scale === undefined ) ? 5 : scale; + constructor( scale = 5 ) { - } + super(); - HeartCurve.prototype = Object.create( Curve.prototype ); - HeartCurve.prototype.constructor = HeartCurve; + this.scale = scale; - HeartCurve.prototype.getPoint = function ( t, optionalTarget ) { + } - var point = optionalTarget || new Vector3(); + getPoint( t, optionalTarget = new Vector3() ) { + + const point = optionalTarget; t *= 2 * Math.PI; - var x = 16 * Math.pow( Math.sin( t ), 3 ); - var y = 13 * Math.cos( t ) - 5 * Math.cos( 2 * t ) - 2 * Math.cos( 3 * t ) - Math.cos( 4 * t ); - var z = 0; + const x = 16 * Math.pow( Math.sin( t ), 3 ); + const y = 13 * Math.cos( t ) - 5 * Math.cos( 2 * t ) - 2 * Math.cos( 3 * t ) - Math.cos( 4 * t ); + const z = 0; return point.set( x, y, z ).multiplyScalar( this.scale ); - }; + } - // Viviani's Curve +} - function VivianiCurve( scale ) { +// Viviani's Curve - Curve.call( this ); +class VivianiCurve extends Curve { - this.scale = ( scale === undefined ) ? 70 : scale; + constructor( scale = 70 ) { - } + super(); - VivianiCurve.prototype = Object.create( Curve.prototype ); - VivianiCurve.prototype.constructor = VivianiCurve; + this.scale = scale; - VivianiCurve.prototype.getPoint = function ( t, optionalTarget ) { + } - var point = optionalTarget || new Vector3(); + getPoint( t, optionalTarget = new Vector3() ) { + + const point = optionalTarget; t = t * 4 * Math.PI; // normalized to 0..1 - var a = this.scale / 2; + const a = this.scale / 2; - var x = a * ( 1 + Math.cos( t ) ); - var y = a * Math.sin( t ); - var z = 2 * a * Math.sin( t / 2 ); + const x = a * ( 1 + Math.cos( t ) ); + const y = a * Math.sin( t ); + const z = 2 * a * Math.sin( t / 2 ); return point.set( x, y, z ); - }; - - // KnotCurve - - function KnotCurve() { + } - Curve.call( this ); +} - } +// KnotCurve - KnotCurve.prototype = Object.create( Curve.prototype ); - KnotCurve.prototype.constructor = KnotCurve; +class KnotCurve extends Curve { - KnotCurve.prototype.getPoint = function ( t, optionalTarget ) { + getPoint( t, optionalTarget = new Vector3() ) { - var point = optionalTarget || new Vector3(); + const point = optionalTarget; t *= 2 * Math.PI; - var R = 10; - var s = 50; + const R = 10; + const s = 50; - var x = s * Math.sin( t ); - var y = Math.cos( t ) * ( R + s * Math.cos( t ) ); - var z = Math.sin( t ) * ( R + s * Math.cos( t ) ); + const x = s * Math.sin( t ); + const y = Math.cos( t ) * ( R + s * Math.cos( t ) ); + const z = Math.sin( t ) * ( R + s * Math.cos( t ) ); return point.set( x, y, z ); - }; - - // HelixCurve + } - function HelixCurve() { +} - Curve.call( this ); - } +// HelixCurve - HelixCurve.prototype = Object.create( Curve.prototype ); - HelixCurve.prototype.constructor = HelixCurve; +class HelixCurve extends Curve { - HelixCurve.prototype.getPoint = function ( t, optionalTarget ) { + getPoint( t, optionalTarget = new Vector3() ) { - var point = optionalTarget || new Vector3(); + const point = optionalTarget; - var a = 30; // radius - var b = 150; // height + const a = 30; // radius + const b = 150; // height - var t2 = 2 * Math.PI * t * b / 30; + const t2 = 2 * Math.PI * t * b / 30; - var x = Math.cos( t2 ) * a; - var y = Math.sin( t2 ) * a; - var z = b * t; + const x = Math.cos( t2 ) * a; + const y = Math.sin( t2 ) * a; + const z = b * t; return point.set( x, y, z ); - }; + } - // TrefoilKnot +} - function TrefoilKnot( scale ) { +// TrefoilKnot - Curve.call( this ); +class TrefoilKnot extends Curve { - this.scale = ( scale === undefined ) ? 10 : scale; + constructor( scale = 10 ) { - } + super(); - TrefoilKnot.prototype = Object.create( Curve.prototype ); - TrefoilKnot.prototype.constructor = TrefoilKnot; + this.scale = scale; - TrefoilKnot.prototype.getPoint = function ( t, optionalTarget ) { + } - var point = optionalTarget || new Vector3(); + getPoint( t, optionalTarget = new Vector3() ) { + + const point = optionalTarget; t *= Math.PI * 2; - var x = ( 2 + Math.cos( 3 * t ) ) * Math.cos( 2 * t ); - var y = ( 2 + Math.cos( 3 * t ) ) * Math.sin( 2 * t ); - var z = Math.sin( 3 * t ); + const x = ( 2 + Math.cos( 3 * t ) ) * Math.cos( 2 * t ); + const y = ( 2 + Math.cos( 3 * t ) ) * Math.sin( 2 * t ); + const z = Math.sin( 3 * t ); return point.set( x, y, z ).multiplyScalar( this.scale ); - }; + } - // TorusKnot +} - function TorusKnot( scale ) { +// TorusKnot - Curve.call( this ); +class TorusKnot extends Curve { - this.scale = ( scale === undefined ) ? 10 : scale; + constructor( scale = 10 ) { - } + super(); - TorusKnot.prototype = Object.create( Curve.prototype ); - TorusKnot.prototype.constructor = TorusKnot; + this.scale = scale; - TorusKnot.prototype.getPoint = function ( t, optionalTarget ) { + } - var point = optionalTarget || new Vector3(); + getPoint( t, optionalTarget = new Vector3() ) { - var p = 3; - var q = 4; + const point = optionalTarget; + + const p = 3; + const q = 4; t *= Math.PI * 2; - var x = ( 2 + Math.cos( q * t ) ) * Math.cos( p * t ); - var y = ( 2 + Math.cos( q * t ) ) * Math.sin( p * t ); - var z = Math.sin( q * t ); + const x = ( 2 + Math.cos( q * t ) ) * Math.cos( p * t ); + const y = ( 2 + Math.cos( q * t ) ) * Math.sin( p * t ); + const z = Math.sin( q * t ); return point.set( x, y, z ).multiplyScalar( this.scale ); - }; + } - // CinquefoilKnot +} - function CinquefoilKnot( scale ) { +// CinquefoilKnot - Curve.call( this ); +class CinquefoilKnot extends Curve { - this.scale = ( scale === undefined ) ? 10 : scale; + constructor( scale = 10 ) { - } + super(); - CinquefoilKnot.prototype = Object.create( Curve.prototype ); - CinquefoilKnot.prototype.constructor = CinquefoilKnot; + this.scale = scale; - CinquefoilKnot.prototype.getPoint = function ( t, optionalTarget ) { + } + + getPoint( t, optionalTarget = new Vector3() ) { - var point = optionalTarget || new Vector3(); + const point = optionalTarget; - var p = 2; - var q = 5; + const p = 2; + const q = 5; t *= Math.PI * 2; - var x = ( 2 + Math.cos( q * t ) ) * Math.cos( p * t ); - var y = ( 2 + Math.cos( q * t ) ) * Math.sin( p * t ); - var z = Math.sin( q * t ); + const x = ( 2 + Math.cos( q * t ) ) * Math.cos( p * t ); + const y = ( 2 + Math.cos( q * t ) ) * Math.sin( p * t ); + const z = Math.sin( q * t ); return point.set( x, y, z ).multiplyScalar( this.scale ); - }; + } - // TrefoilPolynomialKnot +} - function TrefoilPolynomialKnot( scale ) { - Curve.call( this ); +// TrefoilPolynomialKnot - this.scale = ( scale === undefined ) ? 10 : scale; +class TrefoilPolynomialKnot extends Curve { - } + constructor( scale = 10 ) { - TrefoilPolynomialKnot.prototype = Object.create( Curve.prototype ); - TrefoilPolynomialKnot.prototype.constructor = TrefoilPolynomialKnot; + super(); - TrefoilPolynomialKnot.prototype.getPoint = function ( t, optionalTarget ) { + this.scale = scale; - var point = optionalTarget || new Vector3(); + } + + getPoint( t, optionalTarget = new Vector3() ) { + + const point = optionalTarget; t = t * 4 - 2; - var x = Math.pow( t, 3 ) - 3 * t; - var y = Math.pow( t, 4 ) - 4 * t * t; - var z = 1 / 5 * Math.pow( t, 5 ) - 2 * t; + const x = Math.pow( t, 3 ) - 3 * t; + const y = Math.pow( t, 4 ) - 4 * t * t; + const z = 1 / 5 * Math.pow( t, 5 ) - 2 * t; return point.set( x, y, z ).multiplyScalar( this.scale ); - }; + } - var scaleTo = function ( x, y, t ) { +} - var r = y - x; - return t * r + x; +function scaleTo( x, y, t ) { - }; + const r = y - x; + return t * r + x; - // FigureEightPolynomialKnot +} - function FigureEightPolynomialKnot( scale ) { +// FigureEightPolynomialKnot - Curve.call( this ); +class FigureEightPolynomialKnot extends Curve { - this.scale = ( scale === undefined ) ? 1 : scale; + constructor( scale = 1 ) { - } + super(); + + this.scale = scale; - FigureEightPolynomialKnot.prototype = Object.create( Curve.prototype ); - FigureEightPolynomialKnot.prototype.constructor = FigureEightPolynomialKnot; + } - FigureEightPolynomialKnot.prototype.getPoint = function ( t, optionalTarget ) { + getPoint( t, optionalTarget = new Vector3() ) { - var point = optionalTarget || new Vector3(); + const point = optionalTarget; t = scaleTo( - 4, 4, t ); - var x = 2 / 5 * t * ( t * t - 7 ) * ( t * t - 10 ); - var y = Math.pow( t, 4 ) - 13 * t * t; - var z = 1 / 10 * t * ( t * t - 4 ) * ( t * t - 9 ) * ( t * t - 12 ); + const x = 2 / 5 * t * ( t * t - 7 ) * ( t * t - 10 ); + const y = Math.pow( t, 4 ) - 13 * t * t; + const z = 1 / 10 * t * ( t * t - 4 ) * ( t * t - 9 ) * ( t * t - 12 ); return point.set( x, y, z ).multiplyScalar( this.scale ); - }; + } - // DecoratedTorusKnot4a +} - function DecoratedTorusKnot4a( scale ) { +// DecoratedTorusKnot4a - Curve.call( this ); +class DecoratedTorusKnot4a extends Curve { - this.scale = ( scale === undefined ) ? 40 : scale; + constructor( scale = 40 ) { - } + super(); + + this.scale = scale; - DecoratedTorusKnot4a.prototype = Object.create( Curve.prototype ); - DecoratedTorusKnot4a.prototype.constructor = DecoratedTorusKnot4a; + } - DecoratedTorusKnot4a.prototype.getPoint = function ( t, optionalTarget ) { + getPoint( t, optionalTarget = new Vector3() ) { - var point = optionalTarget || new Vector3(); + const point = optionalTarget; t *= Math.PI * 2; - var x = Math.cos( 2 * t ) * ( 1 + 0.6 * ( Math.cos( 5 * t ) + 0.75 * Math.cos( 10 * t ) ) ); - var y = Math.sin( 2 * t ) * ( 1 + 0.6 * ( Math.cos( 5 * t ) + 0.75 * Math.cos( 10 * t ) ) ); - var z = 0.35 * Math.sin( 5 * t ); + const x = Math.cos( 2 * t ) * ( 1 + 0.6 * ( Math.cos( 5 * t ) + 0.75 * Math.cos( 10 * t ) ) ); + const y = Math.sin( 2 * t ) * ( 1 + 0.6 * ( Math.cos( 5 * t ) + 0.75 * Math.cos( 10 * t ) ) ); + const z = 0.35 * Math.sin( 5 * t ); return point.set( x, y, z ).multiplyScalar( this.scale ); - }; + } - // DecoratedTorusKnot4b +} - function DecoratedTorusKnot4b( scale ) { +// DecoratedTorusKnot4b - Curve.call( this ); +class DecoratedTorusKnot4b extends Curve { - this.scale = ( scale === undefined ) ? 40 : scale; + constructor( scale = 40 ) { - } + super(); - DecoratedTorusKnot4b.prototype = Object.create( Curve.prototype ); - DecoratedTorusKnot4b.prototype.constructor = DecoratedTorusKnot4b; + this.scale = scale; + + } - DecoratedTorusKnot4b.prototype.getPoint = function ( t, optionalTarget ) { + getPoint( t, optionalTarget = new Vector3() ) { - var point = optionalTarget || new Vector3(); + const point = optionalTarget; - var fi = t * Math.PI * 2; + const fi = t * Math.PI * 2; - var x = Math.cos( 2 * fi ) * ( 1 + 0.45 * Math.cos( 3 * fi ) + 0.4 * Math.cos( 9 * fi ) ); - var y = Math.sin( 2 * fi ) * ( 1 + 0.45 * Math.cos( 3 * fi ) + 0.4 * Math.cos( 9 * fi ) ); - var z = 0.2 * Math.sin( 9 * fi ); + const x = Math.cos( 2 * fi ) * ( 1 + 0.45 * Math.cos( 3 * fi ) + 0.4 * Math.cos( 9 * fi ) ); + const y = Math.sin( 2 * fi ) * ( 1 + 0.45 * Math.cos( 3 * fi ) + 0.4 * Math.cos( 9 * fi ) ); + const z = 0.2 * Math.sin( 9 * fi ); return point.set( x, y, z ).multiplyScalar( this.scale ); - }; + } - // DecoratedTorusKnot5a +} - function DecoratedTorusKnot5a( scale ) { - Curve.call( this ); +// DecoratedTorusKnot5a - this.scale = ( scale === undefined ) ? 40 : scale; +class DecoratedTorusKnot5a extends Curve { - } + constructor( scale = 40 ) { - DecoratedTorusKnot5a.prototype = Object.create( Curve.prototype ); - DecoratedTorusKnot5a.prototype.constructor = DecoratedTorusKnot5a; + super(); - DecoratedTorusKnot5a.prototype.getPoint = function ( t, optionalTarget ) { + this.scale = scale; + + } - var point = optionalTarget || new Vector3(); + getPoint( t, optionalTarget = new Vector3() ) { - var fi = t * Math.PI * 2; + const point = optionalTarget; - var x = Math.cos( 3 * fi ) * ( 1 + 0.3 * Math.cos( 5 * fi ) + 0.5 * Math.cos( 10 * fi ) ); - var y = Math.sin( 3 * fi ) * ( 1 + 0.3 * Math.cos( 5 * fi ) + 0.5 * Math.cos( 10 * fi ) ); - var z = 0.2 * Math.sin( 20 * fi ); + const fi = t * Math.PI * 2; + + const x = Math.cos( 3 * fi ) * ( 1 + 0.3 * Math.cos( 5 * fi ) + 0.5 * Math.cos( 10 * fi ) ); + const y = Math.sin( 3 * fi ) * ( 1 + 0.3 * Math.cos( 5 * fi ) + 0.5 * Math.cos( 10 * fi ) ); + const z = 0.2 * Math.sin( 20 * fi ); return point.set( x, y, z ).multiplyScalar( this.scale ); - }; + } - // DecoratedTorusKnot5c +} - function DecoratedTorusKnot5c( scale ) { +// DecoratedTorusKnot5c - Curve.call( this ); +class DecoratedTorusKnot5c extends Curve { - this.scale = ( scale === undefined ) ? 40 : scale; + constructor( scale = 40 ) { - } + super(); - DecoratedTorusKnot5c.prototype = Object.create( Curve.prototype ); - DecoratedTorusKnot5c.prototype.constructor = DecoratedTorusKnot5c; + this.scale = scale; - DecoratedTorusKnot5c.prototype.getPoint = function ( t, optionalTarget ) { + } - var point = optionalTarget || new Vector3(); + getPoint( t, optionalTarget = new Vector3() ) { - var fi = t * Math.PI * 2; + const point = optionalTarget; - var x = Math.cos( 4 * fi ) * ( 1 + 0.5 * ( Math.cos( 5 * fi ) + 0.4 * Math.cos( 20 * fi ) ) ); - var y = Math.sin( 4 * fi ) * ( 1 + 0.5 * ( Math.cos( 5 * fi ) + 0.4 * Math.cos( 20 * fi ) ) ); - var z = 0.35 * Math.sin( 15 * fi ); + const fi = t * Math.PI * 2; + + const x = Math.cos( 4 * fi ) * ( 1 + 0.5 * ( Math.cos( 5 * fi ) + 0.4 * Math.cos( 20 * fi ) ) ); + const y = Math.sin( 4 * fi ) * ( 1 + 0.5 * ( Math.cos( 5 * fi ) + 0.4 * Math.cos( 20 * fi ) ) ); + const z = 0.35 * Math.sin( 15 * fi ); return point.set( x, y, z ).multiplyScalar( this.scale ); - }; - - return { - GrannyKnot: GrannyKnot, - HeartCurve: HeartCurve, - VivianiCurve: VivianiCurve, - KnotCurve: KnotCurve, - HelixCurve: HelixCurve, - TrefoilKnot: TrefoilKnot, - TorusKnot: TorusKnot, - CinquefoilKnot: CinquefoilKnot, - TrefoilPolynomialKnot: TrefoilPolynomialKnot, - FigureEightPolynomialKnot: FigureEightPolynomialKnot, - DecoratedTorusKnot4a: DecoratedTorusKnot4a, - DecoratedTorusKnot4b: DecoratedTorusKnot4b, - DecoratedTorusKnot5a: DecoratedTorusKnot5a, - DecoratedTorusKnot5c: DecoratedTorusKnot5c - }; - -} )(); + } + +} + +const Curves = { + GrannyKnot: GrannyKnot, + HeartCurve: HeartCurve, + VivianiCurve: VivianiCurve, + KnotCurve: KnotCurve, + HelixCurve: HelixCurve, + TrefoilKnot: TrefoilKnot, + TorusKnot: TorusKnot, + CinquefoilKnot: CinquefoilKnot, + TrefoilPolynomialKnot: TrefoilPolynomialKnot, + FigureEightPolynomialKnot: FigureEightPolynomialKnot, + DecoratedTorusKnot4a: DecoratedTorusKnot4a, + DecoratedTorusKnot4b: DecoratedTorusKnot4b, + DecoratedTorusKnot5a: DecoratedTorusKnot5a, + DecoratedTorusKnot5c: DecoratedTorusKnot5c +}; export { Curves }; diff --git a/examples/jsm/curves/NURBSCurve.js b/examples/jsm/curves/NURBSCurve.js index b89fe55bc543e5..2b7f2f24a4d76c 100644 --- a/examples/jsm/curves/NURBSCurve.js +++ b/examples/jsm/curves/NURBSCurve.js @@ -14,62 +14,67 @@ import { NURBSUtils } from '../curves/NURBSUtils.js'; * **/ -var NURBSCurve = function ( degree, knots /* array of reals */, controlPoints /* array of Vector(2|3|4) */, startKnot /* index in knots */, endKnot /* index in knots */ ) { +class NURBSCurve extends Curve { - Curve.call( this ); + constructor( + degree, + knots /* array of reals */, + controlPoints /* array of Vector(2|3|4) */, + startKnot /* index in knots */, + endKnot /* index in knots */ + ) { - 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 ); - for ( var i = 0; i < controlPoints.length; ++ i ) { + super(); - // ensure Vector4 for control points - var point = controlPoints[ i ]; - this.controlPoints[ i ] = new Vector4( point.x, point.y, point.z, point.w ); + 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 ); - } - -}; + for ( let i = 0; i < controlPoints.length; ++ i ) { + // ensure Vector4 for control points + const point = controlPoints[ i ]; + this.controlPoints[ i ] = new Vector4( point.x, point.y, point.z, point.w ); -NURBSCurve.prototype = Object.create( Curve.prototype ); -NURBSCurve.prototype.constructor = NURBSCurve; + } + } -NURBSCurve.prototype.getPoint = function ( t, optionalTarget ) { + getPoint( t, optionalTarget = new Vector3() ) { - var point = optionalTarget || new Vector3(); + const point = optionalTarget; - var u = this.knots[ this.startKnot ] + t * ( this.knots[ this.endKnot ] - this.knots[ this.startKnot ] ); // linear mapping t->u + const u = this.knots[ this.startKnot ] + t * ( this.knots[ this.endKnot ] - this.knots[ this.startKnot ] ); // linear mapping t->u - // following results in (wx, wy, wz, w) homogeneous point - var hpoint = NURBSUtils.calcBSplinePoint( this.degree, this.knots, this.controlPoints, u ); + // following results in (wx, wy, wz, w) homogeneous point + const hpoint = NURBSUtils.calcBSplinePoint( this.degree, this.knots, this.controlPoints, u ); - if ( hpoint.w != 1.0 ) { + if ( hpoint.w !== 1.0 ) { - // project to 3D space: (wx, wy, wz, w) -> (x, y, z, 1) - hpoint.divideScalar( hpoint.w ); + // project to 3D space: (wx, wy, wz, w) -> (x, y, z, 1) + hpoint.divideScalar( hpoint.w ); - } + } - return point.set( hpoint.x, hpoint.y, hpoint.z ); + return point.set( hpoint.x, hpoint.y, hpoint.z ); -}; + } + getTangent( t, optionalTarget = new Vector3() ) { -NURBSCurve.prototype.getTangent = function ( t, optionalTarget ) { + const tangent = optionalTarget; - var tangent = optionalTarget || new Vector3(); + const u = this.knots[ 0 ] + t * ( this.knots[ this.knots.length - 1 ] - this.knots[ 0 ] ); + const ders = NURBSUtils.calcNURBSDerivatives( this.degree, this.knots, this.controlPoints, u, 1 ); + tangent.copy( ders[ 1 ] ).normalize(); - var u = this.knots[ 0 ] + t * ( this.knots[ this.knots.length - 1 ] - this.knots[ 0 ] ); - var ders = NURBSUtils.calcNURBSDerivatives( this.degree, this.knots, this.controlPoints, u, 1 ); - tangent.copy( ders[ 1 ] ).normalize(); + return tangent; - return tangent; + } -}; +} export { NURBSCurve }; diff --git a/examples/jsm/curves/NURBSSurface.js b/examples/jsm/curves/NURBSSurface.js index bac72940915a97..3a017d8edc679f 100644 --- a/examples/jsm/curves/NURBSSurface.js +++ b/examples/jsm/curves/NURBSSurface.js @@ -9,45 +9,44 @@ import { NURBSUtils } from '../curves/NURBSUtils.js'; * Implementation is based on (x, y [, z=0 [, w=1]]) control points with w=weight. **/ -var NURBSSurface = function ( degree1, degree2, knots1, knots2 /* arrays of reals */, controlPoints /* array^2 of Vector(2|3|4) */ ) { +class NURBSSurface { - this.degree1 = degree1; - this.degree2 = degree2; - this.knots1 = knots1; - this.knots2 = knots2; - this.controlPoints = []; + constructor( degree1, degree2, knots1, knots2 /* arrays of reals */, controlPoints /* array^2 of Vector(2|3|4) */ ) { - var len1 = knots1.length - degree1 - 1; - var len2 = knots2.length - degree2 - 1; + this.degree1 = degree1; + this.degree2 = degree2; + this.knots1 = knots1; + this.knots2 = knots2; + this.controlPoints = []; - // ensure Vector4 for control points - for ( var i = 0; i < len1; ++ i ) { + const len1 = knots1.length - degree1 - 1; + const len2 = knots2.length - degree2 - 1; - this.controlPoints[ i ] = []; - for ( var j = 0; j < len2; ++ j ) { + // ensure Vector4 for control points + for ( let i = 0; i < len1; ++ i ) { - var point = controlPoints[ i ][ j ]; - this.controlPoints[ i ][ j ] = new Vector4( point.x, point.y, point.z, point.w ); + this.controlPoints[ i ] = []; - } - - } + for ( let j = 0; j < len2; ++ j ) { -}; + const point = controlPoints[ i ][ j ]; + this.controlPoints[ i ][ j ] = new Vector4( point.x, point.y, point.z, point.w ); + } -NURBSSurface.prototype = { + } - constructor: NURBSSurface, + } - getPoint: function ( t1, t2, target ) { + getPoint( t1, t2, target ) { - var u = this.knots1[ 0 ] + t1 * ( this.knots1[ this.knots1.length - 1 ] - this.knots1[ 0 ] ); // linear mapping t1->u - var v = this.knots2[ 0 ] + t2 * ( this.knots2[ this.knots2.length - 1 ] - this.knots2[ 0 ] ); // linear mapping t2->u + const u = this.knots1[ 0 ] + t1 * ( this.knots1[ this.knots1.length - 1 ] - this.knots1[ 0 ] ); // linear mapping t1->u + const v = this.knots2[ 0 ] + t2 * ( this.knots2[ this.knots2.length - 1 ] - this.knots2[ 0 ] ); // linear mapping t2->u NURBSUtils.calcSurfacePoint( this.degree1, this.degree2, this.knots1, this.knots2, this.controlPoints, u, v, target ); } -}; + +} export { NURBSSurface }; diff --git a/examples/jsm/curves/NURBSUtils.js b/examples/jsm/curves/NURBSUtils.js index d86acfc2736d69..40b4fc1b16c552 100644 --- a/examples/jsm/curves/NURBSUtils.js +++ b/examples/jsm/curves/NURBSUtils.js @@ -14,7 +14,7 @@ import { * NURBS Utils **************************************************************/ -var NURBSUtils = { +class NURBSUtils { /* Finds knot vector span. @@ -25,9 +25,9 @@ var NURBSUtils = { returns the span */ - findSpan: function ( p, u, U ) { + static findSpan( p, u, U ) { - var n = U.length - p - 1; + const n = U.length - p - 1; if ( u >= U[ n ] ) { @@ -41,9 +41,9 @@ var NURBSUtils = { } - var low = p; - var high = n; - var mid = Math.floor( ( low + high ) / 2 ); + let low = p; + let high = n; + let mid = Math.floor( ( low + high ) / 2 ); while ( u < U[ mid ] || u >= U[ mid + 1 ] ) { @@ -63,7 +63,7 @@ var NURBSUtils = { return mid; - }, + } /* @@ -76,25 +76,25 @@ var NURBSUtils = { returns array[p+1] with basis functions values. */ - calcBasisFunctions: function ( span, u, p, U ) { + static calcBasisFunctions( span, u, p, U ) { - var N = []; - var left = []; - var right = []; + const N = []; + const left = []; + const right = []; N[ 0 ] = 1.0; - for ( var j = 1; j <= p; ++ j ) { + for ( let j = 1; j <= p; ++ j ) { left[ j ] = u - U[ span + 1 - j ]; right[ j ] = U[ span + j ] - u; - var saved = 0.0; + let saved = 0.0; - for ( var r = 0; r < j; ++ r ) { + for ( let r = 0; r < j; ++ r ) { - var rv = right[ r + 1 ]; - var lv = left[ j - r ]; - var temp = N[ r ] / ( rv + lv ); + const rv = right[ r + 1 ]; + const lv = left[ j - r ]; + const temp = N[ r ] / ( rv + lv ); N[ r ] = saved + rv * temp; saved = lv * temp; @@ -106,7 +106,7 @@ var NURBSUtils = { return N; - }, + } /* @@ -119,17 +119,17 @@ var NURBSUtils = { returns point for given u */ - calcBSplinePoint: function ( p, U, P, u ) { + static calcBSplinePoint( p, U, P, u ) { - var span = this.findSpan( p, u, U ); - var N = this.calcBasisFunctions( span, u, p, U ); - var C = new Vector4( 0, 0, 0, 0 ); + const span = this.findSpan( p, u, U ); + const N = this.calcBasisFunctions( span, u, p, U ); + const C = new Vector4( 0, 0, 0, 0 ); - for ( var j = 0; j <= p; ++ j ) { + for ( let j = 0; j <= p; ++ j ) { - var point = P[ span - p + j ]; - var Nj = N[ j ]; - var wNj = point.w * Nj; + const point = P[ span - p + j ]; + const Nj = N[ j ]; + const wNj = point.w * Nj; C.x += point.x * wNj; C.y += point.y * wNj; C.z += point.z * wNj; @@ -139,7 +139,7 @@ var NURBSUtils = { return C; - }, + } /* @@ -153,39 +153,41 @@ var NURBSUtils = { returns array[n+1][p+1] with basis functions derivatives */ - calcBasisFunctionDerivatives: function ( span, u, p, n, U ) { + static calcBasisFunctionDerivatives( span, u, p, n, U ) { - var zeroArr = []; - for ( var i = 0; i <= p; ++ i ) + const zeroArr = []; + for ( let i = 0; i <= p; ++ i ) zeroArr[ i ] = 0.0; - var ders = []; - for ( var i = 0; i <= n; ++ i ) + const ders = []; + + for ( let i = 0; i <= n; ++ i ) ders[ i ] = zeroArr.slice( 0 ); - var ndu = []; - for ( var i = 0; i <= p; ++ i ) + const ndu = []; + + for ( let i = 0; i <= p; ++ i ) ndu[ i ] = zeroArr.slice( 0 ); ndu[ 0 ][ 0 ] = 1.0; - var left = zeroArr.slice( 0 ); - var right = zeroArr.slice( 0 ); + const left = zeroArr.slice( 0 ); + const right = zeroArr.slice( 0 ); - for ( var j = 1; j <= p; ++ j ) { + for ( let j = 1; j <= p; ++ j ) { left[ j ] = u - U[ span + 1 - j ]; right[ j ] = U[ span + j ] - u; - var saved = 0.0; + let saved = 0.0; - for ( var r = 0; r < j; ++ r ) { + for ( let r = 0; r < j; ++ r ) { - var rv = right[ r + 1 ]; - var lv = left[ j - r ]; + const rv = right[ r + 1 ]; + const lv = left[ j - r ]; ndu[ j ][ r ] = rv + lv; - var temp = ndu[ r ][ j - 1 ] / ndu[ j ][ r ]; + const temp = ndu[ r ][ j - 1 ] / ndu[ j ][ r ]; ndu[ r ][ j ] = saved + rv * temp; saved = lv * temp; @@ -195,19 +197,19 @@ var NURBSUtils = { } - for ( var j = 0; j <= p; ++ j ) { + for ( let j = 0; j <= p; ++ j ) { ders[ 0 ][ j ] = ndu[ j ][ p ]; } - for ( var r = 0; r <= p; ++ r ) { + for ( let r = 0; r <= p; ++ r ) { - var s1 = 0; - var s2 = 1; + let s1 = 0; + let s2 = 1; - var a = []; - for ( var i = 0; i <= p; ++ i ) { + const a = []; + for ( let i = 0; i <= p; ++ i ) { a[ i ] = zeroArr.slice( 0 ); @@ -215,11 +217,11 @@ var NURBSUtils = { a[ 0 ][ 0 ] = 1.0; - for ( var k = 1; k <= n; ++ k ) { + for ( let k = 1; k <= n; ++ k ) { - var d = 0.0; - var rk = r - k; - var pk = p - k; + let d = 0.0; + const rk = r - k; + const pk = p - k; if ( r >= k ) { @@ -228,10 +230,10 @@ var NURBSUtils = { } - var j1 = ( rk >= - 1 ) ? 1 : - rk; - var j2 = ( r - 1 <= pk ) ? k - 1 : p - r; + const j1 = ( rk >= - 1 ) ? 1 : - rk; + const j2 = ( r - 1 <= pk ) ? k - 1 : p - r; - for ( var j = j1; j <= j2; ++ j ) { + for ( let j = j1; j <= j2; ++ j ) { a[ s2 ][ j ] = ( a[ s1 ][ j ] - a[ s1 ][ j - 1 ] ) / ndu[ pk + 1 ][ rk + j ]; d += a[ s2 ][ j ] * ndu[ rk + j ][ pk ]; @@ -247,7 +249,7 @@ var NURBSUtils = { ders[ k ][ r ] = d; - var j = s1; + const j = s1; s1 = s2; s2 = j; @@ -255,11 +257,11 @@ var NURBSUtils = { } - var r = p; + const r = p; - for ( var k = 1; k <= n; ++ k ) { + for ( let k = 1; k <= n; ++ k ) { - for ( var j = 0; j <= p; ++ j ) { + for ( let j = 0; j <= p; ++ j ) { ders[ k ][ j ] *= r; @@ -271,7 +273,7 @@ var NURBSUtils = { return ders; - }, + } /* @@ -285,18 +287,18 @@ var NURBSUtils = { returns array[d+1] with derivatives */ - calcBSplineDerivatives: function ( p, U, P, u, nd ) { + static calcBSplineDerivatives( p, U, P, u, nd ) { - var du = nd < p ? nd : p; - var CK = []; - var span = this.findSpan( p, u, U ); - var nders = this.calcBasisFunctionDerivatives( span, u, p, du, U ); - var Pw = []; + const du = nd < p ? nd : p; + const CK = []; + const span = this.findSpan( p, u, U ); + const nders = this.calcBasisFunctionDerivatives( span, u, p, du, U ); + const Pw = []; - for ( var i = 0; i < P.length; ++ i ) { + for ( let i = 0; i < P.length; ++ i ) { - var point = P[ i ].clone(); - var w = point.w; + const point = P[ i ].clone(); + const w = point.w; point.x *= w; point.y *= w; @@ -306,11 +308,11 @@ var NURBSUtils = { } - for ( var k = 0; k <= du; ++ k ) { + for ( let k = 0; k <= du; ++ k ) { - var point = Pw[ span - p ].clone().multiplyScalar( nders[ k ][ 0 ] ); + const point = Pw[ span - p ].clone().multiplyScalar( nders[ k ][ 0 ] ); - for ( var j = 1; j <= p; ++ j ) { + for ( let j = 1; j <= p; ++ j ) { point.add( Pw[ span - p + j ].clone().multiplyScalar( nders[ k ][ j ] ) ); @@ -320,7 +322,7 @@ var NURBSUtils = { } - for ( var k = du + 1; k <= nd + 1; ++ k ) { + for ( let k = du + 1; k <= nd + 1; ++ k ) { CK[ k ] = new Vector4( 0, 0, 0 ); @@ -328,7 +330,7 @@ var NURBSUtils = { return CK; - }, + } /* @@ -336,25 +338,25 @@ var NURBSUtils = { returns k!/(i!(k-i)!) */ - calcKoverI: function ( k, i ) { + static calcKoverI( k, i ) { - var nom = 1; + let nom = 1; - for ( var j = 2; j <= k; ++ j ) { + for ( let j = 2; j <= k; ++ j ) { nom *= j; } - var denom = 1; + let denom = 1; - for ( var j = 2; j <= i; ++ j ) { + for ( let j = 2; j <= i; ++ j ) { denom *= j; } - for ( var j = 2; j <= k - i; ++ j ) { + for ( let j = 2; j <= k - i; ++ j ) { denom *= j; @@ -362,7 +364,7 @@ var NURBSUtils = { return nom / denom; - }, + } /* @@ -372,27 +374,27 @@ var NURBSUtils = { returns array with derivatives for rational curve. */ - calcRationalCurveDerivatives: function ( Pders ) { + static calcRationalCurveDerivatives( Pders ) { - var nd = Pders.length; - var Aders = []; - var wders = []; + const nd = Pders.length; + const Aders = []; + const wders = []; - for ( var i = 0; i < nd; ++ i ) { + for ( let i = 0; i < nd; ++ i ) { - var point = Pders[ i ]; + const point = Pders[ i ]; Aders[ i ] = new Vector3( point.x, point.y, point.z ); wders[ i ] = point.w; } - var CK = []; + const CK = []; - for ( var k = 0; k < nd; ++ k ) { + for ( let k = 0; k < nd; ++ k ) { - var v = Aders[ k ].clone(); + const v = Aders[ k ].clone(); - for ( var i = 1; i <= k; ++ i ) { + for ( let i = 1; i <= k; ++ i ) { v.sub( CK[ k - i ].clone().multiplyScalar( this.calcKoverI( k, i ) * wders[ i ] ) ); @@ -404,7 +406,7 @@ var NURBSUtils = { return CK; - }, + } /* @@ -418,12 +420,12 @@ var NURBSUtils = { returns array with derivatives. */ - calcNURBSDerivatives: function ( p, U, P, u, nd ) { + static calcNURBSDerivatives( p, U, P, u, nd ) { - var Pders = this.calcBSplineDerivatives( p, U, P, u, nd ); + const Pders = this.calcBSplineDerivatives( p, U, P, u, nd ); return this.calcRationalCurveDerivatives( Pders ); - }, + } /* @@ -436,21 +438,21 @@ var NURBSUtils = { returns point for given (u, v) */ - calcSurfacePoint: function ( p, q, U, V, P, u, v, target ) { + static calcSurfacePoint( p, q, U, V, P, u, v, target ) { - var uspan = this.findSpan( p, u, U ); - var vspan = this.findSpan( q, v, V ); - var Nu = this.calcBasisFunctions( uspan, u, p, U ); - var Nv = this.calcBasisFunctions( vspan, v, q, V ); - var temp = []; + const uspan = this.findSpan( p, u, U ); + const vspan = this.findSpan( q, v, V ); + const Nu = this.calcBasisFunctions( uspan, u, p, U ); + const Nv = this.calcBasisFunctions( vspan, v, q, V ); + const temp = []; - for ( var l = 0; l <= q; ++ l ) { + for ( let l = 0; l <= q; ++ l ) { temp[ l ] = new Vector4( 0, 0, 0, 0 ); - for ( var k = 0; k <= p; ++ k ) { + for ( let k = 0; k <= p; ++ k ) { - var point = P[ uspan - p + k ][ vspan - q + l ].clone(); - var w = point.w; + const point = P[ uspan - p + k ][ vspan - q + l ].clone(); + const w = point.w; point.x *= w; point.y *= w; point.z *= w; @@ -460,8 +462,8 @@ var NURBSUtils = { } - var Sw = new Vector4( 0, 0, 0, 0 ); - for ( var l = 0; l <= q; ++ l ) { + const Sw = new Vector4( 0, 0, 0, 0 ); + for ( let l = 0; l <= q; ++ l ) { Sw.add( temp[ l ].multiplyScalar( Nv[ l ] ) ); @@ -472,6 +474,6 @@ var NURBSUtils = { } -}; +} export { NURBSUtils };