| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "core/error/error_macros.h" |
| 34 |
#include "core/math/math_funcs.h" |
| 35 |
#include "core/templates/hashfuncs.h" |
| 36 |
|
| 37 |
class String; |
| 38 |
struct Vector3; |
| 39 |
|
| 40 |
struct [[nodiscard]] Vector3i { |
| 41 |
static const Vector3i LEFT; |
| 42 |
static const Vector3i RIGHT; |
| 43 |
static const Vector3i UP; |
| 44 |
static const Vector3i DOWN; |
| 45 |
static const Vector3i FORWARD; |
| 46 |
static const Vector3i BACK; |
| 47 |
|
| 48 |
static constexpr int AXIS_COUNT = 3; |
| 49 |
|
| 50 |
enum Axis { |
| 51 |
AXIS_X, |
| 52 |
AXIS_Y, |
| 53 |
AXIS_Z, |
| 54 |
}; |
| 55 |
|
| 56 |
union { |
| 57 |
|
| 58 |
struct { |
| 59 |
int32_t x; |
| 60 |
int32_t y; |
| 61 |
int32_t z; |
| 62 |
}; |
| 63 |
|
| 64 |
int32_t coord[3] = { 0 }; |
| 65 |
|
| 66 |
}; |
| 67 |
|
| 68 |
_FORCE_INLINE_ const int32_t &operator[](int p_axis) const { |
| 69 |
DEV_ASSERT((unsigned int)p_axis < 3); |
| 70 |
return coord[p_axis]; |
| 71 |
} |
| 72 |
|
| 73 |
_FORCE_INLINE_ int32_t &operator[](int p_axis) { |
| 74 |
DEV_ASSERT((unsigned int)p_axis < 3); |
| 75 |
return coord[p_axis]; |
| 76 |
} |
| 77 |
|
| 78 |
Vector3i::Axis min_axis_index() const; |
| 79 |
Vector3i::Axis max_axis_index() const; |
| 80 |
|
| 81 |
Vector3i min(const Vector3i &p_vector3i) const { |
| 82 |
return Vector3i(MIN(x, p_vector3i.x), MIN(y, p_vector3i.y), MIN(z, p_vector3i.z)); |
| 83 |
} |
| 84 |
|
| 85 |
Vector3i mini(int32_t p_scalar) const { |
| 86 |
return Vector3i(MIN(x, p_scalar), MIN(y, p_scalar), MIN(z, p_scalar)); |
| 87 |
} |
| 88 |
|
| 89 |
Vector3i max(const Vector3i &p_vector3i) const { |
| 90 |
return Vector3i(MAX(x, p_vector3i.x), MAX(y, p_vector3i.y), MAX(z, p_vector3i.z)); |
| 91 |
} |
| 92 |
|
| 93 |
Vector3i maxi(int32_t p_scalar) const { |
| 94 |
return Vector3i(MAX(x, p_scalar), MAX(y, p_scalar), MAX(z, p_scalar)); |
| 95 |
} |
| 96 |
|
| 97 |
_FORCE_INLINE_ int64_t length_squared() const; |
| 98 |
_FORCE_INLINE_ double length() const; |
| 99 |
|
| 100 |
_FORCE_INLINE_ void zero(); |
| 101 |
|
| 102 |
_FORCE_INLINE_ Vector3i abs() const; |
| 103 |
_FORCE_INLINE_ Vector3i sign() const; |
| 104 |
Vector3i clamp(const Vector3i &p_min, const Vector3i &p_max) const; |
| 105 |
Vector3i clampi(int32_t p_min, int32_t p_max) const; |
| 106 |
Vector3i snapped(const Vector3i &p_step) const; |
| 107 |
Vector3i snappedi(int32_t p_step) const; |
| 108 |
|
| 109 |
_FORCE_INLINE_ double distance_to(const Vector3i &p_to) const; |
| 110 |
_FORCE_INLINE_ int64_t distance_squared_to(const Vector3i &p_to) const; |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
constexpr Vector3i &operator+=(const Vector3i &p_v); |
| 115 |
constexpr Vector3i operator+(const Vector3i &p_v) const; |
| 116 |
constexpr Vector3i &operator-=(const Vector3i &p_v); |
| 117 |
constexpr Vector3i operator-(const Vector3i &p_v) const; |
| 118 |
constexpr Vector3i &operator*=(const Vector3i &p_v); |
| 119 |
constexpr Vector3i operator*(const Vector3i &p_v) const; |
| 120 |
constexpr Vector3i &operator/=(const Vector3i &p_v); |
| 121 |
constexpr Vector3i operator/(const Vector3i &p_v) const; |
| 122 |
constexpr Vector3i &operator%=(const Vector3i &p_v); |
| 123 |
constexpr Vector3i operator%(const Vector3i &p_v) const; |
| 124 |
|
| 125 |
constexpr Vector3i &operator*=(int32_t p_scalar); |
| 126 |
constexpr Vector3i operator*(int32_t p_scalar) const; |
| 127 |
constexpr Vector3i &operator/=(int32_t p_scalar); |
| 128 |
constexpr Vector3i operator/(int32_t p_scalar) const; |
| 129 |
constexpr Vector3i &operator%=(int32_t p_scalar); |
| 130 |
constexpr Vector3i operator%(int32_t p_scalar) const; |
| 131 |
|
| 132 |
constexpr Vector3i operator-() const; |
| 133 |
|
| 134 |
constexpr bool operator==(const Vector3i &p_v) const; |
| 135 |
constexpr bool operator!=(const Vector3i &p_v) const; |
| 136 |
constexpr bool operator<(const Vector3i &p_v) const; |
| 137 |
constexpr bool operator<=(const Vector3i &p_v) const; |
| 138 |
constexpr bool operator>(const Vector3i &p_v) const; |
| 139 |
constexpr bool operator>=(const Vector3i &p_v) const; |
| 140 |
|
| 141 |
explicit operator String() const; |
| 142 |
operator Vector3() const; |
| 143 |
|
| 144 |
uint32_t hash() const { |
| 145 |
uint32_t h = hash_murmur3_one_32(uint32_t(x)); |
| 146 |
h = hash_murmur3_one_32(uint32_t(y), h); |
| 147 |
h = hash_murmur3_one_32(uint32_t(z), h); |
| 148 |
return hash_fmix32(h); |
| 149 |
} |
| 150 |
|
| 151 |
constexpr Vector3i() : |
| 152 |
x(0), y(0), z(0) {} |
| 153 |
constexpr Vector3i(int32_t p_x, int32_t p_y, int32_t p_z) : |
| 154 |
x(p_x), y(p_y), z(p_z) {} |
| 155 |
}; |
| 156 |
|
| 157 |
inline constexpr Vector3i Vector3i::LEFT = { -1, 0, 0 }; |
| 158 |
inline constexpr Vector3i Vector3i::RIGHT = { 1, 0, 0 }; |
| 159 |
inline constexpr Vector3i Vector3i::UP = { 0, 1, 0 }; |
| 160 |
inline constexpr Vector3i Vector3i::DOWN = { 0, -1, 0 }; |
| 161 |
inline constexpr Vector3i Vector3i::FORWARD = { 0, 0, -1 }; |
| 162 |
inline constexpr Vector3i Vector3i::BACK = { 0, 0, 1 }; |
| 163 |
|
| 164 |
int64_t Vector3i::length_squared() const { |
| 165 |
return x * (int64_t)x + y * (int64_t)y + z * (int64_t)z; |
| 166 |
} |
| 167 |
|
| 168 |
double Vector3i::length() const { |
| 169 |
return Math::sqrt((double)length_squared()); |
| 170 |
} |
| 171 |
|
| 172 |
Vector3i Vector3i::abs() const { |
| 173 |
return Vector3i(Math::abs(x), Math::abs(y), Math::abs(z)); |
| 174 |
} |
| 175 |
|
| 176 |
Vector3i Vector3i::sign() const { |
| 177 |
return Vector3i(SIGN(x), SIGN(y), SIGN(z)); |
| 178 |
} |
| 179 |
|
| 180 |
double Vector3i::distance_to(const Vector3i &p_to) const { |
| 181 |
return (p_to - *this).length(); |
| 182 |
} |
| 183 |
|
| 184 |
int64_t Vector3i::distance_squared_to(const Vector3i &p_to) const { |
| 185 |
return (p_to - *this).length_squared(); |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
constexpr Vector3i &Vector3i::operator+=(const Vector3i &p_v) { |
| 191 |
x += p_v.x; |
| 192 |
y += p_v.y; |
| 193 |
z += p_v.z; |
| 194 |
return *this; |
| 195 |
} |
| 196 |
|
| 197 |
constexpr Vector3i Vector3i::operator+(const Vector3i &p_v) const { |
| 198 |
return Vector3i(x + p_v.x, y + p_v.y, z + p_v.z); |
| 199 |
} |
| 200 |
|
| 201 |
constexpr Vector3i &Vector3i::operator-=(const Vector3i &p_v) { |
| 202 |
x -= p_v.x; |
| 203 |
y -= p_v.y; |
| 204 |
z -= p_v.z; |
| 205 |
return *this; |
| 206 |
} |
| 207 |
|
| 208 |
constexpr Vector3i Vector3i::operator-(const Vector3i &p_v) const { |
| 209 |
return Vector3i(x - p_v.x, y - p_v.y, z - p_v.z); |
| 210 |
} |
| 211 |
|
| 212 |
constexpr Vector3i &Vector3i::operator*=(const Vector3i &p_v) { |
| 213 |
x *= p_v.x; |
| 214 |
y *= p_v.y; |
| 215 |
z *= p_v.z; |
| 216 |
return *this; |
| 217 |
} |
| 218 |
|
| 219 |
constexpr Vector3i Vector3i::operator*(const Vector3i &p_v) const { |
| 220 |
return Vector3i(x * p_v.x, y * p_v.y, z * p_v.z); |
| 221 |
} |
| 222 |
|
| 223 |
constexpr Vector3i &Vector3i::operator/=(const Vector3i &p_v) { |
| 224 |
x /= p_v.x; |
| 225 |
y /= p_v.y; |
| 226 |
z /= p_v.z; |
| 227 |
return *this; |
| 228 |
} |
| 229 |
|
| 230 |
constexpr Vector3i Vector3i::operator/(const Vector3i &p_v) const { |
| 231 |
return Vector3i(x / p_v.x, y / p_v.y, z / p_v.z); |
| 232 |
} |
| 233 |
|
| 234 |
constexpr Vector3i &Vector3i::operator%=(const Vector3i &p_v) { |
| 235 |
x %= p_v.x; |
| 236 |
y %= p_v.y; |
| 237 |
z %= p_v.z; |
| 238 |
return *this; |
| 239 |
} |
| 240 |
|
| 241 |
constexpr Vector3i Vector3i::operator%(const Vector3i &p_v) const { |
| 242 |
return Vector3i(x % p_v.x, y % p_v.y, z % p_v.z); |
| 243 |
} |
| 244 |
|
| 245 |
constexpr Vector3i &Vector3i::operator*=(int32_t p_scalar) { |
| 246 |
x *= p_scalar; |
| 247 |
y *= p_scalar; |
| 248 |
z *= p_scalar; |
| 249 |
return *this; |
| 250 |
} |
| 251 |
|
| 252 |
constexpr Vector3i Vector3i::operator*(int32_t p_scalar) const { |
| 253 |
return Vector3i(x * p_scalar, y * p_scalar, z * p_scalar); |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
constexpr Vector3i operator*(int32_t p_scalar, const Vector3i &p_vector) { |
| 259 |
return p_vector * p_scalar; |
| 260 |
} |
| 261 |
|
| 262 |
constexpr Vector3i operator*(int64_t p_scalar, const Vector3i &p_vector) { |
| 263 |
return p_vector * p_scalar; |
| 264 |
} |
| 265 |
|
| 266 |
constexpr Vector3i operator*(float p_scalar, const Vector3i &p_vector) { |
| 267 |
return p_vector * p_scalar; |
| 268 |
} |
| 269 |
|
| 270 |
constexpr Vector3i operator*(double p_scalar, const Vector3i &p_vector) { |
| 271 |
return p_vector * p_scalar; |
| 272 |
} |
| 273 |
|
| 274 |
constexpr Vector3i &Vector3i::operator/=(int32_t p_scalar) { |
| 275 |
x /= p_scalar; |
| 276 |
y /= p_scalar; |
| 277 |
z /= p_scalar; |
| 278 |
return *this; |
| 279 |
} |
| 280 |
|
| 281 |
constexpr Vector3i Vector3i::operator/(int32_t p_scalar) const { |
| 282 |
return Vector3i(x / p_scalar, y / p_scalar, z / p_scalar); |
| 283 |
} |
| 284 |
|
| 285 |
constexpr Vector3i &Vector3i::operator%=(int32_t p_scalar) { |
| 286 |
x %= p_scalar; |
| 287 |
y %= p_scalar; |
| 288 |
z %= p_scalar; |
| 289 |
return *this; |
| 290 |
} |
| 291 |
|
| 292 |
constexpr Vector3i Vector3i::operator%(int32_t p_scalar) const { |
| 293 |
return Vector3i(x % p_scalar, y % p_scalar, z % p_scalar); |
| 294 |
} |
| 295 |
|
| 296 |
constexpr Vector3i Vector3i::operator-() const { |
| 297 |
return Vector3i(-x, -y, -z); |
| 298 |
} |
| 299 |
|
| 300 |
constexpr bool Vector3i::operator==(const Vector3i &p_v) const { |
| 301 |
return (x == p_v.x && y == p_v.y && z == p_v.z); |
| 302 |
} |
| 303 |
|
| 304 |
constexpr bool Vector3i::operator!=(const Vector3i &p_v) const { |
| 305 |
return (x != p_v.x || y != p_v.y || z != p_v.z); |
| 306 |
} |
| 307 |
|
| 308 |
constexpr bool Vector3i::operator<(const Vector3i &p_v) const { |
| 309 |
if (x == p_v.x) { |
| 310 |
if (y == p_v.y) { |
| 311 |
return z < p_v.z; |
| 312 |
} else { |
| 313 |
return y < p_v.y; |
| 314 |
} |
| 315 |
} else { |
| 316 |
return x < p_v.x; |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
constexpr bool Vector3i::operator>(const Vector3i &p_v) const { |
| 321 |
if (x == p_v.x) { |
| 322 |
if (y == p_v.y) { |
| 323 |
return z > p_v.z; |
| 324 |
} else { |
| 325 |
return y > p_v.y; |
| 326 |
} |
| 327 |
} else { |
| 328 |
return x > p_v.x; |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
constexpr bool Vector3i::operator<=(const Vector3i &p_v) const { |
| 333 |
if (x == p_v.x) { |
| 334 |
if (y == p_v.y) { |
| 335 |
return z <= p_v.z; |
| 336 |
} else { |
| 337 |
return y < p_v.y; |
| 338 |
} |
| 339 |
} else { |
| 340 |
return x < p_v.x; |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
constexpr bool Vector3i::operator>=(const Vector3i &p_v) const { |
| 345 |
if (x == p_v.x) { |
| 346 |
if (y == p_v.y) { |
| 347 |
return z >= p_v.z; |
| 348 |
} else { |
| 349 |
return y > p_v.y; |
| 350 |
} |
| 351 |
} else { |
| 352 |
return x > p_v.x; |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
void Vector3i::zero() { |
| 357 |
x = y = z = 0; |
| 358 |
} |
| 359 |
|
| 360 |
template <> |
| 361 |
struct is_zero_constructible<Vector3i> : std::true_type {}; |