NURBSCurve.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import {
  2. Curve,
  3. Vector3,
  4. Vector4
  5. } from 'three';
  6. import * as NURBSUtils from '../curves/NURBSUtils.js';
  7. /**
  8. * NURBS curve object
  9. *
  10. * Derives from Curve, overriding getPoint and getTangent.
  11. *
  12. * Implementation is based on (x, y [, z=0 [, w=1]]) control points with w=weight.
  13. *
  14. **/
  15. class NURBSCurve extends Curve {
  16. constructor(
  17. degree,
  18. knots /* array of reals */,
  19. controlPoints /* array of Vector(2|3|4) */,
  20. startKnot /* index in knots */,
  21. endKnot /* index in knots */
  22. ) {
  23. super();
  24. const knotsLength = knots ? knots.length - 1 : 0;
  25. const pointsLength = controlPoints ? controlPoints.length : 0;
  26. this.degree = degree;
  27. this.knots = knots;
  28. this.controlPoints = [];
  29. // Used by periodic NURBS to remove hidden spans
  30. this.startKnot = startKnot || 0;
  31. this.endKnot = endKnot || knotsLength;
  32. for ( let i = 0; i < pointsLength; ++ i ) {
  33. // ensure Vector4 for control points
  34. const point = controlPoints[ i ];
  35. this.controlPoints[ i ] = new Vector4( point.x, point.y, point.z, point.w );
  36. }
  37. }
  38. getPoint( t, optionalTarget = new Vector3() ) {
  39. const point = optionalTarget;
  40. const u = this.knots[ this.startKnot ] + t * ( this.knots[ this.endKnot ] - this.knots[ this.startKnot ] ); // linear mapping t->u
  41. // following results in (wx, wy, wz, w) homogeneous point
  42. const hpoint = NURBSUtils.calcBSplinePoint( this.degree, this.knots, this.controlPoints, u );
  43. if ( hpoint.w !== 1.0 ) {
  44. // project to 3D space: (wx, wy, wz, w) -> (x, y, z, 1)
  45. hpoint.divideScalar( hpoint.w );
  46. }
  47. return point.set( hpoint.x, hpoint.y, hpoint.z );
  48. }
  49. getTangent( t, optionalTarget = new Vector3() ) {
  50. const tangent = optionalTarget;
  51. const u = this.knots[ 0 ] + t * ( this.knots[ this.knots.length - 1 ] - this.knots[ 0 ] );
  52. const ders = NURBSUtils.calcNURBSDerivatives( this.degree, this.knots, this.controlPoints, u, 1 );
  53. tangent.copy( ders[ 1 ] ).normalize();
  54. return tangent;
  55. }
  56. toJSON() {
  57. const data = super.toJSON();
  58. data.degree = this.degree;
  59. data.knots = [ ...this.knots ];
  60. data.controlPoints = this.controlPoints.map( p => p.toArray() );
  61. data.startKnot = this.startKnot;
  62. data.endKnot = this.endKnot;
  63. return data;
  64. }
  65. fromJSON( json ) {
  66. super.fromJSON( json );
  67. this.degree = json.degree;
  68. this.knots = [ ...json.knots ];
  69. this.controlPoints = json.controlPoints.map( p => new Vector4( p[ 0 ], p[ 1 ], p[ 2 ], p[ 3 ] ) );
  70. this.startKnot = json.startKnot;
  71. this.endKnot = json.endKnot;
  72. return this;
  73. }
  74. }
  75. export { NURBSCurve };
粤ICP备19079148号