Geometry.tests.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /* global QUnit */
  2. import { Geometry } from '../../../../src/core/Geometry';
  3. import { BufferAttribute } from '../../../../src/core/BufferAttribute';
  4. import { BufferGeometry } from '../../../../src/core/BufferGeometry';
  5. import { BoxBufferGeometry } from '../../../../src/geometries/BoxBufferGeometry';
  6. import { DodecahedronBufferGeometry } from '../../../../src/geometries/DodecahedronBufferGeometry';
  7. import { Vector3 } from '../../../../src/math/Vector3';
  8. import { Matrix4 } from '../../../../src/math/Matrix4';
  9. import { Face3 } from '../../../../src/core/Face3';
  10. import {
  11. x,
  12. y,
  13. z,
  14. eps
  15. } from '../math/Constants.tests';
  16. function getGeometryByParams( x1, y1, z1, x2, y2, z2, x3, y3, z3 ) {
  17. var geometry = new Geometry();
  18. // a triangle
  19. geometry.vertices = [
  20. new Vector3( x1, y1, z1 ),
  21. new Vector3( x2, y2, z2 ),
  22. new Vector3( x3, y3, z3 )
  23. ];
  24. return geometry;
  25. }
  26. function getGeometry() {
  27. return getGeometryByParams( - 0.5, 0, 0, 0.5, 0, 0, 0, 1, 0 );
  28. }
  29. export default QUnit.module( 'Core', () => {
  30. QUnit.module( 'Geometry', () => {
  31. // INHERITANCE
  32. QUnit.todo( "Extending", ( assert ) => {
  33. assert.ok( false, "everything's gonna be alright" );
  34. } );
  35. // INSTANCING
  36. QUnit.todo( "Instancing", ( assert ) => {
  37. assert.ok( false, "everything's gonna be alright" );
  38. } );
  39. // PUBLIC STUFF
  40. QUnit.todo( "isGeometry", ( assert ) => {
  41. assert.ok( false, "everything's gonna be alright" );
  42. } );
  43. QUnit.test( "applyMatrix4", ( assert ) => {
  44. var geometry = getGeometry();
  45. geometry.faces.push( new Face3( 0, 1, 2 ) );
  46. var m = new Matrix4();
  47. var expectedVerts = [
  48. new Vector3( 1.5, 3, 4 ),
  49. new Vector3( 2.5, 3, 4 ),
  50. new Vector3( 2, 3, 5 )
  51. ];
  52. var v0, v1, v2;
  53. m.makeRotationX( Math.PI / 2 );
  54. m.setPosition( new Vector3( x, y, z ) );
  55. geometry.applyMatrix4( m );
  56. v0 = geometry.vertices[ 0 ];
  57. v1 = geometry.vertices[ 1 ];
  58. v2 = geometry.vertices[ 2 ];
  59. assert.ok(
  60. Math.abs( v0.x - expectedVerts[ 0 ].x ) <= eps &&
  61. Math.abs( v0.y - expectedVerts[ 0 ].y ) <= eps &&
  62. Math.abs( v0.z - expectedVerts[ 0 ].z ) <= eps,
  63. "First vertex is as expected"
  64. );
  65. assert.ok(
  66. Math.abs( v1.x - expectedVerts[ 1 ].x ) <= eps &&
  67. Math.abs( v1.y - expectedVerts[ 1 ].y ) <= eps &&
  68. Math.abs( v1.z - expectedVerts[ 1 ].z ) <= eps,
  69. "Second vertex is as expected"
  70. );
  71. assert.ok(
  72. Math.abs( v2.x - expectedVerts[ 2 ].x ) <= eps &&
  73. Math.abs( v2.y - expectedVerts[ 2 ].y ) <= eps &&
  74. Math.abs( v2.z - expectedVerts[ 2 ].z ) <= eps,
  75. "Third vertex is as expected"
  76. );
  77. } );
  78. QUnit.test( "rotateX", ( assert ) => {
  79. var geometry = getGeometry();
  80. var matrix = new Matrix4();
  81. matrix.makeRotationX( Math.PI / 2 ); // 90 degree
  82. geometry.applyMatrix4( matrix );
  83. var v0 = geometry.vertices[ 0 ], v1 = geometry.vertices[ 1 ], v2 = geometry.vertices[ 2 ];
  84. assert.ok( v0.x === - 0.5 && v0.y === 0 && v0.z === 0, "first vertex was rotated" );
  85. assert.ok( v1.x === 0.5 && v1.y === 0 && v1.z === 0, "second vertex was rotated" );
  86. assert.ok( v2.x === 0 && v2.y < Number.EPSILON && v2.z === 1, "third vertex was rotated" );
  87. } );
  88. QUnit.test( "rotateY", ( assert ) => {
  89. var geometry = getGeometry();
  90. var matrix = new Matrix4();
  91. matrix.makeRotationY( Math.PI ); // 180 degrees
  92. geometry.applyMatrix4( matrix );
  93. var v0 = geometry.vertices[ 0 ], v1 = geometry.vertices[ 1 ], v2 = geometry.vertices[ 2 ];
  94. assert.ok( v0.x === 0.5 && v0.y === 0 && v0.z < Number.EPSILON, "first vertex was rotated" );
  95. assert.ok( v1.x === - 0.5 && v1.y === 0 && v1.z < Number.EPSILON, "second vertex was rotated" );
  96. assert.ok( v2.x === 0 && v2.y === 1 && v2.z === 0, "third vertex was rotated" );
  97. } );
  98. QUnit.test( "rotateZ", ( assert ) => {
  99. var geometry = getGeometry();
  100. var matrix = new Matrix4();
  101. matrix.makeRotationZ( Math.PI / 2 * 3 ); // 270 degrees
  102. geometry.applyMatrix4( matrix );
  103. var v0 = geometry.vertices[ 0 ], v1 = geometry.vertices[ 1 ], v2 = geometry.vertices[ 2 ];
  104. assert.ok( v0.x < Number.EPSILON && v0.y === 0.5 && v0.z === 0, "first vertex was rotated" );
  105. assert.ok( v1.x < Number.EPSILON && v1.y === - 0.5 && v1.z === 0, "second vertex was rotated" );
  106. assert.ok( v2.x === 1 && v2.y < Number.EPSILON && v2.z === 0, "third vertex was rotated" );
  107. } );
  108. QUnit.test( "translate", ( assert ) => {
  109. var a = getGeometry();
  110. var expected = [
  111. new Vector3( - 2.5, 3, - 4 ),
  112. new Vector3( - 1.5, 3, - 4 ),
  113. new Vector3( - 2, 4, - 4 )
  114. ];
  115. var v;
  116. a.translate( - x, y, - z );
  117. for ( var i = 0; i < a.vertices.length; i ++ ) {
  118. v = a.vertices[ i ];
  119. assert.ok(
  120. Math.abs( v.x - expected[ i ].x ) <= eps &&
  121. Math.abs( v.y - expected[ i ].y ) <= eps &&
  122. Math.abs( v.z - expected[ i ].z ) <= eps,
  123. "Vertex #" + i + " was translated as expected"
  124. );
  125. }
  126. } );
  127. QUnit.test( "scale", ( assert ) => {
  128. var a = getGeometry();
  129. var expected = [
  130. new Vector3( - 1, 0, 0 ),
  131. new Vector3( 1, 0, 0 ),
  132. new Vector3( 0, 3, 0 )
  133. ];
  134. var v;
  135. a.scale( 2, 3, 4 );
  136. for ( var i = 0; i < a.vertices.length; i ++ ) {
  137. v = a.vertices[ i ];
  138. assert.ok(
  139. Math.abs( v.x - expected[ i ].x ) <= eps &&
  140. Math.abs( v.y - expected[ i ].y ) <= eps &&
  141. Math.abs( v.z - expected[ i ].z ) <= eps,
  142. "Vertex #" + i + " was scaled as expected"
  143. );
  144. }
  145. } );
  146. QUnit.test( "lookAt", ( assert ) => {
  147. var a = getGeometry();
  148. var expected = [
  149. new Vector3( - 0.5, 0, 0 ),
  150. new Vector3( 0.5, 0, 0 ),
  151. new Vector3( 0, 0.5 * Math.sqrt( 2 ), 0.5 * Math.sqrt( 2 ) )
  152. ];
  153. a.lookAt( new Vector3( 0, - 1, 1 ) );
  154. for ( var i = 0; i < a.vertices.length; i ++ ) {
  155. var v = a.vertices[ i ];
  156. assert.ok(
  157. Math.abs( v.x - expected[ i ].x ) <= eps &&
  158. Math.abs( v.y - expected[ i ].y ) <= eps &&
  159. Math.abs( v.z - expected[ i ].z ) <= eps,
  160. "Vertex #" + i + " was adjusted as expected"
  161. );
  162. }
  163. } );
  164. QUnit.test( "fromBufferGeometry", ( assert ) => {
  165. var bufferGeometry = new BufferGeometry();
  166. bufferGeometry.setAttribute( 'position', new BufferAttribute( new Float32Array( [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ] ), 3 ) );
  167. bufferGeometry.setAttribute( 'color', new BufferAttribute( new Float32Array( [ 0, 0, 0, 0.5, 0.5, 0.5, 1, 1, 1 ] ), 3 ) );
  168. bufferGeometry.setAttribute( 'normal', new BufferAttribute( new Float32Array( [ 0, 1, 0, 1, 0, 1, 1, 1, 0 ] ), 3 ) );
  169. bufferGeometry.setAttribute( 'uv', new BufferAttribute( new Float32Array( [ 0, 0, 0, 1, 1, 1 ] ), 2 ) );
  170. bufferGeometry.setAttribute( 'uv2', new BufferAttribute( new Float32Array( [ 0, 0, 0, 1, 1, 1 ] ), 2 ) );
  171. var geometry = new Geometry().fromBufferGeometry( bufferGeometry );
  172. var colors = geometry.colors;
  173. assert.ok(
  174. colors[ 0 ].r === 0 && colors[ 0 ].g === 0 && colors[ 0 ].b === 0 &&
  175. colors[ 1 ].r === 0.5 && colors[ 1 ].g === 0.5 && colors[ 1 ].b === 0.5 &&
  176. colors[ 2 ].r === 1 && colors[ 2 ].g === 1 && colors[ 2 ].b === 1
  177. , "colors were created well" );
  178. var vertices = geometry.vertices;
  179. assert.ok(
  180. vertices[ 0 ].x === 1 && vertices[ 0 ].y === 2 && vertices[ 0 ].z === 3 &&
  181. vertices[ 1 ].x === 4 && vertices[ 1 ].y === 5 && vertices[ 1 ].z === 6 &&
  182. vertices[ 2 ].x === 7 && vertices[ 2 ].y === 8 && vertices[ 2 ].z === 9
  183. , "vertices were created well" );
  184. var vNormals = geometry.faces[ 0 ].vertexNormals;
  185. assert.ok(
  186. vNormals[ 0 ].x === 0 && vNormals[ 0 ].y === 1 && vNormals[ 0 ].z === 0 &&
  187. vNormals[ 1 ].x === 1 && vNormals[ 1 ].y === 0 && vNormals[ 1 ].z === 1 &&
  188. vNormals[ 2 ].x === 1 && vNormals[ 2 ].y === 1 && vNormals[ 2 ].z === 0
  189. , "vertex normals were created well" );
  190. } );
  191. QUnit.todo( "center", ( assert ) => {
  192. assert.ok( false, "everything's gonna be alright" );
  193. } );
  194. QUnit.test( "normalize", ( assert ) => {
  195. var a = getGeometry();
  196. var sqrt = 0.5 * Math.sqrt( 2 );
  197. var expected = [
  198. new Vector3( - sqrt, - sqrt, 0 ),
  199. new Vector3( sqrt, - sqrt, 0 ),
  200. new Vector3( 0, sqrt, 0 )
  201. ];
  202. var v;
  203. a.normalize();
  204. for ( var i = 0; i < a.vertices.length; i ++ ) {
  205. v = a.vertices[ i ];
  206. assert.ok(
  207. Math.abs( v.x - expected[ i ].x ) <= eps &&
  208. Math.abs( v.y - expected[ i ].y ) <= eps &&
  209. Math.abs( v.z - expected[ i ].z ) <= eps,
  210. "Vertex #" + i + " was normalized as expected"
  211. );
  212. }
  213. } );
  214. QUnit.todo( "computeFaceNormals", ( assert ) => {
  215. assert.ok( false, "everything's gonna be alright" );
  216. } );
  217. QUnit.todo( "computeVertexNormals", ( assert ) => {
  218. assert.ok( false, "everything's gonna be alright" );
  219. } );
  220. QUnit.todo( "computeFlatVertexNormals", ( assert ) => {
  221. assert.ok( false, "everything's gonna be alright" );
  222. } );
  223. QUnit.todo( "computeMorphNormals", ( assert ) => {
  224. assert.ok( false, "everything's gonna be alright" );
  225. } );
  226. QUnit.test( "computeBoundingBox", ( assert ) => {
  227. var a = new Geometry().fromBufferGeometry( new DodecahedronBufferGeometry() );
  228. a.computeBoundingBox();
  229. assert.strictEqual( a.boundingBox.isEmpty(), false, "Bounding box isn't empty" );
  230. var allIn = true;
  231. for ( var i = 0; i < a.vertices.length; i ++ ) {
  232. if ( ! a.boundingBox.containsPoint( a.vertices[ i ] ) ) {
  233. allIn = false;
  234. }
  235. }
  236. assert.strictEqual( allIn, true, "All vertices are inside the box" );
  237. } );
  238. QUnit.test( "computeBoundingSphere", ( assert ) => {
  239. var a = new Geometry().fromBufferGeometry( new DodecahedronBufferGeometry() );
  240. a.computeBoundingSphere();
  241. var allIn = true;
  242. for ( var i = 0; i < a.vertices.length; i ++ ) {
  243. if ( ! a.boundingSphere.containsPoint( a.vertices[ i ] ) ) {
  244. allIn = false;
  245. }
  246. }
  247. assert.strictEqual( allIn, true, "All vertices are inside the bounding sphere" );
  248. } );
  249. QUnit.todo( "merge", ( assert ) => {
  250. assert.ok( false, "everything's gonna be alright" );
  251. } );
  252. QUnit.todo( "mergeMesh", ( assert ) => {
  253. assert.ok( false, "everything's gonna be alright" );
  254. } );
  255. QUnit.test( "mergeVertices", ( assert ) => {
  256. var a = new Geometry();
  257. var b = new BoxBufferGeometry( 1, 1, 1 );
  258. var verts, faces, removed;
  259. a.fromBufferGeometry( b );
  260. removed = a.mergeVertices();
  261. verts = a.vertices.length;
  262. faces = a.faces.length;
  263. assert.strictEqual( removed, 16, "Removed the expected number of vertices" );
  264. assert.strictEqual( verts, 8, "Minimum number of vertices remaining" );
  265. assert.strictEqual( faces, 12, "Minimum number of faces remaining" );
  266. } );
  267. QUnit.test( "sortFacesByMaterialIndex", ( assert ) => {
  268. var box = new BoxBufferGeometry( 1, 1, 1 );
  269. var a = new Geometry().fromBufferGeometry( box );
  270. var expected = [ 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5 ];
  271. a.faces.reverse(); // a bit too simple probably, still missing stuff like checking new UVs
  272. a.sortFacesByMaterialIndex();
  273. var indices = [];
  274. for ( var i = 0; i < a.faces.length; i ++ ) {
  275. indices.push( a.faces[ i ].materialIndex );
  276. }
  277. assert.deepEqual( indices, expected, "Faces in correct order" );
  278. } );
  279. QUnit.test( "toJSON", ( assert ) => {
  280. var a = getGeometry();
  281. var gold = {
  282. "metadata": {
  283. "version": 4.5,
  284. "type": "Geometry",
  285. "generator": "Geometry.toJSON"
  286. },
  287. "uuid": null,
  288. "type": "Geometry",
  289. "data": {
  290. "vertices": [ - 0.5, 0, 0, 0.5, 0, 0, 0, 1, 0 ],
  291. "normals": [ 0, 0, 1 ],
  292. "faces": [ 50, 0, 1, 2, 0, 0, 0, 0, 0 ]
  293. }
  294. };
  295. var json;
  296. a.faces.push( new Face3( 0, 1, 2 ) );
  297. a.computeFaceNormals();
  298. a.computeVertexNormals();
  299. json = a.toJSON();
  300. json.uuid = null;
  301. assert.deepEqual( json, gold, "Generated JSON is as expected" );
  302. } );
  303. QUnit.todo( "clone", ( assert ) => {
  304. assert.ok( false, "everything's gonna be alright" );
  305. } );
  306. QUnit.todo( "copy", ( assert ) => {
  307. assert.ok( false, "everything's gonna be alright" );
  308. } );
  309. QUnit.todo( "dispose", ( assert ) => {
  310. assert.ok( false, "everything's gonna be alright" );
  311. } );
  312. } );
  313. } );