1 #ifndef _VN_MATH_VECTOR_H_
7 #define _VN_MATH_VECTOR_H_
21 template <
size_t tdim,
typename T =
float>
43 std::fill_n(c, tdim, val);
76 return const_cast<T&
>(
static_cast<const vec&
>(*this)[index]);
104 for (
size_t i = 0; i < tdim; i++)
116 for (
size_t i = 0; i < tdim; i++)
128 for (
size_t i = 0; i < tdim; i++)
140 for (
size_t i = 0; i < tdim; i++)
153 size_t dim()
const {
return tdim; }
162 for (
size_t i = 0; i < tdim; i++)
175 for (
size_t i = 0; i < tdim; i++)
176 sumOfSquares += c[i] * c[i];
178 return sqrt(sumOfSquares);
189 for (
size_t i = 0; i < tdim; i++)
190 v.
c[i] = c[i] + toAdd.
c[i];
203 for (
size_t i = 0; i < tdim; i++)
204 v.
c[i] = c[i] - to_sub.
c[i];
217 for (
size_t i = 0; i < tdim; i++)
218 v.
c[i] = c[i] * scalar;
231 for (
size_t i = 0; i < tdim; i++)
232 v.
c[i] = c[i] / scalar;
246 for (
size_t i = 0; i < tdim; i++)
260 for (
size_t i = 0; i < tdim; i++)
261 runningSum += c[i] * rhs.
c[i];
270 #if defined(_MSC_VER)
271 #pragma warning(push)
274 #pragma warning(disable:4201)
278 template <
typename T>
311 explicit vec(T val) : x(val), y(val) { }
318 vec(T x_val, T y_val) : x(x_val), y(y_val) { }
367 return const_cast<T&
>(
static_cast<const vec&
>(*this)[index]);
444 size_t dim()
const {
return 2; }
452 #if defined(_MSC_VER)
453 #pragma warning(push)
454 #pragma warning(disable:4146)
459 #if defined (_MSC_VER)
471 #if defined(_MSC_VER)
472 #pragma warning(push)
473 #pragma warning(disable:4244)
476 #if (defined(_MSC_VER) && _MSC_VER <= 1600)
479 return sqrt(static_cast<float>(x*x + y*y));
482 return sqrt(static_cast<float>(x*x + y*y));
486 #if defined (_MSC_VER)
497 return vec(x + toAdd.x, y + toAdd.y);
506 return vec(x - to_sub.x, y - to_sub.y);
515 return vec(x * scalar, y * scalar);
524 return vec(x / scalar, y / scalar);
534 return vec(x / m, y / m);
543 return x*rhs.x + y*rhs.y;
550 template <
typename T>
585 #if __cplusplus >= 201103L
606 explicit vec(T val) : x(val), y(val), z(val) { }
614 vec(
const T& x_val,
const T& y_val,
const T& z_val) : x(x_val), y(y_val), z(z_val) { }
670 return const_cast<T&
>(
static_cast<const vec&
>(*this)[index]);
698 for (
size_t i = 0; i < 3; i++)
710 for (
size_t i = 0; i < 3; i++)
722 for (
size_t i = 0; i < 3; i++)
734 for (
size_t i = 0; i < 3; i++)
747 size_t dim()
const {
return 3; }
755 #if defined(_MSC_VER)
756 #pragma warning(push)
757 #pragma warning(disable:4146)
760 return vec(-x, -y, -z);
762 #if defined (_MSC_VER)
774 #if defined(_MSC_VER)
775 #pragma warning(push)
776 #pragma warning(disable:4244)
779 #if (defined(_MSC_VER) && _MSC_VER <= 1600)
782 return sqrt(static_cast<float>(x*x + y*y + z*z));
785 return sqrt(static_cast<float>(x*x + y*y + z*z));
789 #if defined (_MSC_VER)
800 return vec(x + toAdd.x, y + toAdd.y, z + toAdd.z);
809 return vec(x - to_sub.x, y - to_sub.y, z - to_sub.z);
818 return vec(x * scalar, y * scalar, z * scalar);
827 return vec(x / scalar, y / scalar, z / scalar);
837 return vec(x / m, y / m, z / m);
846 return x*rhs.x + y*rhs.y + z*rhs.z;
858 v.
c[0] =
c[1] * rhs.
c[2] -
c[2] * rhs.
c[1];
859 v.
c[1] =
c[2] * rhs.
c[0] -
c[0] * rhs.
c[2];
860 v.
c[2] =
c[0] * rhs.
c[1] -
c[1] * rhs.
c[0];
868 template <
typename T>
909 #if __cplusplus >= 201103L
936 explicit vec(T val) : x(val), y(val), z(val), w(val) { }
945 vec(T x_val, T y_val, T z_val, T w_val) : x(x_val), y(y_val), z(z_val), w(w_val) { }
1008 return const_cast<T&
>(
static_cast<const vec&
>(*this)[index]);
1093 size_t dim()
const {
return 4; }
1101 #if defined(_MSC_VER)
1102 #pragma warning(push)
1103 #pragma warning(disable:4146)
1106 return vec(-x, -y, -z, -w);
1108 #if defined (_MSC_VER)
1109 #pragma warning(pop)
1120 #if defined(_MSC_VER)
1121 #pragma warning(push)
1122 #pragma warning(disable:4244)
1125 #if (defined(_MSC_VER) && _MSC_VER <= 1600)
1128 return sqrt(static_cast<float>(x*x + y*y + z*z + w*w));
1131 return sqrt(static_cast<float>(x*x + y*y + z*z + w*w));
1135 #if defined (_MSC_VER)
1136 #pragma warning(pop)
1146 return vec(x + toAdd.x, y + toAdd.y, z + toAdd.z, w + toAdd.w);
1155 return vec(x - to_sub.x, y - to_sub.y, z - to_sub.z, w - to_sub.w);
1164 return vec(x * scalar, y * scalar, z * scalar, w * scalar);
1173 return vec(x / scalar, y / scalar, z / scalar, w / scalar);
1183 return vec(x / m, y / m, z / m, w / m);
1192 return x*rhs.x + y*rhs.y + z*rhs.z + w*rhs.w;
1196 #if defined (_MSC_VER)
1197 #pragma warning(pop)
1207 template <
size_t tdim,
typename T>
1208 vec<tdim, T> operator+(vec<tdim, T> lhs,
const vec<tdim, T>& rhs)
1220 template <
size_t tdim,
typename T>
1221 vec<tdim, T> operator-(vec<tdim, T> lhs,
const vec<tdim, T>& rhs)
1228 #if defined(_MSC_VER)
1229 #pragma warning(push)
1232 #pragma warning(disable:4244)
1240 template <
size_t tdim,
typename T,
typename S>
1241 vec<tdim, T> operator*(vec<tdim, T> lhs,
const S& rhs)
1253 template <
size_t tdim,
typename T,
typename S>
1254 vec<tdim, T> operator*(
const S& rhs, vec<tdim, T> lhs)
1266 template <
size_t tdim,
typename T,
typename S>
1267 vec<tdim, T> operator/(vec<tdim, T> lhs,
const S& rhs)
1274 #if defined (_MSC_VER)
1275 #pragma warning(pop)
1281 typedef vec<2> vec2;
1284 typedef vec<3> vec3;
1287 typedef vec<4> vec4;
1290 typedef vec<2, float> vec2f;
1293 typedef vec<3, float> vec3f;
1296 typedef vec<4, float> vec4f;
1299 typedef vec<2, double> vec2d;
1302 typedef vec<3, double> vec3d;
1305 typedef vec<4, double> vec4d;
1308 typedef vec<2, long double> vec2ld;
1311 typedef vec<3, long double> vec3ld;
1314 typedef vec<4, long double> vec4ld;
1317 typedef vec<2, int32_t> vec2i32;
1320 typedef vec2i32 ivec2;
1323 typedef vec<3, int32_t> vec3i32;
1326 typedef vec<4, int32_t> vec4i32;
1329 typedef vec<2, uint32_t> vec2u32;
1332 typedef vec<3, uint32_t> vec3u32;
1335 typedef vec<4, uint32_t> vec4u32;
1344 template <
size_t tdim,
typename T> std::string str(vec<tdim, T> v)
1346 std::stringstream ss;
1348 for (
size_t i = 0; i < v.dim(); i++)
1352 if (i + 1 < v.dim())
1366 template <
size_t tdim,
typename T> std::ostream& operator<<(std::ostream& out, vec<tdim, T> v)
T z
Z (2-component).
Definition: vector.h:569
vec & operator*=(const T &rhs)
Multiplies the vector by a scalar.
Definition: vector.h:720
Template for a Euclidean vector.
Definition: vector.h:22
T & operator[](size_t index)
Indexing into the vector's components.
Definition: vector.h:74
T dot(const vec &rhs) const
Computes the dot product of this and the provided vector.
Definition: vector.h:844
T dot(const vec &rhs) const
Computes the dot product of this and the provided vector.
Definition: vector.h:256
vec & operator+=(const vec &rhs)
Adds a vector to this vector.
Definition: vector.h:696
T w
W (3-component).
Definition: vector.h:890
const T & operator[](size_t index) const
Indexing into the vector's components.
Definition: vector.h:374
Vector with 4 component specialization.
Definition: vector.h:869
T z
Z (2-component).
Definition: vector.h:887
vec(T val)
Creates new vector with components initialized to val.
Definition: vector.h:41
const T & operator[](size_t index) const
Indexing into the vector's components.
Definition: vector.h:677
vec & operator/=(const T &rhs)
Divides the vector by a scalar.
Definition: vector.h:732
T & operator[](size_t index)
Indexing into the vector's components.
Definition: vector.h:668
vec mult(const double &scalar) const
Multiplies the vector by a scalar.
Definition: vector.h:1162
vec sub(const vec &to_sub) const
Subtracts a vector from this vector.
Definition: vector.h:199
size_t dim() const
The vector's dimension.
Definition: vector.h:153
vec(T val)
Creates new vector with components initialized to val.
Definition: vector.h:936
static vec unitX()
Unit vector pointing in the X (0-component) direction.
Definition: vector.h:970
T dot(const vec &rhs) const
Computes the dot product of this and the provided vector.
Definition: vector.h:1190
vec div(const double &scalar) const
Divides the vector by a scalar.
Definition: vector.h:227
vec(const T &x_val, const T &y_val, const T &z_val)
Creates a new vector with its components initialized to the provided values.
Definition: vector.h:614
vec sub(const vec &to_sub) const
Subtracts a vector from this vector.
Definition: vector.h:504
size_t dim() const
The vector's dimension.
Definition: vector.h:1093
vec(T val)
Creates new vector with components initialized to val.
Definition: vector.h:606
T y
Y (1-component).
Definition: vector.h:566
static vec one()
Vector with all of its components set to 1.
Definition: vector.h:335
vec & operator+=(const vec &rhs)
Adds a vector to this vector.
Definition: vector.h:1034
vec & operator/=(const T &rhs)
Divides the vector by a scalar.
Definition: vector.h:138
vec(T x_val, T y_val)
Creates a new vector with its components inintialized to the provided values.
Definition: vector.h:318
T mag() const
The vector's magnitude.
Definition: vector.h:1116
T r
Red (0-component).
Definition: vector.h:896
vec neg() const
Negates the vector.
Definition: vector.h:158
vec norm() const
Normalizes the vector.
Definition: vector.h:1179
static vec unitZ()
Unit vector pointing in the Z (2-component) direction.
Definition: vector.h:655
const T & operator[](size_t index) const
Indexing into the vector's components.
Definition: vector.h:1015
static vec unitY()
Unit vector pointing in the Y (1-component) direction.
Definition: vector.h:978
vec add(const vec &toAdd) const
Adds a vector to this vector.
Definition: vector.h:495
vec sub(const vec &to_sub) const
Subtracts a vector from this vector.
Definition: vector.h:807
T y
Y (1-component).
Definition: vector.h:884
static vec unitY()
Unit vector pointing in the Y (1-component) direction.
Definition: vector.h:647
vec operator-() const
Negates the vector.
Definition: vector.h:384
vec div(const double &scalar) const
Divides the vector by a scalar.
Definition: vector.h:522
T mag() const
The vector's magnitude.
Definition: vector.h:770
vec div(const double &scalar) const
Divides the vector by a scalar.
Definition: vector.h:1171
vec & operator*=(const T &rhs)
Multiplies the vector by a scalar.
Definition: vector.h:1062
vec()
Creates a new vector with uninitialized components.
Definition: vector.h:306
T dot(const vec &rhs) const
Computes the dot product of this and the provided vector.
Definition: vector.h:541
Vector with 3 component specialization.
Definition: vector.h:551
size_t dim() const
The vector's dimension.
Definition: vector.h:444
vec mult(const double &scalar) const
Multiplies the vector by a scalar.
Definition: vector.h:816
size_t dim() const
The vector's dimension.
Definition: vector.h:747
T g
Green (1-component).
Definition: vector.h:899
vec neg() const
Negates the vector.
Definition: vector.h:1098
vec(T val)
Creates new vector with components initialized to val.
Definition: vector.h:311
vec mult(const double &scalar) const
Multiplies the vector by a scalar.
Definition: vector.h:513
T x
X (0-component).
Definition: vector.h:563
T c[tdim]
The vector's components.
Definition: vector.h:29
T b
Blue (2-component).
Definition: vector.h:902
static vec one()
Vector with all of its components set to 1.
Definition: vector.h:61
vec & operator+=(const vec &rhs)
Adds a vector to this vector.
Definition: vector.h:393
vec div(const double &scalar) const
Divides the vector by a scalar.
Definition: vector.h:825
vec(T x_val, T y_val, T z_val, T w_val)
Creates a new vector with its components inintialized to the provided values.
Definition: vector.h:945
T mag() const
The vector's magnitude.
Definition: vector.h:171
vec & operator/=(const T &rhs)
Divides the vector by a scalar.
Definition: vector.h:1076
T g
Green (1-component).
Definition: vector.h:578
static vec zero()
Vector with all of its components set to 0.
Definition: vector.h:954
T & operator[](size_t index)
Indexing into the vector's components.
Definition: vector.h:1006
vec norm() const
Normalizes the vector.
Definition: vector.h:240
static vec zero()
Vector with all of its components set to 0.
Definition: vector.h:623
static vec unitZ()
Unit vector pointing in the Z (2-component) direction.
Definition: vector.h:986
vec()
Creates a new vector with uninitialized components.
Definition: vector.h:36
T b
Blue (2-component).
Definition: vector.h:581
vec & operator-=(const vec &rhs)
Subtracts a vector from this vector.
Definition: vector.h:708
vec & operator*=(const T &rhs)
Multiplies the vector by a scalar.
Definition: vector.h:417
vec & operator+=(const vec &rhs)
Adds a vector to this vector.
Definition: vector.h:102
static vec zero()
Vector with all of its components set to 0.
Definition: vector.h:327
T x
X (0-component).
Definition: vector.h:881
vec operator-() const
Negates the vector.
Definition: vector.h:93
static vec unitX()
Unit vector pointing in the X (0-component) direction.
Definition: vector.h:343
vec & operator-=(const vec &rhs)
Subtracts a vector from this vector.
Definition: vector.h:405
static vec unitY()
Unit vector pointing in the Y (1-component) direction.
Definition: vector.h:351
vec mult(const double &scalar) const
Multiplies the vector by a scalar.
Definition: vector.h:213
T y
Y (1-component).
Definition: vector.h:294
T & operator[](size_t index)
Indexing into the vector's components.
Definition: vector.h:365
vec< 3, T > cross(const vec< 3, T > &rhs) const
Computes the cross product of this and the provided vector.
Definition: vector.h:854
vec()
Creates a new vector with uninitialized components.
Definition: vector.h:931
vec norm() const
Normalizes the vector.
Definition: vector.h:833
T c[3]
The vector's components.
Definition: vector.h:593
static vec zero()
Vector with all of its components set to 0.
Definition: vector.h:53
vec neg() const
Negates the vector.
Definition: vector.h:449
Vector with 2 component specialization.
Definition: vector.h:279
const T & operator[](size_t index) const
Indexing into the vector's components.
Definition: vector.h:83
vec add(const vec &toAdd) const
Adds a vector to this vector.
Definition: vector.h:1144
static vec unitW()
Unit vector pointing in the W (3-component) direction.
Definition: vector.h:994
static vec unitX()
Unit vector pointing in the X (0-component) direction.
Definition: vector.h:639
vec()
Creates a new vector with uninitialized components.
Definition: vector.h:601
T x
X (0-component).
Definition: vector.h:291
vec operator-() const
Negates the vector.
Definition: vector.h:687
vec neg() const
Negates the vector.
Definition: vector.h:752
T a
Alpha (3-component).
Definition: vector.h:905
vec sub(const vec &to_sub) const
Subtracts a vector from this vector.
Definition: vector.h:1153
vec & operator/=(const T &rhs)
Divides the vector by a scalar.
Definition: vector.h:429
T mag() const
The vector's magnitude.
Definition: vector.h:467
vec & operator*=(const T &rhs)
Multiplies the vector by a scalar.
Definition: vector.h:126
static vec one()
Vector with all of its components set to 1.
Definition: vector.h:962
vec norm() const
Normalizes the vector.
Definition: vector.h:530
static vec one()
Vector with all of its components set to 1.
Definition: vector.h:631
vec & operator-=(const vec &rhs)
Subtracts a vector from this vector.
Definition: vector.h:1048
vec add(const vec &toAdd) const
Adds a vector to this vector.
Definition: vector.h:185
vec add(const vec &toAdd) const
Adds a vector to this vector.
Definition: vector.h:798
vec & operator-=(const vec &rhs)
Subtracts a vector from this vector.
Definition: vector.h:114
T r
Red (0-component).
Definition: vector.h:575
vec operator-() const
Negates the vector.
Definition: vector.h:1025