NURBSCurve.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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
  12. /* array of reals */
  13. , controlPoints
  14. /* array of Vector(2|3|4) */
  15. , startKnot
  16. /* index in knots */
  17. , endKnot
  18. /* index in knots */
  19. ) {
  20. super();
  21. this.degree = degree;
  22. this.knots = knots;
  23. this.controlPoints = []; // Used by periodic NURBS to remove hidden spans
  24. this.startKnot = startKnot || 0;
  25. this.endKnot = endKnot || this.knots.length - 1;
  26. for ( let i = 0; i < controlPoints.length; ++ i ) {
  27. // ensure THREE.Vector4 for control points
  28. const point = controlPoints[ i ];
  29. this.controlPoints[ i ] = new THREE.Vector4( point.x, point.y, point.z, point.w );
  30. }
  31. }
  32. getPoint( t, optionalTarget = new THREE.Vector3() ) {
  33. const point = optionalTarget;
  34. const u = this.knots[ this.startKnot ] + t * ( this.knots[ this.endKnot ] - this.knots[ this.startKnot ] ); // linear mapping t->u
  35. // following results in (wx, wy, wz, w) homogeneous point
  36. const hpoint = THREE.NURBSUtils.calcBSplinePoint( this.degree, this.knots, this.controlPoints, u );
  37. if ( hpoint.w !== 1.0 ) {
  38. // project to 3D space: (wx, wy, wz, w) -> (x, y, z, 1)
  39. hpoint.divideScalar( hpoint.w );
  40. }
  41. return point.set( hpoint.x, hpoint.y, hpoint.z );
  42. }
  43. getTangent( t, optionalTarget = new THREE.Vector3() ) {
  44. const tangent = optionalTarget;
  45. const u = this.knots[ 0 ] + t * ( this.knots[ this.knots.length - 1 ] - this.knots[ 0 ] );
  46. const ders = THREE.NURBSUtils.calcNURBSDerivatives( this.degree, this.knots, this.controlPoints, u, 1 );
  47. tangent.copy( ders[ 1 ] ).normalize();
  48. return tangent;
  49. }
  50. }
  51. THREE.NURBSCurve = NURBSCurve;
  52. } )();