EdgesGeometry.tests.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /* global QUnit */
  2. import { EdgesGeometry } from '../../../../src/geometries/EdgesGeometry.js';
  3. import { BufferGeometry } from '../../../../src/core/BufferGeometry.js';
  4. import { BufferAttribute } from '../../../../src/core/BufferAttribute.js';
  5. import { Vector3 } from '../../../../src/math/Vector3.js';
  6. // DEBUGGING
  7. import { Scene } from '../../../../src/scenes/Scene.js';
  8. import { Mesh } from '../../../../src/objects/Mesh.js';
  9. import { LineSegments } from '../../../../src/objects/LineSegments.js';
  10. import { LineBasicMaterial } from '../../../../src/materials/LineBasicMaterial.js';
  11. import { WebGLRenderer } from '../../../../src/renderers/WebGLRenderer.js';
  12. import { PerspectiveCamera } from '../../../../src/cameras/PerspectiveCamera.js';
  13. //
  14. // HELPERS
  15. //
  16. function testEdges( vertList, idxList, numAfter, assert ) {
  17. const geoms = createGeometries( vertList, idxList );
  18. for ( let i = 0; i < geoms.length; i ++ ) {
  19. const geom = geoms[ i ];
  20. const numBefore = idxList.length;
  21. assert.equal( countEdges( geom ), numBefore, 'Edges before!' );
  22. const egeom = new EdgesGeometry( geom );
  23. assert.equal( countEdges( egeom ), numAfter, 'Edges after!' );
  24. output( geom, egeom );
  25. }
  26. }
  27. function createGeometries( vertList, idxList ) {
  28. const geomIB = createIndexedBufferGeometry( vertList, idxList );
  29. const geomDC = addDrawCalls( geomIB.clone() );
  30. return [ geomIB, geomDC ];
  31. }
  32. function createIndexedBufferGeometry( vertList, idxList ) {
  33. const geom = new BufferGeometry();
  34. const indexTable = [];
  35. const numTris = idxList.length / 3;
  36. let numVerts = 0;
  37. const indices = new Uint32Array( numTris * 3 );
  38. let vertices = new Float32Array( vertList.length * 3 );
  39. for ( let i = 0; i < numTris; i ++ ) {
  40. for ( let j = 0; j < 3; j ++ ) {
  41. const idx = idxList[ 3 * i + j ];
  42. if ( indexTable[ idx ] === undefined ) {
  43. const v = vertList[ idx ];
  44. vertices[ 3 * numVerts ] = v.x;
  45. vertices[ 3 * numVerts + 1 ] = v.y;
  46. vertices[ 3 * numVerts + 2 ] = v.z;
  47. indexTable[ idx ] = numVerts;
  48. numVerts ++;
  49. }
  50. indices[ 3 * i + j ] = indexTable[ idx ];
  51. }
  52. }
  53. vertices = vertices.subarray( 0, 3 * numVerts );
  54. geom.setIndex( new BufferAttribute( indices, 1 ) );
  55. geom.setAttribute( 'position', new BufferAttribute( vertices, 3 ) );
  56. return geom;
  57. }
  58. function addDrawCalls( geometry ) {
  59. const numTris = geometry.index.count / 3;
  60. for ( let i = 0; i < numTris; i ++ ) {
  61. const start = i * 3;
  62. const count = 3;
  63. geometry.addGroup( start, count );
  64. }
  65. return geometry;
  66. }
  67. function countEdges( geom ) {
  68. if ( geom instanceof EdgesGeometry ) {
  69. return geom.getAttribute( 'position' ).count / 2;
  70. }
  71. if ( geom.faces !== undefined ) {
  72. return geom.faces.length * 3;
  73. }
  74. const indices = geom.index;
  75. if ( indices ) {
  76. return indices.count;
  77. }
  78. return geom.getAttribute( 'position' ).count;
  79. }
  80. //
  81. // DEBUGGING
  82. //
  83. const DEBUG = false;
  84. let renderer;
  85. let camera;
  86. const scene = new Scene();
  87. let xoffset = 0;
  88. function output( geom, egeom ) {
  89. if ( DEBUG !== true ) return;
  90. if ( ! renderer ) initDebug();
  91. const mesh = new Mesh( geom, undefined );
  92. const edges = new LineSegments( egeom, new LineBasicMaterial( { color: 'black' } ) );
  93. mesh.position.setX( xoffset );
  94. edges.position.setX( xoffset ++ );
  95. scene.add( mesh );
  96. scene.add( edges );
  97. if ( scene.children.length % 8 === 0 ) {
  98. xoffset += 2;
  99. }
  100. }
  101. function initDebug() {
  102. renderer = new WebGLRenderer( {
  103. antialias: true
  104. } );
  105. const width = 600;
  106. const height = 480;
  107. renderer.setSize( width, height );
  108. renderer.setClearColor( 0xCCCCCC );
  109. camera = new PerspectiveCamera( 45, width / height, 1, 100 );
  110. camera.position.x = 30;
  111. camera.position.z = 40;
  112. camera.lookAt( new Vector3( 30, 0, 0 ) );
  113. document.body.appendChild( renderer.domElement );
  114. const controls = new THREE.OrbitControls( camera, renderer.domElement ); // TODO: please do somethings for that -_-'
  115. controls.target = new Vector3( 30, 0, 0 );
  116. animate();
  117. function animate() {
  118. requestAnimationFrame( animate );
  119. controls.update();
  120. renderer.render( scene, camera );
  121. }
  122. }
  123. export default QUnit.module( 'Geometries', () => {
  124. QUnit.module( 'EdgesGeometry', () => {
  125. const vertList = [
  126. new Vector3( 0, 0, 0 ),
  127. new Vector3( 1, 0, 0 ),
  128. new Vector3( 1, 1, 0 ),
  129. new Vector3( 0, 1, 0 ),
  130. new Vector3( 1, 1, 1 ),
  131. ];
  132. // INHERITANCE
  133. QUnit.test( 'Extending', ( assert ) => {
  134. const object = new EdgesGeometry();
  135. assert.strictEqual(
  136. object instanceof BufferGeometry, true,
  137. 'EdgesGeometry extends from BufferGeometry'
  138. );
  139. } );
  140. // INSTANCING
  141. QUnit.test( 'Instancing', ( assert ) => {
  142. const object = new EdgesGeometry();
  143. assert.ok( object, 'Can instantiate an EdgesGeometry.' );
  144. } );
  145. // PROPERTIES
  146. QUnit.test( 'type', ( assert ) => {
  147. const object = new EdgesGeometry();
  148. assert.ok(
  149. object.type === 'EdgesGeometry',
  150. 'EdgesGeometry.type should be EdgesGeometry'
  151. );
  152. } );
  153. QUnit.todo( 'parameters', ( assert ) => {
  154. assert.ok( false, 'everything\'s gonna be alright' );
  155. } );
  156. // OTHERS
  157. QUnit.test( 'singularity', ( assert ) => {
  158. testEdges( vertList, [ 1, 1, 1 ], 0, assert );
  159. } );
  160. QUnit.test( 'needle', ( assert ) => {
  161. testEdges( vertList, [ 0, 0, 1 ], 0, assert );
  162. } );
  163. QUnit.test( 'single triangle', ( assert ) => {
  164. testEdges( vertList, [ 0, 1, 2 ], 3, assert );
  165. } );
  166. QUnit.test( 'two isolated triangles', ( assert ) => {
  167. const vertList = [
  168. new Vector3( 0, 0, 0 ),
  169. new Vector3( 1, 0, 0 ),
  170. new Vector3( 1, 1, 0 ),
  171. new Vector3( 0, 0, 1 ),
  172. new Vector3( 1, 0, 1 ),
  173. new Vector3( 1, 1, 1 ),
  174. ];
  175. testEdges( vertList, [ 0, 1, 2, 3, 4, 5 ], 6, assert );
  176. } );
  177. QUnit.test( 'two flat triangles', ( assert ) => {
  178. testEdges( vertList, [ 0, 1, 2, 0, 2, 3 ], 4, assert );
  179. } );
  180. QUnit.test( 'two flat triangles, inverted', ( assert ) => {
  181. testEdges( vertList, [ 0, 1, 2, 0, 3, 2 ], 5, assert );
  182. } );
  183. QUnit.test( 'two non-coplanar triangles', ( assert ) => {
  184. testEdges( vertList, [ 0, 1, 2, 0, 4, 2 ], 5, assert );
  185. } );
  186. QUnit.test( 'three triangles, coplanar first', ( assert ) => {
  187. testEdges( vertList, [ 0, 2, 3, 0, 1, 2, 0, 4, 2 ], 7, assert );
  188. } );
  189. QUnit.test( 'three triangles, coplanar last', ( assert ) => {
  190. testEdges( vertList, [ 0, 1, 2, 0, 4, 2, 0, 2, 3 ], 6, assert ); // Should be 7
  191. } );
  192. QUnit.test( 'tetrahedron', ( assert ) => {
  193. testEdges( vertList, [ 0, 1, 2, 0, 1, 4, 0, 4, 2, 1, 2, 4 ], 6, assert );
  194. } );
  195. } );
  196. } );