summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2024-12-15 14:38:00 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2024-12-15 14:38:00 +0000
commit39985f36e807126b2c7b9ec6096bda3922903a54 (patch)
tree6f7f69bccb02ac49b722c8b0981b08e928ea2678
parentvector difference works with floating point (diff)
downloadilt-39985f36e807126b2c7b9ec6096bda3922903a54.tar.bz2
ilt-39985f36e807126b2c7b9ec6096bda3922903a54.tar.xz
ilt-39985f36e807126b2c7b9ec6096bda3922903a54.zip
2D triangle area support
-rw-r--r--lib/triangle.h7
-rw-r--r--test/test-geoData.cpp17
-rw-r--r--test/test-maths.cpp34
3 files changed, 41 insertions, 17 deletions
diff --git a/lib/triangle.h b/lib/triangle.h
index 812bfab..5787094 100644
--- a/lib/triangle.h
+++ b/lib/triangle.h
@@ -31,6 +31,13 @@ struct Triangle : public glm::vec<3, glm::vec<Dim, T, Q>> {
return glm::length(crossProduct(sideDifference(1), sideDifference(2))) / T {2};
}
+ [[nodiscard]] constexpr auto
+ area() const
+ requires(Dim == 2)
+ {
+ return std::abs((sideDifference(1).x * sideDifference(2).y) - (sideDifference(2).x * sideDifference(1).y)) / 2;
+ }
+
[[nodiscard]] constexpr Normal3D
normal() const
requires(Dim == 3)
diff --git a/test/test-geoData.cpp b/test/test-geoData.cpp
index 5411b61..1ca050d 100644
--- a/test/test-geoData.cpp
+++ b/test/test-geoData.cpp
@@ -190,23 +190,6 @@ BOOST_DATA_TEST_CASE(walkTerrainUntil,
BOOST_CHECK_EQUAL_COLLECTIONS(visited.begin(), visited.end(), visits.begin(), visits.end());
}
-BOOST_AUTO_TEST_CASE(triangle_helpers)
-{
- constexpr static GeoData::Triangle<3> t {{0, 0, 0}, {5, 0, 0}, {5, 5, 0}};
-
- BOOST_CHECK_EQUAL(t.nnormal(), up);
- BOOST_CHECK_CLOSE(t.angle(0), quarter_pi, 0.01F);
- BOOST_CHECK_CLOSE(t.angleAt({0, 0, 0}), quarter_pi, 0.01F);
- BOOST_CHECK_CLOSE(t.angle(1), half_pi, 0.01F);
- BOOST_CHECK_CLOSE(t.angleAt({5, 0, 0}), half_pi, 0.01F);
- BOOST_CHECK_CLOSE(t.angle(2), quarter_pi, 0.01F);
- BOOST_CHECK_CLOSE(t.angleAt({5, 5, 0}), quarter_pi, 0.01F);
-
- BOOST_CHECK_CLOSE(t.angleAt({0, 1, 0}), 0.F, 0.01F);
-
- BOOST_CHECK_CLOSE(t.area(), 12.5F, 0.01F);
-}
-
using FindEntiesData = std::tuple<GlobalPosition2D, GlobalPosition2D, int>;
BOOST_DATA_TEST_CASE(findEntries,
diff --git a/test/test-maths.cpp b/test/test-maths.cpp
index b9d08bb..1278c44 100644
--- a/test/test-maths.cpp
+++ b/test/test-maths.cpp
@@ -12,6 +12,7 @@
#include <gfx/gl/camera.h>
#include <glm/glm.hpp>
#include <maths.h>
+#include <triangle.h>
#include <tuple>
using vecter_and_angle = std::tuple<glm::vec3, float>;
@@ -341,3 +342,36 @@ static_assert(linesIntersectAt(GlobalPosition2D {311000100, 491100100}, {3110500
.value()
== GlobalPosition2D {311000100, 491100100});
static_assert(!linesIntersectAt(glm::dvec2 {0, 1}, {0, 4}, {1, 8}, {1, 4}).has_value());
+
+BOOST_AUTO_TEST_CASE(triangle2d_helpers)
+{
+ constexpr static Triangle<2, float> t {{0, 0}, {5, 0}, {5, 5}};
+
+ BOOST_CHECK_CLOSE(t.angle(0), quarter_pi, 0.01F);
+ BOOST_CHECK_CLOSE(t.angleAt({0, 0}), quarter_pi, 0.01F);
+ BOOST_CHECK_CLOSE(t.angle(1), half_pi, 0.01F);
+ BOOST_CHECK_CLOSE(t.angleAt({5, 0}), half_pi, 0.01F);
+ BOOST_CHECK_CLOSE(t.angle(2), quarter_pi, 0.01F);
+ BOOST_CHECK_CLOSE(t.angleAt({5, 5}), quarter_pi, 0.01F);
+
+ BOOST_CHECK_CLOSE(t.angleAt({0, 1}), 0.F, 0.01F);
+
+ BOOST_CHECK_CLOSE(t.area(), 12.5F, 0.01F);
+}
+
+BOOST_AUTO_TEST_CASE(triangle3d_helpers)
+{
+ constexpr static Triangle<3, float> t {{0, 0, 0}, {5, 0, 0}, {5, 5, 0}};
+
+ BOOST_CHECK_EQUAL(t.nnormal(), up);
+ BOOST_CHECK_CLOSE(t.angle(0), quarter_pi, 0.01F);
+ BOOST_CHECK_CLOSE(t.angleAt({0, 0, 0}), quarter_pi, 0.01F);
+ BOOST_CHECK_CLOSE(t.angle(1), half_pi, 0.01F);
+ BOOST_CHECK_CLOSE(t.angleAt({5, 0, 0}), half_pi, 0.01F);
+ BOOST_CHECK_CLOSE(t.angle(2), quarter_pi, 0.01F);
+ BOOST_CHECK_CLOSE(t.angleAt({5, 5, 0}), quarter_pi, 0.01F);
+
+ BOOST_CHECK_CLOSE(t.angleAt({0, 1, 0}), 0.F, 0.01F);
+
+ BOOST_CHECK_CLOSE(t.area(), 12.5F, 0.01F);
+}