NURBSCurve.js 2.0 KB

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