-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVector.h
More file actions
166 lines (141 loc) · 4.66 KB
/
Vector.h
File metadata and controls
166 lines (141 loc) · 4.66 KB
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
#ifndef INCLUDED_MATH_VECTOR
#define INCLUDED_MATH_VECTOR
#include "Triple.h"
namespace Dubious {
namespace Math {
template <int T>
class VectorT;
template <int T>
class Unit_vectorT;
template <int T>
bool operator==(const VectorT<T>& a, const VectorT<T>& b);
template <int T>
bool operator!=(const VectorT<T>& a, const VectorT<T>& b);
template <int T>
VectorT<T> operator+(const VectorT<T>& a, const VectorT<T>& b);
template <int T>
VectorT<T> operator-(const VectorT<T>& a, const VectorT<T>& b);
template <int T>
VectorT<T> operator*(const VectorT<T>& a, float b);
template <int T>
VectorT<T> operator*(float a, const VectorT<T>& b);
template <int T>
VectorT<T> operator/(const VectorT<T>& a, float b);
template <int T>
std::ostream& operator<<(std::ostream& o, const VectorT<T>& a);
/// @brief A 3D Vector
///
/// This class represents a 3D vector. This is for non-unit vectors.
/// The template parameter is only so the compiler can enforce
/// different meanings for Vectors (ie local versus global)
template <int T>
class VectorT {
public:
/// @brief Default Constructor
///
/// Creates a Vector of 0 length
VectorT() = default;
/// @brief Constructor
///
/// Creates the vector with the given X, Y, and Z
/// @param x - [in] X component
/// @param y - [in] Y component
/// @param z - [in] Z component
VectorT(float x, float y, float z) : m_coords(x, y, z) {}
/// @brief Construct from Unit Vector
///
/// A simple construction from a Unit Vector. Note that
/// the template parameter must match, so Local Vectors
/// can be constructed from Local Unit Vectors, but not
/// Global Unit Vectors
/// @param u - [in] Unit Vector to copy
VectorT(const Unit_vectorT<T>& u);
/// @brief Length Squared
///
/// Often we don't need the actual length, but we may need to
/// compare the relative lengths of 2 vectors. The length
/// squared is a good way to do that as it doesn't require
/// the costly sqrt function
/// @returns The length of the vector squared
float length_squared() const;
/// @brief Length
///
/// The length of the vector
/// @returns The Vector length
float length() const;
/// @brief X accessor
/// @returns X coordinate
float x() const { return m_coords.m_x; }
/// @brief Y accessor
/// @returns Y coordinate
float y() const { return m_coords.m_y; }
/// @brief Z accessor
/// @returns Z coordinate
float z() const { return m_coords.m_z; }
friend bool operator==<>(const VectorT<T>& a, const VectorT<T>& b);
friend bool operator!=<>(const VectorT<T>& a, const VectorT<T>& b);
friend VectorT<T> operator+<>(const VectorT<T>& a, const VectorT<T>& b);
friend VectorT<T> operator-<>(const VectorT<T>& a, const VectorT<T>& b);
friend VectorT<T> operator*<>(const VectorT<T>& a, float b);
friend VectorT<T> operator*<>(float a, const VectorT<T>& b);
friend VectorT<T> operator/<>(const VectorT<T>& a, float b);
friend std::ostream& operator<<<>(std::ostream& o, const VectorT<T>& a);
VectorT<T> operator-() const { return VectorT<T>(-m_coords); }
VectorT<T>& operator+=(const VectorT<T>& rhs);
VectorT<T>& operator-=(const VectorT<T>& rhs);
private:
VectorT(const Triple& coords) : m_coords(coords) {}
Triple m_coords;
};
template <int T>
bool
operator==(const VectorT<T>& a, const VectorT<T>& b)
{
return a.m_coords == b.m_coords;
}
template <int T>
bool
operator!=(const VectorT<T>& a, const VectorT<T>& b)
{
return a.m_coords != b.m_coords;
}
template <int T>
VectorT<T>
operator+(const VectorT<T>& a, const VectorT<T>& b)
{
return VectorT<T>(a.m_coords + b.m_coords);
}
template <int T>
VectorT<T>
operator-(const VectorT<T>& a, const VectorT<T>& b)
{
return VectorT<T>(a.m_coords - b.m_coords);
}
template <int T>
VectorT<T> operator*(const VectorT<T>& a, float b)
{
return VectorT<T>(a.m_coords.m_x * b, a.m_coords.m_y * b, a.m_coords.m_z * b);
}
template <int T>
VectorT<T> operator*(float a, const VectorT<T>& b)
{
return VectorT<T>(a * b.m_coords.m_x, a * b.m_coords.m_y, a * b.m_coords.m_z);
}
template <int T>
VectorT<T>
operator/(const VectorT<T>& a, float b)
{
return VectorT<T>(a.m_coords.m_x / b, a.m_coords.m_y / b, a.m_coords.m_z / b);
}
template <int T>
std::ostream&
operator<<(std::ostream& o, const VectorT<T>& a)
{
o << a.m_coords;
return o;
}
typedef VectorT<0> Vector;
typedef VectorT<1> Local_vector;
} // namespace Math
} // namespace Dubious
#endif