|
| 1 | +#ifndef FAST_VOXEL_TRAVERSAL_ALGORITHM_VEC3_H |
| 2 | +#define FAST_VOXEL_TRAVERSAL_ALGORITHM_VEC3_H |
| 3 | + |
| 4 | +#include <cmath> |
| 5 | + |
| 6 | +// The type used for the 3-dimensional vectors. |
| 7 | +// In most cases, this will be either double or float. |
| 8 | +using value_type = double; |
| 9 | + |
| 10 | +// Represents a Euclidean vector in 3-dimensional space. |
| 11 | +// Assumes vectors take the form of: |
| 12 | +// [x] |
| 13 | +// [y] |
| 14 | +// [z] |
| 15 | +struct Vec3 { |
| 16 | +public: |
| 17 | + constexpr explicit Vec3(const value_type x, const value_type y, const value_type z) |
| 18 | + : x_{x}, y_{y}, z_{z} {} |
| 19 | + |
| 20 | + [[nodiscard]] inline constexpr value_type x() const { return this->x_; } |
| 21 | + [[nodiscard]] inline constexpr value_type y() const { return this->y_; } |
| 22 | + [[nodiscard]] inline constexpr value_type z() const { return this->z_; } |
| 23 | + |
| 24 | + [[nodiscard]] inline constexpr value_type& x() { return this->x_; } |
| 25 | + [[nodiscard]] inline constexpr value_type& y() { return this->y_; } |
| 26 | + [[nodiscard]] inline constexpr value_type& z() { return this->z_; } |
| 27 | + |
| 28 | + [[nodiscard]] inline value_type length() const { |
| 29 | + return std::hypot(this->x(), this->y(), this->z()); |
| 30 | + } |
| 31 | + |
| 32 | + [[nodiscard]] inline value_type squared_length() const { |
| 33 | + return x() * x() + y() * y() + z() * z(); |
| 34 | + } |
| 35 | + |
| 36 | +private: |
| 37 | + // Represents the x-dimension value of the vector. |
| 38 | + value_type x_; |
| 39 | + // Represents the y-dimension value of the vector. |
| 40 | + value_type y_; |
| 41 | + // Represents the z-dimension value of the vector. |
| 42 | + value_type z_; |
| 43 | +}; |
| 44 | + |
| 45 | +// A 3-dimensional free vector, which has no initial point. It has two main criteria: |
| 46 | +// (1) direction, and (2) magnitude. |
| 47 | +struct FreeVec3 : Vec3 { |
| 48 | + using Vec3::Vec3; |
| 49 | + |
| 50 | + [[nodiscard]] constexpr explicit FreeVec3(const Vec3& vec3) : Vec3::Vec3{vec3} {} |
| 51 | + |
| 52 | + [[nodiscard]] inline constexpr value_type dot(const Vec3& other) const { |
| 53 | + return this->x() * other.x() + this->y() * other.y() + this->z() * other.z(); |
| 54 | + } |
| 55 | + |
| 56 | + [[nodiscard]] inline constexpr FreeVec3 cross(const Vec3& other) const { |
| 57 | + return FreeVec3{this->y() * other.z() - this->z() * other.y(), |
| 58 | + this->z() * other.x() - this->x() * other.z(), |
| 59 | + this->x() * other.y() - this->y() * other.x()}; |
| 60 | + } |
| 61 | + |
| 62 | + [[nodiscard]] inline constexpr FreeVec3& operator+=(const FreeVec3& other) { |
| 63 | + this->x() += other.x(); |
| 64 | + this->y() += other.y(); |
| 65 | + this->z() += other.z(); |
| 66 | + return *this; |
| 67 | + } |
| 68 | + |
| 69 | + [[nodiscard]] inline constexpr FreeVec3& operator-=(const FreeVec3& other) { |
| 70 | + this->x() -= other.x(); |
| 71 | + this->y() -= other.y(); |
| 72 | + this->z() -= other.z(); |
| 73 | + return *this; |
| 74 | + } |
| 75 | + |
| 76 | + [[nodiscard]] inline constexpr FreeVec3& operator*=(const value_type scalar) { |
| 77 | + this->x() *= scalar; |
| 78 | + this->y() *= scalar; |
| 79 | + this->z() *= scalar; |
| 80 | + return *this; |
| 81 | + } |
| 82 | + |
| 83 | + [[nodiscard]] inline constexpr FreeVec3& operator/=(const value_type scalar) { |
| 84 | + this->x() /= scalar; |
| 85 | + this->y() /= scalar; |
| 86 | + this->z() /= scalar; |
| 87 | + return *this; |
| 88 | + } |
| 89 | +}; |
| 90 | + |
| 91 | +[[nodiscard]] inline constexpr FreeVec3 operator+(const FreeVec3& v) { return v; } |
| 92 | + |
| 93 | +[[nodiscard]] inline constexpr FreeVec3 operator-(const FreeVec3& v) { |
| 94 | + return FreeVec3{-v.x(), -v.y(), -v.z()}; |
| 95 | +} |
| 96 | + |
| 97 | +[[nodiscard]] inline constexpr FreeVec3 operator+(FreeVec3 v1, const FreeVec3& v2) { |
| 98 | + return v1 += v2; |
| 99 | +} |
| 100 | + |
| 101 | +[[nodiscard]] inline constexpr FreeVec3 operator-(FreeVec3 v1, const FreeVec3& v2) { |
| 102 | + return v1 -= v2; |
| 103 | +} |
| 104 | + |
| 105 | +[[nodiscard]] inline constexpr FreeVec3 operator*(FreeVec3 v, const value_type scalar) { |
| 106 | + return v *= scalar; |
| 107 | +} |
| 108 | + |
| 109 | +[[nodiscard]] inline constexpr FreeVec3 operator/(FreeVec3 v, const value_type scalar) { |
| 110 | + return v /= scalar; |
| 111 | +} |
| 112 | + |
| 113 | +// A 3-dimensional bounded vector has a fixed start and end point. It represents a fixed point |
| 114 | +// in space, relative to some frame of reference. |
| 115 | +struct BoundVec3 : Vec3 { |
| 116 | + [[nodiscard]]constexpr explicit BoundVec3(const Vec3& vec3) : Vec3::Vec3{vec3} {} |
| 117 | + |
| 118 | + [[nodiscard]] inline constexpr value_type dot(const Vec3& other) const { |
| 119 | + return this->x() * other.x() + this->y() * other.y() + this->z() * other.z(); |
| 120 | + } |
| 121 | + |
| 122 | + [[nodiscard]] inline constexpr BoundVec3& operator+=(const FreeVec3& other) { |
| 123 | + this->x() += other.x(); |
| 124 | + this->y() += other.y(); |
| 125 | + this->z() += other.z(); |
| 126 | + return *this; |
| 127 | + } |
| 128 | + |
| 129 | + [[nodiscard]] inline constexpr BoundVec3& operator-=(const FreeVec3& other) { |
| 130 | + return *this += (-other); |
| 131 | + } |
| 132 | +}; |
| 133 | + |
| 134 | +[[nodiscard]] inline constexpr FreeVec3 operator-(const BoundVec3& v1, const BoundVec3& v2) { |
| 135 | + return FreeVec3{v1.x() - v2.x(), v1.y() - v2.y(), v1.z() - v2.z()}; |
| 136 | +} |
| 137 | + |
| 138 | +[[nodiscard]] inline constexpr BoundVec3 operator+(BoundVec3 v1, const FreeVec3& v2) { |
| 139 | + return v1 += v2; |
| 140 | +} |
| 141 | + |
| 142 | +[[nodiscard]] inline constexpr BoundVec3 operator-(BoundVec3 v1, const FreeVec3& v2) { |
| 143 | + return v1 -= v2; |
| 144 | +} |
| 145 | + |
| 146 | +// Represents a 3-dimensional unit vector, an abstraction over free vectors that guarantees |
| 147 | +// a length of 1. To prevent its length from changing, UnitVec3 does not allow |
| 148 | +// for mutations. |
| 149 | +struct UnitVec3 { |
| 150 | + UnitVec3(value_type x, value_type y, value_type z) |
| 151 | + : UnitVec3{FreeVec3{x, y, z}} {} |
| 152 | + [[nodiscard]] constexpr explicit UnitVec3(const Vec3& vec3) : UnitVec3{FreeVec3{vec3}} {} |
| 153 | + [[nodiscard]] constexpr explicit UnitVec3(const FreeVec3& free_vec3) : |
| 154 | + inner_{free_vec3 / free_vec3.length()} {} |
| 155 | + |
| 156 | + [[nodiscard]] inline constexpr value_type x() const { return this->to_free().x(); } |
| 157 | + [[nodiscard]] inline constexpr value_type y() const { return this->to_free().y(); } |
| 158 | + [[nodiscard]] inline constexpr value_type z() const { return this->to_free().z(); } |
| 159 | + [[nodiscard]] inline constexpr const FreeVec3& to_free() const { return inner_; } |
| 160 | +private: |
| 161 | + const FreeVec3 inner_; |
| 162 | +}; |
| 163 | + |
| 164 | +[[nodiscard]] inline constexpr FreeVec3 operator*(const UnitVec3& v, const value_type scalar) { |
| 165 | + return v.to_free() * scalar; |
| 166 | +} |
| 167 | + |
| 168 | +[[nodiscard]] inline constexpr FreeVec3 operator/(const UnitVec3& v, const value_type scalar) { |
| 169 | + return v.to_free() / scalar; |
| 170 | +} |
| 171 | + |
| 172 | +#endif //FAST_VOXEL_TRAVERSAL_ALGORITHM_VEC3_H |
0 commit comments