NURBSUtils.js 7.8 KB

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