GCC Code Coverage Report


libs/base/src/base/
File: angle.cc
Date: 2025-03-19 20:55:25
Lines:
76/76
100.0%
Functions:
26/26
100.0%
Branches:
16/36
44.4%

Line Branch Exec Source
1 #include "base/angle.h"
2
3 #include "assert/assert.h"
4 #include "base/numeric.h"
5 #include "base/range.h"
6
7 #include <cmath>
8
9 namespace eu
10 {
11 void
12 2 An::wrap()
13 {
14
2/4
✓ Branch 0 (2 → 3) taken 2 times.
✗ Branch 1 (2 → 5) not taken.
✓ Branch 2 (3 → 4) taken 2 times.
✗ Branch 3 (3 → 5) not taken.
2 radians = ::eu::wrap(make_range(pi * 2.0f), radians);
15 2 }
16
17
18 void
19 13 An::operator+=(const An& rhs)
20 {
21 13 radians += rhs.radians;
22 13 }
23
24
25 void
26 23 An::operator-=(const An& rhs)
27 {
28 23 radians -= rhs.radians;
29 23 }
30
31
32 void
33 35 An::operator*=(float rhs)
34 {
35 35 radians *= rhs;
36 35 }
37
38
39 void
40 101 An::operator/=(float rhs)
41 {
42 101 radians /= rhs;
43 101 }
44
45
46 An
47 27 An::operator-() const
48 {
49 27 return An::from_radians(-radians);
50 }
51
52
53 float
54 82 sin(const An& ang)
55 {
56 82 return std::sin(ang.as_radians());
57 }
58
59
60 float
61 77 cos(const An& ang)
62 {
63 77 return std::cos(ang.as_radians());
64 }
65
66
67 float
68 6 tan(const An& ang)
69 {
70 6 return std::tan(ang.as_radians());
71 }
72
73
74 An
75 5 asin(float v)
76 {
77
1/4
✗ Branch 0 (2 → 3) not taken.
✓ Branch 1 (2 → 9) taken 5 times.
✗ Branch 2 (6 → 7) not taken.
✗ Branch 3 (6 → 19) not taken.
5 ASSERT(v <= 1 && "v must be smaller than 1");
78
1/4
✗ Branch 0 (9 → 10) not taken.
✓ Branch 1 (9 → 16) taken 5 times.
✗ Branch 2 (13 → 14) not taken.
✗ Branch 3 (13 → 22) not taken.
5 ASSERT(v >= -1 && "v must be greater than -1");
79 5 return An::from_radians(std::asin(v));
80 }
81
82
83 An
84 22 acos(float v)
85 {
86
1/4
✗ Branch 0 (2 → 3) not taken.
✓ Branch 1 (2 → 9) taken 22 times.
✗ Branch 2 (6 → 7) not taken.
✗ Branch 3 (6 → 19) not taken.
22 ASSERT(v <= 1 && "v must be smaller than 1");
87
1/4
✗ Branch 0 (9 → 10) not taken.
✓ Branch 1 (9 → 16) taken 22 times.
✗ Branch 2 (13 → 14) not taken.
✗ Branch 3 (13 → 22) not taken.
22 ASSERT(v >= -1 && "v must be greater than -1");
88 22 return An::from_radians(std::acos(v));
89 }
90
91
92 An
93 3 atan(float v)
94 {
95 3 return An::from_radians(std::atan(v));
96 }
97
98
99 An
100 2 atan2(float y, float x)
101 {
102 2 return An::from_radians(std::atan2(y, x));
103 }
104
105
106 An
107 1 An::get_wrapped() const
108 {
109 1 An temp = *this;
110
1/2
✓ Branch 0 (2 → 3) taken 1 times.
✗ Branch 1 (2 → 5) not taken.
1 temp.wrap();
111 1 return temp;
112 }
113
114
115 An
116 12 operator+(const An& lhs, const An& rhs)
117 {
118 12 An temp(lhs);
119 12 temp += rhs;
120 12 return temp;
121 }
122
123
124 An
125 22 operator-(const An& lhs, const An& rhs)
126 {
127 22 An temp(lhs);
128 22 temp -= rhs;
129 22 return temp;
130 }
131
132
133 An
134 100 operator/(const An& lhs, float rhs)
135 {
136 100 An temp(lhs);
137 100 temp /= rhs;
138 100 return temp;
139 }
140
141
142 34 An operator*(const An& lhs, float rhs)
143 {
144 34 An temp(lhs);
145 34 temp *= rhs;
146 34 return temp;
147 }
148
149 11 An operator*(float rhs, const An& lhs)
150 {
151 11 return lhs * rhs;
152 }
153
154
155 14 std::string string_from(const An& a)
156
2/4
✓ Branch 0 (2 → 3) taken 14 times.
✗ Branch 1 (2 → 8) not taken.
✓ Branch 2 (3 → 4) taken 14 times.
✗ Branch 3 (3 → 8) not taken.
28 { return fmt::format("{} deg", a.as_degrees()); }
157
158
159 bool
160 10 operator<(const An& lhs, const An& rhs)
161 {
162 10 return lhs.as_radians() < rhs.as_radians();
163 }
164
165
166 bool
167 2 operator<=(const An& lhs, const An& rhs)
168 {
169 2 return lhs.as_radians() <= rhs.as_radians();
170 }
171
172
173 bool
174 11 operator>(const An& lhs, const An& rhs)
175 {
176 11 return lhs.as_radians() > rhs.as_radians();
177 }
178
179
180 bool
181 2 operator>=(const An& lhs, const An& rhs)
182 {
183 2 return lhs.as_radians() >= rhs.as_radians();
184 }
185
186
187 10 An lerp_angle(const An& from, float v, const An& to)
188 {
189 // https://gamedev.stackexchange.com/a/72364
190 10 const auto dtheta = to - from;
191
192 10 const auto new_from = dtheta > half_turn
193
2/2
✓ Branch 0 (4 → 5) taken 1 times.
✓ Branch 1 (4 → 6) taken 9 times.
10 ? from + one_turn
194
1/2
✓ Branch 0 (6 → 7) taken 9 times.
✗ Branch 1 (6 → 16) not taken.
9 : dtheta < -half_turn
195
2/2
✓ Branch 0 (8 → 9) taken 1 times.
✓ Branch 1 (8 → 10) taken 8 times.
9 ? from - one_turn
196 8 : from
197 ;
198
199 10 return from + ( to - new_from ) * v;
200 }
201
202
2/4
✓ Branch 0 (2 → 3) taken 2 times.
✗ Branch 1 (2 → 10) not taken.
✓ Branch 2 (3 → 4) taken 2 times.
✗ Branch 3 (3 → 8) not taken.
2 ADD_CATCH_FORMATTER_IMPL(An)
203 }
204