/
Vector4.h
275 lines (239 loc) · 7.48 KB
/
Vector4.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
#pragma once
#include <sstream>
/* greebo: This file contains the templated class definition of the three-component vector
*
* BasicVector4: A vector with three components of type <Element>
*
* The BasicVector4 is equipped with the most important operators like *, *= and so on.
*
* Note: The most commonly used Vector4 is a BasicVector4<float>, this is also defined in this file
*
* Note: that the multiplication of a Vector4 with another one (Vector4*Vector4) 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 "lrint.h"
#include "FloatTools.h"
#include "Vector3.h"
/// A 4-element vector of type <Element>
template<typename Element>
class BasicVector4
{
// The components of this vector
Element _v[4];
public:
// Public typedef to read the type of our elements
typedef Element ElementType;
// Constructor (no arguments)
BasicVector4() {
_v[0] = 0;
_v[1] = 0;
_v[2] = 0;
_v[3] = 0;
}
/**
* \brief Construct a BasicVector4 out of 4 explicit values.
*
* If the W coordinate is unspecified it will default to 1.
*/
BasicVector4(Element x_, Element y_, Element z_, Element w_ = 1)
{
_v[0] = x_;
_v[1] = y_;
_v[2] = z_;
_v[3] = w_;
}
// Construct a BasicVector4 out of a Vector3 plus a W value (default 1)
BasicVector4(const BasicVector3<Element>& other, Element w_ = 1)
{
_v[0] = other.x();
_v[1] = other.y();
_v[2] = other.z();
_v[3] = w_;
}
// Return non-constant references to the components
Element& x() { return _v[0]; }
Element& y() { return _v[1]; }
Element& z() { return _v[2]; }
Element& w() { return _v[3]; }
// Return constant references to the components
const Element& x() const { return _v[0]; }
const Element& y() const { return _v[1]; }
const Element& z() const { return _v[2]; }
const Element& w() const { return _v[3]; }
/**
* \brief Return a readable (pretty-printed) string representation of the
* vector.
*
* We need a dedicated function for this because the standard operator<< is
* already used for serialisation to the less readable space-separated text
* format.
*/
std::string pp() const
{
std::stringstream ss;
ss << "(" << x() << ", " << y() << ", " << z() << ", " << w() << ")";
return ss.str();
}
/// Compare this BasicVector4 against another for equality.
bool operator== (const BasicVector4& other) const {
return (other.x() == x()
&& other.y() == y()
&& other.z() == z()
&& other.w() == w());
}
/// Compare this BasicVector4 against another for inequality.
bool operator!= (const BasicVector4& other) const {
return !(*this == other);
}
/* Scalar product this vector with another Vector4,
* returning the projection of <self> onto <other>
*
* @param other
* The Vector4 to dot-product with this Vector4.
*
* @returns
* The inner product (a scalar): a[0]*b[0] + a[1]*b[1] + a[2]*b[2] + a[3]*b[3]
*/
template<typename OtherT>
Element dot(const BasicVector4<OtherT>& other) const {
return Element(_v[0] * other.x() +
_v[1] * other.y() +
_v[2] * other.z() +
_v[3] * other.w());
}
/** Project this homogeneous Vector4 into a Cartesian Vector3
* by dividing by w.
*
* @returns
* A Vector3 representing the Cartesian equivalent of this
* homogeneous vector.
*/
BasicVector3<Element> getProjected() {
return BasicVector3<Element>(
_v[0] / _v[3],
_v[1] / _v[3],
_v[2] / _v[3]);
}
/// Cast to const raw array
operator const Element* () const { return _v; }
// Cast to non-const raw array
operator Element* () { return _v; }
/* Cast this Vector4 onto a Vector3, both const and non-const
*/
BasicVector3<Element>& getVector3() {
return *reinterpret_cast<BasicVector3<Element>*>(_v);
}
const BasicVector3<Element>& getVector3() const {
return *reinterpret_cast<const BasicVector3<Element>*>(_v);
}
}; // BasicVector4
/// Componentwise addition of two vectors
template <typename T>
BasicVector4<T> operator+(const BasicVector4<T>& v1, const BasicVector4<T>& v2)
{
return BasicVector4<T>(v1.x() + v2.x(),
v1.y() + v2.y(),
v1.z() + v2.z(),
v1.w() + v2.w());
}
template <typename T>
BasicVector4<T>& operator+=(BasicVector4<T>& v1, const BasicVector4<T>& v2)
{
v1.x() += v2.x();
v1.y() += v2.y();
v1.z() += v2.z();
v1.w() += v2.w();
return v1;
}
/// Componentwise subtraction of two vectors
template<typename T>
BasicVector4<T> operator- (const BasicVector4<T>& v1, const BasicVector4<T>& v2)
{
return BasicVector4<T>(v1.x() - v2.x(),
v1.y() - v2.y(),
v1.z() - v2.z(),
v1.w() - v2.w());
}
template<typename T>
BasicVector4<T>& operator-= (BasicVector4<T>& v1, const BasicVector4<T>& v2)
{
v1.x() -= v2.x();
v1.y() -= v2.y();
v1.z() -= v2.z();
v1.w() -= v2.w();
return v1;
}
/// Multiply BasicVector4 with a scalar
template <
typename T, typename S,
typename = typename std::enable_if<std::is_arithmetic<S>::value, S>::type
>
BasicVector4<T> operator*(const BasicVector4<T>& v, S s)
{
return BasicVector4<T>(v.x() * s, v.y() * s, v.z() * s, v.w() * s);
}
/// Multiply BasicVector4 with a scalar
template <
typename T, typename S,
typename = typename std::enable_if<std::is_arithmetic<S>::value, S>::type
>
BasicVector4<T> operator*(S s, const BasicVector4<T>& v)
{
return v * s;
}
/// Multiply BasicVector4 with a scalar and modify in place
template<typename T, typename S>
BasicVector4<T>& operator*= (BasicVector4<T>& v, S s)
{
v.x() *= s;
v.y() *= s;
v.z() *= s;
v.w() *= s;
return v;
}
/// Divide and assign BasicVector4 by a scalar
template<typename T, typename S>
BasicVector4<T>& operator/= (BasicVector4<T>& v, S s)
{
v.x() /= s;
v.y() /= s;
v.z() /= s;
v.w() /= s;
return v;
}
/// Divide a BasicVector4 by a scalar
template<typename T, typename S>
BasicVector4<T> operator/ (const BasicVector4<T>& v, S s)
{
auto result = v;
result /= s;
return result;
}
/// Stream insertion for BasicVector4
template<typename T>
inline std::ostream& operator<<(std::ostream& st, BasicVector4<T> vec)
{
return st << vec.x() << " " << vec.y() << " " << vec.z() << " " << vec.w();
}
/// Stream extraction for BasicVector4
template<typename T>
inline std::istream& operator>>(std::istream& st, BasicVector4<T>& vec)
{
return st >> std::skipws >> vec.x() >> vec.y() >> vec.z() >> vec.w();
}
// A 4-element vector stored in double-precision floating-point.
typedef BasicVector4<double> Vector4;
// A 4-element vector stored in single-precision floating-point.
typedef BasicVector4<float> Vector4f;
namespace math
{
/// Epsilon equality test for BasicVector4
template <typename T>
inline bool isNear(const BasicVector4<T>& v1, const BasicVector4<T>& v2, double epsilon)
{
BasicVector4<T> diff = v1 - v2;
return std::abs(diff.x()) < epsilon && std::abs(diff.y()) < epsilon
&& std::abs(diff.z()) < epsilon && std::abs(diff.w()) < epsilon;
}
}