/
Vector3.h
383 lines (320 loc) · 10.4 KB
/
Vector3.h
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#pragma once
/* greebo: This file contains the templated class definition of the three-component vector
*
* BasicVector3: A vector with three components of type <T>
*
* The BasicVector3 is equipped with the most important operators like *, *= and so on.
*
* Note: The most commonly used Vector3 is a BasicVector3<float>, this is also defined in this file
*
* Note: that the multiplication of a Vector3 with another one (Vector3*Vector3) does NOT
* result in an inner product but in a component-wise scaling. Use the .dot() method to
* execute an inner product of two vectors.
*/
#include <cmath>
#include <istream>
#include <ostream>
#include <sstream>
#include <float.h>
#include "math/pi.h"
#include "lrint.h"
#include "FloatTools.h"
#undef Success // get rid of fuckwit X.h macro
#include <Eigen/Dense>
/// A 3-element vector of type T
template<typename T>
class BasicVector3
{
public:
/// Eigen vector type to store a BasicVector3's data
using Eigen_T = Eigen::Matrix<T, 3, 1>;
// Public typedef to read the type of our elements
using ElementType = T;
private:
// Eigen vector for storage and calculations
Eigen_T _v;
public:
/// Initialise Vector with all zeroes.
BasicVector3(): _v(0, 0, 0)
{}
/// Construct a BasicVector3 with the 3 provided components.
BasicVector3(T x, T y, T z): _v(x, y, z)
{}
/// Construct directly from the underlying Eigen vector type
BasicVector3(const Eigen_T& vec): _v(vec)
{}
/**
* \brief Construct a BasicVector3 from a 3-element array.
*
* The array must be valid as no bounds checking is done.
*/
BasicVector3(const T* array): _v(array[0], array[1], array[2])
{}
/**
* Named constructor, returning a vector on the unit sphere for the given spherical coordinates.
*/
static BasicVector3<T> createForSpherical(T theta, T phi)
{
return BasicVector3<T>(
cos(theta) * cos(phi),
sin(theta) * cos(phi),
sin(phi)
);
}
/// Read-only access to the internal Eigen vector
const Eigen_T& eigen() const { return _v; }
/// Mutable access to the internal Eigen vector
Eigen_T& eigen() { return _v; }
/// Set all 3 components to the provided values.
void set(T x, T y, T z)
{
_v = Eigen_T(x, y, z);
}
// Return mutable references to the vector components
T& x() { return _v[0]; }
T& y() { return _v[1]; }
T& z() { return _v[2]; }
// Return vector component values
T x() const { return _v[0]; }
T y() const { return _v[1]; }
T z() const { return _v[2]; }
/// Return human readable debug string (pretty print)
std::string pp() const
{
std::stringstream ss;
ss << "[" << x() << ", " << y() << ", " << z() << "]";
return ss.str();
}
/// Compare this BasicVector3 against another for equality.
bool operator== (const BasicVector3& other) const {
return (other.x() == x()
&& other.y() == y()
&& other.z() == z());
}
/// Compare this BasicVector3 against another for inequality.
bool operator!= (const BasicVector3& other) const {
return !(*this == other);
}
/// Return the componentwise negation of this vector
BasicVector3<T> operator- () const
{
return BasicVector3<T>(-_v);
}
/// Return the Pythagorean length of this vector.
double getLength() const
{
return _v.norm();
}
/// Return the squared length of this vector.
T getLengthSquared() const
{
return _v.squaredNorm();
}
/**
* \brief Normalise this vector in-place by scaling by the inverse of its
* size.
* \return The length of the vector before normalisation.
*/
double normalise()
{
double length = getLength();
_v.normalize();
return length;
}
/// Return the result of normalising this vector
BasicVector3<T> getNormalised() const
{
return BasicVector3<T>(_v.normalized());
}
/// Return dot product of this and another vector
T dot(const BasicVector3<T>& other) const
{
return _v.dot(other._v);
}
/// Return the angle between <self> and <other>
T angle(const BasicVector3<T>& other) const
{
// Get dot product of normalised vectors, ensuring it lies between -1
// and 1.
T dot = std::clamp(
getNormalised().dot(other.getNormalised()), -1.0, 1.0
);
// Angle is the arccos of the dot product
return acos(dot);
}
/// Return the cross product of this and another vector
BasicVector3<T> cross(const BasicVector3<T>& other) const
{
return BasicVector3<T>(_v.cross(other._v));
}
/** Implicit cast to C-style array. This allows a Vector3 to be
* passed directly to GL functions that expect an array (e.g.
* glFloat3dv()). These functions implicitly provide operator[]
* as well, since the C-style array provides this function.
*/
operator const T* () const {
return _v.data();
}
operator T* () {
return _v.data();
}
/// Returns a "snapped" copy of this Vector, each component rounded to the given precision.
BasicVector3<T> getSnapped(T snap) const
{
return BasicVector3<T>(float_snapped(x(), snap),
float_snapped(y(), snap),
float_snapped(z(), snap));
}
/// Snaps this vector to the given precision in place.
void snap(T snap)
{
*this = getSnapped(snap);
}
};
/// Multiply vector components with a scalar and return the result
template <
typename T, typename S,
typename = typename std::enable_if<std::is_arithmetic<S>::value, S>::type
>
BasicVector3<T> operator*(const BasicVector3<T>& v, S s)
{
return BasicVector3<T>(v.eigen() * s);
}
/// Multiply vector components with a scalar and return the result
template <
typename T, typename S,
typename = typename std::enable_if<std::is_arithmetic<S>::value, S>::type
>
BasicVector3<T> operator*(S s, const BasicVector3<T>& v)
{
return v * s;
}
/// Multiply vector components with a scalar and modify in place
template <typename T, typename S>
BasicVector3<T>& operator*=(BasicVector3<T>& v, S s)
{
v.eigen() *= s;
return v;
}
/// Divide vector by a scalar and return result
template<typename T, typename S>
BasicVector3<T> operator/ (const BasicVector3<T>& v, S s)
{
return BasicVector3<T>(v.x() / s, v.y() / s, v.z() / s);
}
/// Divide vector by a scalar in place
template<typename T, typename S>
BasicVector3<T>& operator/= (BasicVector3<T>& v, S s)
{
v = v / s;
return v;
}
/// Divide a scalar by a vector and return result
template <
typename S, typename T,
typename = typename std::enable_if<std::is_arithmetic<S>::value, S>::type
>
BasicVector3<T> operator/(S s, const BasicVector3<T>& v)
{
return BasicVector3<T>(s / v.x(), s / v.y(), s / v.z());
}
/// Divide a vector componentwise with another vector and return result
template <typename T>
BasicVector3<T> operator/(const BasicVector3<T>& v1, const BasicVector3<T>& v2)
{
return BasicVector3<T>(v1.x() / v2.x(), v1.y() / v2.y(), v1.z() / v2.z());
}
/// Divide a vector componentwise with another vector, in place
template <typename T>
BasicVector3<T>& operator/=(BasicVector3<T>& v1, const BasicVector3<T>& v2)
{
v1 = v1 / v2;
return v1;
}
/// Componentwise addition of two vectors
template <typename T>
BasicVector3<T> operator+(const BasicVector3<T>& v1, const BasicVector3<T>& v2)
{
return BasicVector3<T>(v1.x() + v2.x(), v1.y() + v2.y(), v1.z() + v2.z());
}
/// Componentwise vector addition in place
template <typename T>
BasicVector3<T>& operator+=(BasicVector3<T>& v1, const BasicVector3<T>& v2)
{
v1 = v1 + v2;
return v1;
}
/// Componentwise subtraction of two vectors
template <typename T>
BasicVector3<T> operator-(const BasicVector3<T>& v1, const BasicVector3<T>& v2)
{
return BasicVector3<T>(v1.x() - v2.x(), v1.y() - v2.y(), v1.z() - v2.z());
}
/// Componentwise vector subtraction in place
template<typename T>
BasicVector3<T>& operator-= (BasicVector3<T>& v1, const BasicVector3<T>& v2)
{
v1 = v1 - v2;
return v1;
}
/// Componentwise (Hadamard) product of two vectors
template <typename T>
BasicVector3<T> operator*(const BasicVector3<T>& v1, const BasicVector3<T>& v2)
{
return BasicVector3<T>(v1.x() * v2.x(), v1.y() * v2.y(), v1.z() * v2.z());
}
/// Componentwise (Hadamard) product of two vectors, in place
template<typename T>
BasicVector3<T>& operator*= (BasicVector3<T>& v1, const BasicVector3<T>& v2)
{
v1 = v1 * v2;
return v1;
}
/// Stream insertion for BasicVector3
template<typename T>
inline std::ostream& operator<<(std::ostream& st, BasicVector3<T> vec)
{
return st << vec.x() << " " << vec.y() << " " << vec.z();
}
/// Stream extraction for BasicVector3
template<typename T>
inline std::istream& operator>>(std::istream& st, BasicVector3<T>& vec)
{
return st >> std::skipws >> vec.x() >> vec.y() >> vec.z();
}
namespace math
{
/// Epsilon equality test for BasicVector3
template <typename T>
bool near(const BasicVector3<T>& v1, const BasicVector3<T>& v2, double epsilon)
{
BasicVector3<T> diff = v1 - v2;
return std::abs(diff.x()) < epsilon && std::abs(diff.y()) < epsilon
&& std::abs(diff.z()) < epsilon;
}
/// Test if two vectors are parallel (in similar or opposite directions)
template<typename T>
bool isParallel(const BasicVector3<T>& v1, const BasicVector3<T>& v2)
{
T angle = v1.angle(v2);
return float_equal_epsilon(angle, 0.0, 0.001)
|| float_equal_epsilon(angle, PI, 0.001);
}
/// Return the midpoint of two vectors
template<typename T>
BasicVector3<T> midPoint(const BasicVector3<T>& v1, const BasicVector3<T>& v2)
{
return (v1 + v2) * 0.5;
}
}
// ==========================================================================================
// A 3-element vector stored in double-precision floating-point.
typedef BasicVector3<double> Vector3;
// A 3-element vector (single-precision variant)
typedef BasicVector3<float> Vector3f;
// =============== Vector3 Constants ==================================================
const Vector3 g_vector3_identity(0, 0, 0);
const Vector3 g_vector3_axis_x(1, 0, 0);
const Vector3 g_vector3_axis_y(0, 1, 0);
const Vector3 g_vector3_axis_z(0, 0, 1);
const Vector3 g_vector3_axes[3] = { g_vector3_axis_x, g_vector3_axis_y, g_vector3_axis_z };