最新代码
This commit is contained in:
422
public/sdk/three/jsm/curves/CurveExtras.js
Normal file
422
public/sdk/three/jsm/curves/CurveExtras.js
Normal file
@ -0,0 +1,422 @@
|
||||
import {
|
||||
Curve,
|
||||
Vector3
|
||||
} from 'three';
|
||||
|
||||
/**
|
||||
* A bunch of parametric curves
|
||||
*
|
||||
* Formulas collected from various sources
|
||||
* http://mathworld.wolfram.com/HeartCurve.html
|
||||
* http://en.wikipedia.org/wiki/Viviani%27s_curve
|
||||
* http://www.mi.sanu.ac.rs/vismath/taylorapril2011/Taylor.pdf
|
||||
* https://prideout.net/blog/old/blog/index.html@p=44.html
|
||||
*/
|
||||
|
||||
// GrannyKnot
|
||||
|
||||
class GrannyKnot extends Curve {
|
||||
|
||||
getPoint( t, optionalTarget = new Vector3() ) {
|
||||
|
||||
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 );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// HeartCurve
|
||||
|
||||
class HeartCurve extends Curve {
|
||||
|
||||
constructor( scale = 5 ) {
|
||||
|
||||
super();
|
||||
|
||||
this.scale = scale;
|
||||
|
||||
}
|
||||
|
||||
getPoint( t, optionalTarget = new Vector3() ) {
|
||||
|
||||
const point = optionalTarget;
|
||||
|
||||
t *= 2 * Math.PI;
|
||||
|
||||
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
|
||||
|
||||
class VivianiCurve extends Curve {
|
||||
|
||||
constructor( scale = 70 ) {
|
||||
|
||||
super();
|
||||
|
||||
this.scale = scale;
|
||||
|
||||
}
|
||||
|
||||
getPoint( t, optionalTarget = new Vector3() ) {
|
||||
|
||||
const point = optionalTarget;
|
||||
|
||||
t = t * 4 * Math.PI; // normalized to 0..1
|
||||
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
|
||||
|
||||
class KnotCurve extends Curve {
|
||||
|
||||
getPoint( t, optionalTarget = new Vector3() ) {
|
||||
|
||||
const point = optionalTarget;
|
||||
|
||||
t *= 2 * Math.PI;
|
||||
|
||||
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
|
||||
|
||||
class HelixCurve extends Curve {
|
||||
|
||||
getPoint( t, optionalTarget = new Vector3() ) {
|
||||
|
||||
const point = optionalTarget;
|
||||
|
||||
const a = 30; // radius
|
||||
const b = 150; // height
|
||||
|
||||
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 );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// TrefoilKnot
|
||||
|
||||
class TrefoilKnot extends Curve {
|
||||
|
||||
constructor( scale = 10 ) {
|
||||
|
||||
super();
|
||||
|
||||
this.scale = scale;
|
||||
|
||||
}
|
||||
|
||||
getPoint( t, optionalTarget = new Vector3() ) {
|
||||
|
||||
const point = optionalTarget;
|
||||
|
||||
t *= Math.PI * 2;
|
||||
|
||||
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
|
||||
|
||||
class TorusKnot extends Curve {
|
||||
|
||||
constructor( scale = 10 ) {
|
||||
|
||||
super();
|
||||
|
||||
this.scale = scale;
|
||||
|
||||
}
|
||||
|
||||
getPoint( t, optionalTarget = new Vector3() ) {
|
||||
|
||||
const point = optionalTarget;
|
||||
|
||||
const p = 3;
|
||||
const q = 4;
|
||||
|
||||
t *= Math.PI * 2;
|
||||
|
||||
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
|
||||
|
||||
class CinquefoilKnot extends Curve {
|
||||
|
||||
constructor( scale = 10 ) {
|
||||
|
||||
super();
|
||||
|
||||
this.scale = scale;
|
||||
|
||||
}
|
||||
|
||||
getPoint( t, optionalTarget = new Vector3() ) {
|
||||
|
||||
const point = optionalTarget;
|
||||
|
||||
const p = 2;
|
||||
const q = 5;
|
||||
|
||||
t *= Math.PI * 2;
|
||||
|
||||
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
|
||||
|
||||
class TrefoilPolynomialKnot extends Curve {
|
||||
|
||||
constructor( scale = 10 ) {
|
||||
|
||||
super();
|
||||
|
||||
this.scale = scale;
|
||||
|
||||
}
|
||||
|
||||
getPoint( t, optionalTarget = new Vector3() ) {
|
||||
|
||||
const point = optionalTarget;
|
||||
|
||||
t = t * 4 - 2;
|
||||
|
||||
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 );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function scaleTo( x, y, t ) {
|
||||
|
||||
const r = y - x;
|
||||
return t * r + x;
|
||||
|
||||
}
|
||||
|
||||
// FigureEightPolynomialKnot
|
||||
|
||||
class FigureEightPolynomialKnot extends Curve {
|
||||
|
||||
constructor( scale = 1 ) {
|
||||
|
||||
super();
|
||||
|
||||
this.scale = scale;
|
||||
|
||||
}
|
||||
|
||||
getPoint( t, optionalTarget = new Vector3() ) {
|
||||
|
||||
const point = optionalTarget;
|
||||
|
||||
t = scaleTo( - 4, 4, t );
|
||||
|
||||
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
|
||||
|
||||
class DecoratedTorusKnot4a extends Curve {
|
||||
|
||||
constructor( scale = 40 ) {
|
||||
|
||||
super();
|
||||
|
||||
this.scale = scale;
|
||||
|
||||
}
|
||||
|
||||
getPoint( t, optionalTarget = new Vector3() ) {
|
||||
|
||||
const point = optionalTarget;
|
||||
|
||||
t *= Math.PI * 2;
|
||||
|
||||
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
|
||||
|
||||
class DecoratedTorusKnot4b extends Curve {
|
||||
|
||||
constructor( scale = 40 ) {
|
||||
|
||||
super();
|
||||
|
||||
this.scale = scale;
|
||||
|
||||
}
|
||||
|
||||
getPoint( t, optionalTarget = new Vector3() ) {
|
||||
|
||||
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
|
||||
|
||||
class DecoratedTorusKnot5a extends Curve {
|
||||
|
||||
constructor( scale = 40 ) {
|
||||
|
||||
super();
|
||||
|
||||
this.scale = scale;
|
||||
|
||||
}
|
||||
|
||||
getPoint( t, optionalTarget = new Vector3() ) {
|
||||
|
||||
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
|
||||
|
||||
class DecoratedTorusKnot5c extends Curve {
|
||||
|
||||
constructor( scale = 40 ) {
|
||||
|
||||
super();
|
||||
|
||||
this.scale = scale;
|
||||
|
||||
}
|
||||
|
||||
getPoint( t, optionalTarget = new Vector3() ) {
|
||||
|
||||
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 );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export {
|
||||
GrannyKnot,
|
||||
HeartCurve,
|
||||
VivianiCurve,
|
||||
KnotCurve,
|
||||
HelixCurve,
|
||||
TrefoilKnot,
|
||||
TorusKnot,
|
||||
CinquefoilKnot,
|
||||
TrefoilPolynomialKnot,
|
||||
FigureEightPolynomialKnot,
|
||||
DecoratedTorusKnot4a,
|
||||
DecoratedTorusKnot4b,
|
||||
DecoratedTorusKnot5a,
|
||||
DecoratedTorusKnot5c
|
||||
};
|
80
public/sdk/three/jsm/curves/NURBSCurve.js
Normal file
80
public/sdk/three/jsm/curves/NURBSCurve.js
Normal file
@ -0,0 +1,80 @@
|
||||
import {
|
||||
Curve,
|
||||
Vector3,
|
||||
Vector4
|
||||
} from 'three';
|
||||
import * as NURBSUtils from '../curves/NURBSUtils.js';
|
||||
|
||||
/**
|
||||
* NURBS curve object
|
||||
*
|
||||
* Derives from Curve, overriding getPoint and getTangent.
|
||||
*
|
||||
* Implementation is based on (x, y [, z=0 [, w=1]]) control points with w=weight.
|
||||
*
|
||||
**/
|
||||
|
||||
class NURBSCurve extends 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 Vector4 for control points
|
||||
const point = controlPoints[ i ];
|
||||
this.controlPoints[ i ] = new Vector4( point.x, point.y, point.z, point.w );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
getPoint( t, optionalTarget = new Vector3() ) {
|
||||
|
||||
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
|
||||
const hpoint = NURBSUtils.calcBSplinePoint( this.degree, this.knots, this.controlPoints, u );
|
||||
|
||||
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 );
|
||||
|
||||
}
|
||||
|
||||
getTangent( t, optionalTarget = new Vector3() ) {
|
||||
|
||||
const tangent = optionalTarget;
|
||||
|
||||
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();
|
||||
|
||||
return tangent;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export { NURBSCurve };
|
52
public/sdk/three/jsm/curves/NURBSSurface.js
Normal file
52
public/sdk/three/jsm/curves/NURBSSurface.js
Normal file
@ -0,0 +1,52 @@
|
||||
import {
|
||||
Vector4
|
||||
} from 'three';
|
||||
import * as NURBSUtils from '../curves/NURBSUtils.js';
|
||||
|
||||
/**
|
||||
* NURBS surface object
|
||||
*
|
||||
* Implementation is based on (x, y [, z=0 [, w=1]]) control points with w=weight.
|
||||
**/
|
||||
|
||||
class NURBSSurface {
|
||||
|
||||
constructor( degree1, degree2, knots1, knots2 /* arrays of reals */, controlPoints /* array^2 of Vector(2|3|4) */ ) {
|
||||
|
||||
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 Vector4 for control points
|
||||
for ( let i = 0; i < len1; ++ i ) {
|
||||
|
||||
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 );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
getPoint( t1, t2, 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->u
|
||||
|
||||
NURBSUtils.calcSurfacePoint( this.degree1, this.degree2, this.knots1, this.knots2, this.controlPoints, u, v, target );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export { NURBSSurface };
|
542
public/sdk/three/jsm/curves/NURBSUtils.js
Normal file
542
public/sdk/three/jsm/curves/NURBSUtils.js
Normal file
@ -0,0 +1,542 @@
|
||||
import {
|
||||
Vector3,
|
||||
Vector4
|
||||
} from 'three';
|
||||
|
||||
/**
|
||||
* NURBS utils
|
||||
*
|
||||
* See NURBSCurve and NURBSSurface.
|
||||
**/
|
||||
|
||||
|
||||
/**************************************************************
|
||||
* NURBS Utils
|
||||
**************************************************************/
|
||||
|
||||
/*
|
||||
Finds knot vector span.
|
||||
|
||||
p : degree
|
||||
u : parametric value
|
||||
U : knot vector
|
||||
|
||||
returns the span
|
||||
*/
|
||||
function findSpan( p, u, U ) {
|
||||
|
||||
const n = U.length - p - 1;
|
||||
|
||||
if ( u >= U[ n ] ) {
|
||||
|
||||
return n - 1;
|
||||
|
||||
}
|
||||
|
||||
if ( u <= U[ p ] ) {
|
||||
|
||||
return p;
|
||||
|
||||
}
|
||||
|
||||
let low = p;
|
||||
let high = n;
|
||||
let mid = Math.floor( ( low + high ) / 2 );
|
||||
|
||||
while ( u < U[ mid ] || u >= U[ mid + 1 ] ) {
|
||||
|
||||
if ( u < U[ mid ] ) {
|
||||
|
||||
high = mid;
|
||||
|
||||
} else {
|
||||
|
||||
low = mid;
|
||||
|
||||
}
|
||||
|
||||
mid = Math.floor( ( low + high ) / 2 );
|
||||
|
||||
}
|
||||
|
||||
return mid;
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
Calculate basis functions. See The NURBS Book, page 70, algorithm A2.2
|
||||
|
||||
span : span in which u lies
|
||||
u : parametric point
|
||||
p : degree
|
||||
U : knot vector
|
||||
|
||||
returns array[p+1] with basis functions values.
|
||||
*/
|
||||
function calcBasisFunctions( span, u, p, U ) {
|
||||
|
||||
const N = [];
|
||||
const left = [];
|
||||
const right = [];
|
||||
N[ 0 ] = 1.0;
|
||||
|
||||
for ( let j = 1; j <= p; ++ j ) {
|
||||
|
||||
left[ j ] = u - U[ span + 1 - j ];
|
||||
right[ j ] = U[ span + j ] - u;
|
||||
|
||||
let saved = 0.0;
|
||||
|
||||
for ( let r = 0; r < j; ++ r ) {
|
||||
|
||||
const rv = right[ r + 1 ];
|
||||
const lv = left[ j - r ];
|
||||
const temp = N[ r ] / ( rv + lv );
|
||||
N[ r ] = saved + rv * temp;
|
||||
saved = lv * temp;
|
||||
|
||||
}
|
||||
|
||||
N[ j ] = saved;
|
||||
|
||||
}
|
||||
|
||||
return N;
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
Calculate B-Spline curve points. See The NURBS Book, page 82, algorithm A3.1.
|
||||
|
||||
p : degree of B-Spline
|
||||
U : knot vector
|
||||
P : control points (x, y, z, w)
|
||||
u : parametric point
|
||||
|
||||
returns point for given u
|
||||
*/
|
||||
function calcBSplinePoint( p, U, P, u ) {
|
||||
|
||||
const span = findSpan( p, u, U );
|
||||
const N = calcBasisFunctions( span, u, p, U );
|
||||
const C = new Vector4( 0, 0, 0, 0 );
|
||||
|
||||
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;
|
||||
C.w += point.w * Nj;
|
||||
|
||||
}
|
||||
|
||||
return C;
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
Calculate basis functions derivatives. See The NURBS Book, page 72, algorithm A2.3.
|
||||
|
||||
span : span in which u lies
|
||||
u : parametric point
|
||||
p : degree
|
||||
n : number of derivatives to calculate
|
||||
U : knot vector
|
||||
|
||||
returns array[n+1][p+1] with basis functions derivatives
|
||||
*/
|
||||
function calcBasisFunctionDerivatives( span, u, p, n, U ) {
|
||||
|
||||
const zeroArr = [];
|
||||
for ( let i = 0; i <= p; ++ i )
|
||||
zeroArr[ i ] = 0.0;
|
||||
|
||||
const ders = [];
|
||||
|
||||
for ( let i = 0; i <= n; ++ i )
|
||||
ders[ i ] = zeroArr.slice( 0 );
|
||||
|
||||
const ndu = [];
|
||||
|
||||
for ( let i = 0; i <= p; ++ i )
|
||||
ndu[ i ] = zeroArr.slice( 0 );
|
||||
|
||||
ndu[ 0 ][ 0 ] = 1.0;
|
||||
|
||||
const left = zeroArr.slice( 0 );
|
||||
const right = zeroArr.slice( 0 );
|
||||
|
||||
for ( let j = 1; j <= p; ++ j ) {
|
||||
|
||||
left[ j ] = u - U[ span + 1 - j ];
|
||||
right[ j ] = U[ span + j ] - u;
|
||||
|
||||
let saved = 0.0;
|
||||
|
||||
for ( let r = 0; r < j; ++ r ) {
|
||||
|
||||
const rv = right[ r + 1 ];
|
||||
const lv = left[ j - r ];
|
||||
ndu[ j ][ r ] = rv + lv;
|
||||
|
||||
const temp = ndu[ r ][ j - 1 ] / ndu[ j ][ r ];
|
||||
ndu[ r ][ j ] = saved + rv * temp;
|
||||
saved = lv * temp;
|
||||
|
||||
}
|
||||
|
||||
ndu[ j ][ j ] = saved;
|
||||
|
||||
}
|
||||
|
||||
for ( let j = 0; j <= p; ++ j ) {
|
||||
|
||||
ders[ 0 ][ j ] = ndu[ j ][ p ];
|
||||
|
||||
}
|
||||
|
||||
for ( let r = 0; r <= p; ++ r ) {
|
||||
|
||||
let s1 = 0;
|
||||
let s2 = 1;
|
||||
|
||||
const a = [];
|
||||
for ( let i = 0; i <= p; ++ i ) {
|
||||
|
||||
a[ i ] = zeroArr.slice( 0 );
|
||||
|
||||
}
|
||||
|
||||
a[ 0 ][ 0 ] = 1.0;
|
||||
|
||||
for ( let k = 1; k <= n; ++ k ) {
|
||||
|
||||
let d = 0.0;
|
||||
const rk = r - k;
|
||||
const pk = p - k;
|
||||
|
||||
if ( r >= k ) {
|
||||
|
||||
a[ s2 ][ 0 ] = a[ s1 ][ 0 ] / ndu[ pk + 1 ][ rk ];
|
||||
d = a[ s2 ][ 0 ] * ndu[ rk ][ pk ];
|
||||
|
||||
}
|
||||
|
||||
const j1 = ( rk >= - 1 ) ? 1 : - rk;
|
||||
const j2 = ( r - 1 <= pk ) ? k - 1 : p - r;
|
||||
|
||||
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 ];
|
||||
|
||||
}
|
||||
|
||||
if ( r <= pk ) {
|
||||
|
||||
a[ s2 ][ k ] = - a[ s1 ][ k - 1 ] / ndu[ pk + 1 ][ r ];
|
||||
d += a[ s2 ][ k ] * ndu[ r ][ pk ];
|
||||
|
||||
}
|
||||
|
||||
ders[ k ][ r ] = d;
|
||||
|
||||
const j = s1;
|
||||
s1 = s2;
|
||||
s2 = j;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
let r = p;
|
||||
|
||||
for ( let k = 1; k <= n; ++ k ) {
|
||||
|
||||
for ( let j = 0; j <= p; ++ j ) {
|
||||
|
||||
ders[ k ][ j ] *= r;
|
||||
|
||||
}
|
||||
|
||||
r *= p - k;
|
||||
|
||||
}
|
||||
|
||||
return ders;
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
Calculate derivatives of a B-Spline. See The NURBS Book, page 93, algorithm A3.2.
|
||||
|
||||
p : degree
|
||||
U : knot vector
|
||||
P : control points
|
||||
u : Parametric points
|
||||
nd : number of derivatives
|
||||
|
||||
returns array[d+1] with derivatives
|
||||
*/
|
||||
function calcBSplineDerivatives( p, U, P, u, nd ) {
|
||||
|
||||
const du = nd < p ? nd : p;
|
||||
const CK = [];
|
||||
const span = findSpan( p, u, U );
|
||||
const nders = calcBasisFunctionDerivatives( span, u, p, du, U );
|
||||
const Pw = [];
|
||||
|
||||
for ( let i = 0; i < P.length; ++ i ) {
|
||||
|
||||
const point = P[ i ].clone();
|
||||
const w = point.w;
|
||||
|
||||
point.x *= w;
|
||||
point.y *= w;
|
||||
point.z *= w;
|
||||
|
||||
Pw[ i ] = point;
|
||||
|
||||
}
|
||||
|
||||
for ( let k = 0; k <= du; ++ k ) {
|
||||
|
||||
const point = Pw[ span - p ].clone().multiplyScalar( nders[ k ][ 0 ] );
|
||||
|
||||
for ( let j = 1; j <= p; ++ j ) {
|
||||
|
||||
point.add( Pw[ span - p + j ].clone().multiplyScalar( nders[ k ][ j ] ) );
|
||||
|
||||
}
|
||||
|
||||
CK[ k ] = point;
|
||||
|
||||
}
|
||||
|
||||
for ( let k = du + 1; k <= nd + 1; ++ k ) {
|
||||
|
||||
CK[ k ] = new Vector4( 0, 0, 0 );
|
||||
|
||||
}
|
||||
|
||||
return CK;
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
Calculate "K over I"
|
||||
|
||||
returns k!/(i!(k-i)!)
|
||||
*/
|
||||
function calcKoverI( k, i ) {
|
||||
|
||||
let nom = 1;
|
||||
|
||||
for ( let j = 2; j <= k; ++ j ) {
|
||||
|
||||
nom *= j;
|
||||
|
||||
}
|
||||
|
||||
let denom = 1;
|
||||
|
||||
for ( let j = 2; j <= i; ++ j ) {
|
||||
|
||||
denom *= j;
|
||||
|
||||
}
|
||||
|
||||
for ( let j = 2; j <= k - i; ++ j ) {
|
||||
|
||||
denom *= j;
|
||||
|
||||
}
|
||||
|
||||
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.
|
||||
*/
|
||||
function calcRationalCurveDerivatives( Pders ) {
|
||||
|
||||
const nd = Pders.length;
|
||||
const Aders = [];
|
||||
const wders = [];
|
||||
|
||||
for ( let i = 0; i < nd; ++ i ) {
|
||||
|
||||
const point = Pders[ i ];
|
||||
Aders[ i ] = new Vector3( point.x, point.y, point.z );
|
||||
wders[ i ] = point.w;
|
||||
|
||||
}
|
||||
|
||||
const CK = [];
|
||||
|
||||
for ( let k = 0; k < nd; ++ k ) {
|
||||
|
||||
const v = Aders[ k ].clone();
|
||||
|
||||
for ( let i = 1; i <= k; ++ i ) {
|
||||
|
||||
v.sub( CK[ k - i ].clone().multiplyScalar( calcKoverI( k, i ) * wders[ i ] ) );
|
||||
|
||||
}
|
||||
|
||||
CK[ k ] = v.divideScalar( wders[ 0 ] );
|
||||
|
||||
}
|
||||
|
||||
return CK;
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
Calculate NURBS curve derivatives. See The NURBS Book, page 127, algorithm A4.2.
|
||||
|
||||
p : degree
|
||||
U : knot vector
|
||||
P : control points in homogeneous space
|
||||
u : parametric points
|
||||
nd : number of derivatives
|
||||
|
||||
returns array with derivatives.
|
||||
*/
|
||||
function calcNURBSDerivatives( p, U, P, u, nd ) {
|
||||
|
||||
const Pders = calcBSplineDerivatives( p, U, P, u, nd );
|
||||
return calcRationalCurveDerivatives( Pders );
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
Calculate rational B-Spline surface point. See The NURBS Book, page 134, algorithm A4.3.
|
||||
|
||||
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)
|
||||
*/
|
||||
function calcSurfacePoint( p, q, U, V, P, u, v, target ) {
|
||||
|
||||
const uspan = findSpan( p, u, U );
|
||||
const vspan = findSpan( q, v, V );
|
||||
const Nu = calcBasisFunctions( uspan, u, p, U );
|
||||
const Nv = calcBasisFunctions( vspan, v, q, V );
|
||||
const temp = [];
|
||||
|
||||
for ( let l = 0; l <= q; ++ l ) {
|
||||
|
||||
temp[ l ] = new Vector4( 0, 0, 0, 0 );
|
||||
for ( let k = 0; k <= p; ++ k ) {
|
||||
|
||||
const point = P[ uspan - p + k ][ vspan - q + l ].clone();
|
||||
const w = point.w;
|
||||
point.x *= w;
|
||||
point.y *= w;
|
||||
point.z *= w;
|
||||
temp[ l ].add( point.multiplyScalar( Nu[ k ] ) );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
const Sw = new Vector4( 0, 0, 0, 0 );
|
||||
for ( let l = 0; l <= q; ++ l ) {
|
||||
|
||||
Sw.add( temp[ l ].multiplyScalar( Nv[ l ] ) );
|
||||
|
||||
}
|
||||
|
||||
Sw.divideScalar( Sw.w );
|
||||
target.set( Sw.x, Sw.y, Sw.z );
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
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 {
|
||||
findSpan,
|
||||
calcBasisFunctions,
|
||||
calcBSplinePoint,
|
||||
calcBasisFunctionDerivatives,
|
||||
calcBSplineDerivatives,
|
||||
calcKoverI,
|
||||
calcRationalCurveDerivatives,
|
||||
calcNURBSDerivatives,
|
||||
calcSurfacePoint,
|
||||
calcVolumePoint,
|
||||
};
|
62
public/sdk/three/jsm/curves/NURBSVolume.js
Normal file
62
public/sdk/three/jsm/curves/NURBSVolume.js
Normal file
@ -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 };
|
Reference in New Issue
Block a user