NURBSCurve.js 1.7 KB

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