SimplifyModifier.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. ( function () {
  2. /**
  3. * Simplification Geometry Modifier
  4. * - based on code and technique
  5. * - by Stan Melax in 1998
  6. * - Progressive Mesh type Polygon Reduction Algorithm
  7. * - http://www.melax.com/polychop/
  8. */
  9. const _cb = new THREE.Vector3(),
  10. _ab = new THREE.Vector3();
  11. class SimplifyModifier {
  12. modify( geometry, count ) {
  13. geometry = geometry.clone();
  14. const attributes = geometry.attributes; // this modifier can only process indexed and non-indexed geomtries with a position attribute
  15. for ( const name in attributes ) {
  16. if ( name !== 'position' ) geometry.deleteAttribute( name );
  17. }
  18. geometry = THREE.BufferGeometryUtils.mergeVertices( geometry ); //
  19. // put data of original geometry in different data structures
  20. //
  21. const vertices = [];
  22. const faces = []; // add vertices
  23. const positionAttribute = geometry.getAttribute( 'position' );
  24. for ( let i = 0; i < positionAttribute.count; i ++ ) {
  25. const v = new THREE.Vector3().fromBufferAttribute( positionAttribute, i );
  26. const vertex = new Vertex( v );
  27. vertices.push( vertex );
  28. } // add faces
  29. let index = geometry.getIndex();
  30. if ( index !== null ) {
  31. for ( let i = 0; i < index.count; i += 3 ) {
  32. const a = index.getX( i );
  33. const b = index.getX( i + 1 );
  34. const c = index.getX( i + 2 );
  35. const triangle = new Triangle( vertices[ a ], vertices[ b ], vertices[ c ], a, b, c );
  36. faces.push( triangle );
  37. }
  38. } else {
  39. for ( let i = 0; i < positionAttribute.count; i += 3 ) {
  40. const a = i;
  41. const b = i + 1;
  42. const c = i + 2;
  43. const triangle = new Triangle( vertices[ a ], vertices[ b ], vertices[ c ], a, b, c );
  44. faces.push( triangle );
  45. }
  46. } // compute all edge collapse costs
  47. for ( let i = 0, il = vertices.length; i < il; i ++ ) {
  48. computeEdgeCostAtVertex( vertices[ i ] );
  49. }
  50. let nextVertex;
  51. let z = count;
  52. while ( z -- ) {
  53. nextVertex = minimumCostEdge( vertices );
  54. if ( ! nextVertex ) {
  55. console.log( 'THREE.SimplifyModifier: No next vertex' );
  56. break;
  57. }
  58. collapse( vertices, faces, nextVertex, nextVertex.collapseNeighbor );
  59. } //
  60. const simplifiedGeometry = new THREE.BufferGeometry();
  61. const position = [];
  62. index = []; //
  63. for ( let i = 0; i < vertices.length; i ++ ) {
  64. const vertex = vertices[ i ].position;
  65. position.push( vertex.x, vertex.y, vertex.z ); // cache final index to GREATLY speed up faces reconstruction
  66. vertices[ i ].id = i;
  67. } //
  68. for ( let i = 0; i < faces.length; i ++ ) {
  69. const face = faces[ i ];
  70. index.push( face.v1.id, face.v2.id, face.v3.id );
  71. } //
  72. simplifiedGeometry.setAttribute( 'position', new THREE.Float32BufferAttribute( position, 3 ) );
  73. simplifiedGeometry.setIndex( index );
  74. return simplifiedGeometry;
  75. }
  76. }
  77. function pushIfUnique( array, object ) {
  78. if ( array.indexOf( object ) === - 1 ) array.push( object );
  79. }
  80. function removeFromArray( array, object ) {
  81. const k = array.indexOf( object );
  82. if ( k > - 1 ) array.splice( k, 1 );
  83. }
  84. function computeEdgeCollapseCost( u, v ) {
  85. // if we collapse edge uv by moving u to v then how
  86. // much different will the model change, i.e. the "error".
  87. const edgelength = v.position.distanceTo( u.position );
  88. let curvature = 0;
  89. const sideFaces = []; // find the "sides" triangles that are on the edge uv
  90. for ( let i = 0, il = u.faces.length; i < il; i ++ ) {
  91. const face = u.faces[ i ];
  92. if ( face.hasVertex( v ) ) {
  93. sideFaces.push( face );
  94. }
  95. } // use the triangle facing most away from the sides
  96. // to determine our curvature term
  97. for ( let i = 0, il = u.faces.length; i < il; i ++ ) {
  98. let minCurvature = 1;
  99. const face = u.faces[ i ];
  100. for ( let j = 0; j < sideFaces.length; j ++ ) {
  101. const sideFace = sideFaces[ j ]; // use dot product of face normals.
  102. const dotProd = face.normal.dot( sideFace.normal );
  103. minCurvature = Math.min( minCurvature, ( 1.001 - dotProd ) / 2 );
  104. }
  105. curvature = Math.max( curvature, minCurvature );
  106. } // crude approach in attempt to preserve borders
  107. // though it seems not to be totally correct
  108. const borders = 0;
  109. if ( sideFaces.length < 2 ) {
  110. // we add some arbitrary cost for borders,
  111. // borders += 10;
  112. curvature = 1;
  113. }
  114. const amt = edgelength * curvature + borders;
  115. return amt;
  116. }
  117. function computeEdgeCostAtVertex( v ) {
  118. // compute the edge collapse cost for all edges that start
  119. // from vertex v. Since we are only interested in reducing
  120. // the object by selecting the min cost edge at each step, we
  121. // only cache the cost of the least cost edge at this vertex
  122. // (in member variable collapse) as well as the value of the
  123. // cost (in member variable collapseCost).
  124. if ( v.neighbors.length === 0 ) {
  125. // collapse if no neighbors.
  126. v.collapseNeighbor = null;
  127. v.collapseCost = - 0.01;
  128. return;
  129. }
  130. v.collapseCost = 100000;
  131. v.collapseNeighbor = null; // search all neighboring edges for "least cost" edge
  132. for ( let i = 0; i < v.neighbors.length; i ++ ) {
  133. const collapseCost = computeEdgeCollapseCost( v, v.neighbors[ i ] );
  134. if ( ! v.collapseNeighbor ) {
  135. v.collapseNeighbor = v.neighbors[ i ];
  136. v.collapseCost = collapseCost;
  137. v.minCost = collapseCost;
  138. v.totalCost = 0;
  139. v.costCount = 0;
  140. }
  141. v.costCount ++;
  142. v.totalCost += collapseCost;
  143. if ( collapseCost < v.minCost ) {
  144. v.collapseNeighbor = v.neighbors[ i ];
  145. v.minCost = collapseCost;
  146. }
  147. } // we average the cost of collapsing at this vertex
  148. v.collapseCost = v.totalCost / v.costCount; // v.collapseCost = v.minCost;
  149. }
  150. function removeVertex( v, vertices ) {
  151. console.assert( v.faces.length === 0 );
  152. while ( v.neighbors.length ) {
  153. const n = v.neighbors.pop();
  154. removeFromArray( n.neighbors, v );
  155. }
  156. removeFromArray( vertices, v );
  157. }
  158. function removeFace( f, faces ) {
  159. removeFromArray( faces, f );
  160. if ( f.v1 ) removeFromArray( f.v1.faces, f );
  161. if ( f.v2 ) removeFromArray( f.v2.faces, f );
  162. if ( f.v3 ) removeFromArray( f.v3.faces, f ); // TODO optimize this!
  163. const vs = [ f.v1, f.v2, f.v3 ];
  164. for ( let i = 0; i < 3; i ++ ) {
  165. const v1 = vs[ i ];
  166. const v2 = vs[ ( i + 1 ) % 3 ];
  167. if ( ! v1 || ! v2 ) continue;
  168. v1.removeIfNonNeighbor( v2 );
  169. v2.removeIfNonNeighbor( v1 );
  170. }
  171. }
  172. function collapse( vertices, faces, u, v ) {
  173. // u and v are pointers to vertices of an edge
  174. // Collapse the edge uv by moving vertex u onto v
  175. if ( ! v ) {
  176. // u is a vertex all by itself so just delete it..
  177. removeVertex( u, vertices );
  178. return;
  179. }
  180. const tmpVertices = [];
  181. for ( let i = 0; i < u.neighbors.length; i ++ ) {
  182. tmpVertices.push( u.neighbors[ i ] );
  183. } // delete triangles on edge uv:
  184. for ( let i = u.faces.length - 1; i >= 0; i -- ) {
  185. if ( u.faces[ i ] && u.faces[ i ].hasVertex( v ) ) {
  186. removeFace( u.faces[ i ], faces );
  187. }
  188. } // update remaining triangles to have v instead of u
  189. for ( let i = u.faces.length - 1; i >= 0; i -- ) {
  190. u.faces[ i ].replaceVertex( u, v );
  191. }
  192. removeVertex( u, vertices ); // recompute the edge collapse costs in neighborhood
  193. for ( let i = 0; i < tmpVertices.length; i ++ ) {
  194. computeEdgeCostAtVertex( tmpVertices[ i ] );
  195. }
  196. }
  197. function minimumCostEdge( vertices ) {
  198. // O(n * n) approach. TODO optimize this
  199. let least = vertices[ 0 ];
  200. for ( let i = 0; i < vertices.length; i ++ ) {
  201. if ( vertices[ i ].collapseCost < least.collapseCost ) {
  202. least = vertices[ i ];
  203. }
  204. }
  205. return least;
  206. } // we use a triangle class to represent structure of face slightly differently
  207. class Triangle {
  208. constructor( v1, v2, v3, a, b, c ) {
  209. this.a = a;
  210. this.b = b;
  211. this.c = c;
  212. this.v1 = v1;
  213. this.v2 = v2;
  214. this.v3 = v3;
  215. this.normal = new THREE.Vector3();
  216. this.computeNormal();
  217. v1.faces.push( this );
  218. v1.addUniqueNeighbor( v2 );
  219. v1.addUniqueNeighbor( v3 );
  220. v2.faces.push( this );
  221. v2.addUniqueNeighbor( v1 );
  222. v2.addUniqueNeighbor( v3 );
  223. v3.faces.push( this );
  224. v3.addUniqueNeighbor( v1 );
  225. v3.addUniqueNeighbor( v2 );
  226. }
  227. computeNormal() {
  228. const vA = this.v1.position;
  229. const vB = this.v2.position;
  230. const vC = this.v3.position;
  231. _cb.subVectors( vC, vB );
  232. _ab.subVectors( vA, vB );
  233. _cb.cross( _ab ).normalize();
  234. this.normal.copy( _cb );
  235. }
  236. hasVertex( v ) {
  237. return v === this.v1 || v === this.v2 || v === this.v3;
  238. }
  239. replaceVertex( oldv, newv ) {
  240. if ( oldv === this.v1 ) this.v1 = newv; else if ( oldv === this.v2 ) this.v2 = newv; else if ( oldv === this.v3 ) this.v3 = newv;
  241. removeFromArray( oldv.faces, this );
  242. newv.faces.push( this );
  243. oldv.removeIfNonNeighbor( this.v1 );
  244. this.v1.removeIfNonNeighbor( oldv );
  245. oldv.removeIfNonNeighbor( this.v2 );
  246. this.v2.removeIfNonNeighbor( oldv );
  247. oldv.removeIfNonNeighbor( this.v3 );
  248. this.v3.removeIfNonNeighbor( oldv );
  249. this.v1.addUniqueNeighbor( this.v2 );
  250. this.v1.addUniqueNeighbor( this.v3 );
  251. this.v2.addUniqueNeighbor( this.v1 );
  252. this.v2.addUniqueNeighbor( this.v3 );
  253. this.v3.addUniqueNeighbor( this.v1 );
  254. this.v3.addUniqueNeighbor( this.v2 );
  255. this.computeNormal();
  256. }
  257. }
  258. class Vertex {
  259. constructor( v ) {
  260. this.position = v;
  261. this.id = - 1; // external use position in vertices list (for e.g. face generation)
  262. this.faces = []; // faces vertex is connected
  263. this.neighbors = []; // neighbouring vertices aka "adjacentVertices"
  264. // these will be computed in computeEdgeCostAtVertex()
  265. this.collapseCost = 0; // cost of collapsing this vertex, the less the better. aka objdist
  266. this.collapseNeighbor = null; // best candinate for collapsing
  267. }
  268. addUniqueNeighbor( vertex ) {
  269. pushIfUnique( this.neighbors, vertex );
  270. }
  271. removeIfNonNeighbor( n ) {
  272. const neighbors = this.neighbors;
  273. const faces = this.faces;
  274. const offset = neighbors.indexOf( n );
  275. if ( offset === - 1 ) return;
  276. for ( let i = 0; i < faces.length; i ++ ) {
  277. if ( faces[ i ].hasVertex( n ) ) return;
  278. }
  279. neighbors.splice( offset, 1 );
  280. }
  281. }
  282. THREE.SimplifyModifier = SimplifyModifier;
  283. } )();