diff --git a/tests/shared/src/AABoxTests.cpp b/tests/shared/src/AABoxTests.cpp new file mode 100644 index 0000000000..fd709a488c --- /dev/null +++ b/tests/shared/src/AABoxTests.cpp @@ -0,0 +1,153 @@ +// +// AABoxTests.cpp +// tests/shared/src +// +// Created by Andrew Meadows on 2016.02.19 +// Copyright 2016 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#include + +#include "AABoxTests.h" + +#include +#include +#include + +#include <../GLMTestUtils.h> +#include <../QTestExtensions.h> + + +QTEST_MAIN(AABoxTests) + +void AABoxTests::testCtorsAndSetters() { + const glm::vec3 corner(1.23f, 4.56f, 7.89f); + const glm::vec3 scale(2.34f, 7.53f, 9.14f); + + // test ctor + AABox box(corner, scale); + QCOMPARE_WITH_ABS_ERROR(box.getCorner(), corner, EPSILON); + QCOMPARE_WITH_ABS_ERROR(box.getScale(), scale, EPSILON); + + // test copy ctor + AABox copyBox(box); + QCOMPARE_WITH_ABS_ERROR(copyBox.getCorner(), corner, EPSILON); + QCOMPARE_WITH_ABS_ERROR(copyBox.getScale(), scale, EPSILON); + + // test setBox() + const glm::vec3 newCorner(9.87f, 6.54f, 3.21f); + const glm::vec3 newScale = glm::vec3(4.32f, 8.95f, 10.31f); + box.setBox(newCorner, newScale); + QCOMPARE_WITH_ABS_ERROR(box.getCorner(), newCorner, EPSILON); + QCOMPARE_WITH_ABS_ERROR(box.getScale(), newScale, EPSILON); + + // test misc + QCOMPARE_WITH_ABS_ERROR(newCorner, box.getMinimumPoint(), EPSILON); + + glm::vec3 expectedMaxCorner = newCorner + glm::vec3(newScale); + QCOMPARE_WITH_ABS_ERROR(expectedMaxCorner, box.getMaximumPoint(), EPSILON); + + glm::vec3 expectedCenter = newCorner + glm::vec3(0.5f * newScale); + QCOMPARE_WITH_ABS_ERROR(expectedCenter, box.calcCenter(), EPSILON); +} + +void AABoxTests::testContainsPoint() { + const glm::vec3 corner(4.56f, 7.89f, -1.35f); + const glm::vec3 scale(2.34f, 7.53f, 9.14f); + AABox box(corner, scale); + + float delta = 0.00001f; + glm::vec3 center = box.calcCenter(); + QCOMPARE(box.contains(center), true); + + for (int i = 0; i < 3; ++i) { + glm::vec3 halfScale = Vectors::ZERO; + halfScale[i] = 0.5f * scale[i]; + glm::vec3 deltaOffset = Vectors::ZERO; + deltaOffset[i] = delta; + + QCOMPARE(box.contains(center + halfScale + deltaOffset), false); // outside +face + QCOMPARE(box.contains(center + halfScale - deltaOffset), true); // inside +face + QCOMPARE(box.contains(center - halfScale + deltaOffset), true); // inside -face + QCOMPARE(box.contains(center - halfScale - deltaOffset), false); // outside -face + } +} + +void AABoxTests::testTouchesSphere() { + glm::vec3 corner(-4.56f, 7.89f, -1.35f); + float scale = 1.23f; + AABox box(corner, scale); + + float delta = 0.00001f; + glm::vec3 cubeCenter = box.calcCenter(); + float sphereRadius = 0.468f; + + for (int i = 0; i < 3; ++i) { + int j = (i + 1) % 3; + int k = (j + 1) % 3; + + { // faces + glm::vec3 scaleOffset = Vectors::ZERO; + scaleOffset[i] = 0.5f * scale + sphereRadius; + + glm::vec3 deltaOffset = Vectors::ZERO; + deltaOffset[i] = delta; + + // outside +face + glm::vec3 sphereCenter = cubeCenter + scaleOffset + deltaOffset; + QCOMPARE(box.touchesSphere(sphereCenter, sphereRadius), false); + + // inside +face + sphereCenter = cubeCenter + scaleOffset - deltaOffset; + QCOMPARE(box.touchesSphere(sphereCenter, sphereRadius), true); + + // inside -face + sphereCenter = cubeCenter - scaleOffset + deltaOffset; + QCOMPARE(box.touchesSphere(sphereCenter, sphereRadius), true); + + // outside -face + sphereCenter = cubeCenter - scaleOffset - deltaOffset; + QCOMPARE(box.touchesSphere(sphereCenter, sphereRadius), false); + } + + { // edges + glm::vec3 edgeOffset = Vectors::ZERO; + edgeOffset[i] = 0.5f * scale; + edgeOffset[j] = 0.5f * scale; + glm::vec3 edgeDirection = glm::normalize(edgeOffset); + glm::vec3 sphereCenter; + + // inside ij + sphereCenter = cubeCenter + edgeOffset + (sphereRadius - delta) * edgeDirection; + QCOMPARE(box.touchesSphere(sphereCenter, sphereRadius), true); + sphereCenter = cubeCenter - edgeOffset - (sphereRadius - delta) * edgeDirection; + QCOMPARE(box.touchesSphere(sphereCenter, sphereRadius), true); + + // outside ij + sphereCenter = cubeCenter + edgeOffset + (sphereRadius + delta) * edgeDirection; + QCOMPARE(box.touchesSphere(sphereCenter, sphereRadius), false); + sphereCenter = cubeCenter - edgeOffset - (sphereRadius + delta) * edgeDirection; + QCOMPARE(box.touchesSphere(sphereCenter, sphereRadius), false); + + edgeOffset[j] = 0.0f; + edgeOffset[k] = 0.5f * scale; + edgeDirection = glm::normalize(edgeOffset); + + // inside ik + sphereCenter = cubeCenter + edgeOffset + (sphereRadius - delta) * edgeDirection; + QCOMPARE(box.touchesSphere(sphereCenter, sphereRadius), true); + sphereCenter = cubeCenter - edgeOffset - (sphereRadius - delta) * edgeDirection; + QCOMPARE(box.touchesSphere(sphereCenter, sphereRadius), true); + + // outside ik + sphereCenter = cubeCenter + edgeOffset + (sphereRadius + delta) * edgeDirection; + QCOMPARE(box.touchesSphere(sphereCenter, sphereRadius), false); + sphereCenter = cubeCenter - edgeOffset - (sphereRadius + delta) * edgeDirection; + QCOMPARE(box.touchesSphere(sphereCenter, sphereRadius), false); + } + } +} + diff --git a/tests/shared/src/AABoxTests.h b/tests/shared/src/AABoxTests.h new file mode 100644 index 0000000000..fe7afede57 --- /dev/null +++ b/tests/shared/src/AABoxTests.h @@ -0,0 +1,28 @@ +// +// AABoxTests.h +// tests/shared/src +// +// Created by Andrew Meadows on 2016.02.19 +// Copyright 2016 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#ifndef hifi_AABoxTests_h +#define hifi_AABoxTests_h + +#include +#include + +#include + +class AABoxTests : public QObject { + Q_OBJECT +private slots: + void testCtorsAndSetters(); + void testContainsPoint(); + void testTouchesSphere(); +}; + +#endif // hifi_AABoxTests_h diff --git a/tests/shared/src/AACubeTests.cpp b/tests/shared/src/AACubeTests.cpp index 1efce12937..177daf89f1 100644 --- a/tests/shared/src/AACubeTests.cpp +++ b/tests/shared/src/AACubeTests.cpp @@ -17,6 +17,7 @@ #include #include +#include <../GLMTestUtils.h> #include <../QTestExtensions.h> @@ -28,38 +29,38 @@ void AACubeTests::ctorsAndSetters() { // test ctor AACube cube(corner, scale); - QCOMPARE_WITH_ABS_ERROR(corner, cube.getCorner(), EPSILON); - QCOMPARE_WITH_ABS_ERROR(scale, cube.getScale(), EPSILON); + QCOMPARE_WITH_ABS_ERROR(cube.getCorner(), corner, EPSILON); + QCOMPARE_WITH_ABS_ERROR(cube.getScale(), scale, EPSILON); // test copy ctor AACube copyCube(cube); - QCOMPARE_WITH_ABS_ERROR(corner, copyCube.getCorner(), EPSILON); - QCOMPARE_WITH_ABS_ERROR(scale, copyCube.getScale(), EPSILON); + QCOMPARE_WITH_ABS_ERROR(copyCube.getCorner(), corner, EPSILON); + QCOMPARE_WITH_ABS_ERROR(copyCube.getScale(), scale, EPSILON); // test setBox() const glm::vec3 newCorner(9.87f, 6.54f, 3.21f); const float newScale = 4.32f; cube.setBox(newCorner, newScale); - QCOMPARE_WITH_ABS_ERROR(newCorner, cube.getCorner(), EPSILON); - QCOMPARE_WITH_ABS_ERROR(newScale, cube.getScale(), EPSILON); + QCOMPARE_WITH_ABS_ERROR(cube.getCorner(), newCorner, EPSILON); + QCOMPARE_WITH_ABS_ERROR(cube.getScale(), newScale, EPSILON); // test misc - QCOMPARE_WITH_ABS_ERROR(newCorner, cube.getMinimumPoint(), EPSILON); + QCOMPARE_WITH_ABS_ERROR(cube.getMinimumPoint(), newCorner, EPSILON); glm::vec3 expectedMaxCorner = newCorner + glm::vec3(newScale); - QCOMPARE_WITH_ABS_ERROR(expectedMaxCorner, cube.getMaximumPoint(), EPSILON); + QCOMPARE_WITH_ABS_ERROR(cube.getMaximumPoint(), expectedMaxCorner, EPSILON); glm::vec3 expectedCenter = newCorner + glm::vec3(0.5f * newScale); - QCOMPARE_WITH_ABS_ERROR(expectedCenter, cube.calcCenter(), EPSILON); + QCOMPARE_WITH_ABS_ERROR(cube.calcCenter(), expectedCenter, EPSILON); } void AACubeTests::containsPoint() { - glm::vec3 corner(4.56f, 7.89f, -1.35f); - float scale = 1.23f; + const glm::vec3 corner(4.56f, 7.89f, -1.35f); + const float scale = 1.23f; AACube cube(corner, scale); - float delta = scale / 1000.0f; - glm::vec3 center = cube.calcCenter(); + const float delta = scale / 1000.0f; + const glm::vec3 center = cube.calcCenter(); QCOMPARE(cube.contains(center), true); for (int i = 0; i < 3; ++i) { @@ -77,13 +78,13 @@ void AACubeTests::containsPoint() { } void AACubeTests::touchesSphere() { - glm::vec3 corner(-4.56f, 7.89f, -1.35f); - float scale = 1.23f; + const glm::vec3 corner(-4.56f, 7.89f, -1.35f); + const float scale = 1.23f; AACube cube(corner, scale); - float delta = scale / 1000.0f; - glm::vec3 cubeCenter = cube.calcCenter(); - float sphereRadius = 0.468f; + const float delta = scale / 1000.0f; + const glm::vec3 cubeCenter = cube.calcCenter(); + const float sphereRadius = 0.468f; for (int i = 0; i < 3; ++i) { int j = (i + 1) % 3; diff --git a/tests/shared/src/AACubeTests.h b/tests/shared/src/AACubeTests.h index c5a5db6ee2..a2b2e08cc5 100644 --- a/tests/shared/src/AACubeTests.h +++ b/tests/shared/src/AACubeTests.h @@ -25,7 +25,4 @@ private slots: void touchesSphere(); }; -float getErrorDifference(const float& a, const float& b); -float getErrorDifference(const glm::vec3& a, const glm::vec3& b); - #endif // hifi_AACubeTests_h