vec3.h
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2020 The ClanLib 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 ClanLib 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 clan
38 {
41 
42  template<typename Type>
43  class Vec2;
44 
45  template<typename Type>
46  class Vec3;
47 
48  template<typename Type>
49  class Vec4;
50 
51  template<typename Type>
52  class Mat2;
53 
54  template<typename Type>
55  class Mat3;
56 
57  template<typename Type>
58  class Mat4;
59 
60  template<typename Type>
61  class Sizex;
62 
63  template<typename Type>
64  class Pointx;
65 
66  class Angle;
67 
73  template<typename Type>
74  class Vec3
75  {
76  public:
77  typedef Type datatype;
78 
79  union { Type x; Type s; Type r; };
80  union { Type y; Type t; Type g; };
81  union { Type z; Type u; Type b; };
82 
83  Vec3() : x(0), y(0), z(0) {}
84  explicit Vec3(const Type &scalar) : x(scalar), y(scalar), z(scalar) {}
85  explicit Vec3(const Vec2<Type> &copy, const Type &p3) : x(copy.x), y(copy.y), z(p3) {}
86  explicit Vec3(const Vec4<Type> &copy): x(copy.x), y(copy.y), z(copy.z) {}
87 
88  Vec3(const Vec3<Type> &copy) = default;
89 
90  template<typename OtherType, typename std::enable_if_t<std::is_integral<Type>::value && !std::is_integral<OtherType>::value, int> = 0>
91  Vec3(const Vec3<OtherType>& copy) : x(static_cast<Type>(std::floor(copy.x + 0.5f))), y(static_cast<Type>(std::floor(copy.y + 0.5f))), z(static_cast<Type>(std::floor(copy.z + 0.5f))) {}
92 
93  template<typename OtherType, typename std::enable_if_t<!std::is_integral<Type>::value || std::is_integral<OtherType>::value, int> = 0>
94  Vec3(const Vec3<OtherType>& copy) : x(static_cast<Type>(copy.x)), y(static_cast<Type>(copy.y)), z(static_cast<Type>(copy.z)) {}
95 
96  explicit Vec3(const Type &p1, const Type &p2, const Type &p3) : x(p1), y(p2), z(p3) {}
97  explicit Vec3(const Type *array_xyz) : x(array_xyz[0]), y(array_xyz[1]), z(array_xyz[2]) {}
98 
104  static Vec3<Type> normalize(const Vec3<Type>& vector);
105 
109  static Type dot(const Vec3<Type>& vector1, const Vec3<Type>& vector2) { return vector1.x*vector2.x + vector1.y*vector2.y + vector1.z*vector2.z; }
110 
116  static Vec3<Type> cross(const Vec3<Type>& vector1, const Vec3<Type>& vector2);
117 
124  static Vec3<Type> rotate(const Vec3<Type>& vector, const Angle &angle, const Vec3<Type>& axis);
125 
131  static Vec3<Type> round(const Vec3<Type>& vector);
132 
136  static Vec3<Type> reflect(const Vec3<Type>& incident, const Vec3<Type>& normal);
137 
143  static bool is_equal(const Vec3<Type> &first, const Vec3<Type> &second, Type epsilon)
144  {
145  Type diff_x = second.x - first.x; Type diff_y = second.y - first.y; Type diff_z = second.z - first.z;
146  return (diff_x >= -epsilon && diff_x <= epsilon && diff_y >= -epsilon && diff_y <= epsilon && diff_z >= -epsilon && diff_z <= epsilon);
147  }
148 
153  Type length() const;
154 
160 
167  Type dot(const Vec3<Type>& vector) const { return x*vector.x + y*vector.y + z*vector.z; }
168 
174  Angle angle(const Vec3<Type>& vector) const;
175 
181  Angle angle_normed(const Vec3<Type>& vector) const;
182 
188  Type distance(const Vec3<Type>& vector) const;
189 
195  Vec3<Type> &cross(const Vec3<Type>& vector);
196 
202  Vec3<Type> &rotate(const Angle &angle, const Vec3<Type>& axis);
203 
208  Vec3<Type> &round();
209 
214  bool is_equal(const Vec3<Type> &other, Type epsilon) const { return Vec3<Type>::is_equal(*this, other, epsilon); }
215 
217  void operator += (const Vec3<Type>& vector) { x += vector.x; y += vector.y; z += vector.z; }
218 
220  void operator += (Type value) { x += value; y += value; z += value; }
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  Vec3<Type> operator - () const { return Vec3<Type>(-x, -y, -z); }
230 
232  void operator *= (const Vec3<Type>& vector) { x *= vector.x; y *= vector.y; z *= vector.z; }
233 
235  void operator *= (Type value) { x *= value; y *= value; z *= value; }
236 
238  void operator /= (const Vec3<Type>& vector) { x /= vector.x; y /= vector.y; z /= vector.z; }
239 
241  void operator /= (Type value) { x /= value; y /= value; z /= value; }
242 
244  Vec3<Type> &operator = (const Vec3<Type>& vector) = default;
245 
247  bool operator == (const Vec3<Type>& vector) const { return ((x == vector.x) && (y == vector.y) && (z == vector.z)); }
248 
250  bool operator != (const Vec3<Type>& vector) const { return ((x != vector.x) || (y != vector.y) || (z != vector.z)); }
251 
253  bool operator < (const Vec3<Type>& vector) const { return z < vector.z || (z == vector.z && (y < vector.y || (y == vector.y && x < vector.x))); }
254  };
255 
257  template<typename Type>
258  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); }
259 
261  template<typename Type>
262  Vec3<Type> operator + (Type s, const Vec3<Type>& v) { return Vec3<Type>(s + v.x, s + v.y, s + v.z); }
263 
265  template<typename Type>
266  Vec3<Type> operator + (const Vec3<Type>& v, Type s) { return Vec3<Type>(v.x + s, v.y + s, v.z + s); }
267 
269  template<typename Type>
270  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); }
271 
273  template<typename Type>
274  Vec3<Type> operator - (Type s, const Vec3<Type>& v) { return Vec3<Type>(s - v.x, s - v.y, s - v.z); }
275 
277  template<typename Type>
278  Vec3<Type> operator - (const Vec3<Type>& v, Type s) { return Vec3<Type>(v.x - s, v.y - s, v.z - s); }
279 
281  template<typename Type>
282  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); }
283 
285  template<typename Type>
286  Vec3<Type> operator * (Type s, const Vec3<Type>& v) { return Vec3<Type>(s * v.x, s * v.y, s * v.z); }
287 
289  template<typename Type>
290  Vec3<Type> operator * (const Vec3<Type>& v, Type s) { return Vec3<Type>(v.x * s, v.y * s, v.z * s); }
291 
293  template<typename Type>
294  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); }
295 
297  template<typename Type>
298  Vec3<Type> operator / (Type s, const Vec3<Type>& v) { return Vec3<Type>(s / v.x, s / v.y, s / v.z); }
299 
301  template<typename Type>
302  Vec3<Type> operator / (const Vec3<Type>& v, Type s) { return Vec3<Type>(v.x / s, v.y / s, v.z / s); }
303 
306  template<typename Type>
307  Vec3<Type> operator * (const Vec3<Type>& v, const Mat3<Type>& matrix)
308  {
309  return Vec3<Type>(
310  matrix[0 * 3 + 0] * v.x + matrix[0 * 3 + 1] * v.y + matrix[0 * 3 + 2] * v.z,
311  matrix[1 * 3 + 0] * v.x + matrix[1 * 3 + 1] * v.y + matrix[1 * 3 + 2] * v.z,
312  matrix[2 * 3 + 0] * v.x + matrix[2 * 3 + 1] * v.y + matrix[2 * 3 + 2] * v.z);
313  }
314 
317  template<typename Type>
318  Vec3<Type> operator * (const Mat3<Type>& matrix, const Vec3<Type>& v)
319  {
320  return Vec3<Type>(
321  matrix[0 * 3 + 0] * v.x + matrix[1 * 3 + 0] * v.y + matrix[2 * 3 + 0] * v.z,
322  matrix[0 * 3 + 1] * v.x + matrix[1 * 3 + 1] * v.y + matrix[2 * 3 + 1] * v.z,
323  matrix[0 * 3 + 2] * v.x + matrix[1 * 3 + 2] * v.y + matrix[2 * 3 + 2] * v.z);
324  }
325 
326  template<typename Type>
327  inline Type Vec3<Type>::length() const { return (Type)floor(sqrt(float(x*x + y*y + z*z)) + 0.5f); }
328 
329  template<>
330  inline double Vec3<double>::length() const { return sqrt(x*x + y*y + z*z); }
331 
332  template<>
333  inline float Vec3<float>::length() const { return sqrt(x*x + y*y + z*z); }
334 
335  template<typename Type>
336  inline Vec3<Type> &Vec3<Type>::normalize() { Type f = length(); if (f != 0) { x /= f; y /= f; z /= f; } return *this; }
337 
338  template<typename Type>
339  inline Vec3<Type> Vec3<Type>::normalize(const Vec3<Type>& vector) { Vec3<Type> dest(vector); dest.normalize(); return dest; }
340 
342  typedef Vec3<char> Vec3b;
346  typedef Vec3<int> Vec3i;
349 
351 }
Definition: clanapp.h:35
Vec3< float > Vec3f
Definition: vec3.h:347
Angle class.
Definition: angle.h:59
void operator-=(const Vec3< Type > &vector)
-= operator.
Definition: vec3.h:223
Type g
Definition: vec3.h:80
Vec3< unsigned int > Vec3ui
Definition: vec3.h:345
Vec3< Type > & normalize()
Normalizes this vector.
Definition: vec3.h:336
Type s
Definition: vec3.h:79
Type x
Definition: vec3.h:79
Type b
Definition: vec3.h:81
Type length() const
Returns the length (magnitude) of this vector.
Definition: vec3.h:327
void operator*=(const Vec3< Type > &vector)
*= operator.
Definition: vec3.h:232
STL namespace.
Vec2< Type > operator/(const Vec2< Type > &v1, const Vec2< Type > &v2)
/ operator.
Definition: vec2.h:306
Type y
Definition: vec3.h:80
Vec3(const Vec4< Type > &copy)
Definition: vec3.h:86
static Vec3< Type > rotate(const Vec3< Type > &vector, const Angle &angle, const Vec3< Type > &axis)
Rotate a vector around an axis. Same as glRotate[f|d](angle, a);.
Type u
Definition: vec3.h:81
Vec3< unsigned char > Vec3ub
Definition: vec3.h:341
Vec3(const Type &scalar)
Definition: vec3.h:84
Vec3< Type > & operator=(const Vec3< Type > &vector)=default
= operator.
Vec3< short > Vec3s
Definition: vec3.h:344
Type r
Definition: vec3.h:79
Vec3< int > Vec3i
Definition: vec3.h:346
static Vec3< Type > cross(const Vec3< Type > &vector1, const Vec3< Type > &vector2)
Calculate the cross product between two vectors.
void operator+=(const Vec3< Type > &vector)
+= operator.
Definition: vec3.h:217
Vec2< Type > operator+(const Vec2< Type > &v1, const Vec2< Type > &v2)
operator.
Definition: vec2.h:270
Vec3()
Definition: vec3.h:83
Type datatype
Definition: vec3.h:77
Vec3< unsigned short > Vec3us
Definition: vec3.h:343
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:143
Vec3< char > Vec3b
Definition: vec3.h:342
2D vector
Definition: line.h:46
3D matrix
Definition: mat2.h:47
Type dot(const Vec3< Type > &vector) const
Dot products this vector with an other vector.
Definition: vec3.h:167
value is a keyword
Vec2< Type > operator*(const Vec2< Type > &v1, const Vec2< Type > &v2)
operator.
Definition: vec2.h:294
void operator/=(const Vec3< Type > &vector)
/= operator.
Definition: vec3.h:238
Vec3(const Type &p1, const Type &p2, const Type &p3)
Definition: vec3.h:96
3D vector
Definition: line_ray.h:46
Vec3(const Vec2< Type > &copy, const Type &p3)
Definition: vec3.h:85
Type t
Definition: vec3.h:80
Angle angle(const Vec3< Type > &vector) const
Calculate the angle between this vector and an other vector.
Vec3(const Vec3< OtherType > &copy)
Definition: vec3.h:91
static Type dot(const Vec3< Type > &vector1, const Vec3< Type > &vector2)
Dot products between two vectors.
Definition: vec3.h:109
bool is_equal(const Vec3< Type > &other, Type epsilon) const
Returns true if equal within the bounds of an epsilon.
Definition: vec3.h:214
Type z
Definition: vec3.h:81
Angle 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:247
Type distance(const Vec3< Type > &vector) const
Calculate the distance between this vector and an other vector.
Vec3< double > Vec3d
Definition: vec3.h:348
bool operator!=(const Vec3< Type > &vector) const
!= operator.
Definition: vec3.h:250
static Vec3< Type > reflect(const Vec3< Type > &incident, const Vec3< Type > &normal)
Calculate the reflection direction for an incident vector.
Vec3< Type > operator-() const
operator.
Definition: vec3.h:229
Vec3(const Type *array_xyz)
Definition: vec3.h:97
4D vector
Definition: size.h:47
Vec2< Type > operator-(const Vec2< Type > &v1, const Vec2< Type > &v2)
operator.
Definition: vec2.h:282
Vec3< Type > & round()
Rounds all components on this vector.