libs/base/src/base/vec3.cc
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "base/vec3.h" | ||
| 2 | |||
| 3 | #include <cmath> | ||
| 4 | |||
| 5 | #include "base/quat.h" | ||
| 6 | |||
| 7 | namespace eu | ||
| 8 | { | ||
| 9 | |||
| 10 | //////////////////////////////////////////////////////////////////////////////// | ||
| 11 | |||
| 12 | float * | ||
| 13 | 1 | v3::get_data_ptr() | |
| 14 | { | ||
| 15 | 1 | return &x; | |
| 16 | } | ||
| 17 | |||
| 18 | [[nodiscard]] const float * | ||
| 19 | 1 | v3::get_data_ptr() const | |
| 20 | { | ||
| 21 | 1 | return &x; | |
| 22 | } | ||
| 23 | |||
| 24 | 1 | v3::v3(float a) | |
| 25 | 1 | : x(a), y(a), z(a) | |
| 26 | { | ||
| 27 | 1 | } | |
| 28 | |||
| 29 | 1 | v3::v3(const std::tuple<float, float, float> &a) | |
| 30 | 1 | : x(std::get<0>(a)), y(std::get<1>(a)), z(std::get<2>(a)) | |
| 31 | { | ||
| 32 | 1 | } | |
| 33 | |||
| 34 | 1 | v3::v3(const float *a) | |
| 35 | 1 | : x(a[0]), y(a[1]), z(a[2]) | |
| 36 | { | ||
| 37 | 1 | } | |
| 38 | |||
| 39 | void | ||
| 40 | 3 | v3::operator+=(const v3 &rhs) | |
| 41 | { | ||
| 42 | 3 | x += rhs.x; | |
| 43 | 3 | y += rhs.y; | |
| 44 | 3 | z += rhs.z; | |
| 45 | 3 | } | |
| 46 | |||
| 47 | void | ||
| 48 | 1 | v3::operator-=(const v3 &rhs) | |
| 49 | { | ||
| 50 | 1 | x -= rhs.x; | |
| 51 | 1 | y -= rhs.y; | |
| 52 | 1 | z -= rhs.z; | |
| 53 | 1 | } | |
| 54 | |||
| 55 | v3 | ||
| 56 | 47 | v3::operator-() const | |
| 57 | { | ||
| 58 | 47 | return {-this->x, -this->y, -this->z}; | |
| 59 | } | ||
| 60 | |||
| 61 | v3 | ||
| 62 | 6 | v3::from_to(const v3 &from, const v3 &to) | |
| 63 | { | ||
| 64 | 6 | return {to.x - from.x, to.y - from.y, to.z - from.z}; | |
| 65 | } | ||
| 66 | |||
| 67 | v3 | ||
| 68 | 1 | v3::from_localspace_rui(const Q& r, float right, float up, float in) | |
| 69 | { | ||
| 70 | 1 | const auto vright = r.get_local_right() * right; | |
| 71 | 1 | const auto vup = r.get_local_up() * up; | |
| 72 | 1 | const auto vin = r.get_local_in() * in; | |
| 73 | 1 | return vright + vup + vin; | |
| 74 | } | ||
| 75 | |||
| 76 | void | ||
| 77 | 58 | v3::operator/=(float rhs) | |
| 78 | { | ||
| 79 | 58 | x /= rhs; | |
| 80 | 58 | y /= rhs; | |
| 81 | 58 | z /= rhs; | |
| 82 | 58 | } | |
| 83 | |||
| 84 | void | ||
| 85 | 56 | v3::operator*=(float rhs) | |
| 86 | { | ||
| 87 | 56 | x *= rhs; | |
| 88 | 56 | y *= rhs; | |
| 89 | 56 | z *= rhs; | |
| 90 | 56 | } | |
| 91 | |||
| 92 | [[nodiscard]] float | ||
| 93 | 1 | v3::get_length() const | |
| 94 | { | ||
| 95 | 1 | return std::sqrt(get_length_squared()); | |
| 96 | } | ||
| 97 | |||
| 98 | bool | ||
| 99 | 53 | v3::normalize() | |
| 100 | { | ||
| 101 | 53 | const float l2 = get_length_squared(); | |
| 102 | 53 | if (is_equal(l2, 0.0f)) | |
| 103 | { | ||
| 104 | 2 | *this = kk::up; | |
| 105 | 2 | return false; | |
| 106 | } | ||
| 107 | else | ||
| 108 | { | ||
| 109 | 51 | *this /= std::sqrt(l2); | |
| 110 | 51 | return true; | |
| 111 | } | ||
| 112 | } | ||
| 113 | |||
| 114 | [[nodiscard]] std::optional<n3> | ||
| 115 | 53 | v3::get_normalized() const | |
| 116 | { | ||
| 117 | 53 | v3 r = *this; | |
| 118 | 53 | if (false == r.normalize()) | |
| 119 | { | ||
| 120 | 2 | return std::nullopt; | |
| 121 | } | ||
| 122 | 51 | return n3{r}; | |
| 123 | } | ||
| 124 | |||
| 125 | //////////////////////////////////////////////////////////////////////////////// | ||
| 126 | // Math operators | ||
| 127 | |||
| 128 | v3 | ||
| 129 | 3 | operator+(const v3 &lhs, const v3 &rhs) | |
| 130 | { | ||
| 131 | 3 | v3 r = lhs; | |
| 132 | 3 | r += rhs; | |
| 133 | 3 | return r; | |
| 134 | } | ||
| 135 | |||
| 136 | v3 | ||
| 137 | 1 | operator-(const v3 &lhs, const v3 &rhs) | |
| 138 | { | ||
| 139 | 1 | v3 r = lhs; | |
| 140 | 1 | r -= rhs; | |
| 141 | 1 | return r; | |
| 142 | } | ||
| 143 | |||
| 144 | 2 | v3 operator*(float lhs, const v3 &rhs) | |
| 145 | { | ||
| 146 | 2 | v3 r = rhs; | |
| 147 | 2 | r *= lhs; | |
| 148 | 2 | return r; | |
| 149 | } | ||
| 150 | |||
| 151 | 54 | v3 operator*(const v3 &lhs, float rhs) | |
| 152 | { | ||
| 153 | 54 | v3 r = lhs; | |
| 154 | 54 | r *= rhs; | |
| 155 | 54 | return r; | |
| 156 | } | ||
| 157 | |||
| 158 | v3 | ||
| 159 | 7 | operator/(const v3 &lhs, float rhs) | |
| 160 | { | ||
| 161 | 7 | v3 r = lhs; | |
| 162 | 7 | r /= rhs; | |
| 163 | 7 | return r; | |
| 164 | } | ||
| 165 | |||
| 166 | v3 | ||
| 167 | 1 | operator/(float lhs, const v3 &rhs) | |
| 168 | { | ||
| 169 | 1 | const v3 r{lhs / rhs.x, lhs / rhs.y, lhs / rhs.z}; | |
| 170 | 1 | return r; | |
| 171 | } | ||
| 172 | |||
| 173 | //////////////////////////////////////////////////////////////////////////////// | ||
| 174 | /// Functions | ||
| 175 | |||
| 176 | 1 | v3 lerp_v3(const v3 &f, float v, const v3 &t) | |
| 177 | { | ||
| 178 | 1 | return v3{ | |
| 179 | 1 | lerp_float(f.x, v, t.x), | |
| 180 | 1 | lerp_float(f.y, v, t.y), | |
| 181 | 1 | lerp_float(f.z, v, t.z)}; | |
| 182 | } | ||
| 183 | |||
| 184 | float | ||
| 185 | 6 | v3::dot(const v3 &rhs) const | |
| 186 | { | ||
| 187 | 6 | return x * rhs.x + y * rhs.y + z * rhs.z; | |
| 188 | } | ||
| 189 | |||
| 190 | v3 | ||
| 191 | 5 | v3::cross(const v3 &u) const | |
| 192 | { | ||
| 193 | return { | ||
| 194 | 5 | (y * u.z) - (z * u.y), | |
| 195 | 5 | (z * u.x) - (x * u.z), | |
| 196 | 5 | (x * u.y) - (y * u.x)}; | |
| 197 | } | ||
| 198 | |||
| 199 | //////////////////////////////////////////////////////////////////////////////// | ||
| 200 | /// Transformations | ||
| 201 | |||
| 202 | // todo(Gustav): implement transformations | ||
| 203 | |||
| 204 | //////////////////////////////////////////////////////////////////////////////// | ||
| 205 | /// Printing | ||
| 206 | |||
| 207 | 4 | std::string string_from(const v3 &v) { return fmt::format("({}, {}, {})", v.x, v.y, v.z); } | |
| 208 | 12 | std::string string_from(const n3 &v) { return fmt::format("({}, {}, {})", v.x, v.y, v.z); } | |
| 209 | |||
| 210 | 1 | ADD_CATCH_FORMATTER_IMPL(v3) | |
| 211 | 1 | ADD_CATCH_FORMATTER_IMPL(n3) | |
| 212 | } | ||
| 213 |