NURBSCurve.js 1.9 KB

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