vec4.h
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2015 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 <cmath>
35 #include "vec2.h"
36 #include "vec3.h"
37 
38 namespace clan
39 {
42 
43 template<typename Type>
44 class Vec2;
45 
46 template<typename Type>
47 class Vec3;
48 
49 template<typename Type>
50 class Vec4;
51 
52 template<typename Type>
53 class Mat2;
54 
55 template<typename Type>
56 class Mat3;
57 
58 template<typename Type>
59 class Mat4;
60 
61 template<typename Type>
62 class Sizex;
63 
64 template<typename Type>
65 class Pointx;
66 
67 class Angle;
68 
74 template<typename Type>
75 class Vec4
76 {
77 public:
78  typedef Type datatype;
79 
80  union { Type x; Type s; Type r; };
81  union { Type y; Type t; Type g; };
82  union { Type z; Type u; Type b; };
83  union { Type w; Type v; Type a; };
84 
85  Vec4() : x(0), y(0), z(0), w(0) { }
86  explicit Vec4(const Type &scalar) : x(scalar), y(scalar), z(scalar), w(scalar) { }
87  explicit Vec4(const Vec2<Type> &copy, const Type &p3, const Type &p4) { x = copy.x; y = copy.y; z = p3; w = p4; }
88  explicit Vec4(const Vec2<Type> &copy, const Vec2<Type> &copy34) { x = copy.x; y = copy.y; z = copy34.x; w = copy34.y; }
89  explicit Vec4(const Vec3<Type> &copy, const Type &p4) { x = copy.x; y = copy.y; z = copy.z; w = p4; }
90  explicit Vec4(const Type &p1, const Type &p2, const Type &p3, const Type &p4) : x(p1), y(p2), z(p3), w(p4) { }
91  explicit Vec4(const Type &p1, const Type &p2, const Vec2<Type> &copy34) : x(p1), y(p2), z(copy34.x), w(copy34.y) { }
92  explicit Vec4(const Type *array_xyzw) : x(array_xyzw[0]), y(array_xyzw[1]), z(array_xyzw[2]), w(array_xyzw[3]) { }
93 
99  static Vec4<Type> normalize3(const Vec4<Type> &vector);
100 
106  static Vec4<Type> normalize4(const Vec4<Type> &vector);
107 
115  static Type dot3(const Vec4<Type>& vector1, const Vec4<Type>& vector2) { return vector1.x*vector2.x + vector1.y*vector2.y + vector1.z*vector2.z; }
116 
124  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; }
125 
131  static Vec4<Type> cross3(const Vec4<Type>& vector1, const Vec4<Type>& vector2);
132 
142  static Vec4<Type> rotate3(const Vec4<Type>& vector, const Angle &angle, const Vec4<Type>& axis);
143 
150  static Vec4<Type> round(const Vec4<Type>& vector);
151 
157  static bool is_equal(const Vec4<Type> &first, const Vec4<Type> &second, Type epsilon)
158  {
159  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;
160  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 );
161  }
162 
165 
166 public:
167  void set_xy(const Vec2<Type> &new_v) { x = new_v.x; y = new_v.y; }
168  void set_zw(const Vec2<Type> &new_v) { z = new_v.x; w = new_v.y; }
169 
175  Type length3() const;
176 
182  Type length4() const;
183 
189 
195 
202  Type dot3(const Vec4<Type>& vector) const {return x*vector.x + y*vector.y + z*vector.z;}
203 
210  Type dot4(const Vec4<Type>& vector) const {return x*vector.x + y*vector.y + z*vector.z + w*vector.w;}
211 
217  Angle angle3(const Vec4<Type>& vector) const;
218 
224  Type distance3(const Vec4<Type>& vector) const;
225 
231  Type distance4(const Vec4<Type>& vector) const;
232 
239  Vec4<Type> &cross3(const Vec4<Type>& vector);
240 
249  Vec4<Type> &rotate3(const Angle &angle, const Vec4<Type>& axis);
250 
257 
262  bool is_equal(const Vec4<Type> &other, Type epsilon) const { return Vec4<Type>::is_equal(*this, other, epsilon); }
263 
267 
268 public:
270  void operator += (const Vec4<Type>& vector) { x+= vector.x; y+= vector.y; z+= vector.z; w+= vector.w; }
271 
273  void operator += ( Type value) { x+= value; y+= value; z+= value; w+= value; }
274 
276  void operator -= (const Vec4<Type>& vector) { x-= vector.x; y-= vector.y; z-= vector.z; w-= vector.w; }
277 
279  void operator -= ( Type value) { x-= value; y-= value; z-= value; w-= value; }
280 
282  Vec4<Type> operator - () const {return Vec4<Type>(-x , -y, -z, -w);}
283 
285  void operator *= (const Vec4<Type>& vector) { x*= vector.x; y*= vector.y; z*= vector.z; w*= vector.w; }
286 
288  void operator *= ( Type value) { x*= value; y*= value; z*= value; w*= value; }
289 
291  void operator /= (const Vec4<Type>& vector) { x/= vector.x; y/= vector.y; z/= vector.z; w/= vector.w; }
292 
294  void operator /= ( Type value) { x/= value; y/= value; z/= value; w/= value; }
295 
297  Vec4<Type> &operator = (const Vec4<Type>& vector) { x = vector.x; y = vector.y; z = vector.z; w = vector.w; return *this; }
298 
300  bool operator == (const Vec4<Type>& vector) const {return ((x == vector.x) && (y == vector.y) && (z == vector.z) && (w == vector.w));}
301 
303  bool operator != (const Vec4<Type>& vector) const {return ((x != vector.x) || (y != vector.y) || (z != vector.z) || (w != vector.w));}
304 
306  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))))); }
308 };
309 
311 template<typename Type>
312 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);}
313 
315 template<typename Type>
316 Vec4<Type> operator + (Type s, const Vec4<Type>& v) {return Vec4<Type>(s + v.x, s + v.y, s + v.z, s + v.w);}
317 
319 template<typename Type>
320 Vec4<Type> operator + (const Vec4<Type>& v, Type s) {return Vec4<Type>(v.x + s, v.y + s, v.z + s, v.w + s);}
321 
323 template<typename Type>
324 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);}
325 
327 template<typename Type>
328 Vec4<Type> operator - (Type s, const Vec4<Type>& v) {return Vec4<Type>(s - v.x, s - v.y, s - v.z, s - v.w);}
329 
331 template<typename Type>
332 Vec4<Type> operator - (const Vec4<Type>& v, Type s) {return Vec4<Type>(v.x - s, v.y - s, v.z - s, v.w - s);}
333 
335 template<typename Type>
336 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);}
337 
339 template<typename Type>
340 Vec4<Type> operator * (Type s, const Vec4<Type>& v) {return Vec4<Type>(s * v.x, s * v.y, s * v.z, s * v.w);}
341 
343 template<typename Type>
344 Vec4<Type> operator * (const Vec4<Type>& v, Type s) {return Vec4<Type>(v.x * s, v.y * s, v.z * s, v.w * s);}
345 
347 template<typename Type>
348 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);}
349 
351 template<typename Type>
352 Vec4<Type> operator / (Type s, const Vec4<Type>& v) {return Vec4<Type>(s / v.x, s / v.y, s / v.z, s / v.w);}
353 
355 template<typename Type>
356 Vec4<Type> operator / (const Vec4<Type>& v, Type s) {return Vec4<Type>(v.x / s, v.y / s, v.z / s, v.w / s);}
357 
358 template<typename Type>
359 Vec4<Type> operator * (const Vec4<Type>& v, const Mat4<Type>& matrix)
360 {
361  return Vec4<Type>(
362  matrix[0*4+0]*v.x + matrix[0*4+1]*v.y + matrix[0*4+2]*v.z + matrix[0*4+3]*v.w,
363  matrix[1*4+0]*v.x + matrix[1*4+1]*v.y + matrix[1*4+2]*v.z + matrix[1*4+3]*v.w,
364  matrix[2*4+0]*v.x + matrix[2*4+1]*v.y + matrix[2*4+2]*v.z + matrix[2*4+3]*v.w,
365  matrix[3*4+0]*v.x + matrix[3*4+1]*v.y + matrix[3*4+2]*v.z + matrix[3*4+3]*v.w);
366 }
367 
368 template<typename Type>
369 Vec4<Type> operator * (const Mat4<Type>& matrix, const Vec4<Type>& v)
370 {
371  return Vec4<Type>(
372  matrix[0*4+0]*v.x + matrix[1*4+0]*v.y + matrix[2*4+0]*v.z + matrix[3*4+0]*v.w,
373  matrix[0*4+1]*v.x + matrix[1*4+1]*v.y + matrix[2*4+1]*v.z + matrix[3*4+1]*v.w,
374  matrix[0*4+2]*v.x + matrix[1*4+2]*v.y + matrix[2*4+2]*v.z + matrix[3*4+2]*v.w,
375  matrix[0*4+3]*v.x + matrix[1*4+3]*v.y + matrix[2*4+3]*v.z + matrix[3*4+3]*v.w);
376 }
377 
378 template<typename Type>
379 inline Type Vec4<Type>::length3() const {return (Type) floor(sqrt(float(x*x+y*y+z*z))+0.5f);}
380 
381 template<>
382 inline double Vec4<double>::length3() const {return sqrt(x*x+y*y+z*z);}
383 
384 template<>
385 inline float Vec4<float>::length3() const {return sqrt(x*x+y*y+z*z);}
386 
387 template<typename Type>
388 inline Type Vec4<Type>::length4() const {return (Type) floor(sqrt(float(x*x+y*y+z*z+w*w))+0.5f);}
389 
390 template<>
391 inline double Vec4<double>::length4() const {return sqrt(x*x+y*y+z*z+w*w);}
392 
393 template<>
394 inline float Vec4<float>::length4() const {return sqrt(x*x+y*y+z*z+w*w);}
395 
401 typedef Vec4<int> Vec4i;
404 
405 }
406 
Type datatype
Definition: vec4.h:78
Type x
Definition: vec4.h:80
Vec4()
Definition: vec4.h:85
bool is_equal(const Vec4< Type > &other, Type epsilon) const
Returns true if equal within the bounds of an epsilon.
Definition: vec4.h:262
Vec4< Type > & operator=(const Vec4< Type > &vector)
= operator.
Definition: vec4.h:297
Vec4< float > Vec4f
Definition: vec4.h:402
Vec4< unsigned short > Vec4us
Definition: vec4.h:398
Type length3() const
Returns the length (magnitude) of this vector (not taking into account the w ordinate).
Definition: vec4.h:379
Vec4< Type > & round()
Rounds all components on this vector.
Type y
Definition: vec2.h:81
Type v
Definition: vec4.h:83
void set_zw(const Vec2< Type > &new_v)
Definition: vec4.h:168
Vec4< unsigned char > Vec4ub
Definition: vec4.h:396
Vec4< int > Vec4i
Definition: vec4.h:401
Type dot4(const Vec4< Type > &vector) const
Dot products this vector with an other vector (taking into account the w ordinate).
Definition: vec4.h:210
Vec4< short > Vec4s
Definition: vec4.h:399
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:115
Vec2< Type > operator+(const Vec2< Type > &v1, const Vec2< Type > &v2)
operator.
Definition: vec2.h:276
Type g
Definition: vec4.h:81
Type r
Definition: vec4.h:80
Vec4< Type > & rotate3(const Angle &angle, const Vec4< Type > &axis)
Rotate this vector around an axis. Same as glRotate[f|d](angle, a);.
void operator/=(const Vec4< Type > &vector)
/= operator.
Definition: vec4.h:291
Type length4() const
Returns the length (magnitude) of this vector (taking into account the w ordinate).
Definition: vec4.h:388
Vec4< Type > operator-() const
operator.
Definition: vec4.h:282
Type x
Definition: vec2.h:80
void operator+=(const Vec4< Type > &vector)
+= operator.
Definition: vec4.h:270
Type z
Definition: vec4.h:82
Vec4(const Vec3< Type > &copy, const Type &p4)
Definition: vec4.h:89
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:202
Vec2< Type > operator/(const Vec2< Type > &v1, const Vec2< Type > &v2)
/ operator.
Definition: vec2.h:312
bool operator==(const Vec4< Type > &vector) const
== operator.
Definition: vec4.h:300
Type distance4(const Vec4< Type > &vector) const
Calculate the distance between this vector and an other vector (taking into account the w ordinate).
Angle angle3(const Vec4< Type > &vector) const
Calculate the angle between this vector and an other vector (not taking into account the w ordinate).
Angle class.
Definition: angle.h:63
Vec2< Type > operator*(const Vec2< Type > &v1, const Vec2< Type > &v2)
operator.
Definition: vec2.h:300
Vec4< unsigned int > Vec4ui
Definition: vec4.h:400
2D vector
Definition: line.h:48
void set_xy(const Vec2< Type > &new_v)
Definition: vec4.h:167
Vec4(const Type &scalar)
Definition: vec4.h:86
Vec4(const Type *array_xyzw)
Definition: vec4.h:92
Vec4(const Type &p1, const Type &p2, const Vec2< Type > &copy34)
Definition: vec4.h:91
Type a
Definition: vec4.h:83
Vec4(const Vec2< Type > &copy, const Vec2< Type > &copy34)
Definition: vec4.h:88
Vec4< char > Vec4b
Definition: vec4.h:397
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);.
Type w
Definition: vec4.h:83
Vec4< Type > & cross3(const Vec4< Type > &vector)
Calculate the cross product between this vector and an other vector (not taking into account the w or...
4D matrix
Definition: mat2.h:51
void operator*=(const Vec4< Type > &vector)
*= operator.
Definition: vec4.h:285
Vec4< Type > & normalize3()
Normalizes this vector (not taking into account the w ordinate)
Type distance3(const Vec4< Type > &vector) const
Calculate the distance between this vector and an other vector (not taking into account the w ordinat...
bool operator!=(const Vec4< Type > &vector) const
!= operator.
Definition: vec4.h:303
Vec4< Type > & normalize4()
Normalizes this vector (taking into account the w ordinate)
4D vector
Definition: size.h:47
Vec4(const Type &p1, const Type &p2, const Type &p3, const Type &p4)
Definition: vec4.h:90
Type s
Definition: vec4.h:80
Definition: clanapp.h:36
Vec4(const Vec2< Type > &copy, const Type &p3, const Type &p4)
Definition: vec4.h:87
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).
Type y
Definition: vec4.h:81
Vec2< Type > operator-(const Vec2< Type > &v1, const Vec2< Type > &v2)
operator.
Definition: vec2.h:288
Type b
Definition: vec4.h:82
bool operator<(const Vec4< Type > &vector) const
< operator.
Definition: vec4.h:306
static Vec4< Type > round(const Vec4< Type > &vector)
Rounds all components on a vector.
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:124
static Vec4< Type > normalize4(const Vec4< Type > &vector)
Normalizes a vector (taking into account the w ordinate)
static Vec4< Type > normalize3(const Vec4< Type > &vector)
Normalizes a vector (not taking into account the w ordinate)
3D vector
Definition: line_ray.h:48
Type x
Definition: vec3.h:80
Type t
Definition: vec4.h:81
Vec4< double > Vec4d
Definition: vec4.h:403
Type z
Definition: vec3.h:82
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:157
void operator-=(const Vec4< Type > &vector)
-= operator.
Definition: vec4.h:276
Type y
Definition: vec3.h:81
Type u
Definition: vec4.h:82