Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Coplanarity test - uncertainty issue - see https://github.com/g… #203

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 52 additions & 7 deletions src/core/TriangleSplitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,23 @@
const EPSILON = 1e-10;
const COPLANAR_EPSILON = 1e-10;
const PARALLEL_EPSILON = 1e-10;
const COPLANAR_EPSILON_MAX = 1e-4;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you considered scaling the epsilon up based on the scale of the smallest triangle edge? Jumping straight to 1e-4 if one of the edges is too small is a large jump.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

COPLANAR_EPSILON_MAX is just to avoid too large epsilon values. We don't jump to this value, we just scale COPLANAR_EPSILON but we make sure that it stays below COPLANAR_EPSILON_MAX.

Copy link
Owner

@gkjohnson gkjohnson Mar 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it - I'm understanding now. How did you conclude that 1e-4 is a good max epsilon? It's a very large value and from looking at the code it is still possible to wind up using 1e-4 as an epsilon at most. Do smaller values still cause issues?

It would be nice to have a couple example operation codes that show how this is fixed.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my specific case this value was reached sometimes. I did not tried to set a lower values. However when it is reached it means that the computation is somewhat undefined (we want to test if a point or edge belongs to a plane but the plane is defined with a very high uncertainty so the answer is not reliable at all...)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might have been reached but that doesn't mean it's needed for the calculations. As it's currently implemented you are scaling the COPLANAR_EPSILON value which is already set to 1e-10, a fairly large value in itself, which is basically assuming that 1e-10 is the necessary error value at the "center" of the plane where it should actually be a much smaller value since we're not dealing with this lever arm effect. It's difficult to calculate how the error has compounded over calculations but it's possible that the COPLANAR_EPSILON could be set to something like 1e-20 or less, now, and then it could be scaled by the approach added in this PR.

I'd much rather set this to a smaller value and if we continue to have issues we can increase it, I think.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the last commit I have lower the EPSILON_MAX to 1e-7, it works with this value

//*/

// XAVIER: increase epsilon values:
/*const EPSILON = 1e-5;
const COPLANAR_EPSILON = 1e-5;
const PARALLEL_EPSILON = 1e-5; //*/
gkjohnson marked this conversation as resolved.
Show resolved Hide resolved

const _edge = new Line3();
const _foundEdge = new Line3();
const _vec = new Vector3();
const _triangleNormal = new Vector3();
const _planeNormal = new Vector3();
const _plane = new Plane();
const _splittingTriangle = new ExtendedTriangle();
const _planeCenter = new Vector3();
let _minEdgeSize = Infinity

Check failure on line 26 in src/core/TriangleSplitter.js

View workflow job for this annotation

GitHub Actions / build (16.x)

Missing semicolon
Fixed Show fixed Hide fixed

// A pool of triangles to avoid unnecessary triangle creation
class TrianglePool {
Expand Down Expand Up @@ -109,6 +119,20 @@

const { normal, triangles } = this;
triangle.getNormal( _triangleNormal ).normalize();

Check failure on line 122 in src/core/TriangleSplitter.js

View workflow job for this annotation

GitHub Actions / build (16.x)

Trailing spaces not allowed
// compute triangleMinEdgeSize:
let triangleMinEdgeSizeSq = Infinity

Check failure on line 124 in src/core/TriangleSplitter.js

View workflow job for this annotation

GitHub Actions / build (16.x)

Missing semicolon
const arr = [ triangle.a, triangle.b, triangle.c ];
gkjohnson marked this conversation as resolved.
Show resolved Hide resolved
for ( let i = 0; i < 3; i ++ ) {

Check failure on line 126 in src/core/TriangleSplitter.js

View workflow job for this annotation

GitHub Actions / build (16.x)

Block must be padded by blank lines
const nexti = ( i + 1 ) % 3;
const v0 = arr[ i ];
const v1 = arr[ nexti ];
const edgeSizeSq = v0.distanceToSquared(v1)

Check failure on line 130 in src/core/TriangleSplitter.js

View workflow job for this annotation

GitHub Actions / build (16.x)

There must be a space after this paren

Check failure on line 130 in src/core/TriangleSplitter.js

View workflow job for this annotation

GitHub Actions / build (16.x)

There must be a space before this paren

Check failure on line 130 in src/core/TriangleSplitter.js

View workflow job for this annotation

GitHub Actions / build (16.x)

Missing semicolon
triangleMinEdgeSizeSq = Math.min(triangleMinEdgeSizeSq, edgeSizeSq)

Check failure on line 131 in src/core/TriangleSplitter.js

View workflow job for this annotation

GitHub Actions / build (16.x)

There must be a space after this paren

Check failure on line 131 in src/core/TriangleSplitter.js

View workflow job for this annotation

GitHub Actions / build (16.x)

There must be a space before this paren

Check failure on line 131 in src/core/TriangleSplitter.js

View workflow job for this annotation

GitHub Actions / build (16.x)

Block must be padded by blank lines
}
const triangleMinEdgeSize = Math.sqrt(triangleMinEdgeSizeSq)
_minEdgeSize = triangleMinEdgeSize


if ( Math.abs( 1.0 - Math.abs( _triangleNormal.dot( normal ) ) ) < PARALLEL_EPSILON ) {

Expand All @@ -133,25 +157,30 @@
// plane positive direction is toward triangle center
_vec.subVectors( v1, v0 ).normalize();
_planeNormal.crossVectors( _triangleNormal, _vec );
_minEdgeSize = Math.min(triangleMinEdgeSize, v1.distanceTo(v0))
_plane.setFromNormalAndCoplanarPoint( _planeNormal, v0 );
_planeCenter.copy(v0);

this.splitByPlane( _plane, triangle );
// we need to provide planeCenter and minEdgeSize to evaluate the plane uncertainty
// the smaller minEdgeSize is, the higher is the uncertainty
// the larger from planeCenter we are, the higher is the uncertainty
this.splitByPlane( _plane, _planeCenter, _minEdgeSize, triangle );

}

} else {

// otherwise split by the triangle plane
triangle.getPlane( _plane );
this.splitByPlane( _plane, triangle );
this.splitByPlane( _plane, _planeCenter, _minEdgeSize, triangle );

}

}

