|
@@ -13,524 +13,533 @@ import { BufferGeometryUtils } from '../utils/BufferGeometryUtils.js';
|
|
|
* - http://www.melax.com/polychop/
|
|
|
*/
|
|
|
|
|
|
-var SimplifyModifier = function () {
|
|
|
+const _cb = new Vector3(), _ab = new Vector3();
|
|
|
|
|
|
- if ( BufferGeometryUtils === undefined ) {
|
|
|
+class SimplifyModifier {
|
|
|
|
|
|
- throw 'THREE.SimplifyModifier relies on BufferGeometryUtils';
|
|
|
+ constructor() {
|
|
|
|
|
|
- }
|
|
|
+ if ( BufferGeometryUtils === undefined ) {
|
|
|
|
|
|
-};
|
|
|
+ throw 'THREE.SimplifyModifier relies on BufferGeometryUtils';
|
|
|
|
|
|
-( function () {
|
|
|
+ }
|
|
|
|
|
|
- var cb = new Vector3(), ab = new Vector3();
|
|
|
+ }
|
|
|
|
|
|
- function pushIfUnique( array, object ) {
|
|
|
+ modify( geometry, count ) {
|
|
|
|
|
|
- if ( array.indexOf( object ) === - 1 ) array.push( object );
|
|
|
+ if ( geometry.isGeometry === true ) {
|
|
|
|
|
|
- }
|
|
|
+ console.error( 'THREE.SimplifyModifier no longer supports Geometry. Use BufferGeometry instead.' );
|
|
|
+ return;
|
|
|
|
|
|
- function removeFromArray( array, object ) {
|
|
|
+ }
|
|
|
|
|
|
- var k = array.indexOf( object );
|
|
|
- if ( k > - 1 ) array.splice( k, 1 );
|
|
|
+ geometry = geometry.clone();
|
|
|
+ const attributes = geometry.attributes;
|
|
|
|
|
|
- }
|
|
|
+ // this modifier can only process indexed and non-indexed geomtries with a position attribute
|
|
|
+
|
|
|
+ for ( const name in attributes ) {
|
|
|
|
|
|
- function computeEdgeCollapseCost( u, v ) {
|
|
|
+ if ( name !== 'position' ) geometry.deleteAttribute( name );
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
- // if we collapse edge uv by moving u to v then how
|
|
|
- // much different will the model change, i.e. the "error".
|
|
|
+ geometry = BufferGeometryUtils.mergeVertices( geometry );
|
|
|
|
|
|
- var edgelength = v.position.distanceTo( u.position );
|
|
|
- var curvature = 0;
|
|
|
+ //
|
|
|
+ // put data of original geometry in different data structures
|
|
|
+ //
|
|
|
|
|
|
- var sideFaces = [];
|
|
|
- var i, il = u.faces.length, face, sideFace;
|
|
|
+ const vertices = [];
|
|
|
+ const faces = [];
|
|
|
|
|
|
- // find the "sides" triangles that are on the edge uv
|
|
|
- for ( i = 0; i < il; i ++ ) {
|
|
|
+ // add vertices
|
|
|
|
|
|
- face = u.faces[ i ];
|
|
|
+ const positionAttribute = geometry.getAttribute( 'position' );
|
|
|
|
|
|
- if ( face.hasVertex( v ) ) {
|
|
|
+ for ( let i = 0; i < positionAttribute.count; i ++ ) {
|
|
|
|
|
|
- sideFaces.push( face );
|
|
|
+ const v = new Vector3().fromBufferAttribute( positionAttribute, i );
|
|
|
|
|
|
- }
|
|
|
+ const vertex = new Vertex( v, i );
|
|
|
+ vertices.push( vertex );
|
|
|
|
|
|
}
|
|
|
|
|
|
- // use the triangle facing most away from the sides
|
|
|
- // to determine our curvature term
|
|
|
- for ( i = 0; i < il; i ++ ) {
|
|
|
+ // add faces
|
|
|
|
|
|
- var minCurvature = 1;
|
|
|
- face = u.faces[ i ];
|
|
|
+ let index = geometry.getIndex();
|
|
|
|
|
|
- for ( var j = 0; j < sideFaces.length; j ++ ) {
|
|
|
+ if ( index !== null ) {
|
|
|
+
|
|
|
+ for ( let i = 0; i < index.count; i += 3 ) {
|
|
|
|
|
|
- sideFace = sideFaces[ j ];
|
|
|
- // use dot product of face normals.
|
|
|
- var dotProd = face.normal.dot( sideFace.normal );
|
|
|
- minCurvature = Math.min( minCurvature, ( 1.001 - dotProd ) / 2 );
|
|
|
+ const a = index.getX( i );
|
|
|
+ const b = index.getX( i + 1 );
|
|
|
+ const c = index.getX( i + 2 );
|
|
|
+
|
|
|
+ const triangle = new Triangle( vertices[ a ], vertices[ b ], vertices[ c ], a, b, c );
|
|
|
+ faces.push( triangle );
|
|
|
|
|
|
}
|
|
|
|
|
|
- curvature = Math.max( curvature, minCurvature );
|
|
|
+ } else {
|
|
|
|
|
|
- }
|
|
|
+ for ( let i = 0; i < positionAttribute.count; i += 3 ) {
|
|
|
+
|
|
|
+ const a = i;
|
|
|
+ const b = i + 1;
|
|
|
+ const c = i + 2;
|
|
|
|
|
|
- // crude approach in attempt to preserve borders
|
|
|
- // though it seems not to be totally correct
|
|
|
- var borders = 0;
|
|
|
- if ( sideFaces.length < 2 ) {
|
|
|
+ const triangle = new Triangle( vertices[ a ], vertices[ b ], vertices[ c ], a, b, c );
|
|
|
+ faces.push( triangle );
|
|
|
|
|
|
- // we add some arbitrary cost for borders,
|
|
|
- // borders += 10;
|
|
|
- curvature = 1;
|
|
|
+ }
|
|
|
|
|
|
}
|
|
|
|
|
|
- var amt = edgelength * curvature + borders;
|
|
|
+ // compute all edge collapse costs
|
|
|
|
|
|
- return amt;
|
|
|
+ for ( let i = 0, il = vertices.length; i < il; i ++ ) {
|
|
|
|
|
|
- }
|
|
|
+ computeEdgeCostAtVertex( vertices[ i ] );
|
|
|
|
|
|
- function computeEdgeCostAtVertex( v ) {
|
|
|
+ }
|
|
|
|
|
|
- // compute the edge collapse cost for all edges that start
|
|
|
- // from vertex v. Since we are only interested in reducing
|
|
|
- // the object by selecting the min cost edge at each step, we
|
|
|
- // only cache the cost of the least cost edge at this vertex
|
|
|
- // (in member variable collapse) as well as the value of the
|
|
|
- // cost (in member variable collapseCost).
|
|
|
+ let nextVertex;
|
|
|
|
|
|
- if ( v.neighbors.length === 0 ) {
|
|
|
+ let z = count;
|
|
|
|
|
|
- // collapse if no neighbors.
|
|
|
- v.collapseNeighbor = null;
|
|
|
- v.collapseCost = - 0.01;
|
|
|
+ while ( z -- ) {
|
|
|
|
|
|
- return;
|
|
|
+ nextVertex = minimumCostEdge( vertices );
|
|
|
|
|
|
- }
|
|
|
+ if ( ! nextVertex ) {
|
|
|
|
|
|
- v.collapseCost = 100000;
|
|
|
- v.collapseNeighbor = null;
|
|
|
+ console.log( 'THREE.SimplifyModifier: No next vertex' );
|
|
|
+ break;
|
|
|
|
|
|
- // search all neighboring edges for "least cost" edge
|
|
|
- for ( var i = 0; i < v.neighbors.length; i ++ ) {
|
|
|
+ }
|
|
|
|
|
|
- var collapseCost = computeEdgeCollapseCost( v, v.neighbors[ i ] );
|
|
|
+ collapse( vertices, faces, nextVertex, nextVertex.collapseNeighbor );
|
|
|
|
|
|
- if ( ! v.collapseNeighbor ) {
|
|
|
+ }
|
|
|
|
|
|
- v.collapseNeighbor = v.neighbors[ i ];
|
|
|
- v.collapseCost = collapseCost;
|
|
|
- v.minCost = collapseCost;
|
|
|
- v.totalCost = 0;
|
|
|
- v.costCount = 0;
|
|
|
+ //
|
|
|
|
|
|
- }
|
|
|
+ const simplifiedGeometry = new BufferGeometry();
|
|
|
+ const position = [];
|
|
|
|
|
|
- v.costCount ++;
|
|
|
- v.totalCost += collapseCost;
|
|
|
+ index = [];
|
|
|
|
|
|
- if ( collapseCost < v.minCost ) {
|
|
|
+ //
|
|
|
|
|
|
- v.collapseNeighbor = v.neighbors[ i ];
|
|
|
- v.minCost = collapseCost;
|
|
|
+ for ( let i = 0; i < vertices.length; i ++ ) {
|
|
|
|
|
|
- }
|
|
|
+ const vertex = vertices[ i ].position;
|
|
|
+ position.push( vertex.x, vertex.y, vertex.z );
|
|
|
|
|
|
}
|
|
|
|
|
|
- // we average the cost of collapsing at this vertex
|
|
|
- v.collapseCost = v.totalCost / v.costCount;
|
|
|
- // v.collapseCost = v.minCost;
|
|
|
-
|
|
|
- }
|
|
|
+ //
|
|
|
|
|
|
- function removeVertex( v, vertices ) {
|
|
|
+ for ( let i = 0; i < faces.length; i ++ ) {
|
|
|
|
|
|
- console.assert( v.faces.length === 0 );
|
|
|
+ const face = faces[ i ];
|
|
|
|
|
|
- while ( v.neighbors.length ) {
|
|
|
+ const a = vertices.indexOf( face.v1 );
|
|
|
+ const b = vertices.indexOf( face.v2 );
|
|
|
+ const c = vertices.indexOf( face.v3 );
|
|
|
|
|
|
- var n = v.neighbors.pop();
|
|
|
- removeFromArray( n.neighbors, v );
|
|
|
+ index.push( a, b, c );
|
|
|
|
|
|
}
|
|
|
|
|
|
- removeFromArray( vertices, v );
|
|
|
+ //
|
|
|
+
|
|
|
+ simplifiedGeometry.setAttribute( 'position', new Float32BufferAttribute( position, 3 ) );
|
|
|
+ simplifiedGeometry.setIndex( index );
|
|
|
+
|
|
|
+ return simplifiedGeometry;
|
|
|
|
|
|
}
|
|
|
|
|
|
- function removeFace( f, faces ) {
|
|
|
+}
|
|
|
|
|
|
- removeFromArray( faces, f );
|
|
|
+function pushIfUnique( array, object ) {
|
|
|
|
|
|
- if ( f.v1 ) removeFromArray( f.v1.faces, f );
|
|
|
- if ( f.v2 ) removeFromArray( f.v2.faces, f );
|
|
|
- if ( f.v3 ) removeFromArray( f.v3.faces, f );
|
|
|
+ if ( array.indexOf( object ) === - 1 ) array.push( object );
|
|
|
|
|
|
- // TODO optimize this!
|
|
|
- var vs = [ f.v1, f.v2, f.v3 ];
|
|
|
- var v1, v2;
|
|
|
+}
|
|
|
|
|
|
- for ( var i = 0; i < 3; i ++ ) {
|
|
|
+function removeFromArray( array, object ) {
|
|
|
|
|
|
- v1 = vs[ i ];
|
|
|
- v2 = vs[ ( i + 1 ) % 3 ];
|
|
|
+ var k = array.indexOf( object );
|
|
|
+ if ( k > - 1 ) array.splice( k, 1 );
|
|
|
|
|
|
- if ( ! v1 || ! v2 ) continue;
|
|
|
+}
|
|
|
|
|
|
- v1.removeIfNonNeighbor( v2 );
|
|
|
- v2.removeIfNonNeighbor( v1 );
|
|
|
+function computeEdgeCollapseCost( u, v ) {
|
|
|
|
|
|
- }
|
|
|
+ // if we collapse edge uv by moving u to v then how
|
|
|
+ // much different will the model change, i.e. the "error".
|
|
|
|
|
|
- }
|
|
|
+ const edgelength = v.position.distanceTo( u.position );
|
|
|
+ let curvature = 0;
|
|
|
|
|
|
- function collapse( vertices, faces, u, v ) { // u and v are pointers to vertices of an edge
|
|
|
+ const sideFaces = [];
|
|
|
|
|
|
- // Collapse the edge uv by moving vertex u onto v
|
|
|
+ // find the "sides" triangles that are on the edge uv
|
|
|
+ for ( let i = 0, il = u.faces.length; i < il; i ++ ) {
|
|
|
|
|
|
- if ( ! v ) {
|
|
|
+ const face = u.faces[ i ];
|
|
|
|
|
|
- // u is a vertex all by itself so just delete it..
|
|
|
- removeVertex( u, vertices );
|
|
|
- return;
|
|
|
+ if ( face.hasVertex( v ) ) {
|
|
|
+
|
|
|
+ sideFaces.push( face );
|
|
|
|
|
|
}
|
|
|
|
|
|
- var i;
|
|
|
- var tmpVertices = [];
|
|
|
+ }
|
|
|
+
|
|
|
+ // use the triangle facing most away from the sides
|
|
|
+ // to determine our curvature term
|
|
|
+ for ( let i = 0, il = u.faces.length; i < il; i ++ ) {
|
|
|
|
|
|
- for ( i = 0; i < u.neighbors.length; i ++ ) {
|
|
|
+ let minCurvature = 1;
|
|
|
+ const face = u.faces[ i ];
|
|
|
|
|
|
- tmpVertices.push( u.neighbors[ i ] );
|
|
|
+ for ( let j = 0; j < sideFaces.length; j ++ ) {
|
|
|
+
|
|
|
+ const sideFace = sideFaces[ j ];
|
|
|
+ // use dot product of face normals.
|
|
|
+ const dotProd = face.normal.dot( sideFace.normal );
|
|
|
+ minCurvature = Math.min( minCurvature, ( 1.001 - dotProd ) / 2 );
|
|
|
|
|
|
}
|
|
|
|
|
|
+ curvature = Math.max( curvature, minCurvature );
|
|
|
|
|
|
- // delete triangles on edge uv:
|
|
|
- for ( i = u.faces.length - 1; i >= 0; i -- ) {
|
|
|
+ }
|
|
|
|
|
|
- if ( u.faces[ i ].hasVertex( v ) ) {
|
|
|
+ // crude approach in attempt to preserve borders
|
|
|
+ // though it seems not to be totally correct
|
|
|
+ const borders = 0;
|
|
|
|
|
|
- removeFace( u.faces[ i ], faces );
|
|
|
+ if ( sideFaces.length < 2 ) {
|
|
|
|
|
|
- }
|
|
|
+ // we add some arbitrary cost for borders,
|
|
|
+ // borders += 10;
|
|
|
+ curvature = 1;
|
|
|
|
|
|
- }
|
|
|
+ }
|
|
|
|
|
|
- // update remaining triangles to have v instead of u
|
|
|
- for ( i = u.faces.length - 1; i >= 0; i -- ) {
|
|
|
+ const amt = edgelength * curvature + borders;
|
|
|
|
|
|
- u.faces[ i ].replaceVertex( u, v );
|
|
|
+ return amt;
|
|
|
|
|
|
- }
|
|
|
+}
|
|
|
|
|
|
+function computeEdgeCostAtVertex( v ) {
|
|
|
|
|
|
- removeVertex( u, vertices );
|
|
|
+ // compute the edge collapse cost for all edges that start
|
|
|
+ // from vertex v. Since we are only interested in reducing
|
|
|
+ // the object by selecting the min cost edge at each step, we
|
|
|
+ // only cache the cost of the least cost edge at this vertex
|
|
|
+ // (in member variable collapse) as well as the value of the
|
|
|
+ // cost (in member variable collapseCost).
|
|
|
|
|
|
- // recompute the edge collapse costs in neighborhood
|
|
|
- for ( i = 0; i < tmpVertices.length; i ++ ) {
|
|
|
+ if ( v.neighbors.length === 0 ) {
|
|
|
|
|
|
- computeEdgeCostAtVertex( tmpVertices[ i ] );
|
|
|
+ // collapse if no neighbors.
|
|
|
+ v.collapseNeighbor = null;
|
|
|
+ v.collapseCost = - 0.01;
|
|
|
|
|
|
- }
|
|
|
+ return;
|
|
|
|
|
|
}
|
|
|
|
|
|
+ v.collapseCost = 100000;
|
|
|
+ v.collapseNeighbor = null;
|
|
|
|
|
|
+ // search all neighboring edges for "least cost" edge
|
|
|
+ for ( let i = 0; i < v.neighbors.length; i ++ ) {
|
|
|
|
|
|
- function minimumCostEdge( vertices ) {
|
|
|
+ const collapseCost = computeEdgeCollapseCost( v, v.neighbors[ i ] );
|
|
|
|
|
|
- // O(n * n) approach. TODO optimize this
|
|
|
+ if ( ! v.collapseNeighbor ) {
|
|
|
|
|
|
- var least = vertices[ 0 ];
|
|
|
+ v.collapseNeighbor = v.neighbors[ i ];
|
|
|
+ v.collapseCost = collapseCost;
|
|
|
+ v.minCost = collapseCost;
|
|
|
+ v.totalCost = 0;
|
|
|
+ v.costCount = 0;
|
|
|
|
|
|
- for ( var i = 0; i < vertices.length; i ++ ) {
|
|
|
+ }
|
|
|
|
|
|
- if ( vertices[ i ].collapseCost < least.collapseCost ) {
|
|
|
+ v.costCount ++;
|
|
|
+ v.totalCost += collapseCost;
|
|
|
|
|
|
- least = vertices[ i ];
|
|
|
+ if ( collapseCost < v.minCost ) {
|
|
|
|
|
|
- }
|
|
|
+ v.collapseNeighbor = v.neighbors[ i ];
|
|
|
+ v.minCost = collapseCost;
|
|
|
|
|
|
}
|
|
|
|
|
|
- return least;
|
|
|
-
|
|
|
}
|
|
|
|
|
|
- // we use a triangle class to represent structure of face slightly differently
|
|
|
+ // we average the cost of collapsing at this vertex
|
|
|
+ v.collapseCost = v.totalCost / v.costCount;
|
|
|
+ // v.collapseCost = v.minCost;
|
|
|
|
|
|
- function Triangle( v1, v2, v3, a, b, c ) {
|
|
|
+}
|
|
|
|
|
|
- this.a = a;
|
|
|
- this.b = b;
|
|
|
- this.c = c;
|
|
|
+function removeVertex( v, vertices ) {
|
|
|
|
|
|
- this.v1 = v1;
|
|
|
- this.v2 = v2;
|
|
|
- this.v3 = v3;
|
|
|
+ console.assert( v.faces.length === 0 );
|
|
|
|
|
|
- this.normal = new Vector3();
|
|
|
+ while ( v.neighbors.length ) {
|
|
|
|
|
|
- this.computeNormal();
|
|
|
+ const n = v.neighbors.pop();
|
|
|
+ removeFromArray( n.neighbors, v );
|
|
|
|
|
|
- v1.faces.push( this );
|
|
|
- v1.addUniqueNeighbor( v2 );
|
|
|
- v1.addUniqueNeighbor( v3 );
|
|
|
+ }
|
|
|
|
|
|
- v2.faces.push( this );
|
|
|
- v2.addUniqueNeighbor( v1 );
|
|
|
- v2.addUniqueNeighbor( v3 );
|
|
|
+ removeFromArray( vertices, v );
|
|
|
|
|
|
+}
|
|
|
|
|
|
- v3.faces.push( this );
|
|
|
- v3.addUniqueNeighbor( v1 );
|
|
|
- v3.addUniqueNeighbor( v2 );
|
|
|
+function removeFace( f, faces ) {
|
|
|
|
|
|
- }
|
|
|
+ removeFromArray( faces, f );
|
|
|
|
|
|
- Triangle.prototype.computeNormal = function () {
|
|
|
+ if ( f.v1 ) removeFromArray( f.v1.faces, f );
|
|
|
+ if ( f.v2 ) removeFromArray( f.v2.faces, f );
|
|
|
+ if ( f.v3 ) removeFromArray( f.v3.faces, f );
|
|
|
|
|
|
- var vA = this.v1.position;
|
|
|
- var vB = this.v2.position;
|
|
|
- var vC = this.v3.position;
|
|
|
+ // TODO optimize this!
|
|
|
+ const vs = [ f.v1, f.v2, f.v3 ];
|
|
|
|
|
|
- cb.subVectors( vC, vB );
|
|
|
- ab.subVectors( vA, vB );
|
|
|
- cb.cross( ab ).normalize();
|
|
|
+ for ( let i = 0; i < 3; i ++ ) {
|
|
|
|
|
|
- this.normal.copy( cb );
|
|
|
+ const v1 = vs[ i ];
|
|
|
+ const v2 = vs[ ( i + 1 ) % 3 ];
|
|
|
|
|
|
- };
|
|
|
+ if ( ! v1 || ! v2 ) continue;
|
|
|
|
|
|
- Triangle.prototype.hasVertex = function ( v ) {
|
|
|
+ v1.removeIfNonNeighbor( v2 );
|
|
|
+ v2.removeIfNonNeighbor( v1 );
|
|
|
|
|
|
- return v === this.v1 || v === this.v2 || v === this.v3;
|
|
|
+ }
|
|
|
|
|
|
- };
|
|
|
+}
|
|
|
|
|
|
- Triangle.prototype.replaceVertex = function ( oldv, newv ) {
|
|
|
+function collapse( vertices, faces, u, v ) { // u and v are pointers to vertices of an edge
|
|
|
|
|
|
- if ( oldv === this.v1 ) this.v1 = newv;
|
|
|
- else if ( oldv === this.v2 ) this.v2 = newv;
|
|
|
- else if ( oldv === this.v3 ) this.v3 = newv;
|
|
|
+ // Collapse the edge uv by moving vertex u onto v
|
|
|
|
|
|
- removeFromArray( oldv.faces, this );
|
|
|
- newv.faces.push( this );
|
|
|
+ if ( ! v ) {
|
|
|
|
|
|
+ // u is a vertex all by itself so just delete it..
|
|
|
+ removeVertex( u, vertices );
|
|
|
+ return;
|
|
|
|
|
|
- oldv.removeIfNonNeighbor( this.v1 );
|
|
|
- this.v1.removeIfNonNeighbor( oldv );
|
|
|
+ }
|
|
|
|
|
|
- oldv.removeIfNonNeighbor( this.v2 );
|
|
|
- this.v2.removeIfNonNeighbor( oldv );
|
|
|
+ const tmpVertices = [];
|
|
|
|
|
|
- oldv.removeIfNonNeighbor( this.v3 );
|
|
|
- this.v3.removeIfNonNeighbor( oldv );
|
|
|
+ for ( let i = 0; i < u.neighbors.length; i ++ ) {
|
|
|
|
|
|
- this.v1.addUniqueNeighbor( this.v2 );
|
|
|
- this.v1.addUniqueNeighbor( this.v3 );
|
|
|
+ tmpVertices.push( u.neighbors[ i ] );
|
|
|
|
|
|
- this.v2.addUniqueNeighbor( this.v1 );
|
|
|
- this.v2.addUniqueNeighbor( this.v3 );
|
|
|
+ }
|
|
|
|
|
|
- this.v3.addUniqueNeighbor( this.v1 );
|
|
|
- this.v3.addUniqueNeighbor( this.v2 );
|
|
|
|
|
|
- this.computeNormal();
|
|
|
+ // delete triangles on edge uv:
|
|
|
+ for ( let i = u.faces.length - 1; i >= 0; i -- ) {
|
|
|
|
|
|
- };
|
|
|
+ if ( u.faces[ i ].hasVertex( v ) ) {
|
|
|
|
|
|
- function Vertex( v, id ) {
|
|
|
+ removeFace( u.faces[ i ], faces );
|
|
|
|
|
|
- this.position = v;
|
|
|
+ }
|
|
|
|
|
|
- this.id = id; // old index id
|
|
|
+ }
|
|
|
|
|
|
- this.faces = []; // faces vertex is connected
|
|
|
- this.neighbors = []; // neighbouring vertices aka "adjacentVertices"
|
|
|
+ // update remaining triangles to have v instead of u
|
|
|
+ for ( let i = u.faces.length - 1; i >= 0; i -- ) {
|
|
|
|
|
|
- // these will be computed in computeEdgeCostAtVertex()
|
|
|
- this.collapseCost = 0; // cost of collapsing this vertex, the less the better. aka objdist
|
|
|
- this.collapseNeighbor = null; // best candinate for collapsing
|
|
|
+ u.faces[ i ].replaceVertex( u, v );
|
|
|
|
|
|
}
|
|
|
|
|
|
- Vertex.prototype.addUniqueNeighbor = function ( vertex ) {
|
|
|
|
|
|
- pushIfUnique( this.neighbors, vertex );
|
|
|
+ removeVertex( u, vertices );
|
|
|
|
|
|
- };
|
|
|
+ // recompute the edge collapse costs in neighborhood
|
|
|
+ for ( let i = 0; i < tmpVertices.length; i ++ ) {
|
|
|
|
|
|
- Vertex.prototype.removeIfNonNeighbor = function ( n ) {
|
|
|
+ computeEdgeCostAtVertex( tmpVertices[ i ] );
|
|
|
|
|
|
- var neighbors = this.neighbors;
|
|
|
- var faces = this.faces;
|
|
|
+ }
|
|
|
|
|
|
- var offset = neighbors.indexOf( n );
|
|
|
- if ( offset === - 1 ) return;
|
|
|
- for ( var i = 0; i < faces.length; i ++ ) {
|
|
|
+}
|
|
|
|
|
|
- if ( faces[ i ].hasVertex( n ) ) return;
|
|
|
|
|
|
- }
|
|
|
|
|
|
- neighbors.splice( offset, 1 );
|
|
|
+function minimumCostEdge( vertices ) {
|
|
|
|
|
|
- };
|
|
|
+ // O(n * n) approach. TODO optimize this
|
|
|
|
|
|
- SimplifyModifier.prototype.modify = function ( geometry, count ) {
|
|
|
+ let least = vertices[ 0 ];
|
|
|
|
|
|
- if ( geometry.isGeometry === true ) {
|
|
|
+ for ( let i = 0; i < vertices.length; i ++ ) {
|
|
|
|
|
|
- console.error( 'THREE.SimplifyModifier no longer supports Geometry. Use BufferGeometry instead.' );
|
|
|
- return;
|
|
|
+ if ( vertices[ i ].collapseCost < least.collapseCost ) {
|
|
|
+
|
|
|
+ least = vertices[ i ];
|
|
|
|
|
|
}
|
|
|
|
|
|
- geometry = geometry.clone();
|
|
|
- var attributes = geometry.attributes;
|
|
|
+ }
|
|
|
|
|
|
- // this modifier can only process indexed and non-indexed geomtries with a position attribute
|
|
|
+ return least;
|
|
|
|
|
|
- for ( var name in attributes ) {
|
|
|
+}
|
|
|
|
|
|
- if ( name !== 'position' ) geometry.deleteAttribute( name );
|
|
|
+// we use a triangle class to represent structure of face slightly differently
|
|
|
|
|
|
- }
|
|
|
+class Triangle {
|
|
|
|
|
|
- geometry = BufferGeometryUtils.mergeVertices( geometry );
|
|
|
+ constructor( v1, v2, v3, a, b, c ) {
|
|
|
|
|
|
- //
|
|
|
- // put data of original geometry in different data structures
|
|
|
- //
|
|
|
+ this.a = a;
|
|
|
+ this.b = b;
|
|
|
+ this.c = c;
|
|
|
|
|
|
- var vertices = [];
|
|
|
- var faces = [];
|
|
|
+ this.v1 = v1;
|
|
|
+ this.v2 = v2;
|
|
|
+ this.v3 = v3;
|
|
|
|
|
|
- // add vertices
|
|
|
+ this.normal = new Vector3();
|
|
|
|
|
|
- var positionAttribute = geometry.getAttribute( 'position' );
|
|
|
+ this.computeNormal();
|
|
|
|
|
|
- for ( var i = 0; i < positionAttribute.count; i ++ ) {
|
|
|
+ v1.faces.push( this );
|
|
|
+ v1.addUniqueNeighbor( v2 );
|
|
|
+ v1.addUniqueNeighbor( v3 );
|
|
|
|
|
|
- var v = new Vector3().fromBufferAttribute( positionAttribute, i );
|
|
|
+ v2.faces.push( this );
|
|
|
+ v2.addUniqueNeighbor( v1 );
|
|
|
+ v2.addUniqueNeighbor( v3 );
|
|
|
|
|
|
- var vertex = new Vertex( v, i );
|
|
|
- vertices.push( vertex );
|
|
|
|
|
|
- }
|
|
|
+ v3.faces.push( this );
|
|
|
+ v3.addUniqueNeighbor( v1 );
|
|
|
+ v3.addUniqueNeighbor( v2 );
|
|
|
|
|
|
- // add faces
|
|
|
+ }
|
|
|
|
|
|
- var index = geometry.getIndex();
|
|
|
+ computeNormal() {
|
|
|
|
|
|
- if ( index !== null ) {
|
|
|
+ const vA = this.v1.position;
|
|
|
+ const vB = this.v2.position;
|
|
|
+ const vC = this.v3.position;
|
|
|
|
|
|
- for ( var i = 0; i < index.count; i += 3 ) {
|
|
|
+ _cb.subVectors( vC, vB );
|
|
|
+ _ab.subVectors( vA, vB );
|
|
|
+ _cb.cross( _ab ).normalize();
|
|
|
|
|
|
- var a = index.getX( i );
|
|
|
- var b = index.getX( i + 1 );
|
|
|
- var c = index.getX( i + 2 );
|
|
|
+ this.normal.copy( _cb );
|
|
|
|
|
|
- var triangle = new Triangle( vertices[ a ], vertices[ b ], vertices[ c ], a, b, c );
|
|
|
- faces.push( triangle );
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
+ hasVertex( v ) {
|
|
|
|
|
|
- } else {
|
|
|
+ return v === this.v1 || v === this.v2 || v === this.v3;
|
|
|
|
|
|
- for ( var i = 0; i < positionAttribute.count; i += 3 ) {
|
|
|
+ }
|
|
|
|
|
|
- var a = i;
|
|
|
- var b = i + 1;
|
|
|
- var c = i + 2;
|
|
|
+ replaceVertex( oldv, newv ) {
|
|
|
|
|
|
- var triangle = new Triangle( vertices[ a ], vertices[ b ], vertices[ c ], a, b, c );
|
|
|
- faces.push( triangle );
|
|
|
+ if ( oldv === this.v1 ) this.v1 = newv;
|
|
|
+ else if ( oldv === this.v2 ) this.v2 = newv;
|
|
|
+ else if ( oldv === this.v3 ) this.v3 = newv;
|
|
|
|
|
|
- }
|
|
|
+ removeFromArray( oldv.faces, this );
|
|
|
+ newv.faces.push( this );
|
|
|
|
|
|
- }
|
|
|
|
|
|
- // compute all edge collapse costs
|
|
|
+ oldv.removeIfNonNeighbor( this.v1 );
|
|
|
+ this.v1.removeIfNonNeighbor( oldv );
|
|
|
|
|
|
- for ( var i = 0, il = vertices.length; i < il; i ++ ) {
|
|
|
+ oldv.removeIfNonNeighbor( this.v2 );
|
|
|
+ this.v2.removeIfNonNeighbor( oldv );
|
|
|
|
|
|
- computeEdgeCostAtVertex( vertices[ i ] );
|
|
|
+ oldv.removeIfNonNeighbor( this.v3 );
|
|
|
+ this.v3.removeIfNonNeighbor( oldv );
|
|
|
|
|
|
- }
|
|
|
+ this.v1.addUniqueNeighbor( this.v2 );
|
|
|
+ this.v1.addUniqueNeighbor( this.v3 );
|
|
|
|
|
|
- var nextVertex;
|
|
|
+ this.v2.addUniqueNeighbor( this.v1 );
|
|
|
+ this.v2.addUniqueNeighbor( this.v3 );
|
|
|
|
|
|
- var z = count;
|
|
|
+ this.v3.addUniqueNeighbor( this.v1 );
|
|
|
+ this.v3.addUniqueNeighbor( this.v2 );
|
|
|
|
|
|
- while ( z -- ) {
|
|
|
+ this.computeNormal();
|
|
|
|
|
|
- nextVertex = minimumCostEdge( vertices );
|
|
|
+ }
|
|
|
|
|
|
- if ( ! nextVertex ) {
|
|
|
+}
|
|
|
|
|
|
- console.log( 'THREE.SimplifyModifier: No next vertex' );
|
|
|
- break;
|
|
|
+class Vertex {
|
|
|
|
|
|
- }
|
|
|
+ constructor( v, id ) {
|
|
|
|
|
|
- collapse( vertices, faces, nextVertex, nextVertex.collapseNeighbor );
|
|
|
-
|
|
|
- }
|
|
|
+ this.position = v;
|
|
|
|
|
|
- //
|
|
|
+ this.id = id; // old index id
|
|
|
|
|
|
- var simplifiedGeometry = new BufferGeometry();
|
|
|
- var position = [];
|
|
|
- var index = [];
|
|
|
+ this.faces = []; // faces vertex is connected
|
|
|
+ this.neighbors = []; // neighbouring vertices aka "adjacentVertices"
|
|
|
|
|
|
- //
|
|
|
+ // these will be computed in computeEdgeCostAtVertex()
|
|
|
+ this.collapseCost = 0; // cost of collapsing this vertex, the less the better. aka objdist
|
|
|
+ this.collapseNeighbor = null; // best candinate for collapsing
|
|
|
|
|
|
- for ( var i = 0; i < vertices.length; i ++ ) {
|
|
|
+ }
|
|
|
|
|
|
- var vertex = vertices[ i ].position;
|
|
|
- position.push( vertex.x, vertex.y, vertex.z );
|
|
|
+ addUniqueNeighbor( vertex ) {
|
|
|
|
|
|
- }
|
|
|
+ pushIfUnique( this.neighbors, vertex );
|
|
|
|
|
|
- //
|
|
|
+ }
|
|
|
|
|
|
- for ( var i = 0; i < faces.length; i ++ ) {
|
|
|
+ removeIfNonNeighbor( n ) {
|
|
|
|
|
|
- var face = faces[ i ];
|
|
|
+ const neighbors = this.neighbors;
|
|
|
+ const faces = this.faces;
|
|
|
|
|
|
- var a = vertices.indexOf( face.v1 );
|
|
|
- var b = vertices.indexOf( face.v2 );
|
|
|
- var c = vertices.indexOf( face.v3 );
|
|
|
+ const offset = neighbors.indexOf( n );
|
|
|
|
|
|
- index.push( a, b, c );
|
|
|
+ if ( offset === - 1 ) return;
|
|
|
|
|
|
- }
|
|
|
+ for ( let i = 0; i < faces.length; i ++ ) {
|
|
|
|
|
|
- //
|
|
|
+ if ( faces[ i ].hasVertex( n ) ) return;
|
|
|
|
|
|
- simplifiedGeometry.setAttribute( 'position', new Float32BufferAttribute( position, 3 ) );
|
|
|
- simplifiedGeometry.setIndex( index );
|
|
|
+ }
|
|
|
|
|
|
- return simplifiedGeometry;
|
|
|
+ neighbors.splice( offset, 1 );
|
|
|
|
|
|
- };
|
|
|
+ }
|
|
|
|
|
|
-} )();
|
|
|
+}
|
|
|
|
|
|
export { SimplifyModifier };
|