NURBSCurve.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /**
  2. * @author renej
  3. * NURBS curve object
  4. *
  5. * Derives from 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. import {
  11. Curve,
  12. Vector3,
  13. Vector4
  14. } from "../../../build/three.module.js";
  15. import { NURBSUtils } from "../curves/NURBSUtils.js";
  16. /**************************************************************
  17. * NURBS curve
  18. **************************************************************/
  19. var NURBSCurve = function ( degree, knots /* array of reals */, controlPoints /* array of Vector(2|3|4) */, startKnot /* index in knots */, endKnot /* index in knots */ ) {
  20. Curve.call( this );
  21. this.degree = degree;
  22. this.knots = knots;
  23. this.controlPoints = [];
  24. // Used by periodic NURBS to remove hidden spans
  25. this.startKnot = startKnot || 0;
  26. this.endKnot = endKnot || ( this.knots.length - 1 );
  27. for ( var i = 0; i < controlPoints.length; ++ i ) {
  28. // ensure Vector4 for control points
  29. var point = controlPoints[ i ];
  30. this.controlPoints[ i ] = new Vector4( point.x, point.y, point.z, point.w );
  31. }
  32. };
  33. NURBSCurve.prototype = Object.create( Curve.prototype );
  34. NURBSCurve.prototype.constructor = NURBSCurve;
  35. NURBSCurve.prototype.getPoint = function ( t, optionalTarget ) {
  36. var point = optionalTarget || new Vector3();
  37. var u = this.knots[ this.startKnot ] + t * ( this.knots[ this.endKnot ] - this.knots[ this.startKnot ] ); // linear mapping t->u
  38. // following results in (wx, wy, wz, w) homogeneous point
  39. var hpoint = NURBSUtils.calcBSplinePoint( this.degree, this.knots, this.controlPoints, u );
  40. if ( hpoint.w != 1.0 ) {
  41. // project to 3D space: (wx, wy, wz, w) -> (x, y, z, 1)
  42. hpoint.divideScalar( hpoint.w );
  43. }
  44. return point.set( hpoint.x, hpoint.y, hpoint.z );
  45. };
  46. NURBSCurve.prototype.getTangent = function ( t ) {
  47. var u = this.knots[ 0 ] + t * ( this.knots[ this.knots.length - 1 ] - this.knots[ 0 ] );
  48. var ders = NURBSUtils.calcNURBSDerivatives( this.degree, this.knots, this.controlPoints, u, 1 );
  49. var tangent = ders[ 1 ].clone();
  50. tangent.normalize();
  51. return tangent;
  52. };
  53. export { NURBSCurve };