NURBSCurve.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import {
  2. Curve,
  3. Vector3,
  4. Vector4
  5. } from 'three';
  6. import * as NURBSUtils from '../curves/NURBSUtils.js';
  7. /**
  8. * This class represents a NURBS curve.
  9. *
  10. * Implementation is based on `(x, y [, z=0 [, w=1]])` control points with `w=weight`.
  11. *
  12. * @augments Curve
  13. * @three_import import { NURBSCurve } from 'three/addons/curves/NURBSCurve.js';
  14. */
  15. class NURBSCurve extends Curve {
  16. /**
  17. * Constructs a new NURBS curve.
  18. *
  19. * @param {number} degree - The NURBS degree.
  20. * @param {Array<number>} knots - The knots as a flat array of numbers.
  21. * @param {Array<Vector2|Vector3|Vector4>} controlPoints - An array holding control points.
  22. * @param {number} [startKnot] - Index of the start knot into the `knots` array.
  23. * @param {number} [endKnot] - Index of the end knot into the `knots` array.
  24. */
  25. constructor( degree, knots, controlPoints, startKnot, endKnot ) {
  26. super();
  27. const knotsLength = knots ? knots.length - 1 : 0;
  28. const pointsLength = controlPoints ? controlPoints.length : 0;
  29. /**
  30. * The NURBS degree.
  31. *
  32. * @type {number}
  33. */
  34. this.degree = degree;
  35. /**
  36. * The knots as a flat array of numbers.
  37. *
  38. * @type {Array<number>}
  39. */
  40. this.knots = knots;
  41. /**
  42. * An array of control points.
  43. *
  44. * @type {Array<Vector4>}
  45. */
  46. this.controlPoints = [];
  47. /**
  48. * Index of the start knot into the `knots` array.
  49. *
  50. * @type {number}
  51. */
  52. this.startKnot = startKnot || 0;
  53. /**
  54. * Index of the end knot into the `knots` array.
  55. *
  56. * @type {number}
  57. */
  58. this.endKnot = endKnot || knotsLength;
  59. for ( let i = 0; i < pointsLength; ++ i ) {
  60. // ensure Vector4 for control points
  61. const point = controlPoints[ i ];
  62. this.controlPoints[ i ] = new Vector4( point.x, point.y, point.z, point.w );
  63. }
  64. }
  65. /**
  66. * This method returns a vector in 3D space for the given interpolation factor.
  67. *
  68. * @param {number} t - A interpolation factor representing a position on the curve. Must be in the range `[0,1]`.
  69. * @param {Vector3} [optionalTarget] - The optional target vector the result is written to.
  70. * @return {Vector3} The position on the curve.
  71. */
  72. getPoint( t, optionalTarget = new Vector3() ) {
  73. const point = optionalTarget;
  74. const u = this.knots[ this.startKnot ] + t * ( this.knots[ this.endKnot ] - this.knots[ this.startKnot ] ); // linear mapping t->u
  75. // following results in (wx, wy, wz, w) homogeneous point
  76. const hpoint = NURBSUtils.calcBSplinePoint( this.degree, this.knots, this.controlPoints, u );
  77. if ( hpoint.w !== 1.0 ) {
  78. // project to 3D space: (wx, wy, wz, w) -> (x, y, z, 1)
  79. hpoint.divideScalar( hpoint.w );
  80. }
  81. return point.set( hpoint.x, hpoint.y, hpoint.z );
  82. }
  83. /**
  84. * Returns a unit vector tangent for the given interpolation factor.
  85. *
  86. * @param {number} t - The interpolation factor.
  87. * @param {Vector3} [optionalTarget] - The optional target vector the result is written to.
  88. * @return {Vector3} The tangent vector.
  89. */
  90. getTangent( t, optionalTarget = new Vector3() ) {
  91. const tangent = optionalTarget;
  92. const u = this.knots[ 0 ] + t * ( this.knots[ this.knots.length - 1 ] - this.knots[ 0 ] );
  93. const ders = NURBSUtils.calcNURBSDerivatives( this.degree, this.knots, this.controlPoints, u, 1 );
  94. tangent.copy( ders[ 1 ] ).normalize();
  95. return tangent;
  96. }
  97. toJSON() {
  98. const data = super.toJSON();
  99. data.degree = this.degree;
  100. data.knots = [ ...this.knots ];
  101. data.controlPoints = this.controlPoints.map( p => p.toArray() );
  102. data.startKnot = this.startKnot;
  103. data.endKnot = this.endKnot;
  104. return data;
  105. }
  106. fromJSON( json ) {
  107. super.fromJSON( json );
  108. this.degree = json.degree;
  109. this.knots = [ ...json.knots ];
  110. this.controlPoints = json.controlPoints.map( p => new Vector4( p[ 0 ], p[ 1 ], p[ 2 ], p[ 3 ] ) );
  111. this.startKnot = json.startKnot;
  112. this.endKnot = json.endKnot;
  113. return this;
  114. }
  115. }
  116. export { NURBSCurve };