SimplifyModifier.js 9.4 KB

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