23 |
namespace math |
namespace math |
24 |
{ |
{ |
25 |
|
|
26 |
|
//! \brief Homogeneous 3D vector |
27 |
|
//! \param T Component type. |
28 |
template <typename T> |
template <typename T> |
29 |
class Vector4 |
class Vector4 |
30 |
{ |
{ |
31 |
public: |
public: |
32 |
|
|
33 |
|
//! \brief Components |
34 |
T v[4]; |
T v[4]; |
35 |
|
|
36 |
|
//! \brief Default constructor |
37 |
|
//! |
38 |
|
//! Initializes all components to 0. |
39 |
Vector4() |
Vector4() |
40 |
{ |
{ |
41 |
v[0] = v[1] = v[2] = v[3] = 0; |
v[0] = v[1] = v[2] = v[3] = 0; |
42 |
} |
} |
43 |
|
|
44 |
|
//! \brief Component-wise constructor |
45 |
|
//! \param[in] x 1st component. |
46 |
|
//! \param[in] y 2nd component. |
47 |
|
//! \param[in] z 3rd component. |
48 |
|
//! \param[in] w 4th component. |
49 |
Vector4(T x, T y, T z, T w = 0) |
Vector4(T x, T y, T z, T w = 0) |
50 |
{ |
{ |
51 |
v[0] = x; |
v[0] = x; |
54 |
v[3] = w; |
v[3] = w; |
55 |
} |
} |
56 |
|
|
57 |
|
//! \brief Output stream operator |
58 |
friend std::ostream& |
friend std::ostream& |
59 |
operator << (std::ostream& out, const Vector4& vector) |
operator << (std::ostream& out, const Vector4& vector) |
60 |
{ |
{ |
64 |
<< ']'; |
<< ']'; |
65 |
} |
} |
66 |
|
|
67 |
|
//! \brief Equality operator |
68 |
|
//! \param[in] lhs Left operand. |
69 |
|
//! \param[in] rhs Right operand. |
70 |
friend bool |
friend bool |
71 |
operator == (const Vector4& lhs, const Vector4& rhs) |
operator == (const Vector4& lhs, const Vector4& rhs) |
72 |
{ |
{ |
74 |
lhs.v[2] == rhs.v[2] && lhs.v[3] == rhs.v[3]); |
lhs.v[2] == rhs.v[2] && lhs.v[3] == rhs.v[3]); |
75 |
} |
} |
76 |
|
|
77 |
|
//! \brief Inequality operator |
78 |
|
//! \param[in] lhs Left operand. |
79 |
|
//! \param[in] rhs Right operand. |
80 |
friend bool |
friend bool |
81 |
operator != (const Vector4& a, const Vector4& b) |
operator != (const Vector4& lhs, const Vector4& rhs) |
82 |
{ |
{ |
83 |
return !(a == b); |
return !(lhs == rhs); |
84 |
} |
} |
85 |
|
|
86 |
|
//! \brief Addition operator |
87 |
|
//! \param[in] lhs Left operand. |
88 |
|
//! \param[in] rhs Right operand. |
89 |
friend Vector4 |
friend Vector4 |
90 |
operator + (const Vector4& lhs, const Vector4& rhs) |
operator + (const Vector4& lhs, const Vector4& rhs) |
91 |
{ |
{ |
95 |
lhs.v[3] + rhs.v[3]); |
lhs.v[3] + rhs.v[3]); |
96 |
} |
} |
97 |
|
|
98 |
|
//! \brief Subtraction operator |
99 |
|
//! \param[in] lhs Left operand. |
100 |
|
//! \param[in] rhs Right operand. |
101 |
friend Vector4 |
friend Vector4 |
102 |
operator - (const Vector4& lhs, const Vector4& rhs) |
operator - (const Vector4& lhs, const Vector4& rhs) |
103 |
{ |
{ |
107 |
lhs.v[3] - rhs.v[3]); |
lhs.v[3] - rhs.v[3]); |
108 |
} |
} |
109 |
|
|
110 |
|
//! \brief Negation operator |
111 |
|
//! \param[in] operand Operand. |
112 |
|
friend Vector4 |
113 |
|
operator - (const Vector4& operand) |
114 |
|
{ |
115 |
|
return Vector4(-operand.v[0], |
116 |
|
-operand.v[1], |
117 |
|
-operand.v[2], |
118 |
|
-operand.v[3]); |
119 |
|
} |
120 |
|
|
121 |
|
//! \brief Scalar multiplication operator |
122 |
|
//! \param[in] lhs Left operand. |
123 |
|
//! \param[in] rhs Right operand. |
124 |
friend Vector4 |
friend Vector4 |
125 |
operator * (const Vector4& lhs, const T& rhs) |
operator * (const Vector4& lhs, const T& rhs) |
126 |
{ |
{ |
128 |
lhs.v[2] * rhs, lhs.v[3] * rhs); |
lhs.v[2] * rhs, lhs.v[3] * rhs); |
129 |
} |
} |
130 |
|
|
131 |
|
//! \brief Scalar multiplication operator |
132 |
|
//! \param[in] lhs Left operand. |
133 |
|
//! \param[in] rhs Right operand. |
134 |
|
friend Vector4 |
135 |
|
operator * (const T& lhs, Vector4& rhs) |
136 |
|
{ |
137 |
|
return rhs * lhs; |
138 |
|
} |
139 |
|
|
140 |
|
//! \brief Scalar division operator |
141 |
|
//! \param[in] lhs Left operand. |
142 |
|
//! \param[in] rhs Right operand. |
143 |
friend Vector4 |
friend Vector4 |
144 |
operator / (const Vector4& lhs, const T& rhs) |
operator / (const Vector4& lhs, const T& rhs) |
145 |
{ |
{ |
149 |
lhs.v[3] / rhs); |
lhs.v[3] / rhs); |
150 |
} |
} |
151 |
|
|
152 |
|
//! \brief Assignment-addition operator |
153 |
|
//! \param[in] rhs Right operand. |
154 |
Vector4& |
Vector4& |
155 |
operator += (const Vector4& rhs) |
operator += (const Vector4& rhs) |
156 |
{ |
{ |
161 |
return *this; |
return *this; |
162 |
} |
} |
163 |
|
|
164 |
|
//! \brief Assignment-subtraction operator |
165 |
|
//! \param[in] rhs Right operand. |
166 |
Vector4& |
Vector4& |
167 |
operator -= (const Vector4& rhs) |
operator -= (const Vector4& rhs) |
168 |
{ |
{ |
173 |
return *this; |
return *this; |
174 |
} |
} |
175 |
|
|
176 |
|
//! \brief Assignment-scalar multiplication operator |
177 |
|
//! \param[in] rhs Right operand. |
178 |
Vector4& |
Vector4& |
179 |
operator *= (const T& rhs) |
operator *= (const T& rhs) |
180 |
{ |
{ |
185 |
return *this; |
return *this; |
186 |
} |
} |
187 |
|
|
188 |
|
//! \brief Assignment-scalar division operator |
189 |
|
//! \param[in] rhs Right operand. |
190 |
Vector4& |
Vector4& |
191 |
operator /= (const T& rhs) |
operator /= (const T& rhs) |
192 |
{ |
{ |
200 |
// Perhaps these should be free functions |
// Perhaps these should be free functions |
201 |
// - descender |
// - descender |
202 |
|
|
203 |
|
//! \brief Returns dot product |
204 |
|
//! \param[in] rhs Right operand. |
205 |
T |
T |
206 |
dot(const Vector4& rhs) const |
dot(const Vector4& rhs) const |
207 |
{ |
{ |
211 |
v[3] * rhs.v[3]); |
v[3] * rhs.v[3]); |
212 |
} |
} |
213 |
|
|
214 |
|
//! \brief Returns cross product |
215 |
|
//! \param[in] rhs Right operand. |
216 |
T |
T |
217 |
cross(const Vector4& rhs) |
cross(const Vector4& rhs) |
218 |
{ |
{ |
221 |
v[0] * rhs.v[1] - v[1] * rhs.v[0]); |
v[0] * rhs.v[1] - v[1] * rhs.v[0]); |
222 |
} |
} |
223 |
|
|
224 |
|
//! \brief Returns unit vector |
225 |
Vector4 |
Vector4 |
226 |
unit() const |
unit() const |
227 |
{ |
{ |
233 |
v[3] / length_); |
v[3] / length_); |
234 |
} |
} |
235 |
|
|
236 |
|
//! \brief Returns magnitude |
237 |
T |
T |
238 |
length() const |
length() const |
239 |
{ |
{ |
240 |
return std::sqrt(dot(*this)); |
return std::sqrt(dot(*this)); |
241 |
} |
} |
242 |
|
|
243 |
|
//! \brief Normalizes vector |
244 |
void |
void |
245 |
normalize() |
normalize() |
246 |
{ |
{ |