improve some shape comparison, fix shape tests

This commit is contained in:
Moritz Zwerger 2025-04-13 10:36:39 +02:00
parent f13699f457
commit abd04a293e
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
4 changed files with 24 additions and 25 deletions

View File

@ -277,10 +277,9 @@ class AABB : Shape {
override fun equals(other: Any?): Boolean {
if (other == null) return false
if (this === other) return true
if (this.hashCode() != other.hashCode()) return false
if (other is AABB) return min == other.min && max == other.max
if (other is Shape) return other == this
return false
if (other !is AABB) return false
return min == other.min && max == other.max
}
fun isOnEdge(min: Vec3d, max: Vec3d): Boolean {

View File

@ -92,9 +92,7 @@ class CombinedShape(
if (this === other) return true
if (other !is Shape) return false
if (other is CombinedShape) return aabbs.contentEquals(other.aabbs)
if (other is AABB) {
return if (aabbs.size == 1) aabbs.first() == other else false
}
if (other is AABB) return false // one aabb is not a combined shape
TODO("Can not compare $this with $other")
}

View File

@ -25,27 +25,27 @@ internal class DirectedPropertyTest {
@Test
fun testSideCovered1() {
assertTrue(CombinedShape(AABB.BLOCK).isSideCovered(Directions.DOWN))
assertTrue(CombinedShape(AABB.BLOCK).isSideCovered(Directions.UP))
assertTrue(CombinedShape(AABB.BLOCK).isSideCovered(Directions.NORTH))
assertTrue(CombinedShape(AABB.BLOCK).isSideCovered(Directions.SOUTH))
assertTrue(CombinedShape(AABB.BLOCK).isSideCovered(Directions.WEST))
assertTrue(CombinedShape(AABB.BLOCK).isSideCovered(Directions.EAST))
assertTrue(AABB.BLOCK.isSideCovered(Directions.DOWN))
assertTrue(AABB.BLOCK.isSideCovered(Directions.UP))
assertTrue(AABB.BLOCK.isSideCovered(Directions.NORTH))
assertTrue(AABB.BLOCK.isSideCovered(Directions.SOUTH))
assertTrue(AABB.BLOCK.isSideCovered(Directions.WEST))
assertTrue(AABB.BLOCK.isSideCovered(Directions.EAST))
}
@Test
fun testSideCovered2() {
assertFalse(CombinedShape(AABB.EMPTY).isSideCovered(Directions.DOWN))
assertFalse(CombinedShape(AABB.EMPTY).isSideCovered(Directions.UP))
assertFalse(CombinedShape(AABB.EMPTY).isSideCovered(Directions.NORTH))
assertFalse(CombinedShape(AABB.EMPTY).isSideCovered(Directions.SOUTH))
assertFalse(CombinedShape(AABB.EMPTY).isSideCovered(Directions.WEST))
assertFalse(CombinedShape(AABB.EMPTY).isSideCovered(Directions.EAST))
assertFalse(AABB.EMPTY.isSideCovered(Directions.DOWN))
assertFalse(AABB.EMPTY.isSideCovered(Directions.UP))
assertFalse(AABB.EMPTY.isSideCovered(Directions.NORTH))
assertFalse(AABB.EMPTY.isSideCovered(Directions.SOUTH))
assertFalse(AABB.EMPTY.isSideCovered(Directions.WEST))
assertFalse(AABB.EMPTY.isSideCovered(Directions.EAST))
}
@Test
fun testSideCovered3() {
val shape = CombinedShape(AABB(0.0f, 0.0f, 0.0f, 1.0f, 0.5f, 1.0f))
val shape = AABB(0.0f, 0.0f, 0.0f, 1.0f, 0.5f, 1.0f)
assertTrue(shape.isSideCovered(Directions.DOWN))
assertFalse(shape.isSideCovered(Directions.UP))
assertFalse(shape.isSideCovered(Directions.NORTH))
@ -56,7 +56,7 @@ internal class DirectedPropertyTest {
@Test
fun testSideCovered4() {
val shape = CombinedShape(AABB(0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.5f))
val shape = AABB(0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.5f)
assertFalse(shape.isSideCovered(Directions.DOWN))
assertFalse(shape.isSideCovered(Directions.UP))
assertTrue(shape.isSideCovered(Directions.NORTH))

View File

@ -23,15 +23,17 @@ internal class CombinedShapeTest {
@Test
fun testEquals() {
val a = CombinedShape(AABB(Vec3(0.0), Vec3(1.0)))
val b = CombinedShape(AABB(Vec3(0.0), Vec3(1.0)))
val a = CombinedShape(AABB(Vec3(0.0), Vec3(1.0)), AABB(Vec3(5.0), Vec3(6.0)))
val b = CombinedShape(AABB(Vec3(0.0), Vec3(1.0)), AABB(Vec3(5.0), Vec3(6.0)))
assertEquals(a, b)
}
@Test
fun testNotEquals() {
val a = CombinedShape(AABB(Vec3(0.1), Vec3(1.0)))
val b = CombinedShape(AABB(Vec3(0.0), Vec3(1.0)))
val a = CombinedShape(AABB(Vec3(0.1), Vec3(1.0)), AABB(Vec3(5.0), Vec3(6.0)))
val b = CombinedShape(AABB(Vec3(0.0), Vec3(1.0)), AABB(Vec3(5.0), Vec3(6.0)))
assertNotEquals(a, b)
}
// TODO: different order
}