CatmullRomCurve3.tests.js 12 KB

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