vec3.h
1 /*
2 ** UICore
3 ** Copyright (c) 1997-2015 The UICore Team
4 **
5 ** This software is provided 'as-is', without any express or implied
6 ** warranty. In no event will the authors be held liable for any damages
7 ** arising from the use of this software.
8 **
9 ** Permission is granted to anyone to use this software for any purpose,
10 ** including commercial applications, and to alter it and redistribute it
11 ** freely, subject to the following restrictions:
12 **
13 ** 1. The origin of this software must not be misrepresented; you must not
14 ** claim that you wrote the original software. If you use this software
15 ** in a product, an acknowledgment in the product documentation would be
16 ** appreciated but is not required.
17 ** 2. Altered source versions must be plainly marked as such, and must not be
18 ** misrepresented as being the original software.
19 ** 3. This notice may not be removed or altered from any source distribution.
20 **
21 ** Note: Some of the libraries UICore may link to may have additional
22 ** requirements or restrictions.
23 **
24 ** File Author(s):
25 **
26 ** Magnus Norddahl
27 ** Mark Page
28 ** Harry Storbacka
29 */
30 
31 #pragma once
32 
33 #include <cmath>
34 #include "vec2.h"
35 #include "vec4.h"
36 
37 namespace uicore
38 {
39  template<typename Type>
40  class Vec2;
41 
42  template<typename Type>
43  class Vec3;
44 
45  template<typename Type>
46  class Vec4;
47 
48  template<typename Type>
49  class Mat2;
50 
51  template<typename Type>
52  class Mat3;
53 
54  template<typename Type>
55  class Mat4;
56 
57  template<typename Type>
58  class Sizex;
59 
60  template<typename Type>
61  class Pointx;
62 
68  template<typename Type>
69  class Vec3
70  {
71  public:
72  typedef Type datatype;
73 
74  Type x;
75  Type y;
76  Type z;
77 
78  Vec3() : x(0), y(0), z(0) { }
79  explicit Vec3(const Type &scalar) : x(scalar), y(scalar), z(scalar) { }
80  explicit Vec3(const Vec2<Type> &copy, const Type &p3) { x = copy.x; y = copy.y; z = p3; }
81  explicit Vec3(const Vec4<Type> &copy) { x = copy.x; y = copy.y; z = copy.z; }
82 
83  Vec3(const Vec3<double> &copy);
84  Vec3(const Vec3<float> &copy);
85  Vec3(const Vec3<int> &copy);
86 
87  explicit Vec3(const Type &p1, const Type &p2, const Type &p3) : x(p1), y(p2), z(p3) { }
88  explicit Vec3(const Type *array_xyz) : x(array_xyz[0]), y(array_xyz[1]), z(array_xyz[2]) { }
89 
95  static Vec3<Type> normalize(const Vec3<Type>& vector);
96 
100  static Type dot(const Vec3<Type>& vector1, const Vec3<Type>& vector2) { return vector1.x*vector2.x + vector1.y*vector2.y + vector1.z*vector2.z; }
101 
107  static Vec3<Type> cross(const Vec3<Type>& vector1, const Vec3<Type>& vector2);
108 
115  static Vec3<Type> rotate(const Vec3<Type>& vector, Type angle, const Vec3<Type>& axis);
116 
122  static Vec3<Type> round(const Vec3<Type>& vector);
123 
127  static Vec3<Type> reflect(const Vec3<Type>& incident, const Vec3<Type>& normal);
128 
134  static bool is_equal(const Vec3<Type> &first, const Vec3<Type> &second, Type epsilon)
135  {
136  Type diff_x = second.x - first.x; Type diff_y = second.y - first.y; Type diff_z = second.z - first.z;
137  return (diff_x >= -epsilon && diff_x <= epsilon && diff_y >= -epsilon && diff_y <= epsilon && diff_z >= -epsilon && diff_z <= epsilon);
138  }
139 
144  Type length() const;
145 
151 
158  Type dot(const Vec3<Type>& vector) const { return x*vector.x + y*vector.y + z*vector.z; }
159 
165  Type angle(const Vec3<Type>& vector) const;
166 
172  Type angle_normed(const Vec3<Type>& vector) const;
173 
179  Type distance(const Vec3<Type>& vector) const;
180 
186  Vec3<Type> &cross(const Vec3<Type>& vector);
187 
193  Vec3<Type> &rotate(Type angle, const Vec3<Type>& axis);
194 
199  Vec3<Type> &round();
200 
205  bool is_equal(const Vec3<Type> &other, Type epsilon) const { return Vec3<Type>::is_equal(*this, other, epsilon); }
206 
208  void operator += (const Vec3<Type>& vector) { x += vector.x; y += vector.y; z += vector.z; }
209 
211  void operator += (Type value) { x += value; y += value; z += value; }
212 
214  void operator -= (const Vec3<Type>& vector) { x -= vector.x; y -= vector.y; z -= vector.z; }
215 
217  void operator -= (Type value) { x -= value; y -= value; z -= value; }
218 
220  Vec3<Type> operator - () const { return Vec3<Type>(-x, -y, -z); }
221 
223  void operator *= (const Vec3<Type>& vector) { x *= vector.x; y *= vector.y; z *= vector.z; }
224 
226  void operator *= (Type value) { x *= value; y *= value; z *= value; }
227 
229  void operator /= (const Vec3<Type>& vector) { x /= vector.x; y /= vector.y; z /= vector.z; }
230 
232  void operator /= (Type value) { x /= value; y /= value; z /= value; }
233 
235  Vec3<Type> &operator = (const Vec3<Type>& vector) { x = vector.x; y = vector.y; z = vector.z; return *this; }
236 
238  bool operator == (const Vec3<Type>& vector) const { return ((x == vector.x) && (y == vector.y) && (z == vector.z)); }
239 
241  bool operator != (const Vec3<Type>& vector) const { return ((x != vector.x) || (y != vector.y) || (z != vector.z)); }
242 
244  bool operator < (const Vec3<Type>& vector) const { return z < vector.z || (z == vector.z && (y < vector.y || (y == vector.y && x < vector.x))); }
245  };
246 
248  template<typename Type>
249  Vec3<Type> operator + (const Vec3<Type>& v1, const Vec3<Type>& v2) { return Vec3<Type>(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z); }
250 
252  template<typename Type>
253  Vec3<Type> operator + (Type s, const Vec3<Type>& v) { return Vec3<Type>(s + v.x, s + v.y, s + v.z); }
254 
256  template<typename Type>
257  Vec3<Type> operator + (const Vec3<Type>& v, Type s) { return Vec3<Type>(v.x + s, v.y + s, v.z + s); }
258 
260  template<typename Type>
261  Vec3<Type> operator - (const Vec3<Type>& v1, const Vec3<Type>& v2) { return Vec3<Type>(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z); }
262 
264  template<typename Type>
265  Vec3<Type> operator - (Type s, const Vec3<Type>& v) { return Vec3<Type>(s - v.x, s - v.y, s - v.z); }
266 
268  template<typename Type>
269  Vec3<Type> operator - (const Vec3<Type>& v, Type s) { return Vec3<Type>(v.x - s, v.y - s, v.z - s); }
270 
272  template<typename Type>
273  Vec3<Type> operator * (const Vec3<Type>& v1, const Vec3<Type>& v2) { return Vec3<Type>(v1.x * v2.x, v1.y * v2.y, v1.z * v2.z); }
274 
276  template<typename Type>
277  Vec3<Type> operator * (Type s, const Vec3<Type>& v) { return Vec3<Type>(s * v.x, s * v.y, s * v.z); }
278 
280  template<typename Type>
281  Vec3<Type> operator * (const Vec3<Type>& v, Type s) { return Vec3<Type>(v.x * s, v.y * s, v.z * s); }
282 
284  template<typename Type>
285  Vec3<Type> operator / (const Vec3<Type>& v1, const Vec3<Type>& v2) { return Vec3<Type>(v1.x / v2.x, v1.y / v2.y, v1.z / v2.z); }
286 
288  template<typename Type>
289  Vec3<Type> operator / (Type s, const Vec3<Type>& v) { return Vec3<Type>(s / v.x, s / v.y, s / v.z); }
290 
292  template<typename Type>
293  Vec3<Type> operator / (const Vec3<Type>& v, Type s) { return Vec3<Type>(v.x / s, v.y / s, v.z / s); }
294 
297  template<typename Type>
298  Vec3<Type> operator * (const Vec3<Type>& v, const Mat3<Type>& matrix)
299  {
300  return Vec3<Type>(
301  matrix[0 * 3 + 0] * v.x + matrix[0 * 3 + 1] * v.y + matrix[0 * 3 + 2] * v.z,
302  matrix[1 * 3 + 0] * v.x + matrix[1 * 3 + 1] * v.y + matrix[1 * 3 + 2] * v.z,
303  matrix[2 * 3 + 0] * v.x + matrix[2 * 3 + 1] * v.y + matrix[2 * 3 + 2] * v.z);
304  }
305 
308  template<typename Type>
309  Vec3<Type> operator * (const Mat3<Type>& matrix, const Vec3<Type>& v)
310  {
311  return Vec3<Type>(
312  matrix[0 * 3 + 0] * v.x + matrix[1 * 3 + 0] * v.y + matrix[2 * 3 + 0] * v.z,
313  matrix[0 * 3 + 1] * v.x + matrix[1 * 3 + 1] * v.y + matrix[2 * 3 + 1] * v.z,
314  matrix[0 * 3 + 2] * v.x + matrix[1 * 3 + 2] * v.y + matrix[2 * 3 + 2] * v.z);
315  }
316 
317  template<>
318  inline Vec3<unsigned char>::Vec3(const Vec3<float> &copy) { x = (unsigned char)floor(copy.x + 0.5f); y = (unsigned char)floor(copy.y + 0.5f); z = (unsigned char)floor(copy.z + 0.5f); }
319 
320  template<>
321  inline Vec3<unsigned char>::Vec3(const Vec3<double> &copy) { x = (unsigned char)floor(copy.x + 0.5); y = (unsigned char)floor(copy.y + 0.5); z = (unsigned char)floor(copy.z + 0.5); }
322 
323  template<>
324  inline Vec3<unsigned char>::Vec3(const Vec3<int> &copy) { x = (unsigned char)copy.x; y = (unsigned char)copy.y; z = (unsigned char)copy.z; }
325 
326  template<>
327  inline Vec3<char>::Vec3(const Vec3<float> &copy) { x = (char)floor(copy.x + 0.5f); y = (char)floor(copy.y + 0.5f); z = (char)floor(copy.z + 0.5f); }
328 
329  template<>
330  inline Vec3<char>::Vec3(const Vec3<double> &copy) { x = (char)floor(copy.x + 0.5); y = (char)floor(copy.y + 0.5); z = (char)floor(copy.z + 0.5); }
331 
332  template<>
333  inline Vec3<char>::Vec3(const Vec3<int> &copy) { x = (char)copy.x; y = (char)copy.y; z = (char)copy.z; }
334 
335  template<>
336  inline Vec3<unsigned short>::Vec3(const Vec3<float> &copy) { x = (unsigned short)floor(copy.x + 0.5f); y = (unsigned short)floor(copy.y + 0.5f); z = (unsigned short)floor(copy.z + 0.5f); }
337 
338  template<>
339  inline Vec3<unsigned short>::Vec3(const Vec3<double> &copy) { x = (unsigned short)floor(copy.x + 0.5); y = (unsigned short)floor(copy.y + 0.5); z = (unsigned short)floor(copy.z + 0.5); }
340 
341  template<>
342  inline Vec3<unsigned short>::Vec3(const Vec3<int> &copy) { x = (unsigned short)copy.x; y = (unsigned short)copy.y; z = (unsigned short)copy.z; }
343 
344  template<>
345  inline Vec3<short>::Vec3(const Vec3<float> &copy) { x = (short)floor(copy.x + 0.5f); y = (short)floor(copy.y + 0.5f); z = (short)floor(copy.z + 0.5f); }
346 
347  template<>
348  inline Vec3<short>::Vec3(const Vec3<double> &copy) { x = (short)floor(copy.x + 0.5); y = (short)floor(copy.y + 0.5); z = (short)floor(copy.z + 0.5); }
349 
350  template<>
351  inline Vec3<short>::Vec3(const Vec3<int> &copy) { x = (short)copy.x; y = (short)copy.y; z = (short)copy.z; }
352 
353  template<>
354  inline Vec3<int>::Vec3(const Vec3<float> &copy) { x = (int)floor(copy.x + 0.5f); y = (int)floor(copy.y + 0.5f); z = (int)floor(copy.z + 0.5f); }
355 
356  template<>
357  inline Vec3<int>::Vec3(const Vec3<double> &copy) { x = (int)floor(copy.x + 0.5); y = (int)floor(copy.y + 0.5); z = (int)floor(copy.z + 0.5); }
358 
359  template<>
360  inline Vec3<int>::Vec3(const Vec3<int> &copy) { x = (int)copy.x; y = (int)copy.y; z = (int)copy.z; }
361 
362  template<>
363  inline Vec3<unsigned int>::Vec3(const Vec3<float> &copy) { x = (unsigned int)floor(copy.x + 0.5f); y = (unsigned int)floor(copy.y + 0.5f); z = (unsigned int)floor(copy.z + 0.5f); }
364 
365  template<>
366  inline Vec3<unsigned int>::Vec3(const Vec3<double> &copy) { x = (unsigned int)floor(copy.x + 0.5); y = (unsigned int)floor(copy.y + 0.5); z = (unsigned int)floor(copy.z + 0.5); }
367 
368  template<>
369  inline Vec3<unsigned int>::Vec3(const Vec3<int> &copy) { x = (unsigned int)copy.x; y = (unsigned int)copy.y; z = (unsigned int)copy.z; }
370 
371  template<>
372  inline Vec3<float>::Vec3(const Vec3<float> &copy) { x = (float)copy.x; y = (float)copy.y; z = (float)copy.z; }
373 
374  template<>
375  inline Vec3<float>::Vec3(const Vec3<double> &copy) { x = (float)copy.x; y = (float)copy.y; z = (float)copy.z; }
376 
377  template<>
378  inline Vec3<float>::Vec3(const Vec3<int> &copy) { x = (float)copy.x; y = (float)copy.y; z = (float)copy.z; }
379 
380  template<>
381  inline Vec3<double>::Vec3(const Vec3<float> &copy) { x = (double)copy.x; y = (double)copy.y; z = (double)copy.z; }
382 
383  template<>
384  inline Vec3<double>::Vec3(const Vec3<double> &copy) { x = (double)copy.x; y = (double)copy.y; z = (double)copy.z; }
385 
386  template<>
387  inline Vec3<double>::Vec3(const Vec3<int> &copy) { x = (double)copy.x; y = (double)copy.y; z = (double)copy.z; }
388 
389  template<typename Type>
390  inline Type Vec3<Type>::length() const { return (Type)floor(sqrt(float(x*x + y*y + z*z)) + 0.5f); }
391 
392  template<>
393  inline double Vec3<double>::length() const { return sqrt(x*x + y*y + z*z); }
394 
395  template<>
396  inline float Vec3<float>::length() const { return sqrt(x*x + y*y + z*z); }
397 
398  template<typename Type>
399  inline Vec3<Type> &Vec3<Type>::normalize() { Type f = length(); if (f != 0) { x /= f; y /= f; z /= f; } return *this; }
400 
401  template<typename Type>
402  inline Vec3<Type> Vec3<Type>::normalize(const Vec3<Type>& vector) { Vec3<Type> dest(vector); dest.normalize(); return dest; }
403 
405  typedef Vec3<char> Vec3b;
409  typedef Vec3<int> Vec3i;
412 }
Vec3< Type > & operator=(const Vec3< Type > &vector)
= operator.
Definition: vec3.h:235
Type dot(const Vec3< Type > &vector) const
Dot products this vector with an other vector.
Definition: vec3.h:158
Vec3()
Definition: vec3.h:78
Vec3(const Vec2< Type > &copy, const Type &p3)
Definition: vec3.h:80
Type distance(const Vec3< Type > &vector) const
Calculate the distance between this vector and an other vector.
2D vector
Definition: line.h:43
Vec3(const Type &scalar)
Definition: vec3.h:79
Type x
Definition: vec3.h:74
Vec3< Type > & normalize()
Normalizes this vector.
Definition: vec3.h:399
Type angle_normed(const Vec3< Type > &vector) const
Calculate the angle between this vector and an other vector, where the vectors are unit vectors...
bool operator==(const Vec3< Type > &vector) const
== operator.
Definition: vec3.h:238
3D matrix
Definition: mat2.h:44
Vec3(const Vec4< Type > &copy)
Definition: vec3.h:81
Type x
Definition: vec4.h:74
Type x
Definition: vec2.h:75
Type datatype
Definition: vec3.h:72
Vec2< Type > operator-(const Vec2< Type > &v1, const Vec2< Type > &v2)
operator.
Definition: vec2.h:272
static bool is_equal(const Vec3< Type > &first, const Vec3< Type > &second, Type epsilon)
Returns true if equal within the bounds of an epsilon.
Definition: vec3.h:134
Type length() const
Returns the length (magnitude) of this vector.
Definition: vec3.h:390
static Vec3< Type > rotate(const Vec3< Type > &vector, Type angle, const Vec3< Type > &axis)
Rotate a vector around an axis. Same as glRotate[f|d](angle, a);.
Vec2< Type > operator+(const Vec2< Type > &v1, const Vec2< Type > &v2)
operator.
Definition: vec2.h:260
Type z
Definition: vec3.h:76
static Type dot(const Vec3< Type > &vector1, const Vec3< Type > &vector2)
Dot products between two vectors.
Definition: vec3.h:100
Type y
Definition: vec2.h:76
bool operator!=(const Vec3< Type > &vector) const
!= operator.
Definition: vec3.h:241
Vec3< Type > operator-() const
operator.
Definition: vec3.h:220
Type y
Definition: vec4.h:75
bool is_equal(const Vec3< Type > &other, Type epsilon) const
Returns true if equal within the bounds of an epsilon.
Definition: vec3.h:205
Vec2< Type > operator/(const Vec2< Type > &v1, const Vec2< Type > &v2)
/ operator.
Definition: vec2.h:296
Vec3(const Type &p1, const Type &p2, const Type &p3)
Definition: vec3.h:87
static Vec3< Type > reflect(const Vec3< Type > &incident, const Vec3< Type > &normal)
Calculate the reflection direction for an incident vector.
static Vec3< Type > cross(const Vec3< Type > &vector1, const Vec3< Type > &vector2)
Calculate the cross product between two vectors.
Vec2< Type > operator*(const Vec2< Type > &v1, const Vec2< Type > &v2)
operator.
Definition: vec2.h:284
Type y
Definition: vec3.h:75
Vec3< Type > & round()
Rounds all components on this vector.
Type z
Definition: vec4.h:76
void operator-=(const Vec3< Type > &vector)
-= operator.
Definition: vec3.h:214
Vec3(const Type *array_xyz)
Definition: vec3.h:88
void operator/=(const Vec3< Type > &vector)
/= operator.
Definition: vec3.h:229
void operator+=(const Vec3< Type > &vector)
+= operator.
Definition: vec3.h:208
void operator*=(const Vec3< Type > &vector)
*= operator.
Definition: vec3.h:223
Definition: Application/application.h:35
4D vector
Definition: size.h:44
Type angle(const Vec3< Type > &vector) const
Calculate the angle between this vector and an other vector.