NURBSCurve.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 ) {
  36. var u = this.knots[ this.startKnot ] + t * ( this.knots[ this.endKnot ] - this.knots[ this.startKnot ] ); // linear mapping t->u
  37. // following results in (wx, wy, wz, w) homogeneous point
  38. var hpoint = NURBSUtils.calcBSplinePoint( this.degree, this.knots, this.controlPoints, u );
  39. if ( hpoint.w != 1.0 ) {
  40. // project to 3D space: (wx, wy, wz, w) -> (x, y, z, 1)
  41. hpoint.divideScalar( hpoint.w );
  42. }
  43. return new Vector3( hpoint.x, hpoint.y, hpoint.z );
  44. };
  45. NURBSCurve.prototype.getTangent = function ( t ) {
  46. var u = this.knots[ 0 ] + t * ( this.knots[ this.knots.length - 1 ] - this.knots[ 0 ] );
  47. var ders = NURBSUtils.calcNURBSDerivatives( this.degree, this.knots, this.controlPoints, u, 1 );
  48. var tangent = ders[ 1 ].clone();
  49. tangent.normalize();
  50. return tangent;
  51. };
  52. export { NURBSCurve };