2
0

NURBSCurve.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /**
  2. * NURBS curve object
  3. *
  4. * Derives from Curve, overriding getPoint and getTangent.
  5. *
  6. * Implementation is based on (x, y [, z=0 [, w=1]]) control points with w=weight.
  7. *
  8. **/
  9. THREE.NURBSCurve = function ( degree, knots /* array of reals */, controlPoints /* array of Vector(2|3|4) */, startKnot /* index in knots */, endKnot /* index in knots */ ) {
  10. THREE.Curve.call( this );
  11. this.degree = degree;
  12. this.knots = knots;
  13. this.controlPoints = [];
  14. // Used by periodic NURBS to remove hidden spans
  15. this.startKnot = startKnot || 0;
  16. this.endKnot = endKnot || ( this.knots.length - 1 );
  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, optionalTarget ) {
  26. var point = optionalTarget || new THREE.Vector3();
  27. var u = this.knots[ this.startKnot ] + t * ( this.knots[ this.endKnot ] - this.knots[ this.startKnot ] ); // linear mapping t->u
  28. // following results in (wx, wy, wz, w) homogeneous point
  29. var hpoint = THREE.NURBSUtils.calcBSplinePoint( this.degree, this.knots, this.controlPoints, u );
  30. if ( hpoint.w != 1.0 ) {
  31. // project to 3D space: (wx, wy, wz, w) -> (x, y, z, 1)
  32. hpoint.divideScalar( hpoint.w );
  33. }
  34. return point.set( hpoint.x, hpoint.y, hpoint.z );
  35. };
  36. THREE.NURBSCurve.prototype.getTangent = function ( t, optionalTarget ) {
  37. var tangent = optionalTarget || new THREE.Vector3();
  38. var u = this.knots[ 0 ] + t * ( this.knots[ this.knots.length - 1 ] - this.knots[ 0 ] );
  39. var ders = THREE.NURBSUtils.calcNURBSDerivatives( this.degree, this.knots, this.controlPoints, u, 1 );
  40. tangent.copy( ders[ 1 ] ).normalize();
  41. return tangent;
  42. };