2
0

SimplifyModifier.js 10 KB

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