NURBSCurve.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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, optionalTarget ) {
  30. var point = optionalTarget || new THREE.Vector3();
  31. var u = this.knots[ this.startKnot ] + t * ( this.knots[ this.endKnot ] - this.knots[ this.startKnot ] ); // linear mapping t->u
  32. // following results in (wx, wy, wz, w) homogeneous point
  33. var hpoint = THREE.NURBSUtils.calcBSplinePoint( this.degree, this.knots, this.controlPoints, u );
  34. if ( hpoint.w != 1.0 ) {
  35. // project to 3D space: (wx, wy, wz, w) -> (x, y, z, 1)
  36. hpoint.divideScalar( hpoint.w );
  37. }
  38. return point.set( hpoint.x, hpoint.y, hpoint.z );
  39. };
  40. THREE.NURBSCurve.prototype.getTangent = function ( t, optionalTarget ) {
  41. var tangent = optionalTarget || new THREE.Vector3();
  42. var u = this.knots[ 0 ] + t * ( this.knots[ this.knots.length - 1 ] - this.knots[ 0 ] );
  43. var ders = THREE.NURBSUtils.calcNURBSDerivatives( this.degree, this.knots, this.controlPoints, u, 1 );
  44. tangent.copy( ders[ 1 ] ).normalize();
  45. return tangent;
  46. };