| 1 | /**************************************************************************/ |
| 2 | /* vector4.h */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #ifndef VECTOR4_H |
| 32 | #define VECTOR4_H |
| 33 | |
| 34 | #include "core/error/error_macros.h" |
| 35 | #include "core/math/math_funcs.h" |
| 36 | |
| 37 | class String; |
| 38 | |
| 39 | struct _NO_DISCARD_ Vector4 { |
| 40 | static const int AXIS_COUNT = 4; |
| 41 | |
| 42 | enum Axis { |
| 43 | AXIS_X, |
| 44 | AXIS_Y, |
| 45 | AXIS_Z, |
| 46 | AXIS_W, |
| 47 | }; |
| 48 | |
| 49 | union { |
| 50 | struct { |
| 51 | real_t x; |
| 52 | real_t y; |
| 53 | real_t z; |
| 54 | real_t w; |
| 55 | }; |
| 56 | real_t components[4] = { 0, 0, 0, 0 }; |
| 57 | }; |
| 58 | |
| 59 | _FORCE_INLINE_ real_t &operator[](const int p_axis) { |
| 60 | DEV_ASSERT((unsigned int)p_axis < 4); |
| 61 | return components[p_axis]; |
| 62 | } |
| 63 | _FORCE_INLINE_ const real_t &operator[](const int p_axis) const { |
| 64 | DEV_ASSERT((unsigned int)p_axis < 4); |
| 65 | return components[p_axis]; |
| 66 | } |
| 67 | |
| 68 | Vector4::Axis min_axis_index() const; |
| 69 | Vector4::Axis max_axis_index() const; |
| 70 | |
| 71 | Vector4 min(const Vector4 &p_vector4) const { |
| 72 | return Vector4(MIN(x, p_vector4.x), MIN(y, p_vector4.y), MIN(z, p_vector4.z), MIN(w, p_vector4.w)); |
| 73 | } |
| 74 | |
| 75 | Vector4 max(const Vector4 &p_vector4) const { |
| 76 | return Vector4(MAX(x, p_vector4.x), MAX(y, p_vector4.y), MAX(z, p_vector4.z), MAX(w, p_vector4.w)); |
| 77 | } |
| 78 | |
| 79 | _FORCE_INLINE_ real_t length_squared() const; |
| 80 | bool is_equal_approx(const Vector4 &p_vec4) const; |
| 81 | bool is_zero_approx() const; |
| 82 | bool is_finite() const; |
| 83 | real_t length() const; |
| 84 | void normalize(); |
| 85 | Vector4 normalized() const; |
| 86 | bool is_normalized() const; |
| 87 | |
| 88 | real_t distance_to(const Vector4 &p_to) const; |
| 89 | real_t distance_squared_to(const Vector4 &p_to) const; |
| 90 | Vector4 direction_to(const Vector4 &p_to) const; |
| 91 | |
| 92 | Vector4 abs() const; |
| 93 | Vector4 sign() const; |
| 94 | Vector4 floor() const; |
| 95 | Vector4 ceil() const; |
| 96 | Vector4 round() const; |
| 97 | Vector4 lerp(const Vector4 &p_to, const real_t p_weight) const; |
| 98 | Vector4 cubic_interpolate(const Vector4 &p_b, const Vector4 &p_pre_a, const Vector4 &p_post_b, const real_t p_weight) const; |
| 99 | Vector4 cubic_interpolate_in_time(const Vector4 &p_b, const Vector4 &p_pre_a, const Vector4 &p_post_b, const real_t p_weight, const real_t &p_b_t, const real_t &p_pre_a_t, const real_t &p_post_b_t) const; |
| 100 | |
| 101 | Vector4 posmod(const real_t p_mod) const; |
| 102 | Vector4 posmodv(const Vector4 &p_modv) const; |
| 103 | void snap(const Vector4 &p_step); |
| 104 | Vector4 snapped(const Vector4 &p_step) const; |
| 105 | Vector4 clamp(const Vector4 &p_min, const Vector4 &p_max) const; |
| 106 | |
| 107 | Vector4 inverse() const; |
| 108 | _FORCE_INLINE_ real_t dot(const Vector4 &p_vec4) const; |
| 109 | |
| 110 | _FORCE_INLINE_ void operator+=(const Vector4 &p_vec4); |
| 111 | _FORCE_INLINE_ void operator-=(const Vector4 &p_vec4); |
| 112 | _FORCE_INLINE_ void operator*=(const Vector4 &p_vec4); |
| 113 | _FORCE_INLINE_ void operator/=(const Vector4 &p_vec4); |
| 114 | _FORCE_INLINE_ void operator*=(const real_t &s); |
| 115 | _FORCE_INLINE_ void operator/=(const real_t &s); |
| 116 | _FORCE_INLINE_ Vector4 operator+(const Vector4 &p_vec4) const; |
| 117 | _FORCE_INLINE_ Vector4 operator-(const Vector4 &p_vec4) const; |
| 118 | _FORCE_INLINE_ Vector4 operator*(const Vector4 &p_vec4) const; |
| 119 | _FORCE_INLINE_ Vector4 operator/(const Vector4 &p_vec4) const; |
| 120 | _FORCE_INLINE_ Vector4 operator-() const; |
| 121 | _FORCE_INLINE_ Vector4 operator*(const real_t &s) const; |
| 122 | _FORCE_INLINE_ Vector4 operator/(const real_t &s) const; |
| 123 | |
| 124 | _FORCE_INLINE_ bool operator==(const Vector4 &p_vec4) const; |
| 125 | _FORCE_INLINE_ bool operator!=(const Vector4 &p_vec4) const; |
| 126 | _FORCE_INLINE_ bool operator>(const Vector4 &p_vec4) const; |
| 127 | _FORCE_INLINE_ bool operator<(const Vector4 &p_vec4) const; |
| 128 | _FORCE_INLINE_ bool operator>=(const Vector4 &p_vec4) const; |
| 129 | _FORCE_INLINE_ bool operator<=(const Vector4 &p_vec4) const; |
| 130 | |
| 131 | operator String() const; |
| 132 | |
| 133 | _FORCE_INLINE_ Vector4() {} |
| 134 | |
| 135 | _FORCE_INLINE_ Vector4(real_t p_x, real_t p_y, real_t p_z, real_t p_w) : |
| 136 | x(p_x), |
| 137 | y(p_y), |
| 138 | z(p_z), |
| 139 | w(p_w) { |
| 140 | } |
| 141 | |
| 142 | Vector4(const Vector4 &p_vec4) : |
| 143 | x(p_vec4.x), |
| 144 | y(p_vec4.y), |
| 145 | z(p_vec4.z), |
| 146 | w(p_vec4.w) { |
| 147 | } |
| 148 | |
| 149 | void operator=(const Vector4 &p_vec4) { |
| 150 | x = p_vec4.x; |
| 151 | y = p_vec4.y; |
| 152 | z = p_vec4.z; |
| 153 | w = p_vec4.w; |
| 154 | } |
| 155 | }; |
| 156 | |
| 157 | real_t Vector4::dot(const Vector4 &p_vec4) const { |
| 158 | return x * p_vec4.x + y * p_vec4.y + z * p_vec4.z + w * p_vec4.w; |
| 159 | } |
| 160 | |
| 161 | real_t Vector4::length_squared() const { |
| 162 | return dot(*this); |
| 163 | } |
| 164 | |
| 165 | void Vector4::operator+=(const Vector4 &p_vec4) { |
| 166 | x += p_vec4.x; |
| 167 | y += p_vec4.y; |
| 168 | z += p_vec4.z; |
| 169 | w += p_vec4.w; |
| 170 | } |
| 171 | |
| 172 | void Vector4::operator-=(const Vector4 &p_vec4) { |
| 173 | x -= p_vec4.x; |
| 174 | y -= p_vec4.y; |
| 175 | z -= p_vec4.z; |
| 176 | w -= p_vec4.w; |
| 177 | } |
| 178 | |
| 179 | void Vector4::operator*=(const Vector4 &p_vec4) { |
| 180 | x *= p_vec4.x; |
| 181 | y *= p_vec4.y; |
| 182 | z *= p_vec4.z; |
| 183 | w *= p_vec4.w; |
| 184 | } |
| 185 | |
| 186 | void Vector4::operator/=(const Vector4 &p_vec4) { |
| 187 | x /= p_vec4.x; |
| 188 | y /= p_vec4.y; |
| 189 | z /= p_vec4.z; |
| 190 | w /= p_vec4.w; |
| 191 | } |
| 192 | void Vector4::operator*=(const real_t &s) { |
| 193 | x *= s; |
| 194 | y *= s; |
| 195 | z *= s; |
| 196 | w *= s; |
| 197 | } |
| 198 | |
| 199 | void Vector4::operator/=(const real_t &s) { |
| 200 | *this *= 1.0f / s; |
| 201 | } |
| 202 | |
| 203 | Vector4 Vector4::operator+(const Vector4 &p_vec4) const { |
| 204 | return Vector4(x + p_vec4.x, y + p_vec4.y, z + p_vec4.z, w + p_vec4.w); |
| 205 | } |
| 206 | |
| 207 | Vector4 Vector4::operator-(const Vector4 &p_vec4) const { |
| 208 | return Vector4(x - p_vec4.x, y - p_vec4.y, z - p_vec4.z, w - p_vec4.w); |
| 209 | } |
| 210 | |
| 211 | Vector4 Vector4::operator*(const Vector4 &p_vec4) const { |
| 212 | return Vector4(x * p_vec4.x, y * p_vec4.y, z * p_vec4.z, w * p_vec4.w); |
| 213 | } |
| 214 | |
| 215 | Vector4 Vector4::operator/(const Vector4 &p_vec4) const { |
| 216 | return Vector4(x / p_vec4.x, y / p_vec4.y, z / p_vec4.z, w / p_vec4.w); |
| 217 | } |
| 218 | |
| 219 | Vector4 Vector4::operator-() const { |
| 220 | return Vector4(-x, -y, -z, -w); |
| 221 | } |
| 222 | |
| 223 | Vector4 Vector4::operator*(const real_t &s) const { |
| 224 | return Vector4(x * s, y * s, z * s, w * s); |
| 225 | } |
| 226 | |
| 227 | Vector4 Vector4::operator/(const real_t &s) const { |
| 228 | return *this * (1.0f / s); |
| 229 | } |
| 230 | |
| 231 | bool Vector4::operator==(const Vector4 &p_vec4) const { |
| 232 | return x == p_vec4.x && y == p_vec4.y && z == p_vec4.z && w == p_vec4.w; |
| 233 | } |
| 234 | |
| 235 | bool Vector4::operator!=(const Vector4 &p_vec4) const { |
| 236 | return x != p_vec4.x || y != p_vec4.y || z != p_vec4.z || w != p_vec4.w; |
| 237 | } |
| 238 | |
| 239 | bool Vector4::operator<(const Vector4 &p_v) const { |
| 240 | if (x == p_v.x) { |
| 241 | if (y == p_v.y) { |
| 242 | if (z == p_v.z) { |
| 243 | return w < p_v.w; |
| 244 | } |
| 245 | return z < p_v.z; |
| 246 | } |
| 247 | return y < p_v.y; |
| 248 | } |
| 249 | return x < p_v.x; |
| 250 | } |
| 251 | |
| 252 | bool Vector4::operator>(const Vector4 &p_v) const { |
| 253 | if (x == p_v.x) { |
| 254 | if (y == p_v.y) { |
| 255 | if (z == p_v.z) { |
| 256 | return w > p_v.w; |
| 257 | } |
| 258 | return z > p_v.z; |
| 259 | } |
| 260 | return y > p_v.y; |
| 261 | } |
| 262 | return x > p_v.x; |
| 263 | } |
| 264 | |
| 265 | bool Vector4::operator<=(const Vector4 &p_v) const { |
| 266 | if (x == p_v.x) { |
| 267 | if (y == p_v.y) { |
| 268 | if (z == p_v.z) { |
| 269 | return w <= p_v.w; |
| 270 | } |
| 271 | return z < p_v.z; |
| 272 | } |
| 273 | return y < p_v.y; |
| 274 | } |
| 275 | return x < p_v.x; |
| 276 | } |
| 277 | |
| 278 | bool Vector4::operator>=(const Vector4 &p_v) const { |
| 279 | if (x == p_v.x) { |
| 280 | if (y == p_v.y) { |
| 281 | if (z == p_v.z) { |
| 282 | return w >= p_v.w; |
| 283 | } |
| 284 | return z > p_v.z; |
| 285 | } |
| 286 | return y > p_v.y; |
| 287 | } |
| 288 | return x > p_v.x; |
| 289 | } |
| 290 | |
| 291 | _FORCE_INLINE_ Vector4 operator*(const float p_scalar, const Vector4 &p_vec) { |
| 292 | return p_vec * p_scalar; |
| 293 | } |
| 294 | |
| 295 | _FORCE_INLINE_ Vector4 operator*(const double p_scalar, const Vector4 &p_vec) { |
| 296 | return p_vec * p_scalar; |
| 297 | } |
| 298 | |
| 299 | _FORCE_INLINE_ Vector4 operator*(const int32_t p_scalar, const Vector4 &p_vec) { |
| 300 | return p_vec * p_scalar; |
| 301 | } |
| 302 | |
| 303 | _FORCE_INLINE_ Vector4 operator*(const int64_t p_scalar, const Vector4 &p_vec) { |
| 304 | return p_vec * p_scalar; |
| 305 | } |
| 306 | |
| 307 | #endif // VECTOR4_H |
| 308 | |