NURBSCurve.js 2.2 KB

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