NURBSSurface.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import {
  2. Vector4
  3. } from 'three';
  4. import * as NURBSUtils from '../curves/NURBSUtils.js';
  5. /**
  6. * NURBS surface object
  7. *
  8. * Implementation is based on (x, y [, z=0 [, w=1]]) control points with w=weight.
  9. **/
  10. class NURBSSurface {
  11. constructor( degree1, degree2, knots1, knots2 /* arrays of reals */, controlPoints /* array^2 of Vector(2|3|4) */ ) {
  12. this.degree1 = degree1;
  13. this.degree2 = degree2;
  14. this.knots1 = knots1;
  15. this.knots2 = knots2;
  16. this.controlPoints = [];
  17. const len1 = knots1.length - degree1 - 1;
  18. const len2 = knots2.length - degree2 - 1;
  19. // ensure Vector4 for control points
  20. for ( let i = 0; i < len1; ++ i ) {
  21. this.controlPoints[ i ] = [];
  22. for ( let j = 0; j < len2; ++ j ) {
  23. const point = controlPoints[ i ][ j ];
  24. this.controlPoints[ i ][ j ] = new Vector4( point.x, point.y, point.z, point.w );
  25. }
  26. }
  27. }
  28. getPoint( t1, t2, target ) {
  29. const u = this.knots1[ 0 ] + t1 * ( this.knots1[ this.knots1.length - 1 ] - this.knots1[ 0 ] ); // linear mapping t1->u
  30. const v = this.knots2[ 0 ] + t2 * ( this.knots2[ this.knots2.length - 1 ] - this.knots2[ 0 ] ); // linear mapping t2->u
  31. NURBSUtils.calcSurfacePoint( this.degree1, this.degree2, this.knots1, this.knots2, this.controlPoints, u, v, target );
  32. }
  33. }
  34. export { NURBSSurface };