1
0

SimplifyModifier.js 9.5 KB

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