NURBSCurve.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. console.warn( "THREE.NURBSCurve: As part of the transition to ES6 Modules, the files in 'examples/js' were deprecated in May 2020 (r117) and will be deleted in December 2020 (r124). You can find more information about developing using ES6 Modules in https://threejs.org/docs/index.html#manual/en/introduction/Import-via-modules." );
  2. /**
  3. * @author renej
  4. * NURBS curve object
  5. *
  6. * Derives from Curve, overriding getPoint and getTangent.
  7. *
  8. * Implementation is based on (x, y [, z=0 [, w=1]]) control points with w=weight.
  9. *
  10. **/
  11. /**************************************************************
  12. * NURBS curve
  13. **************************************************************/
  14. THREE.NURBSCurve = function ( degree, knots /* array of reals */, controlPoints /* array of Vector(2|3|4) */, startKnot /* index in knots */, endKnot /* index in knots */ ) {
  15. THREE.Curve.call( this );
  16. this.degree = degree;
  17. this.knots = knots;
  18. this.controlPoints = [];
  19. // Used by periodic NURBS to remove hidden spans
  20. this.startKnot = startKnot || 0;
  21. this.endKnot = endKnot || ( this.knots.length - 1 );
  22. for ( var i = 0; i < controlPoints.length; ++ i ) {
  23. // ensure Vector4 for control points
  24. var point = controlPoints[ i ];
  25. this.controlPoints[ i ] = new THREE.Vector4( point.x, point.y, point.z, point.w );
  26. }
  27. };
  28. THREE.NURBSCurve.prototype = Object.create( THREE.Curve.prototype );
  29. THREE.NURBSCurve.prototype.constructor = THREE.NURBSCurve;
  30. THREE.NURBSCurve.prototype.getPoint = function ( t, optionalTarget ) {
  31. var point = optionalTarget || new THREE.Vector3();
  32. var u = this.knots[ this.startKnot ] + t * ( this.knots[ this.endKnot ] - this.knots[ this.startKnot ] ); // linear mapping t->u
  33. // following results in (wx, wy, wz, w) homogeneous point
  34. var hpoint = THREE.NURBSUtils.calcBSplinePoint( this.degree, this.knots, this.controlPoints, u );
  35. if ( hpoint.w != 1.0 ) {
  36. // project to 3D space: (wx, wy, wz, w) -> (x, y, z, 1)
  37. hpoint.divideScalar( hpoint.w );
  38. }
  39. return point.set( hpoint.x, hpoint.y, hpoint.z );
  40. };
  41. THREE.NURBSCurve.prototype.getTangent = function ( t, optionalTarget ) {
  42. var tangent = optionalTarget || new THREE.Vector3();
  43. var u = this.knots[ 0 ] + t * ( this.knots[ this.knots.length - 1 ] - this.knots[ 0 ] );
  44. var ders = THREE.NURBSUtils.calcNURBSDerivatives( this.degree, this.knots, this.controlPoints, u, 1 );
  45. tangent.copy( ders[ 1 ] ).normalize();
  46. return tangent;
  47. };