NURBSCurve.js 2.0 KB

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