From 89e0f631d6dcc4f893faca37f8e210c1b32dbd8f Mon Sep 17 00:00:00 2001 From: Gawaboumga Date: Fri, 21 Aug 2015 12:05:46 +0200 Subject: [PATCH] No problems No problems found in Rect, Sphere, Vector2, Vector3 and Vector4 Former-commit-id: 6688608a2beaa9cf5328daf7e5108b6cfebd843d --- include/Nazara/Math/Rect.inl | 4 +-- include/Nazara/Math/Sphere.inl | 14 ++++---- tests/Nazara/Math/Rect.cpp | 56 ++++++++++++++++++++++++++++++ tests/Nazara/Math/Sphere.cpp | 63 ++++++++++++++++++++++++++++++++++ tests/Nazara/Math/Vector2.cpp | 49 ++++++++++++++++++++++++++ tests/Nazara/Math/Vector3.cpp | 56 ++++++++++++++++++++++++++++++ tests/Nazara/Math/Vector4.cpp | 43 +++++++++++++++++++++++ 7 files changed, 276 insertions(+), 9 deletions(-) create mode 100644 tests/Nazara/Math/Rect.cpp create mode 100644 tests/Nazara/Math/Sphere.cpp create mode 100644 tests/Nazara/Math/Vector2.cpp create mode 100644 tests/Nazara/Math/Vector3.cpp create mode 100644 tests/Nazara/Math/Vector4.cpp diff --git a/include/Nazara/Math/Rect.inl b/include/Nazara/Math/Rect.inl index 9fba791c0..9dc595708 100644 --- a/include/Nazara/Math/Rect.inl +++ b/include/Nazara/Math/Rect.inl @@ -64,7 +64,7 @@ template bool NzRect::Contains(const NzRect& rect) const { return Contains(rect.x, rect.y) && - Contains(rect.x + rect.width, rect.y + rect.height); + Contains(rect.x + rect.width, rect.y + rect.height); } template @@ -410,7 +410,7 @@ template bool NzRect::operator==(const NzRect& rect) const { return NzNumberEquals(x, rect.x) && NzNumberEquals(y, rect.y) && - NzNumberEquals(width, rect.width) && NzNumberEquals(height, rect.height); + NzNumberEquals(width, rect.width) && NzNumberEquals(height, rect.height); } template diff --git a/include/Nazara/Math/Sphere.inl b/include/Nazara/Math/Sphere.inl index f8ece724c..340e1319d 100644 --- a/include/Nazara/Math/Sphere.inl +++ b/include/Nazara/Math/Sphere.inl @@ -51,13 +51,13 @@ bool NzSphere::Contains(T X, T Y, T Z) const template bool NzSphere::Contains(const NzBox& box) const { - if (box.GetMinimum().SquaredDistance(GetPosition()) <= radius * radius) - { - if (box.GetMaximum().SquaredDistance(GetPosition()) <= radius * radius) - return true; - } + if (box.GetMinimum().SquaredDistance(GetPosition()) <= radius * radius) + { + if (box.GetMaximum().SquaredDistance(GetPosition()) <= radius * radius) + return true; + } - return false; + return false; } template bool NzSphere::Contains(const NzVector3& point) const @@ -156,7 +156,7 @@ bool NzSphere::Intersect(const NzBox& box) const squaredDistance += diff*diff; } - return squaredDistance <= radius * radius; + return squaredDistance <= radius * radius; } template diff --git a/tests/Nazara/Math/Rect.cpp b/tests/Nazara/Math/Rect.cpp new file mode 100644 index 000000000..a09eb747c --- /dev/null +++ b/tests/Nazara/Math/Rect.cpp @@ -0,0 +1,56 @@ +#include +#include + +SCENARIO("Rect", "[MATH][RECT]") +{ + GIVEN("Two same rectangles center and unit lengths") + { + NzRectf firstCenterAndUnit(0.f, 0.f, 1.f, 1.f); + NzRectf secondCenterAndUnit(NzRecti(NzVector2i::Unit(), NzVector2i::Zero())); + + WHEN("We ask if they are the same") + { + THEN("They should be") + { + REQUIRE(firstCenterAndUnit == secondCenterAndUnit); + REQUIRE(firstCenterAndUnit.GetCenter() == secondCenterAndUnit.GetCenter()); + REQUIRE(firstCenterAndUnit.GetCorner(nzRectCorner_LeftBottom) == secondCenterAndUnit.GetCorner(nzRectCorner_LeftBottom)); + CHECK(firstCenterAndUnit.IsValid()); + } + } + + WHEN("We move one from (0.5, 0.5)") + { + firstCenterAndUnit.Translate(NzVector2f(0.5f, 0.5f)); + + THEN("The collision should be (0.5, 0.5) -> (0.5, 0.5)") + { + NzRectf tmp; + CHECK(firstCenterAndUnit.Intersect(secondCenterAndUnit, &tmp)); + REQUIRE(tmp == NzRectf(0.5f, 0.5f, 0.5f, 0.5f)); + } + } + + WHEN("We make an empty") + { + THEN("It's not valid") + { + CHECK(!(firstCenterAndUnit * 0.f).IsValid()); + } + } + + WHEN("We ask for infos") + { + THEN("These results are expected") + { + REQUIRE(firstCenterAndUnit.GetLengths() == NzVector2f::Unit()); + REQUIRE(firstCenterAndUnit.GetMaximum() == NzVector2f::Unit()); + REQUIRE(firstCenterAndUnit.GetMinimum() == NzVector2f::Zero()); + REQUIRE(firstCenterAndUnit.GetNegativeVertex(NzVector2f::Unit()) == NzVector2f::Zero()); + REQUIRE(firstCenterAndUnit.GetPosition() == NzVector2f::Zero()); + REQUIRE(firstCenterAndUnit.GetPositiveVertex(NzVector2f::Unit()) == NzVector2f::Unit()); + + } + } + } +} diff --git a/tests/Nazara/Math/Sphere.cpp b/tests/Nazara/Math/Sphere.cpp new file mode 100644 index 000000000..cb668999a --- /dev/null +++ b/tests/Nazara/Math/Sphere.cpp @@ -0,0 +1,63 @@ +#include +#include + +SCENARIO("Sphere", "[MATH][SPHERE]") +{ + GIVEN("Two same sphere center and unit") + { + NzSpheref firstCenterAndUnit(0.f, 0.f, 0.f, 1.f); + NzSpheref secondCenterAndUnit(NzSphere(NzVector3i::Zero(), 1)); + + WHEN("We compare them") + { + THEN("They are the same") + { + REQUIRE(firstCenterAndUnit == secondCenterAndUnit); + } + } + + WHEN("We ask if they intersect or contain") + { + THEN("These results are expected for Contains") + { + CHECK(firstCenterAndUnit.Contains(0.5f, 0.5f, 0.5f)); + CHECK(firstCenterAndUnit.Contains(NzBoxf(NzVector3f::Zero(), NzVector3f::Unit() * 0.5f))); + CHECK(!firstCenterAndUnit.Contains(NzBoxf(NzVector3f::Zero(), NzVector3f::Unit() * 5.f))); + } + + THEN("There are for Intersect") + { + CHECK(firstCenterAndUnit.Intersect(NzBoxf(NzVector3f::Zero(), NzVector3f::Unit() * 0.5f))); + CHECK(firstCenterAndUnit.Intersect(NzBoxf(NzVector3f::Zero(), NzVector3f::Unit() * 5.f))); + CHECK(!firstCenterAndUnit.Intersect(NzBoxf(NzVector3f::Unit() * 5.f, NzVector3f::Unit()))); + + CHECK(firstCenterAndUnit.Intersect(NzSpheref(NzVector3f::Zero(), 0.5f))); + CHECK(firstCenterAndUnit.Intersect(NzSpheref(NzVector3f::Zero(), 5.f))); + CHECK(!firstCenterAndUnit.Intersect(NzSpheref(NzVector3f::Unit() * 5.f, 1.f))); + } + } + + WHEN("We ask for distance") + { + THEN("These results are expected") + { + REQUIRE(firstCenterAndUnit.Distance(NzVector3f::UnitX()) == Approx(1.f)); + REQUIRE(firstCenterAndUnit.SquaredDistance(NzVector3f::UnitX()) == Approx(1.f)); + + NzSpheref tmp(NzVector3f::UnitX(), 1.f); + REQUIRE(tmp.Distance(NzVector3f::UnitX() * 4.f) == Approx(3.f)); + REQUIRE(tmp.SquaredDistance(NzVector3f::UnitX() * 4.f) == Approx(9.f)); + } + } + + WHEN("We get sphere from box unit and center") + { + NzBoxf centerUnitBox(NzVector3f::Unit() * -0.5f, NzVector3f::Unit() * 0.5f); + + THEN("This is equal to sphere center and radius 0.75") + { + REQUIRE(centerUnitBox.GetSquaredBoundingSphere() == NzSpheref(NzVector3f::Zero(), 0.75f)); + } + } + } +} diff --git a/tests/Nazara/Math/Vector2.cpp b/tests/Nazara/Math/Vector2.cpp new file mode 100644 index 000000000..533a6bf35 --- /dev/null +++ b/tests/Nazara/Math/Vector2.cpp @@ -0,0 +1,49 @@ +#include +#include + +#include + +SCENARIO("Vector2", "[MATH][VECTOR2]") +{ + GIVEN("Two same vectors (1, 1)") + { + NzVector2f firstUnit(1.f); + NzVector2f secondUnit(NzVector2i(NzVector4i(1, 1, 3, 5))); + + WHEN("We compare them") + { + THEN("They are the same") + { + REQUIRE(firstUnit == secondUnit); + } + } + + WHEN("We test the dot product") + { + NzVector2f tmp(-1.f, 1.f); + + THEN("These results are expected") + { + REQUIRE(firstUnit.AbsDotProduct(tmp) == Approx(2.f)); + REQUIRE(firstUnit.DotProduct(tmp) == Approx(0.f)); + REQUIRE(firstUnit.AngleBetween(tmp) == Approx(90.f)); + } + } + + WHEN("We ask for distance from (-2, -3)") + { + NzVector2f tmp(-2.f, -3.f); + NzVector2f tmp2(-1.f, -1.f); + + THEN("These are expected") + { + REQUIRE(firstUnit.Distance(tmp2) == Approx(2.f * std::sqrt(2.f))); + REQUIRE(firstUnit.Distance(tmp) == Approx(5.f)); + REQUIRE(firstUnit.SquaredDistance(tmp) == Approx(25.f)); + + REQUIRE(firstUnit.GetSquaredLength() == Approx(2.f)); + REQUIRE(firstUnit.GetLength() == Approx(std::sqrt(2.f))); + } + } + } +} diff --git a/tests/Nazara/Math/Vector3.cpp b/tests/Nazara/Math/Vector3.cpp new file mode 100644 index 000000000..de8290a64 --- /dev/null +++ b/tests/Nazara/Math/Vector3.cpp @@ -0,0 +1,56 @@ +#include +#include + +#include + +SCENARIO("Vector3", "[MATH][VECTOR3]") +{ + GIVEN("Two same unit vector") + { + NzVector3f firstUnit(1.f, 1.f, 1.f); + NzVector3f secondUnit(NzVector3i(NzVector4i(1, 1, 1, 5))); + + WHEN("We compare them") + { + THEN("They are the same") + { + REQUIRE(firstUnit == secondUnit); + } + } + + WHEN("We test the dot product") + { + NzVector3f tmp(-1.f, 0.f, 1.f); + + THEN("These results are expected") + { + REQUIRE(firstUnit.AbsDotProduct(tmp) == Approx(2.f)); + REQUIRE(firstUnit.DotProduct(tmp) == Approx(0.f)); + REQUIRE(firstUnit.AngleBetween(tmp) == Approx(90.f)); + } + } + + WHEN("We test the cross product") + { + THEN("These results are expected") + { + REQUIRE(NzVector3f::CrossProduct(NzVector3f::UnitX(), NzVector3f::UnitY()) == NzVector3f::UnitZ()); + REQUIRE(NzVector3f::CrossProduct(NzVector3f(1.f, 2.f, 3.f), NzVector3f(3.f, 2.f, 1.f)) == NzVector3f(-4.f, 8.f, -4.f)); + } + } + + WHEN("We ask for distance") + { + NzVector3f tmp(-1.f, -5.f, -8.f); + + THEN("These are expected") + { + REQUIRE(firstUnit.Distance(tmp) == Approx(11.f)); + REQUIRE(firstUnit.SquaredDistance(tmp) == Approx(121.f)); + + REQUIRE(firstUnit.GetSquaredLength() == Approx(3.f)); + REQUIRE(firstUnit.GetLength() == Approx(std::sqrt(3.f))); + } + } + } +} diff --git a/tests/Nazara/Math/Vector4.cpp b/tests/Nazara/Math/Vector4.cpp new file mode 100644 index 000000000..ba60c2232 --- /dev/null +++ b/tests/Nazara/Math/Vector4.cpp @@ -0,0 +1,43 @@ +#include +#include + +#include + +SCENARIO("Vector4", "[MATH][VECTOR4]") +{ + GIVEN("Two same unit vector") + { + NzVector4f firstUnit(1.f, 1.f, 1.f); + NzVector4f secondUnit(NzVector4i(1, 1, 1, 1)); + + WHEN("We compare them") + { + THEN("They are the same") + { + REQUIRE(firstUnit == secondUnit); + } + } + + WHEN("We test the dot product") + { + NzVector4f tmp(-1.f, 0.f, 1.f, 0.f); + + THEN("These results are expected") + { + REQUIRE(firstUnit.AbsDotProduct(tmp) == Approx(2.f)); + REQUIRE(firstUnit.DotProduct(tmp) == Approx(0.f)); + } + } + + WHEN("We normalize") + { + NzVector4f tmp(1.f, 1.f, 1.f, 3.f); + + THEN("These results are expected") + { + REQUIRE(firstUnit.Normalize() == NzVector4f(1.f, NzVector3f::Unit())); + REQUIRE(tmp.Normalize() == NzVector4f(NzVector3f::Unit() * (1.f / 3.f), 1.f)); + } + } + } +}