SimplifyModifier.js 9.5 KB

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