0
0

NURBSUtils.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. import {
  2. Vector3,
  3. Vector4
  4. } from '../../../build/three.module.js';
  5. /**
  6. * NURBS utils
  7. *
  8. * See NURBSCurve and NURBSSurface.
  9. **/
  10. /**************************************************************
  11. * NURBS Utils
  12. **************************************************************/
  13. /*
  14. Finds knot vector span.
  15. p : degree
  16. u : parametric value
  17. U : knot vector
  18. returns the span
  19. */
  20. function findSpan( p, u, U ) {
  21. const n = U.length - p - 1;
  22. if ( u >= U[ n ] ) {
  23. return n - 1;
  24. }
  25. if ( u <= U[ p ] ) {
  26. return p;
  27. }
  28. let low = p;
  29. let high = n;
  30. let mid = Math.floor( ( low + high ) / 2 );
  31. while ( u < U[ mid ] || u >= U[ mid + 1 ] ) {
  32. if ( u < U[ mid ] ) {
  33. high = mid;
  34. } else {
  35. low = mid;
  36. }
  37. mid = Math.floor( ( low + high ) / 2 );
  38. }
  39. return mid;
  40. }
  41. /*
  42. Calculate basis functions. See The NURBS Book, page 70, algorithm A2.2
  43. span : span in which u lies
  44. u : parametric point
  45. p : degree
  46. U : knot vector
  47. returns array[p+1] with basis functions values.
  48. */
  49. function calcBasisFunctions( span, u, p, U ) {
  50. const N = [];
  51. const left = [];
  52. const right = [];
  53. N[ 0 ] = 1.0;
  54. for ( let j = 1; j <= p; ++ j ) {
  55. left[ j ] = u - U[ span + 1 - j ];
  56. right[ j ] = U[ span + j ] - u;
  57. let saved = 0.0;
  58. for ( let r = 0; r < j; ++ r ) {
  59. const rv = right[ r + 1 ];
  60. const lv = left[ j - r ];
  61. const temp = N[ r ] / ( rv + lv );
  62. N[ r ] = saved + rv * temp;
  63. saved = lv * temp;
  64. }
  65. N[ j ] = saved;
  66. }
  67. return N;
  68. }
  69. /*
  70. Calculate B-Spline curve points. See The NURBS Book, page 82, algorithm A3.1.
  71. p : degree of B-Spline
  72. U : knot vector
  73. P : control points (x, y, z, w)
  74. u : parametric point
  75. returns point for given u
  76. */
  77. function calcBSplinePoint( p, U, P, u ) {
  78. const span = findSpan( p, u, U );
  79. const N = calcBasisFunctions( span, u, p, U );
  80. const C = new Vector4( 0, 0, 0, 0 );
  81. for ( let j = 0; j <= p; ++ j ) {
  82. const point = P[ span - p + j ];
  83. const Nj = N[ j ];
  84. const wNj = point.w * Nj;
  85. C.x += point.x * wNj;
  86. C.y += point.y * wNj;
  87. C.z += point.z * wNj;
  88. C.w += point.w * Nj;
  89. }
  90. return C;
  91. }
  92. /*
  93. Calculate basis functions derivatives. See The NURBS Book, page 72, algorithm A2.3.
  94. span : span in which u lies
  95. u : parametric point
  96. p : degree
  97. n : number of derivatives to calculate
  98. U : knot vector
  99. returns array[n+1][p+1] with basis functions derivatives
  100. */
  101. function calcBasisFunctionDerivatives( span, u, p, n, U ) {
  102. const zeroArr = [];
  103. for ( let i = 0; i <= p; ++ i )
  104. zeroArr[ i ] = 0.0;
  105. const ders = [];
  106. for ( let i = 0; i <= n; ++ i )
  107. ders[ i ] = zeroArr.slice( 0 );
  108. const ndu = [];
  109. for ( let i = 0; i <= p; ++ i )
  110. ndu[ i ] = zeroArr.slice( 0 );
  111. ndu[ 0 ][ 0 ] = 1.0;
  112. const left = zeroArr.slice( 0 );
  113. const right = zeroArr.slice( 0 );
  114. for ( let j = 1; j <= p; ++ j ) {
  115. left[ j ] = u - U[ span + 1 - j ];
  116. right[ j ] = U[ span + j ] - u;
  117. let saved = 0.0;
  118. for ( let r = 0; r < j; ++ r ) {
  119. const rv = right[ r + 1 ];
  120. const lv = left[ j - r ];
  121. ndu[ j ][ r ] = rv + lv;
  122. const temp = ndu[ r ][ j - 1 ] / ndu[ j ][ r ];
  123. ndu[ r ][ j ] = saved + rv * temp;
  124. saved = lv * temp;
  125. }
  126. ndu[ j ][ j ] = saved;
  127. }
  128. for ( let j = 0; j <= p; ++ j ) {
  129. ders[ 0 ][ j ] = ndu[ j ][ p ];
  130. }
  131. for ( let r = 0; r <= p; ++ r ) {
  132. let s1 = 0;
  133. let s2 = 1;
  134. const a = [];
  135. for ( let i = 0; i <= p; ++ i ) {
  136. a[ i ] = zeroArr.slice( 0 );
  137. }
  138. a[ 0 ][ 0 ] = 1.0;
  139. for ( let k = 1; k <= n; ++ k ) {
  140. let d = 0.0;
  141. const rk = r - k;
  142. const pk = p - k;
  143. if ( r >= k ) {
  144. a[ s2 ][ 0 ] = a[ s1 ][ 0 ] / ndu[ pk + 1 ][ rk ];
  145. d = a[ s2 ][ 0 ] * ndu[ rk ][ pk ];
  146. }
  147. const j1 = ( rk >= - 1 ) ? 1 : - rk;
  148. const j2 = ( r - 1 <= pk ) ? k - 1 : p - r;
  149. for ( let j = j1; j <= j2; ++ j ) {
  150. a[ s2 ][ j ] = ( a[ s1 ][ j ] - a[ s1 ][ j - 1 ] ) / ndu[ pk + 1 ][ rk + j ];
  151. d += a[ s2 ][ j ] * ndu[ rk + j ][ pk ];
  152. }
  153. if ( r <= pk ) {
  154. a[ s2 ][ k ] = - a[ s1 ][ k - 1 ] / ndu[ pk + 1 ][ r ];
  155. d += a[ s2 ][ k ] * ndu[ r ][ pk ];
  156. }
  157. ders[ k ][ r ] = d;
  158. const j = s1;
  159. s1 = s2;
  160. s2 = j;
  161. }
  162. }
  163. let r = p;
  164. for ( let k = 1; k <= n; ++ k ) {
  165. for ( let j = 0; j <= p; ++ j ) {
  166. ders[ k ][ j ] *= r;
  167. }
  168. r *= p - k;
  169. }
  170. return ders;
  171. }
  172. /*
  173. Calculate derivatives of a B-Spline. See The NURBS Book, page 93, algorithm A3.2.
  174. p : degree
  175. U : knot vector
  176. P : control points
  177. u : Parametric points
  178. nd : number of derivatives
  179. returns array[d+1] with derivatives
  180. */
  181. function calcBSplineDerivatives( p, U, P, u, nd ) {
  182. const du = nd < p ? nd : p;
  183. const CK = [];
  184. const span = findSpan( p, u, U );
  185. const nders = calcBasisFunctionDerivatives( span, u, p, du, U );
  186. const Pw = [];
  187. for ( let i = 0; i < P.length; ++ i ) {
  188. const point = P[ i ].clone();
  189. const w = point.w;
  190. point.x *= w;
  191. point.y *= w;
  192. point.z *= w;
  193. Pw[ i ] = point;
  194. }
  195. for ( let k = 0; k <= du; ++ k ) {
  196. const point = Pw[ span - p ].clone().multiplyScalar( nders[ k ][ 0 ] );
  197. for ( let j = 1; j <= p; ++ j ) {
  198. point.add( Pw[ span - p + j ].clone().multiplyScalar( nders[ k ][ j ] ) );
  199. }
  200. CK[ k ] = point;
  201. }
  202. for ( let k = du + 1; k <= nd + 1; ++ k ) {
  203. CK[ k ] = new Vector4( 0, 0, 0 );
  204. }
  205. return CK;
  206. }
  207. /*
  208. Calculate "K over I"
  209. returns k!/(i!(k-i)!)
  210. */
  211. function calcKoverI( k, i ) {
  212. let nom = 1;
  213. for ( let j = 2; j <= k; ++ j ) {
  214. nom *= j;
  215. }
  216. let denom = 1;
  217. for ( let j = 2; j <= i; ++ j ) {
  218. denom *= j;
  219. }
  220. for ( let j = 2; j <= k - i; ++ j ) {
  221. denom *= j;
  222. }
  223. return nom / denom;
  224. }
  225. /*
  226. Calculate derivatives (0-nd) of rational curve. See The NURBS Book, page 127, algorithm A4.2.
  227. Pders : result of function calcBSplineDerivatives
  228. returns array with derivatives for rational curve.
  229. */
  230. function calcRationalCurveDerivatives( Pders ) {
  231. const nd = Pders.length;
  232. const Aders = [];
  233. const wders = [];
  234. for ( let i = 0; i < nd; ++ i ) {
  235. const point = Pders[ i ];
  236. Aders[ i ] = new Vector3( point.x, point.y, point.z );
  237. wders[ i ] = point.w;
  238. }
  239. const CK = [];
  240. for ( let k = 0; k < nd; ++ k ) {
  241. const v = Aders[ k ].clone();
  242. for ( let i = 1; i <= k; ++ i ) {
  243. v.sub( CK[ k - i ].clone().multiplyScalar( calcKoverI( k, i ) * wders[ i ] ) );
  244. }
  245. CK[ k ] = v.divideScalar( wders[ 0 ] );
  246. }
  247. return CK;
  248. }
  249. /*
  250. Calculate NURBS curve derivatives. See The NURBS Book, page 127, algorithm A4.2.
  251. p : degree
  252. U : knot vector
  253. P : control points in homogeneous space
  254. u : parametric points
  255. nd : number of derivatives
  256. returns array with derivatives.
  257. */
  258. function calcNURBSDerivatives( p, U, P, u, nd ) {
  259. const Pders = calcBSplineDerivatives( p, U, P, u, nd );
  260. return calcRationalCurveDerivatives( Pders );
  261. }
  262. /*
  263. Calculate rational B-Spline surface point. See The NURBS Book, page 134, algorithm A4.3.
  264. p1, p2 : degrees of B-Spline surface
  265. U1, U2 : knot vectors
  266. P : control points (x, y, z, w)
  267. u, v : parametric values
  268. returns point for given (u, v)
  269. */
  270. function calcSurfacePoint( p, q, U, V, P, u, v, target ) {
  271. const uspan = findSpan( p, u, U );
  272. const vspan = findSpan( q, v, V );
  273. const Nu = calcBasisFunctions( uspan, u, p, U );
  274. const Nv = calcBasisFunctions( vspan, v, q, V );
  275. const temp = [];
  276. for ( let l = 0; l <= q; ++ l ) {
  277. temp[ l ] = new Vector4( 0, 0, 0, 0 );
  278. for ( let k = 0; k <= p; ++ k ) {
  279. const point = P[ uspan - p + k ][ vspan - q + l ].clone();
  280. const w = point.w;
  281. point.x *= w;
  282. point.y *= w;
  283. point.z *= w;
  284. temp[ l ].add( point.multiplyScalar( Nu[ k ] ) );
  285. }
  286. }
  287. const Sw = new Vector4( 0, 0, 0, 0 );
  288. for ( let l = 0; l <= q; ++ l ) {
  289. Sw.add( temp[ l ].multiplyScalar( Nv[ l ] ) );
  290. }
  291. Sw.divideScalar( Sw.w );
  292. target.set( Sw.x, Sw.y, Sw.z );
  293. }
  294. export {
  295. findSpan,
  296. calcBasisFunctions,
  297. calcBSplinePoint,
  298. calcBasisFunctionDerivatives,
  299. calcBSplineDerivatives,
  300. calcKoverI,
  301. calcRationalCurveDerivatives,
  302. calcNURBSDerivatives,
  303. calcSurfacePoint,
  304. };