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

Volumetric NURBS #27601

Merged
merged 10 commits into from
Jan 23, 2024
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
1 change: 1 addition & 0 deletions examples/jsm/Addons.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export * from './csm/CSMShader.js';
export * as Curves from './curves/CurveExtras.js';
export * from './curves/NURBSCurve.js';
export * from './curves/NURBSSurface.js';
export * from './curves/NURBSVolume.js';
export * as NURBSUtils from './curves/NURBSUtils.js';

export * from './effects/AnaglyphEffect.js';
Expand Down
63 changes: 59 additions & 4 deletions examples/jsm/curves/NURBSUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -429,10 +429,10 @@ function calcNURBSDerivatives( p, U, P, u, nd ) {
/*
Calculate rational B-Spline surface point. See The NURBS Book, page 134, algorithm A4.3.

p1, p2 : degrees of B-Spline surface
U1, U2 : knot vectors
P : control points (x, y, z, w)
u, v : parametric values
p, q : degrees of B-Spline surface
U, V : knot vectors
P : control points (x, y, z, w)
u, v : parametric values

returns point for given (u, v)
*/
Expand Down Expand Up @@ -472,6 +472,60 @@ function calcSurfacePoint( p, q, U, V, P, u, v, target ) {

}

/*
Calculate rational B-Spline volume point. See The NURBS Book, page 134, algorithm A4.3.

p, q, r : degrees of B-Splinevolume
U, V, W : knot vectors
P : control points (x, y, z, w)
u, v, w : parametric values

returns point for given (u, v, w)
*/
function calcVolumePoint( p, q, r, U, V, W, P, u, v, w, target ) {

const uspan = findSpan( p, u, U );
const vspan = findSpan( q, v, V );
const wspan = findSpan( r, w, W );
const Nu = calcBasisFunctions( uspan, u, p, U );
const Nv = calcBasisFunctions( vspan, v, q, V );
const Nw = calcBasisFunctions( wspan, w, r, W );
const temp = [];

for ( let m = 0; m <= r; ++ m ) {

temp[ m ] = [];

for ( let l = 0; l <= q; ++ l ) {

temp[ m ][ l ] = new Vector4( 0, 0, 0, 0 );
for ( let k = 0; k <= p; ++ k ) {

const point = P[ uspan - p + k ][ vspan - q + l ][ wspan - r + m ].clone();
const w = point.w;
point.x *= w;
point.y *= w;
point.z *= w;
temp[ m ][ l ].add( point.multiplyScalar( Nu[ k ] ) );

}

}

}
const Sw = new Vector4( 0, 0, 0, 0 );
for ( let m = 0; m <= r; ++ m ) {
for ( let l = 0; l <= q; ++ l ) {

Sw.add( temp[ m ][ l ].multiplyScalar( Nw[ m ] ).multiplyScalar( Nv[ l ] ) );

}
}

Sw.divideScalar( Sw.w );
target.set( Sw.x, Sw.y, Sw.z );

}


export {
Expand All @@ -484,4 +538,5 @@ export {
calcRationalCurveDerivatives,
calcNURBSDerivatives,
calcSurfacePoint,
calcVolumePoint,
};
62 changes: 62 additions & 0 deletions examples/jsm/curves/NURBSVolume.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import {
Vector4
} from 'three';
import * as NURBSUtils from '../curves/NURBSUtils.js';

/**
* NURBS volume object
*
* Implementation is based on (x, y, z [, w=1]]) control points with w=weight.
**/

class NURBSVolume {

constructor( degree1, degree2, degree3, knots1, knots2, knots3 /* arrays of reals */, controlPoints /* array^3 of Vector(2|3|4) */ ) {

this.degree1 = degree1;
this.degree2 = degree2;
this.degree3 = degree3;
this.knots1 = knots1;
this.knots2 = knots2;
this.knots3 = knots3;
this.controlPoints = [];

const len1 = knots1.length - degree1 - 1;
const len2 = knots2.length - degree2 - 1;
const len3 = knots3.length - degree3 - 1;

// ensure Vector4 for control points
for ( let i = 0; i < len1; ++ i ) {

this.controlPoints[ i ] = [];

for ( let j = 0; j < len2; ++ j ) {

this.controlPoints[ i ][ j ] = [];

for ( let k = 0; k < len3; ++ k ) {

const point = controlPoints[ i ][ j ][ k ];
this.controlPoints[ i ][ j ][ k ] = new Vector4( point.x, point.y, point.z, point.w );

}

}

}

}

getPoint( t1, t2, t3, target ) {

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->v
const w = this.knots3[ 0 ] + t3 * ( this.knots3[ this.knots3.length - 1 ] - this.knots3[ 0 ] ); // linear mapping t3->w

NURBSUtils.calcVolumePoint( this.degree1, this.degree2, this.degree3, this.knots1, this.knots2, this.knots3, this.controlPoints, u, v, w, target );

}

}

export { NURBSVolume };
Binary file modified examples/screenshots/webgl_geometry_nurbs.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading