NURBSCurve.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. ( function () {
  2. /**
  3. * NURBS curve object
  4. *
  5. * Derives from THREE.Curve, overriding getPoint and getTangent.
  6. *
  7. * Implementation is based on (x, y [, z=0 [, w=1]]) control points with w=weight.
  8. *
  9. **/
  10. class NURBSCurve extends THREE.Curve {
  11. constructor( degree, knots /* array of reals */, controlPoints /* array of Vector(2|3|4) */, startKnot /* index in knots */, endKnot /* index in knots */ ) {
  12. super();
  13. this.degree = degree;
  14. this.knots = knots;
  15. this.controlPoints = [];
  16. // Used by periodic NURBS to remove hidden spans
  17. this.startKnot = startKnot || 0;
  18. this.endKnot = endKnot || this.knots.length - 1;
  19. for ( let i = 0; i < controlPoints.length; ++ i ) {
  20. // ensure THREE.Vector4 for control points
  21. const point = controlPoints[ i ];
  22. this.controlPoints[ i ] = new THREE.Vector4( point.x, point.y, point.z, point.w );
  23. }
  24. }
  25. getPoint( t, optionalTarget = new THREE.Vector3() ) {
  26. const point = optionalTarget;
  27. const u = this.knots[ this.startKnot ] + t * ( this.knots[ this.endKnot ] - this.knots[ this.startKnot ] ); // linear mapping t->u
  28. // following results in (wx, wy, wz, w) homogeneous point
  29. const hpoint = THREE.NURBSUtils.calcBSplinePoint( this.degree, this.knots, this.controlPoints, u );
  30. if ( hpoint.w !== 1.0 ) {
  31. // project to 3D space: (wx, wy, wz, w) -> (x, y, z, 1)
  32. hpoint.divideScalar( hpoint.w );
  33. }
  34. return point.set( hpoint.x, hpoint.y, hpoint.z );
  35. }
  36. getTangent( t, optionalTarget = new THREE.Vector3() ) {
  37. const tangent = optionalTarget;
  38. const u = this.knots[ 0 ] + t * ( this.knots[ this.knots.length - 1 ] - this.knots[ 0 ] );
  39. const ders = THREE.NURBSUtils.calcNURBSDerivatives( this.degree, this.knots, this.controlPoints, u, 1 );
  40. tangent.copy( ders[ 1 ] ).normalize();
  41. return tangent;
  42. }
  43. }
  44. THREE.NURBSCurve = NURBSCurve;
  45. } )();