Skip to content

Commit

Permalink
Checking vertexs in the Face and Vector3d for NaN
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Harrington committed May 31, 2024
1 parent f30597c commit 3312e35
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 52 deletions.
32 changes: 21 additions & 11 deletions src/main/java/eu/mihosoft/vrl/v3d/ext/quickhull3d/Face.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ public void computeCentroid (Point3d centroid)
public void computeNormal (Vector3d normal, double minArea)
{
computeNormal(normal);
if(Math.abs(area)<Double.MIN_VALUE) {
throw new NumberFormatException("The computed area of this triangle is too small: "+area);
}

if (area < minArea)
{
Expand All @@ -120,6 +123,9 @@ public void computeNormal (Vector3d normal, double minArea)
double uy = (p2.y - p1.y)/lenMax;
double uz = (p2.z - p1.z)/lenMax;
double dot = normal.x*ux + normal.y*uy + normal.z*uz;
if(Double.isNaN(dot))
throw new NumberFormatException("Normal is NaN"+dot);

normal.x -= dot*ux;
normal.y -= dot*uy;
normal.z -= dot*uz;
Expand Down Expand Up @@ -651,17 +657,21 @@ public void triangulate (FaceList newFaces, double minArea)
HalfEdge oppPrev = hedge.opposite;
Face face0 = null;

for (hedge=hedge.next; hedge!=he0.prev; hedge=hedge.next)
{ Face face =
createTriangle (v0, hedge.prev.head(), hedge.head(), minArea);
face.he0.next.setOpposite (oppPrev);
face.he0.prev.setOpposite (hedge.opposite);
oppPrev = face.he0;
newFaces.add (face);
if (face0 == null)
{ face0 = face;
}
}
for (hedge = hedge.next; hedge != he0.prev; hedge = hedge.next) {
try {
Face face = createTriangle(v0, hedge.prev.head(), hedge.head(), minArea);
face.he0.next.setOpposite(oppPrev);
face.he0.prev.setOpposite(hedge.opposite);
oppPrev = face.he0;
newFaces.add(face);
if (face0 == null) {
face0 = face;
}
}catch(Throwable t) {
System.err.println("Face processing threw exception");
t.printStackTrace();
}
}
hedge = new HalfEdge (he0.prev.prev.head(), this);
hedge.setOpposite (oppPrev);

Expand Down
86 changes: 45 additions & 41 deletions src/main/java/eu/mihosoft/vrl/v3d/ext/quickhull3d/Vector3d.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,26 +103,27 @@ public double get (int i)
* @param value element value
* if i is not in the range 0 to 2.
*/
public void set (int i, double value)
{
switch (i)
{ case 0:
{ x = value;
break;
}
case 1:
{ y = value;
break;
}
case 2:
{ z = value;
break;
}
default:
{ throw new ArrayIndexOutOfBoundsException (i);
}
}
}
public void set(int i, double value) {
if (Double.isNaN(value))
throw new NumberFormatException("value is NaN");
switch (i) {
case 0: {
x = value;
break;
}
case 1: {
y = value;
break;
}
case 2: {
z = value;
break;
}
default: {
throw new ArrayIndexOutOfBoundsException(i);
}
}
}

/**
* Sets the values of this vector to those of v1.
Expand All @@ -131,9 +132,7 @@ public void set (int i, double value)
*/
public void set (Vector3d v1)
{
x = v1.x;
y = v1.y;
z = v1.z;
set(v1.x,v1.y,v1.z);
}

/**
Expand Down Expand Up @@ -279,18 +278,18 @@ public double dot (Vector3d v1)
/**
* Normalizes this vector in place.
*/
public void normalize()
{
double lenSqr = x*x + y*y + z*z;
double err = lenSqr - 1;
if (err > (2*DOUBLE_PREC) ||
err < -(2*DOUBLE_PREC))
{ double len = Math.sqrt(lenSqr);
x /= len;
y /= len;
z /= len;
}
}
public void normalize() {
double lenSqr = x * x + y * y + z * z;
if (Double.isNaN(lenSqr))
throw new NumberFormatException("Normal is NaN" + lenSqr);
double err = lenSqr - 1;
if (err > (2 * DOUBLE_PREC) || err < -(2 * DOUBLE_PREC)) {
double len = Math.sqrt(lenSqr);
x /= len;
y /= len;
z /= len;
}
}

/**
* Sets the elements of this vector to zero.
Expand All @@ -309,12 +308,17 @@ public void setZero()
* @param y value for second element
* @param z value for third element
*/
public void set (double x, double y, double z)
{
this.x = x;
this.y = y;
this.z = z;
}
public void set(double x, double y, double z) {
if (Double.isNaN(x))
throw new NumberFormatException("X is NaN");
if (Double.isNaN(y))
throw new NumberFormatException("Y is NaN");
if (Double.isNaN(z))
throw new NumberFormatException("Z is NaN");
this.x = x;
this.y = y;
this.z = z;
}

/**
* Computes the cross product of v1 and v2 and places the result
Expand Down

0 comments on commit 3312e35

Please sign in to comment.