From 8e6339f524585253c726e1909d45b93a552da46d Mon Sep 17 00:00:00 2001 From: Mugen87 Date: Thu, 9 Mar 2017 17:44:42 +0100 Subject: [PATCH] QuickHull3: Enhance API --- src/math/convexhull/Face.js | 16 ++-- src/math/convexhull/QuickHull3.js | 128 ++++++++++++++++++++++++------ src/math/convexhull/Vertex.js | 3 +- 3 files changed, 113 insertions(+), 34 deletions(-) diff --git a/src/math/convexhull/Face.js b/src/math/convexhull/Face.js index 1564ef43bc..275b05ffc6 100644 --- a/src/math/convexhull/Face.js +++ b/src/math/convexhull/Face.js @@ -120,16 +120,16 @@ Object.assign( Face.prototype, { } - twinEdge = next.twin.prev.twin; - oppositeFace.mark = Deleted; - discardedFace = oppositeFace; + twinEdge = next.twin.prev.twin; + oppositeFace.mark = Deleted; + discardedFace = oppositeFace; - next.prev = prev.prev; - next.prev.next = next; + next.prev = prev.prev; + next.prev.next = next; - next.setTwin( twinEdge ); + next.setTwin( twinEdge ); - oppositeFace.compute(); + oppositeFace.compute(); } else { @@ -168,7 +168,7 @@ Object.assign( Face.prototype, { // right edge - while ( adjacentEdgeNext.opposite.face === oppositeFace ) { + while ( adjacentEdgeNext.twin.face === oppositeFace ) { adjacentEdgeNext = adjacentEdgeNext.next; twinEdgePrev = twinEdgePrev.prev; diff --git a/src/math/convexhull/QuickHull3.js b/src/math/convexhull/QuickHull3.js index a1332a065c..90ef33788c 100644 --- a/src/math/convexhull/QuickHull3.js +++ b/src/math/convexhull/QuickHull3.js @@ -12,19 +12,9 @@ import { MergeNonConvexLargerFace, MergeNonConvex } from '../../constants'; * */ -function QuickHull3( points ) { +var c = 0; - if ( Array.isArray( points ) !== true ) { - - console.error( 'THREE.QuickHull3: Points parameter is not an array.' ); - - } - - if ( points.length < 4 ) { - - console.error( 'THREE.QuickHull3: The algorithm needs at least four points.' ); - - } +function QuickHull3() { this.tolerance = - 1; @@ -36,15 +26,105 @@ function QuickHull3( points ) { this.vertices = []; // vertices of the hull (internal representation of given points) - for ( var i = 0, l = points.length; i < l; i ++ ) { +} + +Object.assign( QuickHull3.prototype, { - this.vertices.push( new Vertex( points[ i ], i ) ); + setFromPoints: function ( points ) { - } + if ( Array.isArray( points ) !== true ) { -} + console.error( 'THREE.QuickHull3: Points parameter is not an array.' ); -Object.assign( QuickHull3.prototype, { + } + + if ( points.length < 4 ) { + + console.error( 'THREE.QuickHull3: The algorithm needs at least four points.' ); + + } + + this.makeEmpty(); + + for ( var i = 0, l = points.length; i < l; i ++ ) { + + this.vertices.push( new Vertex( points[ i ] ) ); + + } + + this.compute(); + + return this; + + }, + + setFromObject: function ( object ) { + + var scope = this; + + this.makeEmpty(); + + object.updateMatrixWorld( true ); + + object.traverse( function ( node ) { + + var i, l, point; + + var geometry = node.geometry; + + if ( geometry !== undefined ) { + + if ( geometry.isGeometry ) { + + var vertices = geometry.vertices; + + for ( i = 0, l = vertices.length; i < l; i ++ ) { + + point = vertices[ i ].clone(); + point.applyMatrix4( node.matrixWorld ); + + scope.vertices.push( new Vertex( point ) ); + + } + + } else if ( geometry.isBufferGeometry ) { + + var attribute = geometry.attributes.position; + + if ( attribute !== undefined ) { + + for ( i = 0, l = attribute.count; i < l; i ++ ) { + + point = new Vector3(); + + point.fromBufferAttribute( attribute, i ).applyMatrix4( node.matrixWorld ); + + scope.vertices.push( new Vertex( point ) ); + + } + + } + + } + + } + + } ); + + this.compute(); + + return this; + + }, + + makeEmpty: function () { + + this.faces = []; + this.vertices = []; + + return this; + + }, // Adds a 'vertex' to the 'assigned' list of vertices and assigns it to the given face. @@ -699,8 +779,8 @@ Object.assign( QuickHull3.prototype, { // The result is: // - // - a positive number when the centroid of the opposite face is above the face i.e. when the faces are concave - // - a negative number when the centroid of the opposite face is below the face i.e. when the faces are convex + // - a positive number when the midpoint of the opposite face is above the face i.e. when the faces are concave + // - a negative number when the midpoint of the opposite face is below the face i.e. when the faces are convex return edge.face.distanceToPoint( edge.twin.face.midpoint ); @@ -720,7 +800,7 @@ Object.assign( QuickHull3.prototype, { if ( mergeType === MergeNonConvex ) { - if ( this.oppositeFaceDistance( edge ) > this.tolerance || + if ( this.oppositeFaceDistance( edge ) > - this.tolerance || this.oppositeFaceDistance( edge.twin ) > - this.tolerance ) { merge = true; @@ -757,7 +837,7 @@ Object.assign( QuickHull3.prototype, { if ( merge === true ) { - var discardedFaces = this.mergeAdjacentFaces( edge, [] ); + var discardedFaces = face.mergeAdjacentFaces( edge, [] ); for ( var i = 0; i < discardedFaces.length; i ++ ) { @@ -811,7 +891,7 @@ Object.assign( QuickHull3.prototype, { if ( face.mark === Visible ) { - while ( this.doAdjacentMerge( face, MergeNonConvexLargerFace ) ) {} + // while ( this.doAdjacentMerge( face, MergeNonConvexLargerFace ) ) {} } @@ -828,7 +908,7 @@ Object.assign( QuickHull3.prototype, { face.mark = Visible; - while ( this.doAdjacentMerge( face, MergeNonConvex ) ) {} + // while ( this.doAdjacentMerge( face, MergeNonConvex ) ) {} } @@ -848,7 +928,7 @@ Object.assign( QuickHull3.prototype, { }, - build: function () { + compute: function () { var vertex; diff --git a/src/math/convexhull/Vertex.js b/src/math/convexhull/Vertex.js index f4150804b6..6943f129d9 100644 --- a/src/math/convexhull/Vertex.js +++ b/src/math/convexhull/Vertex.js @@ -5,10 +5,9 @@ * */ -function Vertex( point, index ) { +function Vertex( point ) { this.point = point; - this.index = index; // index in the input array of points this.next = null; this.prev = null; this.face = null; // the face that is able to see this point -- GitLab