NURBSCurve.js 1.9 KB

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