SimplifyModifier.js 9.7 KB

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