diff --git a/include/Nazara/Math/Box.inl b/include/Nazara/Math/Box.inl index 58904abc3..19da1ef63 100644 --- a/include/Nazara/Math/Box.inl +++ b/include/Nazara/Math/Box.inl @@ -65,7 +65,7 @@ template bool NzBox::Contains(const NzBox& box) const { return Contains(box.x, box.y, box.z) && - Contains(box.x + box.width, box.y + box.height, box.z + box.depth); + Contains(box.x + box.width, box.y + box.height, box.z + box.depth); } template @@ -399,9 +399,9 @@ NzBox& NzBox::Transform(const NzMatrix4& matrix, bool applyTranslation) NzVector3 center = matrix.Transform(GetCenter(), (applyTranslation) ? F(1.0) : F(0.0)); // Valeur multipliant la translation NzVector3 halfSize = GetLengths()/F(2.0); - halfSize.Set(std::fabs(matrix(0,0))*halfSize.x + std::fabs(matrix(1,0))*halfSize.y + std::fabs(matrix(2,0))*halfSize.z, - std::fabs(matrix(0,1))*halfSize.x + std::fabs(matrix(1,1))*halfSize.y + std::fabs(matrix(2,1))*halfSize.z, - std::fabs(matrix(0,2))*halfSize.x + std::fabs(matrix(1,2))*halfSize.y + std::fabs(matrix(2,2))*halfSize.z); + halfSize.Set(std::abs(matrix(0,0))*halfSize.x + std::abs(matrix(1,0))*halfSize.y + std::abs(matrix(2,0))*halfSize.z, + std::abs(matrix(0,1))*halfSize.x + std::abs(matrix(1,1))*halfSize.y + std::abs(matrix(2,1))*halfSize.z, + std::abs(matrix(0,2))*halfSize.x + std::abs(matrix(1,2))*halfSize.y + std::abs(matrix(2,2))*halfSize.z); return Set(center - halfSize, center + halfSize); } @@ -486,7 +486,7 @@ template bool NzBox::operator==(const NzBox& box) const { return NzNumberEquals(x, box.x) && NzNumberEquals(y, box.y) && NzNumberEquals(z, box.z) && - NzNumberEquals(width, box.width) && NzNumberEquals(height, box.height) && NzNumberEquals(depth, box.depth); + NzNumberEquals(width, box.width) && NzNumberEquals(height, box.height) && NzNumberEquals(depth, box.depth); } template diff --git a/tests/Nazara/Math/Box.cpp b/tests/Nazara/Math/Box.cpp new file mode 100644 index 000000000..724fe1ec5 --- /dev/null +++ b/tests/Nazara/Math/Box.cpp @@ -0,0 +1,113 @@ +#include +#include + +SCENARIO("Box", "[MATH][BOX]") +{ + GIVEN("Two zero boxes") + { + NzBoxf firstZero(NzBoxf::Zero()); + NzBoxf secondZero(NzVector3f::Zero(), NzVector3f::Zero()); + + WHEN("We multiply them") + { + firstZero = firstZero * 1.f; + secondZero = secondZero * NzVector3f::Unit() * 3.f; + + THEN("They should stay the same") + { + REQUIRE(firstZero == secondZero); + CHECK(!firstZero.IsValid()); + CHECK(!secondZero.IsValid()); + } + } + } + + GIVEN("Two unit and center boxes") + { + NzBoxf firstCenterAndUnit(NzRectf(NzVector2f::Zero(), NzVector2f::Unit())); + NzBoxf secondCenterAndUnit(1.f, 1.f, 1.f); + + WHEN("We ask for some informations") + { + THEN("These results are expected") + { + REQUIRE(firstCenterAndUnit.GetBoundingSphere() == NzSpheref(NzVector3f::Unit() * 0.5f, std::sqrt(3.f * 0.5f * 0.5f))); + REQUIRE(firstCenterAndUnit.GetCenter() == (NzVector3f::Unit() * 0.5f)); + REQUIRE(firstCenterAndUnit.GetCorner(nzBoxCorner_FarLeftTop) == NzVector3f::UnitY()); + REQUIRE(firstCenterAndUnit.GetLengths() == NzVector3f::Unit()); + REQUIRE(firstCenterAndUnit.GetMaximum() == NzVector3f::Unit()); + REQUIRE(firstCenterAndUnit.GetMinimum() == NzVector3f::Zero()); + REQUIRE(firstCenterAndUnit.GetNegativeVertex(NzVector3f::Unit()) == NzVector3f::Zero()); + REQUIRE(firstCenterAndUnit.GetPosition() == NzVector3f::Zero()); + REQUIRE(firstCenterAndUnit.GetPositiveVertex(NzVector3f::Unit()) == NzVector3f::Unit()); + REQUIRE(firstCenterAndUnit.GetRadius() == Approx(std::sqrt(3.f * 0.5f * 0.5f))); + REQUIRE(firstCenterAndUnit.GetSquaredBoundingSphere() == NzSpheref(NzVector3f::Unit() * 0.5f, 3.f * 0.5f * 0.5f)); + REQUIRE(firstCenterAndUnit.GetSquaredRadius() == Approx(3.f * 0.5f * 0.5f)); + } + } + + WHEN("We ask for the intersection between the two") + { + THEN("We should have a center and unit") + { + NzBoxf thirdCenterAndUnit; + CHECK(firstCenterAndUnit.Intersect(secondCenterAndUnit, &thirdCenterAndUnit)); + REQUIRE(firstCenterAndUnit == secondCenterAndUnit); + } + } + + WHEN("We use the constructor of conversion") + { + THEN("Shouldn't be a problem") + { + NzBoxf tmp(NzBoxi(0, 0, 0, 1, 1, 1)); + REQUIRE(tmp == firstCenterAndUnit); + } + } + } + + GIVEN("Two wrong box (negative width, height and depth") + { + NzBoxf firstWrongBox(-NzVector3f::Unit()); + NzBoxf secondWrongBox(-NzVector3f::Unit()); + + WHEN("We check if valid") + { + THEN("Result if false") + { + CHECK(!firstWrongBox.IsValid()); + CHECK(!secondWrongBox.IsValid()); + } + } + + WHEN("We correct them") + { + firstWrongBox.ExtendTo(NzVector3f::Unit()); + secondWrongBox.Transform(NzMatrix4f::Scale(-NzVector3f::Unit())); + + THEN("They should be valid") + { + CHECK(firstWrongBox.IsValid()); + CHECK(secondWrongBox.IsValid()); + } + + AND_WHEN("We ask if they contain boxes") + { + THEN("These results are expected") + { + CHECK(firstWrongBox.Contains(0.f, 0.f, 0.f)); + CHECK(secondWrongBox.Contains(0.f, 0.f, 0.f)); + + secondWrongBox = secondWrongBox.Lerp(NzBoxf::Zero(), secondWrongBox, 0.f); // Zeroed + secondWrongBox.ExtendTo(NzBoxf(NzVector3f(0.1f, 0.1f, 0.1f), NzVector3f(0.9f, 0.9f, 0.9f))); + secondWrongBox.Translate(NzVector3f(0.05f, 0.05f, 0.05f)); // Box 0.15 to 0.95 + CHECK(firstWrongBox.Contains(secondWrongBox)); + + NzBoxf test(1.f, -500.f, -500.f, 1000.f, 1000.f, 1000.f); + CHECK(test.Contains(NzBoxf(500.f, -0.5f, -0.5f, 1.f, 1.f, 1.f))); + CHECK(test.Contains(500.f, 0.f, 0.f)); + } + } + } + } +}