| 1 |
/* |
| 2 |
* Portions of this file are copyright Rebirth contributors and licensed as |
| 3 |
* described in COPYING.txt. |
| 4 |
* Portions of this file are copyright Parallax Software and licensed |
| 5 |
* according to the Parallax license below. |
| 6 |
* See COPYING.txt for license details. |
| 7 |
|
| 8 |
THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX |
| 9 |
SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO |
| 10 |
END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A |
| 11 |
ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS |
| 12 |
IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS |
| 13 |
SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE |
| 14 |
FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE |
| 15 |
CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS |
| 16 |
AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE. |
| 17 |
COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED. |
| 18 |
*/ |
| 19 |
|
| 20 |
/* |
| 21 |
* |
| 22 |
* Header file for vector/matrix library |
| 23 |
* |
| 24 |
*/ |
| 25 |
|
| 26 |
#pragma once |
| 27 |
|
| 28 |
#include <cassert> |
| 29 |
#include <cstdint> |
| 30 |
#include <utility> |
| 31 |
#include "fwd-vecmat.h" |
| 32 |
|
| 33 |
namespace dcx { |
| 34 |
|
| 35 |
//The basic fixed-point vector. Access elements by name or position |
| 36 |
struct vms_vector |
| 37 |
{ |
| 38 |
fix x, y, z; |
| 39 |
constexpr bool operator==(const vms_vector &rhs) const = default; |
| 40 |
}; |
| 41 |
|
| 42 |
class vm_distance |
| 43 |
{ |
| 44 |
public: |
| 45 |
fix d{0}; |
| 46 |
/* Default constructor only required because Fcd_cache,SoundObjects |
| 47 |
* have global scope instances of vm_distance. They should be |
| 48 |
* converted to construct as needed, then the default constructor |
| 49 |
* should be removed. |
| 50 |
*/ |
| 51 |
constexpr vm_distance() = default; |
| 52 |
constexpr explicit vm_distance(const fix &f) : |
| 53 |
d{f} |
| 54 |
{ |
| 55 |
} |
| 56 |
template <typename T> |
| 57 |
vm_distance &operator+=(T &&rhs) |
| 58 |
{ |
| 59 |
return *this = (*this + std::forward<T>(rhs)); |
| 60 |
} |
| 61 |
template <typename T> |
| 62 |
vm_distance &operator*=(T &&rhs) |
| 63 |
{ |
| 64 |
return *this = (*this * std::forward<T>(rhs)); |
| 65 |
} |
| 66 |
template <typename T> |
| 67 |
vm_distance &operator/=(T &&rhs) |
| 68 |
{ |
| 69 |
return *this = (*this / std::forward<T>(rhs)); |
| 70 |
} |
| 71 |
[[nodiscard]] |
| 72 |
constexpr vm_distance operator+(const vm_distance &rhs) const |
| 73 |
{ |
| 74 |
return vm_distance{d + rhs.d}; |
| 75 |
} |
| 76 |
[[nodiscard]] |
| 77 |
constexpr vm_distance operator*(const int &f) const |
| 78 |
{ |
| 79 |
return vm_distance{d * f}; |
| 80 |
} |
| 81 |
[[nodiscard]] |
| 82 |
constexpr vm_distance operator/(const int &f) const |
| 83 |
{ |
| 84 |
return vm_distance{d / f}; |
| 85 |
} |
| 86 |
[[nodiscard]] |
| 87 |
constexpr bool operator<(const fix &f) const |
| 88 |
{ |
| 89 |
return d < f; |
| 90 |
} |
| 91 |
[[nodiscard]] |
| 92 |
constexpr bool operator<(const vm_distance &rhs) const |
| 93 |
{ |
| 94 |
return d < rhs.d; |
| 95 |
} |
| 96 |
/* Clang chokes using a template operator> to swap the arguments. |
| 97 |
* Use a non-template operator> for the one needed type. |
| 98 |
*/ |
| 99 |
[[nodiscard]] |
| 100 |
constexpr bool operator>(const fix &f) const |
| 101 |
{ |
| 102 |
return d > f; |
| 103 |
} |
| 104 |
bool operator>(const auto &t) const = delete; |
| 105 |
[[nodiscard]] |
| 106 |
constexpr explicit operator bool() const { return d; } |
| 107 |
#ifndef __clang__ |
| 108 |
/* When this catch-all overload is defined, clang becomes confused |
| 109 |
* and treats all arithmetic operations as ambiguous because it |
| 110 |
* cannot choose between the deleted template function and the |
| 111 |
* non-deleted operator fix(). |
| 112 |
*/ |
| 113 |
template <typename T> |
| 114 |
operator T() const = delete; |
| 115 |
#endif |
| 116 |
constexpr operator fix() const |
| 117 |
{ |
| 118 |
return d; |
| 119 |
} |
| 120 |
constexpr vm_distance_squared operator*(const vm_distance &) const; |
| 121 |
}; |
| 122 |
|
| 123 |
class vm_magnitude : public vm_distance |
| 124 |
{ |
| 125 |
public: |
| 126 |
constexpr explicit vm_magnitude(const uint32_t &f) : |
| 127 |
vm_distance(f) |
| 128 |
{ |
| 129 |
} |
| 130 |
}; |
| 131 |
|
| 132 |
enum class vm_distance_squared : fix64 |
| 133 |
{ |
| 134 |
minimum_value = 0, |
| 135 |
maximum_value = INT64_MAX, |
| 136 |
}; |
| 137 |
|
| 138 |
[[nodiscard]] |
| 139 |
static constexpr vm_distance_squared build_vm_distance_squared(const vm_magnitude_squared m) |
| 140 |
{ |
| 141 |
return vm_distance_squared{static_cast<int64_t>(static_cast<uint64_t>(m))}; |
| 142 |
} |
| 143 |
|
| 144 |
[[nodiscard]] |
| 145 |
static constexpr auto operator<=>(const vm_distance_squared a, const vm_distance_squared b) |
| 146 |
{ |
| 147 |
return static_cast<fix64>(a) <=> static_cast<fix64>(b); |
| 148 |
} |
| 149 |
|
| 150 |
constexpr vm_distance_squared vm_distance::operator*(const vm_distance &rhs) const |
| 151 |
{ |
| 152 |
return vm_distance_squared{static_cast<fix64>(static_cast<fix>(*this)) * static_cast<fix64>(static_cast<fix>(rhs))}; |
| 153 |
} |
| 154 |
|
| 155 |
[[nodiscard]] |
| 156 |
constexpr bool operator<(const vm_magnitude_squared a, const fix &b) |
| 157 |
{ |
| 158 |
return static_cast<uint64_t>(a) < b; |
| 159 |
} |
| 160 |
|
| 161 |
#define DEFINE_SERIAL_VMS_VECTOR_TO_MESSAGE() \ |
| 162 |
DEFINE_SERIAL_UDT_TO_MESSAGE(vms_vector, v, (v.x, v.y, v.z)); \ |
| 163 |
ASSERT_SERIAL_UDT_MESSAGE_SIZE(vms_vector, 12) |
| 164 |
|
| 165 |
//Angle vector. Used to store orientations |
| 166 |
struct vms_angvec |
| 167 |
{ |
| 168 |
fixang p, b, h; |
| 169 |
}; |
| 170 |
|
| 171 |
|
| 172 |
//A 3x3 rotation matrix. Sorry about the numbering starting with one. |
| 173 |
//Ordering is across then down, so <m1,m2,m3> is the first row |
| 174 |
struct vms_matrix |
| 175 |
{ |
| 176 |
vms_vector rvec, uvec, fvec; |
| 177 |
}; |
| 178 |
|
| 179 |
// Quaternion structure |
| 180 |
struct vms_quaternion |
| 181 |
{ |
| 182 |
signed short w, x, y, z; |
| 183 |
}; |
| 184 |
|
| 185 |
|
| 186 |
//Macros/functions to fill in fields of structures |
| 187 |
|
| 188 |
//negate a vector |
| 189 |
static inline void vm_vec_negate(vms_vector &v) |
| 190 |
{ |
| 191 |
v.x = -v.x; |
| 192 |
v.y = -v.y; |
| 193 |
v.z = -v.z; |
| 194 |
} |
| 195 |
|
| 196 |
[[nodiscard]] |
| 197 |
static inline vms_vector vm_vec_build_negated(vms_vector v) |
| 198 |
{ |
| 199 |
return vm_vec_negate(v), v; |
| 200 |
} |
| 201 |
|
| 202 |
//Functions in library |
| 203 |
|
| 204 |
/* Return the sum of two vectors. If one vector should be destructively |
| 205 |
* modified, use `vm_vec_add2` instead, which will update one vector in place. |
| 206 |
*/ |
| 207 |
[[nodiscard]] |
| 208 |
vms_vector vm_vec_build_add(const vms_vector &src0, const vms_vector &src1); |
| 209 |
|
| 210 |
//subs two vectors, fills in dest, returns ptr to dest |
| 211 |
//ok for dest to equal either source, but should use vm_vec_sub2() if so |
| 212 |
static inline vms_vector &vm_vec_sub(vms_vector &dest, const vms_vector &src0, const vms_vector &src1) |
| 213 |
{ |
| 214 |
#ifdef DXX_CONSTANT_TRUE |
| 215 |
if (DXX_CONSTANT_TRUE(&src0 == &src1)) |
| 216 |
DXX_ALWAYS_ERROR_FUNCTION("vm_vec_sub with &src0 == &src1"); |
| 217 |
else if (DXX_CONSTANT_TRUE(src0 == src1)) |
| 218 |
DXX_ALWAYS_ERROR_FUNCTION("vm_vec_sub with equal value inputs"); |
| 219 |
#endif |
| 220 |
return _vm_vec_sub(dest, src0, src1); |
| 221 |
} |
| 222 |
|
| 223 |
[[nodiscard]] |
| 224 |
static inline vms_vector vm_vec_build_sub(const vms_vector &src0, const vms_vector &src1) |
| 225 |
{ |
| 226 |
vms_vector dest; |
| 227 |
return vm_vec_sub(dest, src0, src1), dest; |
| 228 |
} |
| 229 |
|
| 230 |
//averages two vectors. returns average |
| 231 |
[[nodiscard]] |
| 232 |
vms_vector vm_vec_build_avg(const vms_vector &src0, const vms_vector &src1); |
| 233 |
|
| 234 |
//scales a vector. returns scaled result |
| 235 |
[[nodiscard]] |
| 236 |
vms_vector vm_vec_copy_scale(vms_vector src, fix s); |
| 237 |
|
| 238 |
//scales a vector, adds it to another, and stores in a 3rd vector |
| 239 |
//dest = src1 + k * src2 |
| 240 |
[[nodiscard]] |
| 241 |
vms_vector vm_vec_scale_add(const vms_vector &src1, const vms_vector &src2, fix k); |
| 242 |
|
| 243 |
[[nodiscard]] |
| 244 |
static inline vms_vector vm_vec_normalized(vms_vector v) |
| 245 |
{ |
| 246 |
return vm_vec_normalize(v), v; |
| 247 |
} |
| 248 |
|
| 249 |
[[nodiscard]] |
| 250 |
static inline vms_vector vm_vec_normalized_quick(vms_vector v) |
| 251 |
{ |
| 252 |
return vm_vec_normalize_quick(v), v; |
| 253 |
} |
| 254 |
|
| 255 |
[[nodiscard]] |
| 256 |
static inline std::pair<vm_magnitude, vms_vector> vm_vec_normalize_quick_with_magnitude(vms_vector v) |
| 257 |
{ |
| 258 |
const auto mag = vm_vec_normalize_quick(v); |
| 259 |
return {mag, v}; |
| 260 |
} |
| 261 |
|
| 262 |
[[nodiscard]] |
| 263 |
vms_vector vm_vec_cross(const vms_vector &src0, const vms_vector &src1); |
| 264 |
|
| 265 |
[[nodiscard]] |
| 266 |
vms_vector vm_vec_normal(const vms_vector &p0, const vms_vector &p1, const vms_vector &p2); |
| 267 |
|
| 268 |
[[nodiscard]] |
| 269 |
vms_vector vm_vec_perp (const vms_vector &p0, const vms_vector &p1, const vms_vector &p2); |
| 270 |
|
| 271 |
//transpose a matrix in place. returns ptr to matrix |
| 272 |
static inline void vm_transpose_matrix(vms_matrix &m) |
| 273 |
{ |
| 274 |
using std::swap; |
| 275 |
swap(m.uvec.x, m.rvec.y); |
| 276 |
swap(m.fvec.x, m.rvec.z); |
| 277 |
swap(m.fvec.y, m.uvec.z); |
| 278 |
} |
| 279 |
|
| 280 |
[[nodiscard]] |
| 281 |
static inline vms_matrix vm_transposed_matrix(vms_matrix m) |
| 282 |
{ |
| 283 |
vm_transpose_matrix(m); |
| 284 |
return m; |
| 285 |
} |
| 286 |
|
| 287 |
//mulitply 2 matrices, fill in dest. returns ptr to dest |
| 288 |
static inline void vm_matrix_x_matrix(vms_matrix &dest, const vms_matrix &src0, const vms_matrix &src1) |
| 289 |
{ |
| 290 |
#ifdef DXX_CONSTANT_TRUE |
| 291 |
if (DXX_CONSTANT_TRUE(&dest == &src0)) |
| 292 |
DXX_ALWAYS_ERROR_FUNCTION("vm_matrix_x_matrix with &dest == &src0"); |
| 293 |
else if (DXX_CONSTANT_TRUE(&dest == &src1)) |
| 294 |
DXX_ALWAYS_ERROR_FUNCTION("vm_matrix_x_matrix with &dest == &src1"); |
| 295 |
#endif |
| 296 |
assert(&dest != &src0); |
| 297 |
assert(&dest != &src1); |
| 298 |
return _vm_matrix_x_matrix(dest, src0, src1); |
| 299 |
} |
| 300 |
|
| 301 |
[[nodiscard]] |
| 302 |
static inline vms_matrix vm_matrix_x_matrix(const vms_matrix &src0, const vms_matrix &src1) |
| 303 |
{ |
| 304 |
vms_matrix dest; |
| 305 |
vm_matrix_x_matrix(dest, src0, src1); |
| 306 |
return dest; |
| 307 |
} |
| 308 |
|
| 309 |
extern const vms_vector vmd_zero_vector; |
| 310 |
|
| 311 |
} |