SimplifyModifier.js 10 KB

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