summaryrefslogtreecommitdiff
path: root/lib/maths.cpp
blob: 68662fc4c0fb6c81cf863293df21c68936f8f0e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include "maths.h"
#include <cmath>
#include <glm/glm.hpp>
#include <glm/gtx/rotate_vector.hpp>
#include <glm/gtx/transform.hpp>

glm::mat4
flat_orientation(const Direction3D & diff)
{
	static const auto oneeighty {glm::rotate(pi, up)};
	const auto flatdiff {glm::normalize(diff.xy() || 0.F)};
	auto e {glm::orientation(flatdiff, north)};
	// Handle if diff is exactly opposite to north
	return (std::isnan(e[0][0])) ? oneeighty : e;
}

// Helper to lookup into a matrix given an xy vector coordinate
template<typename M, typename I>
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<typename M, typename I>
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<glm::mat2, glm::length_t>(a, {0, 0}, {0, 1}, {1, 1}, {1, 0});
}

// Create a yaw transformation matrix
glm::mat4
rotate_yaw(float a)
{
	return rotation<glm::mat4, glm::length_t>(a, {0, 0}, {1, 0}, {1, 1}, {0, 1});
}

// Create a roll transformation matrix
glm::mat4
rotate_roll(float a)
{
	return rotation<glm::mat4, glm::length_t>(a, {0, 0}, {2, 0}, {2, 2}, {0, 2});
}

// Create a pitch transformation matrix
glm::mat4
rotate_pitch(float a)
{
	return rotation<glm::mat4, glm::length_t>(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_yaw(a.y) * rotate_pitch(a.x);
}

float
vector_yaw(const Direction3D & 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;
}

float
operator"" _mph(const long double v)
{
	return static_cast<float>(mph_to_ms(v));
}

float
operator"" _kph(const long double v)
{
	return static_cast<float>(kph_to_ms(v));
}