Skip to content

Commit 954b3ae

Browse files
stephengoldscenemax3d
authored andcommitted
implement equals(), hashCode(), and isSimilar() for bounding volumes (#2252)
* implement equals() and hashCode() for BoundingBox * implement equals() and hashCode() for BoundingSphere * implement isSimilar() for BoundingBox * implement isSimilar() for BoundingSphere * BoundingSphere: add comments to emphasize the treatment of checkPlane * TestBoundingSphere: test equals() and isSimilar() * jme3-core tests: add the TestBoundingBox class * BoundingVolume: add equals() and hashCode() methods * utilize super.equals() and super.hashCode() * refactor to utilize Objects.hash()
1 parent 9a11aa6 commit 954b3ae

File tree

5 files changed

+330
-3
lines changed

5 files changed

+330
-3
lines changed

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

+72-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2009-2021 jMonkeyEngine
2+
* Copyright (c) 2009-2024 jMonkeyEngine
33
* All rights reserved.
44
*
55
* Redistribution and use in source and binary forms, with or without
@@ -46,6 +46,7 @@
4646
import java.io.IOException;
4747
import java.nio.FloatBuffer;
4848
//import com.jme.scene.TriMesh;
49+
import java.util.Objects;
4950

5051
/**
5152
* <code>BoundingBox</code> describes a bounding volume as an axis-aligned box.
@@ -587,6 +588,76 @@ public BoundingVolume clone(BoundingVolume store) {
587588
return rVal;
588589
}
589590

591+
/**
592+
* Tests for exact equality with the argument, distinguishing -0 from 0. If
593+
* {@code other} is null, false is returned. Either way, the current
594+
* instance is unaffected.
595+
*
596+
* @param other the object to compare (may be null, unaffected)
597+
* @return true if {@code this} and {@code other} have identical values,
598+
* otherwise false
599+
*/
600+
@Override
601+
public boolean equals(Object other) {
602+
if (!(other instanceof BoundingBox)) {
603+
return false;
604+
}
605+
606+
if (this == other) {
607+
return true;
608+
}
609+
610+
BoundingBox otherBoundingBox = (BoundingBox) other;
611+
if (Float.compare(xExtent, otherBoundingBox.xExtent) != 0) {
612+
return false;
613+
} else if (Float.compare(yExtent, otherBoundingBox.yExtent) != 0) {
614+
return false;
615+
} else if (Float.compare(zExtent, otherBoundingBox.zExtent) != 0) {
616+
return false;
617+
} else {
618+
return super.equals(otherBoundingBox);
619+
}
620+
}
621+
622+
/**
623+
* Returns a hash code. If two bounding boxes have identical values, they
624+
* will have the same hash code. The current instance is unaffected.
625+
*
626+
* @return a 32-bit value for use in hashing
627+
*/
628+
@Override
629+
public int hashCode() {
630+
int hash = Objects.hash(xExtent, yExtent, zExtent);
631+
hash = 59 * hash + super.hashCode();
632+
633+
return hash;
634+
}
635+
636+
/**
637+
* Tests for approximate equality with the specified bounding box, using the
638+
* specified tolerance. If {@code other} is null, false is returned. Either
639+
* way, the current instance is unaffected.
640+
*
641+
* @param aabb the bounding box to compare (unaffected) or null for none
642+
* @param epsilon the tolerance for each component
643+
* @return true if all components are within tolerance, otherwise false
644+
*/
645+
public boolean isSimilar(BoundingBox aabb, float epsilon) {
646+
if (aabb == null) {
647+
return false;
648+
} else if (Float.compare(Math.abs(aabb.xExtent - xExtent), epsilon) > 0) {
649+
return false;
650+
} else if (Float.compare(Math.abs(aabb.yExtent - yExtent), epsilon) > 0) {
651+
return false;
652+
} else if (Float.compare(Math.abs(aabb.zExtent - zExtent), epsilon) > 0) {
653+
return false;
654+
} else if (!center.isSimilar(aabb.getCenter(), epsilon)) {
655+
return false;
656+
}
657+
// The checkPlane field is ignored.
658+
return true;
659+
}
660+
590661
/**
591662
* <code>toString</code> returns the string representation of this object.
592663
* The form is: "[Center: vector xExtent: X.XX yExtent: Y.YY zExtent:

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

+64-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2009-2021 jMonkeyEngine
2+
* Copyright (c) 2009-2024 jMonkeyEngine
33
* All rights reserved.
44
*
55
* Redistribution and use in source and binary forms, with or without
@@ -43,6 +43,7 @@
4343
import com.jme3.util.TempVars;
4444
import java.io.IOException;
4545
import java.nio.FloatBuffer;
46+
import java.util.Objects;
4647
import java.util.logging.Level;
4748
import java.util.logging.Logger;
4849

@@ -651,6 +652,68 @@ public BoundingVolume clone(BoundingVolume store) {
651652
return new BoundingSphere(radius, center.clone());
652653
}
653654

655+
/**
656+
* Tests for exact equality with the argument, distinguishing -0 from 0. If
657+
* {@code other} is null, false is returned. Either way, the current
658+
* instance is unaffected.
659+
*
660+
* @param other the object to compare (may be null, unaffected)
661+
* @return true if {@code this} and {@code other} have identical values,
662+
* otherwise false
663+
*/
664+
@Override
665+
public boolean equals(Object other) {
666+
if (!(other instanceof BoundingSphere)) {
667+
return false;
668+
}
669+
670+
if (this == other) {
671+
return true;
672+
}
673+
674+
BoundingSphere otherBoundingSphere = (BoundingSphere) other;
675+
if (Float.compare(radius, otherBoundingSphere.getRadius()) != 0) {
676+
return false;
677+
} else {
678+
return super.equals(otherBoundingSphere);
679+
}
680+
}
681+
682+
/**
683+
* Returns a hash code. If two bounding boxes have identical values, they
684+
* will have the same hash code. The current instance is unaffected.
685+
*
686+
* @return a 32-bit value for use in hashing
687+
*/
688+
@Override
689+
public int hashCode() {
690+
int hash = Objects.hash(radius);
691+
hash = 59 * hash + super.hashCode();
692+
693+
return hash;
694+
}
695+
696+
/**
697+
* Tests for approximate equality with the specified bounding sphere, using
698+
* the specified tolerance. If {@code other} is null, false is returned.
699+
* Either way, the current instance is unaffected.
700+
*
701+
* @param sphere the bounding sphere to compare (unaffected) or null for none
702+
* @param epsilon the tolerance for each component
703+
* @return true if all components are within tolerance, otherwise false
704+
*/
705+
public boolean isSimilar(BoundingSphere sphere, float epsilon) {
706+
if (sphere == null) {
707+
return false;
708+
} else if (Float.compare(Math.abs(sphere.getRadius() - radius), epsilon) > 0) {
709+
return false;
710+
} else if (!center.isSimilar(sphere.getCenter(), epsilon)) {
711+
return false;
712+
}
713+
// The checkPlane field is ignored.
714+
return true;
715+
}
716+
654717
/**
655718
* <code>toString</code> returns the string representation of this object.
656719
* The form is: "Radius: RRR.SSSS Center: vector".

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

+44-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2009-2021 jMonkeyEngine
2+
* Copyright (c) 2009-2024 jMonkeyEngine
33
* All rights reserved.
44
*
55
* Redistribution and use in source and binary forms, with or without
@@ -40,6 +40,7 @@
4040
import com.jme3.util.TempVars;
4141
import java.io.IOException;
4242
import java.nio.FloatBuffer;
43+
import java.util.Objects;
4344

4445
/**
4546
* <code>BoundingVolume</code> defines an interface for dealing with
@@ -180,6 +181,48 @@ public final BoundingVolume transform(Transform trans) {
180181
*/
181182
public abstract BoundingVolume clone(BoundingVolume store);
182183

184+
/**
185+
* Tests for exact equality with the argument, distinguishing -0 from 0. If
186+
* {@code other} is null, false is returned. Either way, the current
187+
* instance is unaffected.
188+
*
189+
* @param other the object to compare (may be null, unaffected)
190+
* @return true if {@code this} and {@code other} have identical values,
191+
* otherwise false
192+
*/
193+
@Override
194+
public boolean equals(Object other) {
195+
if (!(other instanceof BoundingVolume)) {
196+
return false;
197+
}
198+
199+
if (this == other) {
200+
return true;
201+
}
202+
203+
BoundingVolume otherBoundingVolume = (BoundingVolume) other;
204+
if (!center.equals(otherBoundingVolume.getCenter())) {
205+
return false;
206+
}
207+
// The checkPlane field is ignored.
208+
209+
return true;
210+
}
211+
212+
/**
213+
* Returns a hash code. If two bounding volumes have identical values, they
214+
* will have the same hash code. The current instance is unaffected.
215+
*
216+
* @return a 32-bit value for use in hashing
217+
*/
218+
@Override
219+
public int hashCode() {
220+
int hash = Objects.hash(center);
221+
// The checkPlane field is ignored.
222+
223+
return hash;
224+
}
225+
183226
public final Vector3f getCenter() {
184227
return center;
185228
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright (c) 2024 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 BoundingBox class.
40+
*
41+
* @author Stephen Gold
42+
*/
43+
public class TestBoundingBox {
44+
/**
45+
* Verify that equals() behaves as expected.
46+
*/
47+
@Test
48+
public void testEquals() {
49+
BoundingBox bb1 = new BoundingBox(new Vector3f(3f, 4f, 5f), 0f, 1f, 2f);
50+
BoundingBox bb2
51+
= new BoundingBox(new Vector3f(3f, 4f, 5f), -0f, 1f, 2f);
52+
53+
BoundingBox bb3 = new BoundingBox(new Vector3f(3f, 0f, 2f), 9f, 8f, 7f);
54+
BoundingBox bb4
55+
= new BoundingBox(new Vector3f(3f, -0f, 2f), 9f, 8f, 7f);
56+
57+
BoundingBox bb5 = new BoundingBox(new Vector3f(4f, 5f, 6f), 9f, 8f, 7f);
58+
BoundingBox bb6 = (BoundingBox) bb5.clone();
59+
bb6.setCheckPlane(1);
60+
61+
// Clones are equal to their base instances:
62+
Assert.assertEquals(bb1, bb1.clone());
63+
Assert.assertEquals(bb2, bb2.clone());
64+
Assert.assertEquals(bb3, bb3.clone());
65+
Assert.assertEquals(bb4, bb4.clone());
66+
Assert.assertEquals(bb5, bb5.clone());
67+
Assert.assertEquals(bb6, bb6.clone());
68+
69+
Assert.assertNotEquals(bb1, bb2); // because their extents differ
70+
Assert.assertNotEquals(bb3, bb4); // because their centers differ
71+
Assert.assertEquals(bb5, bb6); // because check planes are ignored
72+
}
73+
74+
/**
75+
* Verify that isSimilar() behaves as expected.
76+
*/
77+
@Test
78+
public void testIsSimilar() {
79+
BoundingBox bb1 = new BoundingBox(new Vector3f(3f, 4f, 5f), 0f, 1f, 2f);
80+
BoundingBox bb2
81+
= new BoundingBox(new Vector3f(3f, 4f, 5f), 0f, 1.1f, 2f);
82+
83+
BoundingBox bb3 = new BoundingBox(new Vector3f(3f, 4f, 2f), 9f, 8f, 7f);
84+
BoundingBox bb4
85+
= new BoundingBox(new Vector3f(3f, 3.9f, 2f), 9f, 8f, 7f);
86+
87+
BoundingBox bb5 = new BoundingBox(new Vector3f(4f, 5f, 6f), 9f, 8f, 7f);
88+
BoundingBox bb6 = (BoundingBox) bb5.clone();
89+
bb6.setCheckPlane(1);
90+
91+
Assert.assertFalse(bb1.isSimilar(bb2, 0.09999f));
92+
Assert.assertTrue(bb1.isSimilar(bb2, 0.10001f));
93+
94+
Assert.assertFalse(bb3.isSimilar(bb4, 0.09999f));
95+
Assert.assertTrue(bb3.isSimilar(bb4, 0.10001f));
96+
97+
Assert.assertTrue(bb5.isSimilar(bb6, 0f)); // check planes are ignored
98+
}
99+
}

jme3-core/src/test/java/com/jme3/bounding/TestBoundingSphere.java

+51
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,57 @@
4141
* @author Stephen Gold
4242
*/
4343
public class TestBoundingSphere {
44+
/**
45+
* Verify that equals() behaves as expected.
46+
*/
47+
@Test
48+
public void testEquals() {
49+
BoundingSphere bs1 = new BoundingSphere(0f, new Vector3f(3f, 4f, 5f));
50+
BoundingSphere bs2 = new BoundingSphere(-0f, new Vector3f(3f, 4f, 5f));
51+
52+
BoundingSphere bs3 = new BoundingSphere(1f, new Vector3f(3f, 0f, 2f));
53+
BoundingSphere bs4 = new BoundingSphere(1f, new Vector3f(3f, -0f, 2f));
54+
55+
BoundingSphere bs5 = new BoundingSphere(2f, new Vector3f(4f, 5f, 6f));
56+
BoundingSphere bs6 = (BoundingSphere) bs5.clone();
57+
bs6.setCheckPlane(1);
58+
59+
// Clones are equal to their base instances:
60+
Assert.assertEquals(bs1, bs1.clone());
61+
Assert.assertEquals(bs2, bs2.clone());
62+
Assert.assertEquals(bs3, bs3.clone());
63+
Assert.assertEquals(bs4, bs4.clone());
64+
Assert.assertEquals(bs5, bs5.clone());
65+
Assert.assertEquals(bs6, bs6.clone());
66+
67+
Assert.assertNotEquals(bs1, bs2); // because their radii differ
68+
Assert.assertNotEquals(bs3, bs4); // because their centers differ
69+
Assert.assertEquals(bs5, bs6); // because check planes are ignored
70+
}
71+
72+
/**
73+
* Verify that isSimilar() behaves as expected.
74+
*/
75+
@Test
76+
public void testIsSimilar() {
77+
BoundingSphere bs1 = new BoundingSphere(0f, new Vector3f(3f, 4f, 5f));
78+
BoundingSphere bs2 = new BoundingSphere(0.1f, new Vector3f(3f, 4f, 5f));
79+
80+
BoundingSphere bs3 = new BoundingSphere(1f, new Vector3f(3f, 4f, 2f));
81+
BoundingSphere bs4 = new BoundingSphere(1f, new Vector3f(3f, 3.9f, 2f));
82+
83+
BoundingSphere bs5 = new BoundingSphere(2f, new Vector3f(4f, 5f, 6f));
84+
BoundingSphere bs6 = (BoundingSphere) bs5.clone();
85+
bs6.setCheckPlane(1);
86+
87+
Assert.assertFalse(bs1.isSimilar(bs2, 0.09999f));
88+
Assert.assertTrue(bs1.isSimilar(bs2, 0.10001f));
89+
90+
Assert.assertFalse(bs3.isSimilar(bs4, 0.09999f));
91+
Assert.assertTrue(bs3.isSimilar(bs4, 0.10001f));
92+
93+
Assert.assertTrue(bs5.isSimilar(bs6, 0f)); // check planes are ignored
94+
}
4495

4596
/**
4697
* Verify that an infinite bounding sphere can be merged with a very

0 commit comments

Comments
 (0)