summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/maths.cpp47
-rw-r--r--lib/maths.h21
2 files changed, 68 insertions, 0 deletions
diff --git a/lib/maths.cpp b/lib/maths.cpp
index 543c887..60413f4 100644
--- a/lib/maths.cpp
+++ b/lib/maths.cpp
@@ -15,6 +15,53 @@ flat_orientation(const glm::vec3 & diff)
return (std::isnan(e[0][0])) ? oneeighty : e;
}
+// Helper to lookup into a matrix given an xy vector coordinate
+template<typename M>
+inline auto &
+operator^(M & m, glm::ivec2 xy)
+{
+ return m[xy.x][xy.y];
+}
+
+// Create a matrix for the angle, given the targets into the matrix
+inline auto
+rotation(float a, glm::ivec2 c1, glm::ivec2 s1, glm::ivec2 c2, glm::ivec2 ms2)
+{
+ glm::mat4 m(1);
+ sincosf(a, m ^ s1, m ^ c1);
+ m ^ c2 = m ^ c1;
+ m ^ ms2 = -(m ^ s1);
+ return m;
+}
+
+// Create a roll transformation matrix
+glm::mat4
+rotate_roll(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}, {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 bomcined yaw, pitch, roll transformation matrix
+glm::mat4
+rotate_ypr(glm::vec3 a)
+{
+ return rotate_yaw(a.y) * rotate_pitch(a.x) * rotate_roll(a.z);
+}
+
float
vector_yaw(const glm::vec3 & diff)
{
diff --git a/lib/maths.h b/lib/maths.h
index c072283..d304b65 100644
--- a/lib/maths.h
+++ b/lib/maths.h
@@ -1,6 +1,7 @@
#ifndef MATH_H
#define MATH_H
+#include <cmath>
#include <glm/glm.hpp>
#include <glm/gtc/constants.hpp>
#include <utility>
@@ -29,6 +30,26 @@ constexpr auto two_pi {glm::two_pi<float>()};
glm::mat4 flat_orientation(const glm::vec3 & diff);
+// C++ wrapper for C's sincosf, but with references, not pointers
+inline auto
+sincosf(float a, float & s, float & c)
+{
+ return sincosf(a, &s, &c);
+}
+
+inline std::pair<float, float>
+sincosf(float a)
+{
+ std::pair<float, float> sc;
+ sincosf(a, &sc.first, &sc.second);
+ return sc;
+}
+
+glm::mat4 rotate_roll(float);
+glm::mat4 rotate_yaw(float);
+glm::mat4 rotate_pitch(float);
+glm::mat4 rotate_ypr(glm::vec3);
+
float vector_yaw(const glm::vec3 & diff);
float vector_pitch(const glm::vec3 & diff);