Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Examples: Convert curves to ES6. #21593

Merged
merged 1 commit into from
Apr 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
410 changes: 201 additions & 209 deletions examples/js/curves/CurveExtras.js

Large diffs are not rendered by default.

89 changes: 45 additions & 44 deletions examples/js/curves/NURBSCurve.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
51 changes: 26 additions & 25 deletions examples/js/curves/NURBSSurface.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Loading