Geometry.tests.js 12 KB

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