// Split the triangles by the given plan. If a triangle is provided then we ensure we
// intersect the triangle before splitting the plane
splitByPlane( plane, clippingTriangle ) {
splitByPlane( plane, planeCenter, planeEdgeSize, clippingTriangle ) {

const { triangles, trianglePool } = this;

Expand Down Expand Up @@ -189,7 +218,23 @@
// so we can use that information to determine whether to split later.
const startDist = plane.distanceToPoint( _edge.start );
const endDist = plane.distanceToPoint( _edge.end );
if ( Math.abs( startDist ) < COPLANAR_EPSILON && Math.abs( endDist ) < COPLANAR_EPSILON ) {
let coPlanarEpsilonStart = COPLANAR_EPSILON * Math.max(1, 0.5 * _edge.start.distanceTo(planeCenter) / planeEdgeSize);
let coPlanarEpsilonEnd = COPLANAR_EPSILON * Math.max(1, 0.5 * _edge.end.distanceTo(planeCenter) / planeEdgeSize);

if (coPlanarEpsilonStart > COPLANAR_EPSILON_MAX){
coPlanarEpsilonStart = COPLANAR_EPSILON_MAX
Fixed Show fixed Hide fixed
console.warn('High coplanar epsilon start because of degenerate faces')
Fixed Show fixed Hide fixed
}
if (coPlanarEpsilonEnd > COPLANAR_EPSILON_MAX){
coPlanarEpsilonEnd = COPLANAR_EPSILON_MAX
Fixed Show fixed Hide fixed
console.warn('High coplanar epsilon end because of degenerate faces')
Fixed Show fixed Hide fixed
}

if ( Math.abs( startDist ) < coPlanarEpsilonStart && Math.abs( endDist ) < coPlanarEpsilonEnd ) {

// we project the edge in the plane
plane.projectPoint(_edge.start, arr[ t ])
Fixed Show fixed Hide fixed
plane.projectPoint(_edge.end, arr[ tNext ])
Fixed Show fixed Hide fixed

coplanarEdge = true;
break;
Expand All @@ -207,8 +252,8 @@
}

// we only don't consider this an intersection if the start points hits the plane
if ( Math.abs( startDist ) < COPLANAR_EPSILON ) {

if ( Math.abs( startDist ) < coPlanarEpsilonStart ) {
plane.projectPoint(_edge.start, arr[ t ])
Fixed Show fixed Hide fixed
continue;

}
Expand All @@ -218,7 +263,7 @@
// Because we ignore the start point intersection above we have to make sure we check the end
// point intersection here.
let didIntersect = ! ! plane.intersectLine( _edge, _vec );
if ( ! didIntersect && Math.abs( endDist ) < COPLANAR_EPSILON ) {
if ( ! didIntersect && Math.abs( endDist ) < coPlanarEpsilonEnd ) {

_vec.copy( _edge.end );
didIntersect = true;
Expand Down
Loading