1 | // tVector4.h  |
2 | //  |
3 | // A 3D vector class with the expected member functions and overloads. Backs off of the tVec4 POD type and the  |
4 | // tLinearAlgebra library functions.  |
5 | //  |
6 | // Copyright (c) 2004-2006, 2015, 2017 Tristan Grimmer.  |
7 | // Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby  |
8 | // granted, provided that the above copyright notice and this permission notice appear in all copies.  |
9 | //  |
10 | // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL  |
11 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,  |
12 | // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN  |
13 | // AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR  |
14 | // PERFORMANCE OF THIS SOFTWARE.  |
15 |   |
16 | #pragma once  |
17 | #include "Math/tLinearAlgebra.h"  |
18 |   |
19 |   |
20 | namespace tMath  |
21 | {  |
22 |   |
23 |   |
24 | struct tVector4 : public tVec4  |
25 | {  |
26 | tVector4() { }  |
27 | tVector4(const tVec2& v, float z = 0.0f, float w = 0.0f) { tSet(*this, v, z, w); }  |
28 | tVector4(const tVec3& v) { tSet(*this, v); }  |
29 | tVector4(const tVec4& v) { tSet(*this, v); }  |
30 | tVector4(float xyzw) { tSet(*this, xyzw); }  |
31 | tVector4(float x, float y, float z, float w) { tSet(*this, x, y, z, w); }  |
32 | tVector4(const float* a) { tSet(*this, a); }  |
33 | const tVec4& Pod() const { return *this; }  |
34 |   |
35 | void Set(const tVec2& v, float z = 0.0f, float w = 0.0f) { tSet(*this, v, z, w); }  |
36 | void Set(const tVec3& v, float w = 0.0f) { tSet(*this, v, w); }  |
37 | void Set(const tVec4& v) { tSet(*this, v); }  |
38 | void Set(float xyzw) { tSet(*this, xyzw); }  |
39 | void Set(float x, float y, float z, float w) { tSet(*this, x, y, z, w); }  |
40 | void Set(const float* a) { tSet(*this, a); }  |
41 | void Get(float& x, float& y, float& z, float& w) const { tGet(x, y, z, w, *this); }  |
42 | void Get(float* a) const { tGet(a, *this); }  |
43 |   |
44 | void Zero() { tZero(*this); }  |
45 | void Zero(tComponents c) { tZero(*this, c); }  |
46 | bool IsZero() const { return tIsZero(*this); }  |
47 | bool IsZero(tComponents c) const { return tIsZero(*this, c); }  |
48 | bool ApproxEqual(const tVec4& v, float e = Epsilon) const { return tApproxEqual(*this, v, e); }  |
49 | bool ApproxEqual(const tVec4& v, tComponents c, float e = Epsilon) const { return tApproxEqual(*this, v, c, e); }  |
50 |   |
51 | float LengthSq() const { return tLengthSq(*this); }  |
52 | float Length() const { return tLength(*this); }  |
53 | float LengthFast() const { return tLengthFast(*this); }  |
54 |   |
55 | void Normalize() { tNormalize(*this); }  |
56 | float NormalizeGetLength() /* Returns pre-normalized length. */ { return tNormalizeGetLength(*this); }  |
57 | bool NormalizeSafe() /* Returns success. */ { return tNormalizeSafe(*this); }  |
58 | float NormalizeSafeGetLength() /* Returns pre-normalized length. 0.0f if no go. */ { return tNormalizeSafeGetLength(*this); }  |
59 | void NormalizeScale(float s) /* Normalize then scale. Resizes the vector. */ { tNormalizeScale(*this, s); }  |
60 | bool NormalizeScaleSafe(float s) { return tNormalizeScaleSafe(*this, s); }  |
61 | void NormalizeFast() { tNormalizeFast(*this); }  |
62 | bool NormalizeSafeFast() { return tNormalizeSafeFast(*this); }  |
63 |   |
64 | void Lerp(const tVec4& a, const tVec4& b, float t) /* Also extrapolates. See tLerp comments. */ { tLerp(*this, a, b, t); }  |
65 | const tMat4 MulByTranspose(const tVec4& a) { tMat4 d; tMulByTranspose(d, *this, a); return d; }  |
66 | tVec3 GetCartesian() const /* Assumes homogeneous representation. */ { tVec3 d; tSet(d, x/w, y/w, z/w); return d; }  |
67 | inline static int GetNumComponents() { return 4; }  |
68 |   |
69 | // Friend is used for commutative binary operators where one of the operators is a built-in type. This essentially  |
70 | // puts these operators outside of the class where they belong. If the other operand type is user-defined, the  |
71 | // operator should be completely outside as there would be ambiguity as to which one owned the friend declaration.  |
72 | tVector4& operator=(const tVec2& v) { tSet(*this, v); return *this; }  |
73 | tVector4& operator=(const tVec3& v) { tSet(*this, v); return *this; }  |
74 | tVector4& operator=(const tVec4& v) { tSet(*this, v); return *this; }  |
75 |   |
76 | // Addition is not defined for types other than the same type as the object itself, so no friend needed.  |
77 | tVector4& operator+=(const tVec4& a) { tAdd(*this, a); return *this; }  |
78 | const tVector4 operator+(const tVec4& a) const { tVector4 d; tAdd(d, *this, a); return d; }  |
79 |   |
80 | tVector4& operator-=(const tVec4& a) { tSub(*this, a); return *this; }  |
81 | const tVector4 operator-(const tVec4& a) const { tVector4 d; tSub(d, *this, a); return d; }  |
82 | const tVector4 operator-() const { tVector4 d; tNeg(d, *this); return d; }  |
83 |   |
84 | tVector4& operator*=(float a) { tMul(*this, a); return *this; }  |
85 | inline friend const tVector4 operator*(const tVec4& a, float b) { tVector4 d; tMul(d, a, b); return d; }  |
86 | inline friend const tVector4 operator*(float a, const tVec4& b) { tVector4 d; tMul(d, a, b); return d; }  |
87 | float operator*(const tVec4& a) const /* Dot (inner) product. Same as Transpose(this) * a. */ { return tDot(*this, a); }  |
88 |   |
89 | // Divide is not commutative so no need for friend.  |
90 | tVector4& operator/=(float a) { tDiv(*this, a); return *this; }  |
91 | const tVector4 operator/(float a) const { tVector4 d; tDiv(d, *this, a); return d; }  |
92 |   |
93 | tVector4& operator%=(const tVec4& v) /* Cross product for homogeneous coords. */ { tCross(*this, *this, v); return *this; }  |
94 | tVector4 operator%(const tVec4& v) const /* Cross product for homogeneous coords. */ { tVec4 c; tCross(c, *this, v); return c; }  |
95 |   |
96 | bool operator==(const tVec4& a) const { return tEqual(*this, a); }  |
97 | bool operator!=(const tVec4& a) const { return tNotEqual(*this, a); }  |
98 | operator const float*() { return E; }  |
99 | operator const float*() const { return E; }  |
100 | float& operator[](int i) { return E[i]; }  |
101 | float operator[](int i) const { return E[i]; }  |
102 |   |
103 | const static tVector4 zero; // Zero vector (0, 0, 0, 0).  |
104 | const static tVector4 i; // Basis vector (1, 0, 0, 1).  |
105 | const static tVector4 j; // Basis vector (0, 1, 0, 1).  |
106 | const static tVector4 k; // Basis vector (0, 0, 1, 1).  |
107 | const static tVector4 origin; // Basis vector (0, 0, 0, 1).  |
108 | };  |
109 |   |
110 |   |
111 | }  |
112 | |