diff --git a/tests/shared/src/AACubeTests.cpp b/tests/shared/src/AACubeTests.cpp new file mode 100644 index 0000000000..1efce12937 --- /dev/null +++ b/tests/shared/src/AACubeTests.cpp @@ -0,0 +1,153 @@ +// +// AACubeTests.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 "AACubeTests.h" + +#include +#include +#include + +#include <../QTestExtensions.h> + + +QTEST_MAIN(AACubeTests) + +void AACubeTests::ctorsAndSetters() { + const glm::vec3 corner(1.23f, 4.56f, 7.89f); + const float scale = 2.34f; + + // test ctor + AACube cube(corner, scale); + QCOMPARE_WITH_ABS_ERROR(corner, cube.getCorner(), EPSILON); + QCOMPARE_WITH_ABS_ERROR(scale, cube.getScale(), EPSILON); + + // test copy ctor + AACube copyCube(cube); + QCOMPARE_WITH_ABS_ERROR(corner, copyCube.getCorner(), EPSILON); + QCOMPARE_WITH_ABS_ERROR(scale, copyCube.getScale(), 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); + + // test misc + QCOMPARE_WITH_ABS_ERROR(newCorner, cube.getMinimumPoint(), EPSILON); + + glm::vec3 expectedMaxCorner = newCorner + glm::vec3(newScale); + QCOMPARE_WITH_ABS_ERROR(expectedMaxCorner, cube.getMaximumPoint(), EPSILON); + + glm::vec3 expectedCenter = newCorner + glm::vec3(0.5f * newScale); + QCOMPARE_WITH_ABS_ERROR(expectedCenter, cube.calcCenter(), EPSILON); +} + +void AACubeTests::containsPoint() { + glm::vec3 corner(4.56f, 7.89f, -1.35f); + float scale = 1.23f; + AACube cube(corner, scale); + + float delta = scale / 1000.0f; + glm::vec3 center = cube.calcCenter(); + QCOMPARE(cube.contains(center), true); + + for (int i = 0; i < 3; ++i) { + glm::vec3 scaleOffset = Vectors::ZERO; + scaleOffset[i] = 0.5f * scale; + + glm::vec3 deltaOffset = Vectors::ZERO; + deltaOffset[i] = delta; + + QCOMPARE(cube.contains(center + scaleOffset + deltaOffset), false); // outside +face + QCOMPARE(cube.contains(center + scaleOffset - deltaOffset), true); // inside +face + QCOMPARE(cube.contains(center - scaleOffset + deltaOffset), true); // inside -face + QCOMPARE(cube.contains(center - scaleOffset - deltaOffset), false); // outside -face + } +} + +void AACubeTests::touchesSphere() { + glm::vec3 corner(-4.56f, 7.89f, -1.35f); + float scale = 1.23f; + AACube cube(corner, scale); + + float delta = scale / 1000.0f; + glm::vec3 cubeCenter = cube.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(cube.touchesSphere(sphereCenter, sphereRadius), false); + + // inside +face + sphereCenter = cubeCenter + scaleOffset - deltaOffset; + QCOMPARE(cube.touchesSphere(sphereCenter, sphereRadius), true); + + // inside -face + sphereCenter = cubeCenter - scaleOffset + deltaOffset; + QCOMPARE(cube.touchesSphere(sphereCenter, sphereRadius), true); + + // outside -face + sphereCenter = cubeCenter - scaleOffset - deltaOffset; + QCOMPARE(cube.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(cube.touchesSphere(sphereCenter, sphereRadius), true); + sphereCenter = cubeCenter - edgeOffset - (sphereRadius - delta) * edgeDirection; + QCOMPARE(cube.touchesSphere(sphereCenter, sphereRadius), true); + + // outside ij + sphereCenter = cubeCenter + edgeOffset + (sphereRadius + delta) * edgeDirection; + QCOMPARE(cube.touchesSphere(sphereCenter, sphereRadius), false); + sphereCenter = cubeCenter - edgeOffset - (sphereRadius + delta) * edgeDirection; + QCOMPARE(cube.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(cube.touchesSphere(sphereCenter, sphereRadius), true); + sphereCenter = cubeCenter - edgeOffset - (sphereRadius - delta) * edgeDirection; + QCOMPARE(cube.touchesSphere(sphereCenter, sphereRadius), true); + + // outside ik + sphereCenter = cubeCenter + edgeOffset + (sphereRadius + delta) * edgeDirection; + QCOMPARE(cube.touchesSphere(sphereCenter, sphereRadius), false); + sphereCenter = cubeCenter - edgeOffset - (sphereRadius + delta) * edgeDirection; + QCOMPARE(cube.touchesSphere(sphereCenter, sphereRadius), false); + } + } +} + diff --git a/tests/shared/src/AACubeTests.h b/tests/shared/src/AACubeTests.h new file mode 100644 index 0000000000..c5a5db6ee2 --- /dev/null +++ b/tests/shared/src/AACubeTests.h @@ -0,0 +1,31 @@ +// +// AACubeTests.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_AACubeTests_h +#define hifi_AACubeTests_h + +#include +#include + +#include + +class AACubeTests : public QObject { + Q_OBJECT +private slots: + void ctorsAndSetters(); + void containsPoint(); + void touchesSphere(); +}; + +float getErrorDifference(const float& a, const float& b); +float getErrorDifference(const glm::vec3& a, const glm::vec3& b); + +#endif // hifi_AACubeTests_h