CatmullRomCurve3.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /**
  2. * @author zz85 / http://joshuakoo.com
  3. */
  4. QUnit.module( "CatmullRomCurve3" );
  5. var positions = [
  6. new THREE.Vector3( - 60, - 100, 60 ),
  7. new THREE.Vector3( - 60, 20, 60 ),
  8. new THREE.Vector3( - 60, 120, 60 ),
  9. new THREE.Vector3( 60, 20, - 60 ),
  10. new THREE.Vector3( 60, - 100, - 60 )
  11. ];
  12. QUnit.test( "catmullrom check", function( assert ) {
  13. var curve = new THREE.CatmullRomCurve3( positions );
  14. curve.type = 'catmullrom';
  15. var expectedPoints = [
  16. new THREE.Vector3( - 60, - 100, 60 ),
  17. new THREE.Vector3( - 60, - 51.04, 60 ),
  18. new THREE.Vector3( - 60, - 2.7199999999999998, 60 ),
  19. new THREE.Vector3( - 61.92, 44.48, 61.92 ),
  20. new THREE.Vector3( - 68.64, 95.36000000000001, 68.64 ),
  21. new THREE.Vector3( - 60, 120, 60 ),
  22. new THREE.Vector3( - 14.880000000000017, 95.36000000000001, 14.880000000000017 ),
  23. new THREE.Vector3( 41.75999999999997, 44.48000000000003, - 41.75999999999997 ),
  24. new THREE.Vector3( 67.68, - 2.720000000000023, - 67.68 ),
  25. new THREE.Vector3( 65.75999999999999, - 51.04000000000001, - 65.75999999999999 ),
  26. new THREE.Vector3( 60, - 100, - 60 )
  27. ];
  28. var points = curve.getPoints( 10 );
  29. assert.equal( points.length, expectedPoints.length, 'correct number of points.' );
  30. points.forEach( function ( point, i ) {
  31. assert.numEqual( point.x, expectedPoints[ i ].x, 'points[' + i + '].x' );
  32. assert.numEqual( point.y, expectedPoints[ i ].y, 'points[' + i + '].y' );
  33. assert.numEqual( point.z, expectedPoints[ i ].z, 'points[' + i + '].z' );
  34. } );
  35. } );
  36. QUnit.test( "chordal basic check", function( assert ) {
  37. var curve = new THREE.CatmullRomCurve3( positions );
  38. curve.type = 'chordal';
  39. var expectedPoints = [
  40. new THREE.Vector3( - 60, - 100, 60 ),
  41. new THREE.Vector3( - 60, - 52, 60 ),
  42. new THREE.Vector3( - 60, - 4, 60 ),
  43. new THREE.Vector3( - 60.656435889910924, 41.62455386421379, 60.656435889910924 ),
  44. new THREE.Vector3( - 62.95396150459915, 87.31049238896205, 62.95396150459915 ),
  45. new THREE.Vector3( - 60, 120, 60 ),
  46. new THREE.Vector3( - 16.302568199486444, 114.1500463116312, 16.302568199486444 ),
  47. new THREE.Vector3( 42.998098664956586, 54.017050116427455, - 42.998098664956586 ),
  48. new THREE.Vector3( 63.542500175682434, - 1.137153397546383, - 63.542500175682434 ),
  49. new THREE.Vector3( 62.65687513176183, - 49.85286504815978, - 62.65687513176183 ),
  50. new THREE.Vector3( 60.00000000000001, - 100, - 60.00000000000001 )
  51. ];
  52. var points = curve.getPoints( 10 );
  53. assert.equal( points.length, expectedPoints.length, 'correct number of points.' );
  54. points.forEach( function ( point, i ) {
  55. assert.numEqual( point.x, expectedPoints[ i ].x, 'points[' + i + '].x' );
  56. assert.numEqual( point.y, expectedPoints[ i ].y, 'points[' + i + '].y' );
  57. assert.numEqual( point.z, expectedPoints[ i ].z, 'points[' + i + '].z' );
  58. } );
  59. } );
  60. QUnit.test( "centripetal basic check", function( assert ) {
  61. var curve = new THREE.CatmullRomCurve3( positions );
  62. curve.type = 'centripetal';
  63. var expectedPoints = [
  64. new THREE.Vector3( - 60, - 100, 60 ),
  65. new THREE.Vector3( - 60, - 51.47527724919028, 60 ),
  66. new THREE.Vector3( - 60, - 3.300369665587032, 60 ),
  67. new THREE.Vector3( - 61.13836565863938, 42.86306307781241, 61.13836565863938 ),
  68. new THREE.Vector3( - 65.1226454638772, 90.69743905511538, 65.1226454638772 ),
  69. new THREE.Vector3( - 60, 120, 60 ),
  70. new THREE.Vector3( - 15.620412575504497, 103.10790870179872, 15.620412575504497 ),
  71. new THREE.Vector3( 42.384384731047874, 48.35477686933143, - 42.384384731047874 ),
  72. new THREE.Vector3( 65.25545512241153, - 1.646250966068339, - 65.25545512241153 ),
  73. new THREE.Vector3( 63.94159134180865, - 50.234688224551256, - 63.94159134180865 ),
  74. new THREE.Vector3( 59.99999999999999, - 100, - 59.99999999999999 ),
  75. ];
  76. var points = curve.getPoints( 10 );
  77. assert.equal( points.length, expectedPoints.length, 'correct number of points.' );
  78. points.forEach( function ( point, i ) {
  79. assert.numEqual( point.x, expectedPoints[ i ].x, 'points[' + i + '].x' );
  80. assert.numEqual( point.y, expectedPoints[ i ].y, 'points[' + i + '].y' );
  81. assert.numEqual( point.z, expectedPoints[ i ].z, 'points[' + i + '].z' );
  82. } );
  83. } );
  84. QUnit.test( "closed catmullrom basic check", function( assert ) {
  85. var curve = new THREE.CatmullRomCurve3( positions );
  86. curve.type = 'catmullrom';
  87. curve.closed = true;
  88. var expectedPoints = [
  89. new THREE.Vector3( - 60, - 100, 60 ),
  90. new THREE.Vector3( - 67.5, - 46.25, 67.5 ),
  91. new THREE.Vector3( - 60, 20, 60 ),
  92. new THREE.Vector3( - 67.5, 83.75, 67.5 ),
  93. new THREE.Vector3( - 60, 120, 60 ),
  94. new THREE.Vector3( 0, 83.75, 0 ),
  95. new THREE.Vector3( 60, 20, - 60 ),
  96. new THREE.Vector3( 75, - 46.25, - 75 ),
  97. new THREE.Vector3( 60, - 100, - 60 ),
  98. new THREE.Vector3( 0, - 115, 0 ),
  99. new THREE.Vector3( - 60, - 100, 60 ),
  100. ];
  101. var points = curve.getPoints( 10 );
  102. assert.equal( points.length, expectedPoints.length, 'correct number of points.' );
  103. points.forEach( function ( point, i ) {
  104. assert.numEqual( point.x, expectedPoints[ i ].x, 'points[' + i + '].x' );
  105. assert.numEqual( point.y, expectedPoints[ i ].y, 'points[' + i + '].y' );
  106. assert.numEqual( point.z, expectedPoints[ i ].z, 'points[' + i + '].z' );
  107. } );
  108. } );
  109. //
  110. // curve.type = 'catmullrom'; only from here on
  111. //
  112. QUnit.test( "getLength/getLengths", function ( assert ) {
  113. var curve = new THREE.CatmullRomCurve3( positions );
  114. curve.type = 'catmullrom';
  115. var length = curve.getLength();
  116. var expectedLength = 551.549686276872;
  117. assert.numEqual( length, expectedLength, "Correct length of curve" );
  118. var expectedLengths = [
  119. 0,
  120. 120,
  121. 220,
  122. 416.9771560359221,
  123. 536.9771560359221
  124. ];
  125. var lengths = curve.getLengths( expectedLengths.length - 1 );
  126. assert.strictEqual( lengths.length, expectedLengths.length, "Correct number of segments" );
  127. lengths.forEach( function ( segment, i ) {
  128. assert.numEqual( segment, expectedLengths[ i ], "segment[" + i + "] correct" );
  129. } );
  130. } );
  131. QUnit.test( "getPointAt", function ( assert ) {
  132. var curve = new THREE.CatmullRomCurve3( positions );
  133. curve.type = 'catmullrom';
  134. var expectedPoints = [
  135. new THREE.Vector3( - 60, - 100, 60 ),
  136. new THREE.Vector3( - 64.84177333183106, 64.86956465359813, 64.84177333183106 ),
  137. new THREE.Vector3( - 28.288507045700854, 104.83101184518996, 28.288507045700854 ),
  138. new THREE.Vector3( 60, - 100, - 60 )
  139. ];
  140. var points = [
  141. curve.getPointAt( 0 ),
  142. curve.getPointAt( 0.3 ),
  143. curve.getPointAt( 0.5 ),
  144. curve.getPointAt( 1 )
  145. ];
  146. assert.deepEqual( points, expectedPoints, "Correct points" );
  147. } );
  148. QUnit.test( "getTangent/getTangentAt", function ( assert ) {
  149. var curve = new THREE.CatmullRomCurve3( positions );
  150. curve.type = 'catmullrom';
  151. var expectedTangents = [
  152. new THREE.Vector3( 0, 1, 0 ),
  153. new THREE.Vector3( - 0.0001090274561657922, 0.9999999881130137, 0.0001090274561657922 ),
  154. new THREE.Vector3( 0.7071067811865475, - 2.0930381713877622e-13, - 0.7071067811865475 ),
  155. new THREE.Vector3( 0.43189437062802816, - 0.7917919583070032, - 0.43189437062802816 ),
  156. new THREE.Vector3( - 0.00019991333100812723, - 0.9999999600346592, 0.00019991333100812723 )
  157. ];
  158. var tangents = [
  159. curve.getTangent( 0 ),
  160. curve.getTangent( 0.25 ),
  161. curve.getTangent( 0.5 ),
  162. curve.getTangent( 0.75 ),
  163. curve.getTangent( 1 )
  164. ];
  165. expectedTangents.forEach( function ( exp, i ) {
  166. var tangent = tangents[ i ];
  167. assert.numEqual( tangent.x, exp.x, "getTangent #" + i + ": x correct" );
  168. assert.numEqual( tangent.y, exp.y, "getTangent #" + i + ": y correct" );
  169. } );
  170. //
  171. expectedTangents = [
  172. new THREE.Vector3( 0, 1, 0 ),
  173. new THREE.Vector3( - 0.10709018822205997, 0.9884651653817284, 0.10709018822205997 ),
  174. new THREE.Vector3( 0.6396363672964268, - 0.4262987629159402, - 0.6396363672964268 ),
  175. new THREE.Vector3( 0.5077298411616501, - 0.6960034603275557, - 0.5077298411616501 ),
  176. new THREE.Vector3( - 0.00019991333100812723, - 0.9999999600346592, 0.00019991333100812723 )
  177. ];
  178. tangents = [
  179. curve.getTangentAt( 0 ),
  180. curve.getTangentAt( 0.25 ),
  181. curve.getTangentAt( 0.5 ),
  182. curve.getTangentAt( 0.75 ),
  183. curve.getTangentAt( 1 )
  184. ];
  185. expectedTangents.forEach( function ( exp, i ) {
  186. var tangent = tangents[ i ];
  187. assert.numEqual( tangent.x, exp.x, "getTangentAt #" + i + ": x correct" );
  188. assert.numEqual( tangent.y, exp.y, "getTangentAt #" + i + ": y correct" );
  189. } );
  190. } );
  191. QUnit.test( "computeFrenetFrames", function ( assert ) {
  192. var curve = new THREE.CatmullRomCurve3( positions );
  193. curve.type = 'catmullrom';
  194. var expected = {
  195. binormals: [
  196. new THREE.Vector3( - 1, 0, 0 ),
  197. new THREE.Vector3( - 0.28685061854203, 0.6396363672964267, - 0.7131493814579701 ),
  198. new THREE.Vector3( - 1.9982670528160395e-8, - 0.0001999133310081272, - 0.9999999800173295 )
  199. ],
  200. normals: [
  201. new THREE.Vector3( 0, 0, - 1 ),
  202. new THREE.Vector3( - 0.7131493814579699, - 0.6396363672964268, - 0.2868506185420297 ),
  203. new THREE.Vector3( - 0.9999999800173294, 0.00019991333100810582, - 1.99826701852146e-8 )
  204. ],
  205. tangents: [
  206. new THREE.Vector3( 0, 1, 0 ),
  207. new THREE.Vector3( 0.6396363672964269, - 0.4262987629159403, - 0.6396363672964269 ),
  208. new THREE.Vector3( - 0.0001999133310081273, - 0.9999999600346594, 0.0001999133310081273 )
  209. ]
  210. };
  211. var frames = curve.computeFrenetFrames( 2, false );
  212. Object.keys( expected ).forEach( function ( group, i ) {
  213. expected[ group ].forEach( function ( vec, j ) {
  214. assert.numEqual( frames[ group ][ j ].x, vec.x, "Frenet frames [" + i + ", " + j + "].x correct" );
  215. assert.numEqual( frames[ group ][ j ].y, vec.y, "Frenet frames [" + i + ", " + j + "].y correct" );
  216. assert.numEqual( frames[ group ][ j ].z, vec.z, "Frenet frames [" + i + ", " + j + "].z correct" );
  217. } );
  218. } );
  219. } );
  220. QUnit.test( "getUtoTmapping", function ( assert ) {
  221. var curve = new THREE.CatmullRomCurve3( positions );
  222. curve.type = 'catmullrom';
  223. var start = curve.getUtoTmapping( 0, 0 );
  224. var end = curve.getUtoTmapping( 0, curve.getLength() );
  225. var somewhere = curve.getUtoTmapping( 0.5, 500 );
  226. var expectedSomewhere = 0.8964116382083199;
  227. assert.strictEqual( start, 0, "getUtoTmapping( 0, 0 ) is the starting point" );
  228. assert.strictEqual( end, 1, "getUtoTmapping( 0, length ) is the ending point" );
  229. assert.numEqual( somewhere, expectedSomewhere, "getUtoTmapping( 0.5, 500 ) is correct" );
  230. } );
  231. QUnit.test( "getSpacedPoints", function ( assert ) {
  232. var curve = new THREE.CatmullRomCurve3( positions );
  233. curve.type = 'catmullrom';
  234. var expectedPoints = [
  235. new THREE.Vector3( - 60, - 100, 60 ),
  236. new THREE.Vector3( - 60, 10.311489426555056, 60 ),
  237. new THREE.Vector3( - 65.05889864636504, 117.99691802595966, 65.05889864636504 ),
  238. new THREE.Vector3( 6.054276900088592, 78.7153118386369, - 6.054276900088592 ),
  239. new THREE.Vector3( 64.9991491385602, 8.386980812799566, - 64.9991491385602 ),
  240. new THREE.Vector3( 60, - 100, - 60 )
  241. ];
  242. var points = curve.getSpacedPoints();
  243. assert.strictEqual( points.length, expectedPoints.length, "Correct number of points" );
  244. assert.deepEqual( points, expectedPoints, "Correct points calculated" );
  245. } );