godot_voxel/vector3i.h

171 lines
3.5 KiB
C
Raw Normal View History

#ifndef VOXEL_VECTOR3I_H
#define VOXEL_VECTOR3I_H
2017-03-26 18:08:43 +02:00
#include <core/hashfuncs.h>
2017-08-13 01:19:39 +02:00
#include <core/math/vector3.h>
struct Vector3i {
2017-01-01 04:40:16 +01:00
union {
struct {
int x;
int y;
int z;
};
int coords[3];
};
2019-04-24 02:29:47 +02:00
_FORCE_INLINE_ Vector3i() :
x(0),
y(0),
z(0) {}
_FORCE_INLINE_ Vector3i(int xyz) :
x(xyz),
y(xyz),
z(xyz) {}
_FORCE_INLINE_ Vector3i(int px, int py, int pz) :
x(px),
y(py),
z(pz) {}
2017-01-01 04:40:16 +01:00
2017-08-13 01:19:39 +02:00
_FORCE_INLINE_ Vector3i(const Vector3i &other) {
2017-01-01 04:40:16 +01:00
*this = other;
}
2017-08-13 01:19:39 +02:00
_FORCE_INLINE_ Vector3i(const Vector3 &f) {
2017-01-01 04:40:16 +01:00
x = Math::floor(f.x);
y = Math::floor(f.y);
z = Math::floor(f.z);
}
_FORCE_INLINE_ Vector3 to_vec3() const {
return Vector3(x, y, z);
}
_FORCE_INLINE_ int volume() const {
2017-08-13 01:19:39 +02:00
return x * y * z;
2017-01-01 04:40:16 +01:00
}
_FORCE_INLINE_ int length_sq() const {
2017-08-13 01:19:39 +02:00
return x * x + y * y + z * z;
2017-01-01 04:40:16 +01:00
}
2017-03-25 01:23:36 +01:00
_FORCE_INLINE_ real_t length() const {
return Math::sqrt((real_t)length_sq());
2017-01-01 04:40:16 +01:00
}
2017-08-13 01:19:39 +02:00
_FORCE_INLINE_ int distance_sq(const Vector3i &other) const;
2017-08-13 01:19:39 +02:00
_FORCE_INLINE_ Vector3i &operator=(const Vector3i &other) {
2017-01-01 04:40:16 +01:00
x = other.x;
y = other.y;
z = other.z;
return *this;
}
2017-08-13 01:19:39 +02:00
_FORCE_INLINE_ void operator+=(const Vector3i &other) {
2017-01-01 04:40:16 +01:00
x += other.x;
y += other.y;
z += other.z;
}
2017-08-13 01:19:39 +02:00
_FORCE_INLINE_ void operator-=(const Vector3i &other) {
2017-01-01 04:40:16 +01:00
x -= other.x;
y -= other.y;
z -= other.z;
}
_FORCE_INLINE_ Vector3i operator-() const {
return Vector3i(-x, -y, -z);
}
2017-08-13 01:19:39 +02:00
_FORCE_INLINE_ int &operator[](unsigned int i) {
2017-01-01 04:40:16 +01:00
return coords[i];
}
void clamp_to(const Vector3i min, const Vector3i max) {
if (x < min.x) x = min.x;
if (y < min.y) y = min.y;
if (z < min.z) z = min.z;
// TODO Not sure it should clamp like that...
2017-01-01 04:40:16 +01:00
if (x >= max.x) x = max.x - 1;
if (y >= max.y) y = max.y - 1;
if (z >= max.z) z = max.z - 1;
}
2017-08-13 01:19:39 +02:00
_FORCE_INLINE_ bool is_contained_in(const Vector3i &min, const Vector3i &max) {
return x >= min.x && y >= min.y && z >= min.z && x < max.x && y < max.y && z < max.z;
2017-01-01 04:40:16 +01:00
}
2017-08-13 01:19:39 +02:00
_FORCE_INLINE_ Vector3i wrap(const Vector3i &size) {
2017-01-01 04:40:16 +01:00
return Vector3i(
2017-08-13 01:19:39 +02:00
x % size.x,
y % size.y,
z % size.z);
2017-01-01 04:40:16 +01:00
}
2017-08-13 01:19:39 +02:00
static void sort_min_max(Vector3i &a, Vector3i &b) {
2017-01-01 04:40:16 +01:00
sort_min_max(a.x, b.x);
sort_min_max(a.y, b.y);
sort_min_max(a.z, b.z);
}
private:
2017-08-13 01:19:39 +02:00
static _FORCE_INLINE_ void sort_min_max(int &a, int &b) {
2017-01-01 04:40:16 +01:00
if (a > b) {
int temp = a;
a = b;
b = temp;
}
}
};
2017-08-13 01:19:39 +02:00
_FORCE_INLINE_ Vector3i operator+(const Vector3i a, const Vector3i &b) {
2017-01-01 04:40:16 +01:00
return Vector3i(a.x + b.x, a.y + b.y, a.z + b.z);
}
2017-08-13 01:19:39 +02:00
_FORCE_INLINE_ Vector3i operator-(const Vector3i &a, const Vector3i &b) {
2017-01-01 04:40:16 +01:00
return Vector3i(a.x - b.x, a.y - b.y, a.z - b.z);
}
2017-08-20 15:17:54 +02:00
_FORCE_INLINE_ Vector3i operator*(const Vector3i &a, const Vector3i &b) {
return Vector3i(a.x * b.x, a.y * b.y, a.z * b.z);
}
2017-08-13 01:19:39 +02:00
_FORCE_INLINE_ Vector3i operator*(const Vector3i &a, int n) {
2017-01-01 04:40:16 +01:00
return Vector3i(a.x * n, a.y * n, a.z * n);
}
2017-08-13 01:19:39 +02:00
_FORCE_INLINE_ Vector3i operator*(int n, const Vector3i &a) {
2017-01-01 04:40:16 +01:00
return Vector3i(a.x * n, a.y * n, a.z * n);
}
2017-08-13 01:19:39 +02:00
_FORCE_INLINE_ Vector3i operator/(const Vector3i &a, int n) {
2017-01-01 04:40:16 +01:00
return Vector3i(a.x / n, a.y / n, a.z / n);
}
2017-08-13 01:19:39 +02:00
_FORCE_INLINE_ bool operator==(const Vector3i &a, const Vector3i &b) {
2017-01-01 04:40:16 +01:00
return a.x == b.x && a.y == b.y && a.z == b.z;
}
2017-08-13 01:19:39 +02:00
_FORCE_INLINE_ bool operator!=(const Vector3i &a, const Vector3i &b) {
2017-08-28 01:44:12 +02:00
return a.x != b.x || a.y != b.y || a.z != b.z;
}
2017-08-13 01:19:39 +02:00
_FORCE_INLINE_ int Vector3i::distance_sq(const Vector3i &other) const {
return (other - *this).length_sq();
}
struct Vector3iHasher {
2017-08-13 01:19:39 +02:00
static _FORCE_INLINE_ uint32_t hash(const Vector3i &v) {
uint32_t hash = hash_djb2_one_32(v.x);
2017-01-01 03:58:35 +01:00
hash = hash_djb2_one_32(v.y, hash);
return hash_djb2_one_32(v.z, hash);
2017-01-01 04:40:16 +01:00
}
};
#endif // VOXEL_VECTOR3I_H