GCC Code Coverage Report


libs/base/src/base/
File: vec2.cc
Date: 2025-03-19 20:55:25
Lines:
113/116
97.4%
Functions:
38/38
100.0%
Branches:
12/28
42.9%

Line Branch Exec Source
1 #include "base/vec2.h"
2
3 #include <cmath>
4
5 #include "base/angle.h"
6 #include "assert/assert.h"
7
8
9 namespace eu
10 {
11 1 v2::v2(const std::tuple<float, float>& a)
12 1 : x(std::get<0>(a))
13 1 , y(std::get<1>(a))
14 {
15 1 }
16
17 2 v2::v2(const n2& u)
18 2 : x(u.x)
19 2 , y(u.y)
20 {
21 2 }
22
23
24 float*
25 1 v2::get_data_ptr()
26 {
27 1 return &x;
28 }
29
30 const float*
31 1 v2::get_data_ptr() const
32 {
33 1 return &x;
34 }
35
36 v2
37 1 v2::get_rotated(const An& a) const
38 {
39 1 const float nx = x * cos(a) - y * sin(a);
40 1 const float ny = x * sin(a) + y * cos(a);
41 1 return {nx, ny};
42 }
43
44 v2
45 1 v2::get_flipped_y() const
46 {
47 1 return {x, -y};
48 }
49
50 void
51 1 v2::operator+=(const v2& rhs)
52 {
53 1 x = x + rhs.x;
54 1 y = y + rhs.y;
55 1 }
56
57 void
58 1 v2::operator-=(const v2& rhs)
59 {
60 1 x = x - rhs.x;
61 1 y = y - rhs.y;
62 1 }
63
64 v2
65 1 v2::operator-() const
66 {
67 1 return {-x, -y};
68 }
69
70 float
71 3 v2::get_length_squared() const
72 {
73 3 return x * x + y * y;
74 }
75
76
77 [[nodiscard]] v2
78 1 v2::from_to(const v2& from, const v2& to)
79 {
80 1 return {to.x - from.x, to.y - from.y};
81 }
82
83 v2
84 1 v2::from(const n2& n)
85 {
86 1 return {n.x, n.y};
87 }
88
89 void
90 3 v2::operator/=(float rhs)
91 {
92 3 x = x / rhs;
93 3 y = y / rhs;
94 3 }
95
96 void
97 4 v2::operator*=(float rhs)
98 {
99 4 x = x * rhs;
100 4 y = y * rhs;
101 4 }
102
103 float
104 2 v2::get_length() const
105 {
106 2 return std::sqrt(get_length_squared());
107 }
108
109 bool
110 1 v2::normalize()
111 {
112 1 const auto l2 = get_length();
113
1/2
✗ Branch 0 (4 → 5) not taken.
✓ Branch 1 (4 → 7) taken 1 times.
1 if (is_equal(l2, 0.0f))
114 {
115 *this = v2{ 1.0f, 0.0f };
116 return false;
117 }
118
119 1 *this /= l2;
120 1 return true;
121 }
122
123 std::optional<n2>
124 1 v2::get_normalized() const
125 {
126 1 v2 r = *this;
127
1/2
✗ Branch 0 (3 → 4) not taken.
✓ Branch 1 (3 → 7) taken 1 times.
1 if(r.normalize() == false)
128 {
129 return std::nullopt;
130 }
131
1/2
✓ Branch 0 (7 → 8) taken 1 times.
✗ Branch 1 (7 → 13) not taken.
1 return n2{r};
132 }
133
134 const float*
135 1 n2::get_data_ptr() const
136 {
137 1 return &x;
138 }
139
140 n2
141 1 n2::get_rotated(const An& a) const
142 {
143 1 const float nx = x * cos(a) - y * sin(a);
144 1 const float ny = x * sin(a) + y * cos(a);
145
1/2
✓ Branch 0 (6 → 7) taken 1 times.
✗ Branch 1 (6 → 10) not taken.
1 return n2{nx, ny};
146 }
147
148 n2
149 1 n2::get_flipped_y() const
150 {
151
1/2
✓ Branch 0 (2 → 3) taken 1 times.
✗ Branch 1 (2 → 6) not taken.
1 return n2{x, -y};
152 }
153
154 n2
155 1 n2::operator-() const
156 {
157
1/2
✓ Branch 0 (2 → 3) taken 1 times.
✗ Branch 1 (2 → 6) not taken.
1 return n2{-x, -y};
158 }
159
160 float
161 17 length_squared_from(const n2& n)
162 {
163 17 return n.x * n.x + n.y * n.y;
164 }
165
166 [[nodiscard]] bool
167 17 n2::is_valid() const
168 {
169 17 return is_equal(length_squared_from(*this), 1.0f);
170 }
171
172 16 n2::n2(float ax, float ay) : x(ax), y(ay)
173 {
174
1/4
✗ Branch 0 (3 → 4) not taken.
✓ Branch 1 (3 → 10) taken 16 times.
✗ Branch 2 (7 → 8) not taken.
✗ Branch 3 (7 → 11) not taken.
16 ASSERT(is_valid());
175 16 }
176
177 1 n2::n2(const v2& v) : x(v.x), y(v.y)
178 {
179
1/4
✗ Branch 0 (3 → 4) not taken.
✓ Branch 1 (3 → 10) taken 1 times.
✗ Branch 2 (7 → 8) not taken.
✗ Branch 3 (7 → 11) not taken.
1 ASSERT(is_valid());
180 1 }
181
182 1 v2 operator+(const v2& lhs, const v2& rhs)
183 {
184 1 v2 r = lhs;
185 1 r += rhs;
186 1 return r;
187 }
188
189 1 v2 operator-(const v2& lhs, const v2& rhs)
190 {
191 1 v2 r = lhs;
192 1 r -= rhs;
193 1 return r;
194 }
195
196 1 v2 operator*(const v2& lhs, float rhs)
197 {
198 1 v2 r = lhs;
199 1 r *= rhs;
200 1 return r;
201 }
202
203 1 v2 operator*(float lhs, const v2& rhs)
204 {
205 1 v2 r = rhs;
206 1 r *= lhs;
207 1 return r;
208 }
209
210 1 v2 operator*(const n2& lhs, float rhs)
211 {
212 1 v2 r = v2{lhs};
213 1 r *= rhs;
214 1 return r;
215 }
216
217 1 v2 operator*(float lhs, const n2& rhs)
218 {
219 1 v2 r{rhs};
220 1 r *= lhs;
221 1 return r;
222 }
223
224 v2
225 1 operator/(const v2& lhs, float rhs)
226 {
227 1 v2 r = lhs;
228 1 r /= rhs;
229 1 return r;
230 }
231
232
233 float
234 1 dot(const v2& lhs, const v2& rhs)
235 {
236 1 return lhs.x * rhs.x + lhs.y * rhs.y;
237 }
238
239
240 1 v2 lerp_vec2f(const v2& from, float v, const v2& to)
241 {
242 return
243 {
244 1 lerp_float(from.x, v, to.x),
245 1 lerp_float(from.y, v, to.y)
246 1 };
247 }
248
249
250 2 std::string string_from(const v2& v)
251 4 { return fmt::format("({}, {})", v.x, v.y); }
252
253 2 std::string string_from(const n2& v)
254 4 { return fmt::format("({}, {})", v.x, v.y); }
255
256
2/4
✓ Branch 0 (2 → 3) taken 1 times.
✗ Branch 1 (2 → 10) not taken.
✓ Branch 2 (3 → 4) taken 1 times.
✗ Branch 3 (3 → 8) not taken.
1 ADD_CATCH_FORMATTER_IMPL(v2)
257
2/4
✓ Branch 0 (2 → 3) taken 1 times.
✗ Branch 1 (2 → 10) not taken.
✓ Branch 2 (3 → 4) taken 1 times.
✗ Branch 3 (3 → 8) not taken.
1 ADD_CATCH_FORMATTER_IMPL(n2)
258 }
259