NURBSVolume.js 2.8 KB

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