1// tVector3.h 
2// 
3// A 3D vector class with the expected member functions and overloads. Backs off of the tVec3 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 
20namespace tMath 
21
22 
23 
24struct tVector3 : public tVec3 
25
26 tVector3() { } 
27 tVector3(const tVec2& v, float z = 0.0f) { tSet(*this, v, z); } 
28 tVector3(const tVec3& v) { tSet(*this, v); } 
29 tVector3(const tVec4& v) /* Discards 4th component. */ { tSet(*this, v); } 
30 tVector3(float xyz) { tSet(*this, xyz); } 
31 tVector3(float x, float y, float z) { tSet(*this, x, y, z); } 
32 tVector3(const float* a) { tSet(*this, a); } 
33 const tVec3& Pod() const { return *this; } 
34 
35 void Set(const tVec2& v, float z = 0.0f) { tSet(*this, v, z); } 
36 void Set(const tVec3& v) { tSet(*this, v); } 
37 void Set(const tVec4& v) { tSet(*this, v); } 
38 void Set(float xyz) { tSet(*this, xyz); } 
39 void Set(float x, float y, float z) { tSet(*this, x, y, z); } 
40 void Set(const float* a) { tSet(*this, a); } 
41 void Get(float& x, float& y, float& z) const { tGet(x, y, z, *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 tVec3& v, float e = Epsilon) const { return tApproxEqual(*this, v, e); } 
49 bool ApproxEqual(const tVec3& 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 tVec3& a, const tVec3& b, float t) /* Also extrapolates. See tLerp comments. */ { tLerp(*this, a, b, t); } 
65 const tMat4 MulByTranspose(const tVec3& a) { tMat4 d; tMulByTranspose(d, *this, a); return d; } 
66 inline static int GetNumComponents() { return 3; } 
67 
68 // Friend is used for commutative binary operators where one of the operators is a built-in type. This essentially 
69 // puts these operators outside of the class where they belong. If the other operand type is user-defined, the 
70 // operator should be completely outside as there would be ambiguity as to which one owned the friend declaration. 
71 tVector3& operator=(const tVec2& v) { tSet(*this, v); return *this; } 
72 tVector3& operator=(const tVec3& v) { tSet(*this, v); return *this; } 
73 tVector3& operator=(const tVec4& v) { tSet(*this, v); return *this; } 
74 
75 // Addition is not defined for types other than the same type as the object itself, so no friend needed. 
76 tVector3& operator+=(const tVec3& a) { tAdd(*this, a); return *this; } 
77 const tVector3 operator+(const tVec3& a) const { tVector3 d; tAdd(d, *this, a); return d; } 
78 
79 tVector3& operator-=(const tVec3& a) { tSub(*this, a); return *this; } 
80 const tVector3 operator-(const tVec3& a) const { tVector3 d; tSub(d, *this, a); return d; } 
81 const tVector3 operator-() const { tVector3 d; tNeg(d, *this); return d; } 
82 
83 tVector3& operator*=(float a) { tMul(*this, a); return *this; } 
84 inline friend const tVector3 operator*(const tVec3& a, float b) { tVector3 d; tMul(d, a, b); return d; } 
85 inline friend const tVector3 operator*(float a, const tVec3& b) { tVector3 d; tMul(d, a, b); return d; } 
86 float operator*(const tVec3& a) const /* Dot (inner) product. Same as Transpose(this) * a. */ { return tDot(*this, a); } 
87 
88 // Divide is not commutative so no need for friend. 
89 tVector3& operator/=(float a) { tDiv(*this, a); return *this; } 
90 const tVector3 operator/(float a) const { tVector3 d; tDiv(d, *this, a); return d; } 
91 
92 tVector3& operator%=(const tVec3& v) /* Cross product for 3D vectors. */ { tCross(*this, *this, v); return *this; } 
93 tVector3 operator%(const tVec3& v) const /* Cross product for 3D vectors. */ { tVec3 c; tCross(c, *this, v); return c; } 
94 
95 bool operator==(const tVec3& a) const { return tEqual(*this, a); } 
96 bool operator!=(const tVec3& a) const { return tNotEqual(*this, a); } 
97 operator const float*() { return E; } 
98 operator const float*() const { return E; } 
99 float& operator[](int i) { return E[i]; } 
100 float operator[](int i) const { return E[i]; } 
101 
102 const static tVector3 zero; // Zero vector (0, 0, 0). 
103 const static tVector3 i; // Basis vector (1, 0, 0). 
104 const static tVector3 j; // Basis vector (0, 1, 0). 
105 const static tVector3 k; // Basis vector (0, 0, 1). 
106}; 
107 
108 
109
110