ClanLib  2.3.7
vec4.h
Go to the documentation of this file.
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2011 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 
33 
34 #pragma once
35 
36 #include "../api_core.h"
37 #include "cl_math.h"
38 #include "vec1.h"
39 #include "vec2.h"
40 #include "vec3.h"
41 
42 template<typename Type>
43 class CL_Vec1;
44 
45 template<typename Type>
46 class CL_Vec2;
47 
48 template<typename Type>
49 class CL_Vec3;
50 
51 template<typename Type>
52 class CL_Vec4;
53 
54 template<typename Type>
55 class CL_Mat2;
56 
57 template<typename Type>
58 class CL_Mat3;
59 
60 template<typename Type>
61 class CL_Mat4;
62 
63 template<typename Type>
64 class CL_Sizex;
65 
66 template<typename Type>
67 class CL_Pointx;
68 
69 class CL_Angle;
70 
77 template<typename Type>
78 class CL_Vec4
79 {
80 public:
81  typedef Type datatype;
82 
83  union { Type x; Type s; Type r; };
84  union { Type y; Type t; Type g; };
85  union { Type z; Type u; Type b; };
86  union { Type w; Type v; Type a; };
87 
88  CL_Vec4() : x(0), y(0), z(0), w(0) { }
89  CL_Vec4(const CL_Vec1<Type> &copy) { x = copy.x; y = 0; z = 0; w = 0; }
90  CL_Vec4(const CL_Vec2<Type> &copy) { x = copy.x; y = copy.y; z = 0; w = 0; }
91  CL_Vec4(const CL_Vec3<Type> &copy) { x = copy.x; y = copy.y; z = copy.z; w = 0; }
92  CL_Vec4(const Type &p1, const Type &p2 = 0, const Type &p3 = 0, const Type &p4 = 0) : x(p1), y(p2), z(p3), w(p4) { }
93  CL_Vec4(const Type *array_xyzw) : x(array_xyzw[0]), y(array_xyzw[1]), z(array_xyzw[2]), w(array_xyzw[3]) { }
94 
100  static CL_Vec4<Type> normalize3(const CL_Vec4<Type> &vector);
101 
107  static CL_Vec4<Type> normalize4(const CL_Vec4<Type> &vector);
108 
116  static Type dot3(const CL_Vec4<Type>& vector1, const CL_Vec4<Type>& vector2) { return vector1.x*vector2.x + vector1.y*vector2.y + vector1.z*vector2.z; }
117 
125  static Type dot4(const CL_Vec4<Type>& vector1, const CL_Vec4<Type>& vector2) { return vector1.x*vector2.x + vector1.y*vector2.y + vector1.z*vector2.z + vector1.w*vector2.w; }
126 
132  static CL_Vec4<Type> cross3(const CL_Vec4<Type>& vector1, const CL_Vec4<Type>& vector2);
133 
143  static CL_Vec4<Type> rotate3(const CL_Vec4<Type>& vector, const CL_Angle &angle, const CL_Vec4<Type>& axis);
144 
151  static CL_Vec4<Type> round(const CL_Vec4<Type>& vector);
152 
155 
156 public:
162  Type length3() const;
163 
169  Type length4() const;
170 
176 
182 
189  Type dot3(const CL_Vec4<Type>& vector) const {return x*vector.x + y*vector.y + z*vector.z;}
190 
197  Type dot4(const CL_Vec4<Type>& vector) const {return x*vector.x + y*vector.y + z*vector.z + w*vector.w;}
198 
204  CL_Angle angle3(const CL_Vec4<Type>& vector) const;
205 
211  Type distance3(const CL_Vec4<Type>& vector) const;
212 
218  Type distance4(const CL_Vec4<Type>& vector) const;
219 
226  CL_Vec4<Type> &cross3(const CL_Vec4<Type>& vector);
227 
236  CL_Vec4<Type> &rotate3(const CL_Angle &angle, const CL_Vec4<Type>& axis);
237 
243  CL_Vec4<Type> &round();
244 
248 
249 public:
250  const Type &operator[](unsigned int i) const { return ((Type *) this)[i]; }
251  Type &operator[](unsigned int i) { return ((Type *) this)[i]; }
252  operator Type *() { return (Type *) this; }
253  operator Type * const() const { return (Type * const) this; }
254 
256  void operator += (const CL_Vec4<Type>& vector) { x+= vector.x; y+= vector.y; z+= vector.z; w+= vector.w; }
257 
259  void operator += ( Type value) { x+= value; y+= value; z+= value; w+= value; }
260 
262  CL_Vec4<Type> operator + (const CL_Vec4<Type>& vector) const {return CL_Vec4<Type>(vector.x + x, vector.y + y, vector.z + z, vector.w + w);}
263 
265  CL_Vec4<Type> operator + (Type value) const {return CL_Vec4<Type>(value + x, value + y, value + z, value + w);}
266 
268  void operator -= (const CL_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  CL_Vec4<Type> operator - (const CL_Vec4<Type>& vector) const {return CL_Vec4<Type>(x - vector.x, y - vector.y, z - vector.z, w - vector.w);}
275 
277  CL_Vec4<Type> operator - (Type value) const {return CL_Vec4<Type>(x - value, y - value, z - value, w - value);}
278 
280  void operator *= (const CL_Vec4<Type>& vector) { x*= vector.x; y*= vector.y; z*= vector.z; w*= vector.w; }
281 
283  void operator *= ( Type value) { x*= value; y*= value; z*= value; w*= value; }
284 
286  CL_Vec4<Type> operator * (const CL_Vec4<Type>& vector) const {return CL_Vec4<Type>(vector.x * x, vector.y * y, vector.z * z, vector.w * w);}
287 
289  CL_Vec4<Type> operator * (Type value) const {return CL_Vec4<Type>(value * x, value * y, value * z, value * w);}
290 
292  void operator /= (const CL_Vec4<Type>& vector) { x/= vector.x; y/= vector.y; z/= vector.z; w/= vector.w; }
293 
295  void operator /= ( Type value) { x/= value; y/= value; z/= value; w/= value; }
296 
298  CL_Vec4<Type> operator / (const CL_Vec4<Type>& vector) const {return CL_Vec4<Type>(x / vector.x, y / vector.y, z / vector.z, w / vector.w);}
299 
301  CL_Vec4<Type> operator / (Type value) const {return CL_Vec4<Type>(x / value, y / value, z / value, w / value);}
302 
304  CL_Vec4<Type> &operator = (const CL_Vec4<Type>& vector) { x = vector.x; y = vector.y; z = vector.z; w = vector.w; return *this; }
305 
307  bool operator == (const CL_Vec4<Type>& vector) const {return ((x == vector.x) && (y == vector.y) && (z == vector.z) && (w == vector.w));}
308 
310  bool operator != (const CL_Vec4<Type>& vector) const {return ((x != vector.x) || (y != vector.y) || (z != vector.z) || (w != vector.w));}
312 };
313 
314 template<typename Type>
316 {
317  return CL_Vec4<Type>(
318  matrix[0*4+0]*v.x + matrix[0*4+1]*v.y + matrix[0*4+2]*v.z + matrix[0*4+3]*v.w,
319  matrix[1*4+0]*v.x + matrix[1*4+1]*v.y + matrix[1*4+2]*v.z + matrix[1*4+3]*v.w,
320  matrix[2*4+0]*v.x + matrix[2*4+1]*v.y + matrix[2*4+2]*v.z + matrix[2*4+3]*v.w,
321  matrix[3*4+0]*v.x + matrix[3*4+1]*v.y + matrix[3*4+2]*v.z + matrix[3*4+3]*v.w);
322 }
323 
324 template<typename Type>
326 {
327  return CL_Vec4<Type>(
328  matrix[0*4+0]*v.x + matrix[1*4+0]*v.y + matrix[2*4+0]*v.z + matrix[3*4+0]*v.w,
329  matrix[0*4+1]*v.x + matrix[1*4+1]*v.y + matrix[2*4+1]*v.z + matrix[3*4+1]*v.w,
330  matrix[0*4+2]*v.x + matrix[1*4+2]*v.y + matrix[2*4+2]*v.z + matrix[3*4+2]*v.w,
331  matrix[0*4+3]*v.x + matrix[1*4+3]*v.y + matrix[2*4+3]*v.z + matrix[3*4+3]*v.w);
332 }
333 
342