From 38a839533e76d3e9570591a6aeec7c5c500270cf Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 5 Oct 2024 12:15:28 +0100 Subject: Add rotate_yp taking two separate parameters --- lib/maths.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'lib/maths.cpp') diff --git a/lib/maths.cpp b/lib/maths.cpp index 51e27fe..ea245f0 100644 --- a/lib/maths.cpp +++ b/lib/maths.cpp @@ -77,7 +77,13 @@ rotate_ypr(Rotation3D a) glm::mat4 rotate_yp(Rotation2D a) { - return rotate_yaw(a.y) * rotate_pitch(a.x); + return rotate_yp(a.y, a.x); +} + +glm::mat4 +rotate_yp(Angle yaw, Angle pitch) +{ + return rotate_yaw(yaw) * rotate_pitch(pitch); } float -- cgit v1.2.3 From cfec8308a39ae4a2a1a1b1fe0df0a099470d9c78 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Thu, 17 Oct 2024 02:58:16 +0100 Subject: Simple constexpr pow function --- lib/maths.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'lib/maths.cpp') diff --git a/lib/maths.cpp b/lib/maths.cpp index ea245f0..f527881 100644 --- a/lib/maths.cpp +++ b/lib/maths.cpp @@ -116,6 +116,18 @@ normalize(float ang) return ang; } +static_assert(pow(1, 0) == 1); +static_assert(pow(1, 1) == 1); +static_assert(pow(1, 2) == 1); +static_assert(pow(2, 0) == 1); +static_assert(pow(2, 1) == 2); +static_assert(pow(2, 2) == 4); +static_assert(pow(2, 3) == 8); +static_assert(pow(3, 0) == 1); +static_assert(pow(3, 1) == 3); +static_assert(pow(3, 2) == 9); +static_assert(pow(pi, 3) == 31.006278991699219F); + float operator"" _mph(const long double v) { -- cgit v1.2.3 From 5b6a6f3b241fea6d19521ddbb705e27d5e4c0268 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 20 Oct 2024 16:24:14 +0100 Subject: Move lots of maths helpers to inline, constexpr, templates Always for working with different dimensions/types --- lib/maths.cpp | 97 ----------------------------------------------------------- 1 file changed, 97 deletions(-) (limited to 'lib/maths.cpp') diff --git a/lib/maths.cpp b/lib/maths.cpp index f527881..3a9bf9b 100644 --- a/lib/maths.cpp +++ b/lib/maths.cpp @@ -19,103 +19,6 @@ flat_orientation(const Direction3D & diff) return (std::isnan(e[0][0])) ? oneeighty : e; } -// Helper to lookup into a matrix given an xy vector coordinate -template -inline auto & -operator^(M & m, glm::vec<2, I> xy) -{ - return m[xy.x][xy.y]; -} - -// Create a matrix for the angle, given the targets into the matrix -template -inline auto -rotation(typename M::value_type a, glm::vec<2, I> c1, glm::vec<2, I> s1, glm::vec<2, I> c2, glm::vec<2, I> ms2) -{ - M m(1); - sincosf(a, m ^ s1, m ^ c1); - m ^ c2 = m ^ c1; - m ^ ms2 = -(m ^ s1); - return m; -} - -// Create a flat (2D) transformation matrix -glm::mat2 -rotate_flat(float a) -{ - return rotation(a, {0, 0}, {0, 1}, {1, 1}, {1, 0}); -} - -// Create a yaw transformation matrix -glm::mat4 -rotate_yaw(float a) -{ - return rotation(a, {0, 0}, {1, 0}, {1, 1}, {0, 1}); -} - -// Create a roll transformation matrix -glm::mat4 -rotate_roll(float a) -{ - return rotation(a, {0, 0}, {2, 0}, {2, 2}, {0, 2}); -} - -// Create a pitch transformation matrix -glm::mat4 -rotate_pitch(float a) -{ - return rotation(a, {1, 1}, {1, 2}, {2, 2}, {2, 1}); -} - -// Create a combined yaw, pitch, roll transformation matrix -glm::mat4 -rotate_ypr(Rotation3D a) -{ - return rotate_yaw(a.y) * rotate_pitch(a.x) * rotate_roll(a.z); -} - -glm::mat4 -rotate_yp(Rotation2D a) -{ - return rotate_yp(a.y, a.x); -} - -glm::mat4 -rotate_yp(Angle yaw, Angle pitch) -{ - return rotate_yaw(yaw) * rotate_pitch(pitch); -} - -float -vector_yaw(const Direction2D & diff) -{ - return std::atan2(diff.x, diff.y); -} - -float -vector_pitch(const Direction3D & diff) -{ - return std::atan(diff.z); -} - -float -round_frac(const float & v, const float & frac) -{ - return std::round(v / frac) * frac; -} - -float -normalize(float ang) -{ - while (ang > pi) { - ang -= two_pi; - } - while (ang <= -pi) { - ang += two_pi; - } - return ang; -} - static_assert(pow(1, 0) == 1); static_assert(pow(1, 1) == 1); static_assert(pow(1, 2) == 1); -- cgit v1.2.3