NURBSSurface.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import {
  2. Vector4
  3. } from 'three';
  4. import * as NURBSUtils from '../curves/NURBSUtils.js';
  5. /**
  6. * This class represents a NURBS surface.
  7. *
  8. * Implementation is based on `(x, y [, z=0 [, w=1]])` control points with `w=weight`.
  9. *
  10. * @three_import import { NURBSSurface } from 'three/addons/curves/NURBSSurface.js';
  11. */
  12. class NURBSSurface {
  13. /**
  14. * Constructs a new NURBS surface.
  15. *
  16. * @param {number} degree1 - The first NURBS degree.
  17. * @param {number} degree2 - The second NURBS degree.
  18. * @param {Array<number>} knots1 - The first knots as a flat array of numbers.
  19. * @param {Array<number>} knots2 - The second knots as a flat array of numbers.
  20. * @param {Array<Array<Vector2|Vector3|Vector4>>} controlPoints - An array^2 holding control points.
  21. */
  22. constructor( degree1, degree2, knots1, knots2, controlPoints ) {
  23. /**
  24. * The first NURBS degree.
  25. *
  26. * @type {number}
  27. */
  28. this.degree1 = degree1;
  29. /**
  30. * The second NURBS degree.
  31. *
  32. * @type {number}
  33. */
  34. this.degree2 = degree2;
  35. /**
  36. * The first knots as a flat array of numbers.
  37. *
  38. * @type {Array<number>}
  39. */
  40. this.knots1 = knots1;
  41. /**
  42. * The second knots as a flat array of numbers.
  43. *
  44. * @type {Array<number>}
  45. */
  46. this.knots2 = knots2;
  47. /**
  48. * An array holding arrays of control points.
  49. *
  50. * @type {Array<Array<Vector2|Vector3|Vector4>>}
  51. */
  52. this.controlPoints = [];
  53. const len1 = knots1.length - degree1 - 1;
  54. const len2 = knots2.length - degree2 - 1;
  55. // ensure Vector4 for control points
  56. for ( let i = 0; i < len1; ++ i ) {
  57. this.controlPoints[ i ] = [];
  58. for ( let j = 0; j < len2; ++ j ) {
  59. const point = controlPoints[ i ][ j ];
  60. this.controlPoints[ i ][ j ] = new Vector4( point.x, point.y, point.z, point.w );
  61. }
  62. }
  63. }
  64. /**
  65. * This method returns a vector in 3D space for the given interpolation factor. This vector lies on the NURBS surface.
  66. *
  67. * @param {number} t1 - The first interpolation factor representing the `u` position on the surface. Must be in the range `[0,1]`.
  68. * @param {number} t2 - The second interpolation factor representing the `v` position on the surface. Must be in the range `[0,1]`.
  69. * @param {Vector3} target - The target vector the result is written to.
  70. */
  71. getPoint( t1, t2, target ) {
  72. const u = this.knots1[ 0 ] + t1 * ( this.knots1[ this.knots1.length - 1 ] - this.knots1[ 0 ] ); // linear mapping t1->u
  73. const v = this.knots2[ 0 ] + t2 * ( this.knots2[ this.knots2.length - 1 ] - this.knots2[ 0 ] ); // linear mapping t2->u
  74. NURBSUtils.calcSurfacePoint( this.degree1, this.degree2, this.knots1, this.knots2, this.controlPoints, u, v, target );
  75. }
  76. }
  77. export { NURBSSurface };