libs/base/src/base/vec2.cc
| 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 | void | ||
| 104 | ✗ | v2::operator*=(const v2& rhs) | |
| 105 | { | ||
| 106 | ✗ | x = x * rhs.x; | |
| 107 | ✗ | y = y * rhs.y; | |
| 108 | ✗ | } | |
| 109 | |||
| 110 | float | ||
| 111 | 2 | v2::get_length() const | |
| 112 | { | ||
| 113 | 2 | return std::sqrt(get_length_squared()); | |
| 114 | } | ||
| 115 | |||
| 116 | bool | ||
| 117 | 1 | v2::normalize() | |
| 118 | { | ||
| 119 | 1 | const auto l2 = get_length(); | |
| 120 | 1 | if (is_equal(l2, 0.0f)) | |
| 121 | { | ||
| 122 | ✗ | *this = v2{ 1.0f, 0.0f }; | |
| 123 | ✗ | return false; | |
| 124 | } | ||
| 125 | |||
| 126 | 1 | *this /= l2; | |
| 127 | 1 | return true; | |
| 128 | } | ||
| 129 | |||
| 130 | std::optional<n2> | ||
| 131 | 1 | v2::get_normalized() const | |
| 132 | { | ||
| 133 | 1 | v2 r = *this; | |
| 134 | 1 | if(r.normalize() == false) | |
| 135 | { | ||
| 136 | ✗ | return std::nullopt; | |
| 137 | } | ||
| 138 | 1 | return n2{r}; | |
| 139 | } | ||
| 140 | |||
| 141 | const float* | ||
| 142 | 1 | n2::get_data_ptr() const | |
| 143 | { | ||
| 144 | 1 | return &x; | |
| 145 | } | ||
| 146 | |||
| 147 | n2 | ||
| 148 | 1 | n2::get_rotated(const An& a) const | |
| 149 | { | ||
| 150 | 1 | const float nx = x * cos(a) - y * sin(a); | |
| 151 | 1 | const float ny = x * sin(a) + y * cos(a); | |
| 152 | 1 | return n2{nx, ny}; | |
| 153 | } | ||
| 154 | |||
| 155 | n2 | ||
| 156 | 1 | n2::get_flipped_y() const | |
| 157 | { | ||
| 158 | 1 | return n2{x, -y}; | |
| 159 | } | ||
| 160 | |||
| 161 | n2 | ||
| 162 | 1 | n2::operator-() const | |
| 163 | { | ||
| 164 | 1 | return n2{-x, -y}; | |
| 165 | } | ||
| 166 | |||
| 167 | float | ||
| 168 | 17 | length_squared_from(const n2& n) | |
| 169 | { | ||
| 170 | 17 | return n.x * n.x + n.y * n.y; | |
| 171 | } | ||
| 172 | |||
| 173 | [[nodiscard]] bool | ||
| 174 | 17 | n2::is_valid() const | |
| 175 | { | ||
| 176 | 17 | return is_equal(length_squared_from(*this), 1.0f); | |
| 177 | } | ||
| 178 | |||
| 179 | 16 | n2::n2(float ax, float ay) : x(ax), y(ay) | |
| 180 | { | ||
| 181 | 16 | ASSERT(is_valid()); | |
| 182 | 16 | } | |
| 183 | |||
| 184 | 1 | n2::n2(const v2& v) : x(v.x), y(v.y) | |
| 185 | { | ||
| 186 | 1 | ASSERT(is_valid()); | |
| 187 | 1 | } | |
| 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, const v2& rhs) | |
| 197 | { | ||
| 198 | 1 | v2 r = lhs; | |
| 199 | 1 | r -= rhs; | |
| 200 | 1 | return r; | |
| 201 | } | ||
| 202 | |||
| 203 | ✗ | v2 operator*(const v2& lhs, const v2& rhs) | |
| 204 | { | ||
| 205 | ✗ | v2 r = lhs; | |
| 206 | ✗ | r *= rhs; | |
| 207 | ✗ | return r; | |
| 208 | } | ||
| 209 | |||
| 210 | 1 | v2 operator*(const v2& lhs, float rhs) | |
| 211 | { | ||
| 212 | 1 | v2 r = lhs; | |
| 213 | 1 | r *= rhs; | |
| 214 | 1 | return r; | |
| 215 | } | ||
| 216 | |||
| 217 | 1 | v2 operator*(float lhs, const v2& rhs) | |
| 218 | { | ||
| 219 | 1 | v2 r = rhs; | |
| 220 | 1 | r *= lhs; | |
| 221 | 1 | return r; | |
| 222 | } | ||
| 223 | |||
| 224 | 1 | v2 operator*(const n2& lhs, float rhs) | |
| 225 | { | ||
| 226 | 1 | v2 r = v2{lhs}; | |
| 227 | 1 | r *= rhs; | |
| 228 | 1 | return r; | |
| 229 | } | ||
| 230 | |||
| 231 | 1 | v2 operator*(float lhs, const n2& rhs) | |
| 232 | { | ||
| 233 | 1 | v2 r{rhs}; | |
| 234 | 1 | r *= lhs; | |
| 235 | 1 | return r; | |
| 236 | } | ||
| 237 | |||
| 238 | v2 | ||
| 239 | 1 | operator/(const v2& lhs, float rhs) | |
| 240 | { | ||
| 241 | 1 | v2 r = lhs; | |
| 242 | 1 | r /= rhs; | |
| 243 | 1 | return r; | |
| 244 | } | ||
| 245 | |||
| 246 | |||
| 247 | float | ||
| 248 | 1 | dot(const v2& lhs, const v2& rhs) | |
| 249 | { | ||
| 250 | 1 | return lhs.x * rhs.x + lhs.y * rhs.y; | |
| 251 | } | ||
| 252 | |||
| 253 | |||
| 254 | 1 | v2 lerp_vec2f(const v2& from, float v, const v2& to) | |
| 255 | { | ||
| 256 | return | ||
| 257 | { | ||
| 258 | 1 | lerp_float(from.x, v, to.x), | |
| 259 | 1 | lerp_float(from.y, v, to.y) | |
| 260 | 1 | }; | |
| 261 | } | ||
| 262 | |||
| 263 | |||
| 264 | 2 | std::string string_from(const v2& v) | |
| 265 | 4 | { return fmt::format("({}, {})", v.x, v.y); } | |
| 266 | |||
| 267 | 2 | std::string string_from(const n2& v) | |
| 268 | 4 | { return fmt::format("({}, {})", v.x, v.y); } | |
| 269 | |||
| 270 | 1 | ADD_CATCH_FORMATTER_IMPL(v2) | |
| 271 | 1 | ADD_CATCH_FORMATTER_IMPL(n2) | |
| 272 | } | ||
| 273 |