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