NURBSUtils.js 8.2 KB

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