Skip to content

Commit 6b829c6

Browse files
authored
testcase and fix for issue #1459 (BoundingSphere merge yields NaNs) (#1465)
* testcase and fix for issue #1459 (BoundingSphere merge yields NaNs) * Vector3f: fix overflow/underflow bugs in length() and distance() methods
1 parent 9e0a85b commit 6b829c6

4 files changed

Lines changed: 112 additions & 10 deletions

File tree

jme3-core/src/main/java/com/jme3/bounding/BoundingSphere.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2009-2020 jMonkeyEngine
2+
* Copyright (c) 2009-2021 jMonkeyEngine
33
* All rights reserved.
44
*
55
* Redistribution and use in source and binary forms, with or without
@@ -615,7 +615,7 @@ private BoundingVolume merge(float temp_radius, Vector3f temp_center,
615615
if (rCenter == null) {
616616
rVal.setCenter(rCenter = new Vector3f());
617617
}
618-
if (length > RADIUS_EPSILON) {
618+
if (length > RADIUS_EPSILON && Float.isFinite(length)) {
619619
float coeff = (length + radiusDiff) / (2.0f * length);
620620
rCenter.set(center.addLocal(diff.multLocal(coeff)));
621621
} else {

jme3-core/src/main/java/com/jme3/math/Vector3f.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2009-2020 jMonkeyEngine
2+
* Copyright (c) 2009-2021 jMonkeyEngine
33
* All rights reserved.
44
*
55
* Redistribution and use in source and binary forms, with or without
@@ -435,7 +435,18 @@ public boolean isUnitVector() {
435435
* @return the length or magnitude of the vector.
436436
*/
437437
public float length() {
438-
return FastMath.sqrt(lengthSquared());
438+
/*
439+
* Use double-precision arithmetic to reduce the chance of overflow
440+
* (when lengthSquared > Float.MAX_VALUE) or underflow (when
441+
* lengthSquared is < Float.MIN_VALUE).
442+
*/
443+
double xx = x;
444+
double yy = y;
445+
double zz = z;
446+
double lengthSquared = xx * xx + yy * yy + zz * zz;
447+
float result = (float) Math.sqrt(lengthSquared);
448+
449+
return result;
439450
}
440451

441452
/**
@@ -470,7 +481,18 @@ public float distanceSquared(Vector3f v) {
470481
* @return the distance between the two vectors.
471482
*/
472483
public float distance(Vector3f v) {
473-
return FastMath.sqrt(distanceSquared(v));
484+
/*
485+
* Use double-precision arithmetic to reduce the chance of overflow
486+
* (when distanceSquared > Float.MAX_VALUE) or underflow (when
487+
* distanceSquared is < Float.MIN_VALUE).
488+
*/
489+
double dx = x - v.x;
490+
double dy = y - v.y;
491+
double dz = z - v.z;
492+
double distanceSquared = dx * dx + dy * dy + dz * dz;
493+
float result = (float) Math.sqrt(distanceSquared);
494+
495+
return result;
474496
}
475497

476498
/**
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright (c) 2021 jMonkeyEngine
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are
7+
* met:
8+
*
9+
* * Redistributions of source code must retain the above copyright
10+
* notice, this list of conditions and the following disclaimer.
11+
*
12+
* * Redistributions in binary form must reproduce the above copyright
13+
* notice, this list of conditions and the following disclaimer in the
14+
* documentation and/or other materials provided with the distribution.
15+
*
16+
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors
17+
* may be used to endorse or promote products derived from this software
18+
* without specific prior written permission.
19+
*
20+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22+
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24+
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26+
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+
*/
32+
package com.jme3.bounding;
33+
34+
import com.jme3.math.Vector3f;
35+
import org.junit.Assert;
36+
import org.junit.Test;
37+
38+
/**
39+
* Test cases for the BoundingSphere class.
40+
*
41+
* @author Stephen Gold
42+
*/
43+
public class TestBoundingSphere {
44+
45+
/**
46+
* Verify that an infinite bounding sphere can be merged with a very
47+
* eccentric bounding box without producing NaNs. This was issue #1459 at
48+
* GitHub.
49+
*/
50+
@Test
51+
public void testIssue1459() {
52+
Vector3f boxCenter = new Vector3f(-92f, 3.3194322e29f, 674.89886f);
53+
BoundingBox boundingBox = new BoundingBox(boxCenter,
54+
1.0685959f, 3.3194322e29f, 2.705017f);
55+
56+
Vector3f sphCenter = new Vector3f(0f, 0f, 0f);
57+
float radius = Float.POSITIVE_INFINITY;
58+
BoundingSphere boundingSphere = new BoundingSphere(radius, sphCenter);
59+
60+
boundingSphere.mergeLocal(boundingBox);
61+
62+
Vector3f copyCenter = new Vector3f();
63+
boundingSphere.getCenter(copyCenter);
64+
Assert.assertTrue(Vector3f.isValidVector(copyCenter));
65+
}
66+
}

jme3-core/src/test/java/com/jme3/math/Vector3fTest.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2009-2015 jMonkeyEngine
2+
* Copyright (c) 2009-2021 jMonkeyEngine
33
* All rights reserved.
44
*
55
* Redistribution and use in source and binary forms, with or without
@@ -235,12 +235,16 @@ public void testCrossLocal() {
235235
assertEquals(-0.0f, retval.z, 0.0f);
236236
}
237237

238+
/**
239+
* Verify that distance() doesn't always overflow when distanceSquared >
240+
* Float.MAX_VALUE .
241+
*/
238242
@Test
239243
public void testDistance() {
240244
final Vector3f target = new Vector3f(3.86405e+18f, 3.02146e+23f, 0.171875f);
241245
final Vector3f v = new Vector3f(-2.0f, -1.61503e+19f, 0.171875f);
242-
243-
assertEquals(Float.POSITIVE_INFINITY, target.distance(v), 0.0f);
246+
247+
assertEquals(3.0216215e23f, target.distance(v), 0f);
244248
}
245249

246250
@Test
@@ -540,11 +544,21 @@ public void testIsValidVector() {
540544

541545
@Test
542546
public void testLength() {
543-
assertEquals(0.0f, new Vector3f(1.88079e-37f, 0.0f, 1.55077e-36f).length(), 0.0f);
547+
/*
548+
* avoid underflow when lengthSquared is < Float.MIN_VALUE
549+
*/
550+
assertEquals(1.5621336e-36f,
551+
new Vector3f(1.88079e-37f, 0.0f, 1.55077e-36f).length(), 0f);
552+
544553
assertEquals(Float.NaN, new Vector3f(Float.NaN, 0.0f, 1.55077e-36f).length(), 0.0f);
545554
assertEquals(Float.POSITIVE_INFINITY, new Vector3f(Float.POSITIVE_INFINITY, 0.0f, 1.0f).length(), 0.0f);
555+
546556
assertEquals(4.0124f, new Vector3f(1.9f, 3.2f, 1.5f).length(), 0.001f);
547-
assertEquals(Float.POSITIVE_INFINITY, new Vector3f(1.8e37f, 1.8e37f, 1.5e36f).length(), 0.0f);
557+
/*
558+
* avoid overflow when lengthSquared > Float.MAX_VALUE
559+
*/
560+
assertEquals(2.5499999e37f,
561+
new Vector3f(1.8e37f, 1.8e37f, 1.5e36f).length(), 0.0f);
548562
}
549563

550564
@Test

0 commit comments

Comments
 (0)