vec4.h
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2013 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 
32 #pragma once
33 
34 #include "../api_core.h"
35 #include <cmath>
36 #include "vec2.h"
37 #include "vec3.h"
38 
39 namespace clan
40 {
43 
44 template<typename Type>
45 class Vec2;
46 
47 template<typename Type>
48 class Vec3;
49 
50 template<typename Type>
51 class Vec4;
52 
53 template<typename Type>
54 class Mat2;
55 
56 template<typename Type>
57 class Mat3;
58 
59 template<typename Type>
60 class Mat4;
61 
62 template<typename Type>
63 class Sizex;
64 
65 template<typename Type>
66 class Pointx;
67 
68 class Angle;
69 
75 template<typename Type>
76 class Vec4
77 {
78 public:
79  typedef Type datatype;
80 
81  union { Type x; Type s; Type r; };
82  union { Type y; Type t; Type g; };
83  union { Type z; Type u; Type b; };
84  union { Type w; Type v; Type a; };
85 
86  Vec4() : x(0), y(0), z(0), w(0) { }
87  explicit Vec4(const Type &scalar) : x(scalar), y(scalar), z(scalar), w(scalar) { }
88  explicit Vec4(const Vec2<Type> &copy, const Type &p3, const Type &p4) { x = copy.x; y = copy.y; z = p3; w = p4; }
89  explicit Vec4(const Vec2<Type> &copy, const Vec2<Type> &copy34) { x = copy.x; y = copy.y; z = copy34.x; w = copy34.y; }
90  explicit Vec4(const Vec3<Type> &copy, const Type &p4) { x = copy.x; y = copy.y; z = copy.z; w = p4; }
91  explicit Vec4(const Type &p1, const Type &p2, const Type &p3, const Type &p4) : x(p1), y(p2), z(p3), w(p4) { }
92  explicit Vec4(const Type &p1, const Type &p2, const Vec2<Type> &copy34) : x(p1), y(p2), z(copy34.x), w(copy34.y) { }
93  explicit Vec4(const Type *array_xyzw) : x(array_xyzw[0]), y(array_xyzw[1]), z(array_xyzw[2]), w(array_xyzw[3]) { }
94 
100  static Vec4<Type> normalize3(const Vec4<Type> &vector);
101 
107  static Vec4<Type> normalize4(const Vec4<Type> &vector);
108 
116  static Type dot3(const Vec4<Type>& vector1, const Vec4<Type>& vector2) { return vector1.x*vector2.x + vector1.y*vector2.y + vector1.z*vector2.z; }
117 
125  static Type dot4(const Vec4<Type>& vector1, const Vec4<Type>& vector2) { return vector1.x*vector2.x + vector1.y*vector2.y + vector1.z*vector2.z + vector1.w*vector2.w; }
126 
132  static Vec4<Type> cross3(const Vec4<Type>& vector1, const Vec4<Type>& vector2);
133 
143  static Vec4<Type> rotate3(const Vec4<Type>& vector, const Angle &angle, const Vec4<Type>& axis);
144 
151  static Vec4<Type> round(const Vec4<Type>& vector);
152 
158  static bool is_equal(const Vec4<Type> &first, const Vec4<Type> &second, Type epsilon)
159  {
160  Type diff_x = second.x - first.x; Type diff_y = second.y - first.y; Type diff_z = second.z - first.z; Type diff_w = second.w - first.w;
161  return (diff_x >= -epsilon && diff_x <= epsilon && diff_y >= -epsilon && diff_y <= epsilon && diff_z >= -epsilon && diff_z <= epsilon && diff_w >= -epsilon && diff_w <= epsilon );
162  }
163 
166 
167 public:
173  Type length3() const;
174 
180  Type length4() const;
181 
187 
193 
200  Type dot3(const Vec4<Type>& vector) const {return x*vector.x + y*vector.y + z*vector.z;}
201 
208  Type dot4(const Vec4<Type>& vector) const {return x*vector.x + y*vector.y + z*vector.z + w*vector.w;}
209 
215  Angle angle3(const Vec4<Type>& vector) const;
216 
222  Type distance3(const Vec4<Type>& vector) const;
223 
229  Type distance4(const Vec4<Type>& vector) const;
230 
237  Vec4<Type> &cross3(const Vec4<Type>& vector);
238 
247  Vec4<Type> &rotate3(const Angle &angle, const Vec4<Type>& axis);
248 
254  Vec4<Type> &round();
255 
260  bool is_equal(const Vec4<Type> &other, Type epsilon) const { return Vec4<Type>::is_equal(*this, other, epsilon); }
261 
265 
266 public:
268  void operator += (const Vec4<Type>& vector) { x+= vector.x; y+= vector.y; z+= vector.z; w+= vector.w; }
269 
271  void operator += ( Type value) { x+= value; y+= value; z+= value; w+= value; }
272 
274  void operator -= (const Vec4<Type>& vector) { x-= vector.x; y-= vector.y; z-= vector.z; w-= vector.w; }
275 
277  void operator -= ( Type value) { x-= value; y-= value; z-= value; w-= value; }
278 
280  Vec4<Type> operator - () const {return Vec4<Type>(-x , -y, -z, -w);}
281 
283  void operator *= (const Vec4<Type>& vector) { x*= vector.x; y*= vector.y; z*= vector.z; w*= vector.w; }
284 
286  void operator *= ( Type value) { x*= value; y*= value; z*= value; w*= value; }
287 
289  void operator /= (const Vec4<Type>& vector) { x/= vector.x; y/= vector.y; z/= vector.z; w/= vector.w; }
290 
292  void operator /= ( Type value) { x/= value; y/= value; z/= value; w/= value; }
293 
295  Vec4<Type> &operator = (const Vec4<Type>& vector) { x = vector.x; y = vector.y; z = vector.z; w = vector.w; return *this; }
296 
298  bool operator == (const Vec4<Type>& vector) const {return ((x == vector.x) && (y == vector.y) && (z == vector.z) && (w == vector.w));}
299 
301  bool operator != (const Vec4<Type>& vector) const {return ((x != vector.x) || (y != vector.y) || (z != vector.z) || (w != vector.w));}
302 
304  bool operator < (const Vec4<Type>& vector) const { return w < vector.w || (w == vector.w && (z < vector.z || (z == vector.z && (y < vector.y || (y == vector.y && x < vector.x))))); }
306 };
307 
309 template<typename Type>
310 Vec4<Type> operator + (const Vec4<Type>& v1, const Vec4<Type>& v2) {return Vec4<Type>(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z, v1.w + v2.w);}
311 
313 template<typename Type>
314 Vec4<Type> operator + (Type s, const Vec4<Type>& v) {return Vec4<Type>(s + v.x, s + v.y, s + v.z, s + v.w);}
315 
317 template<typename Type>
318 Vec4<Type> operator + (const Vec4<Type>& v, Type s) {return Vec4<Type>(v.x + s, v.y + s, v.z + s, v.w + s);}
319 
321 template<typename Type>
322 Vec4<Type> operator - (const Vec4<Type>& v1, const Vec4<Type>& v2) {return Vec4<Type>(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z, v1.w - v2.w);}
323 
325 template<typename Type>
326 Vec4<Type> operator - (Type s, const Vec4<Type>& v) {return Vec4<Type>(s - v.x, s - v.y, s - v.z, s - v.w);}
327 
329 template<typename Type>
330 Vec4<Type> operator - (const Vec4<Type>& v, Type s) {return Vec4<Type>(v.x - s, v.y - s, v.z - s, v.w - s);}
331 
333 template<typename Type>
334 Vec4<Type> operator * (const Vec4<Type>& v1, const Vec4<Type>& v2) {return Vec4<Type>(v1.x * v2.x, v1.y * v2.y, v1.z * v2.z, v1.w * v2.w);}
335 
337 template<typename Type>
338 Vec4<Type> operator * (Type s, const Vec4<Type>& v) {return Vec4<Type>(s * v.x, s * v.y, s * v.z, s * v.w);}
339 
341 template<typename Type>
342 Vec4<Type> operator * (const Vec4<Type>& v, Type s) {return Vec4<Type>(v.x * s, v.y * s, v.z * s, v.w * s);}
343 
345 template<typename Type>
346 Vec4<Type> operator / (const Vec4<Type>& v1, const Vec4<Type>& v2) {return Vec4<Type>(v1.x / v2.x, v1.y / v2.y, v1.z / v2.z, v1.w / v2.w);}
347 
349 template<typename Type>
350 Vec4<Type> operator / (Type s, const Vec4<Type>& v) {return Vec4<Type>(s / v.x, s / v.y, s / v.z, s / v.w);}
351 
353 template<typename Type>
354 Vec4<Type> operator / (const Vec4<Type>& v, Type s) {return Vec4<Type>(v.x / s, v.y / s, v.z / s, v.w / s);}
355 
356 template<typename Type>
357 Vec4<Type> operator * (const Vec4<Type>& v, const Mat4<Type>& matrix)
358 {
359  return Vec4<Type>(
360  matrix[0*4+0]*v.x + matrix[0*4+1]*v.y + matrix[0*4+2]*v.z + matrix[0*4+3]*v.w,
361  matrix[1*4+0]*v.x + matrix[1*4+1]*v.y + matrix[1*4+2]*v.z + matrix[1*4+3]*v.w,
362  matrix[2*4+0]*v.x + matrix[2*4+1]*v.y + matrix[2*4+2]*v.z + matrix[2*4+3]*v.w,
363  matrix[3*4+0]*v.x + matrix[3*4+1]*v.y + matrix[3*4+2]*v.z + matrix[3*4+3]*v.w);
364 }
365 
366 template<typename Type>
367 Vec4<Type> operator * (const Mat4<Type>& matrix, const Vec4<Type>& v)
368 {
369  return Vec4<Type>(
370  matrix[0*4+0]*v.x + matrix[1*4+0]*v.y + matrix[2*4+0]*v.z + matrix[3*4+0]*v.w,
371  matrix[0*4+1]*v.x + matrix[1*4+1]*v.y + matrix[2*4+1]*v.z + matrix[3*4+1]*v.w,
372  matrix[0*4+2]*v.x + matrix[1*4+2]*v.y + matrix[2*4+2]*v.z + matrix[3*4+2]*v.w,
373  matrix[0*4+3]*v.x + matrix[1*4+3]*v.y + matrix[2*4+3]*v.z + matrix[3*4+3]*v.w);
374 }
375 
376 template<typename Type>
377 inline Type Vec4<Type>::length3() const {return (Type) floor(sqrt(float(x*x+y*y+z*z))+0.5f);}
378 
379 template<>
380 inline double Vec4<double>::length3() const {return sqrt(x*x+y*y+z*z);}
381 
382 template<>
383 inline float Vec4<float>::length3() const {return sqrt(x*x+y*y+z*z);}
384 
385 template<typename Type>
386 inline Type Vec4<Type>::length4() const {return (Type) floor(sqrt(float(x*x+y*y+z*z+w*w))+0.5f);}
387 
388 template<>
389 inline double Vec4<double>::length4() const {return sqrt(x*x+y*y+z*z+w*w);}
390 
391 template<>
392 inline float Vec4<float>::length4() const {return sqrt(x*x+y*y+z*z+w*w);}
393 
399 typedef Vec4<int> Vec4i;
402 
403 }
404 
bool operator!=(const Vec4< Type > &vector) const
!= operator.
Definition: vec4.h:301
Type x
Definition: vec3.h:81
Type t
Definition: vec4.h:82
Angle class.
Definition: angle.h:63
Vec4< Type > & operator=(const Vec4< Type > &vector)
= operator.
Definition: vec4.h:295
void operator+=(const Vec4< Type > &vector)
+= operator.
Definition: vec4.h:268
static bool is_equal(const Vec4< Type > &first, const Vec4< Type > &second, Type epsilon)
Returns true if equal within the bounds of an epsilon.
Definition: vec4.h:158
Type y
Definition: vec3.h:82
Type z
Definition: vec4.h:83
Type datatype
Definition: vec4.h:79
Vec4< char > Vec4b
Definition: vec4.h:395
static Type dot3(const Vec4< Type > &vector1, const Vec4< Type > &vector2)
Dot products between two vectors (not taking into account the w ordinate).
Definition: vec4.h:116
void operator-=(const Vec4< Type > &vector)
-= operator.
Definition: vec4.h:274
*Type length3() const
Returns the length (magnitude) of this vector (not taking into account the w ordinate).
Definition: vec4.h:377
Vec4< unsigned short > Vec4us
Definition: vec4.h:396
Vec4()
Definition: vec4.h:86
Vec4< Type > & round()
Rounds all components on this vector.
Vec2< Type > operator/(const Vec2< Type > &v1, const Vec2< Type > &v2)
/ operator.
Definition: vec2.h:314
Type dot4(const Vec4< Type > &vector) const
Dot products this vector with an other vector (taking into account the w ordinate).
Definition: vec4.h:208
Vec4< Type > & normalize3()
Normalizes this vector (not taking into account the w ordinate)
Vec4< short > Vec4s
Definition: vec4.h:397
Type a
Definition: vec4.h:84
static Type dot4(const Vec4< Type > &vector1, const Vec4< Type > &vector2)
Dot products between two vectors (taking into account the w ordinate).
Definition: vec4.h:125
Type x
Definition: vec2.h:82
void operator/=(const Vec4< Type > &vector)
/= operator.
Definition: vec4.h:289
Type r
Definition: vec4.h:81
static Vec4< Type > rotate3(const Vec4< Type > &vector, const Angle &angle, const Vec4< 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:278
Vec4(const Type *array_xyzw)
Definition: vec4.h:93
Type s
Definition: vec4.h:81
2D vector
Definition: line.h:49
Type distance4(const Vec4< Type > &vector) const
Calculate the distance between this vector and an other vector (taking into account the w ordinate)...
Vec4(const Vec2< Type > &copy, const Vec2< Type > &copy34)
Definition: vec4.h:89
Vec4< unsigned int > Vec4ui
Definition: vec4.h:398
void operator*=(const Vec4< Type > &vector)
*= operator.
Definition: vec4.h:283
Vec2< Type > operator*(const Vec2< Type > &v1, const Vec2< Type > &v2)
operator.
Definition: vec2.h:302
*Type length4() const
Returns the length (magnitude) of this vector (taking into account the w ordinate).
Definition: vec4.h:386
Vec4(const Vec3< Type > &copy, const Type &p4)
Definition: vec4.h:90
3D vector
Definition: line_ray.h:49
Vec4< Type > operator-() const
operator.
Definition: vec4.h:280
Vec4(const Type &scalar)
Definition: vec4.h:87
Type y
Definition: vec4.h:82
Type distance3(const Vec4< Type > &vector) const
Calculate the distance between this vector and an other vector (not taking into account the w ordinat...
Vec4(const Vec2< Type > &copy, const Type &p3, const Type &p4)
Definition: vec4.h:88
Vec4(const Type &p1, const Type &p2, const Vec2< Type > &copy34)
Definition: vec4.h:92
Type u
Definition: vec4.h:83
Type w
Definition: vec4.h:84
bool is_equal(const Vec4< Type > &other, Type epsilon) const
Returns true if equal within the bounds of an epsilon.
Definition: vec4.h:260
Vec4< unsigned char > Vec4ub
Definition: vec4.h:394
bool operator==(const Vec4< Type > &vector) const
== operator.
Definition: vec4.h:298
Vec4< double > Vec4d
Definition: vec4.h:401
4D matrix
Definition: mat2.h:52
Angle angle3(const Vec4< Type > &vector) const
Calculate the angle between this vector and an other vector (not taking into account the w ordinate)...
Type b
Definition: vec4.h:83
Type dot3(const Vec4< Type > &vector) const
Dot products this vector with an other vector (not taking into account the w ordinate).
Definition: vec4.h:200
Vec4< Type > & normalize4()
Normalizes this vector (taking into account the w ordinate)
Type x
Definition: vec4.h:81
Type v
Definition: vec4.h:84
4D vector
Definition: size.h:48
Vec2< Type > operator-(const Vec2< Type > &v1, const Vec2< Type > &v2)
operator.
Definition: vec2.h:290
Vec4< float > Vec4f
Definition: vec4.h:400
Vec4< int > Vec4i
Definition: vec4.h:399
Vec4(const Type &p1, const Type &p2, const Type &p3, const Type &p4)
Definition: vec4.h:91
Type z
Definition: vec3.h:83
Type y
Definition: vec2.h:83
Type g
Definition: vec4.h:82
static Vec4< Type > cross3(const Vec4< Type > &vector1, const Vec4< Type > &vector2)
Calculate the cross product between two vectors (not taking into account the w ordinate).