ClanLib  2.3.7
vec3.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 "vec4.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_Vec3
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 
87  CL_Vec3() : x(0), y(0), z(0) { }
88  CL_Vec3(const CL_Vec2<Type> &copy) { x = copy.x; y = copy.y; z = 0; }
89  CL_Vec3(const CL_Vec1<Type> &copy) { x = copy.x; y = 0; z = 0; }
90  CL_Vec3(const CL_Vec4<Type> &copy) { x = copy.x; y = copy.y; z = copy.z; }
91 
92  CL_Vec3(const CL_Vec3<double> &copy);
93  CL_Vec3(const CL_Vec3<float> &copy);
94  CL_Vec3(const CL_Vec3<int> &copy);
95 
96  CL_Vec3(const Type &p1, const Type &p2 = 0, const Type &p3 = 0) : x(p1), y(p2), z(p3) { }
97  CL_Vec3(const Type *array_xyz) : x(array_xyz[0]), y(array_xyz[1]), z(array_xyz[2]) { }
98 
104  static CL_Vec3<Type> normalize(const CL_Vec3<Type>& vector);
105 
109  static Type dot(const CL_Vec3<Type>& vector1, const CL_Vec3<Type>& vector2) { return vector1.x*vector2.x + vector1.y*vector2.y + vector1.z*vector2.z; }
110 
116  static CL_Vec3<Type> cross(const CL_Vec3<Type>& vector1, const CL_Vec3<Type>& vector2);
117 
124  static CL_Vec3<Type> rotate(const CL_Vec3<Type>& vector, const CL_Angle &angle, const CL_Vec3<Type>& axis);
125 
131  static CL_Vec3<Type> round(const CL_Vec3<Type>& vector);
132 
135 
136 public:
141  Type length() const;
142 
148 
155  Type dot(const CL_Vec3<Type>& vector) const { return x*vector.x + y*vector.y + z*vector.z; }
156 
162  CL_Angle angle(const CL_Vec3<Type>& vector) const;
163 
169  Type distance(const CL_Vec3<Type>& vector) const;
170 
176  CL_Vec3<Type> &cross(const CL_Vec3<Type>& vector);
177 
183  CL_Vec3<Type> &rotate(const CL_Angle &angle, const CL_Vec3<Type>& axis);
184 
189  CL_Vec3<Type> &round();
190 
194 
195 public:
196  const Type &operator[](unsigned int i) const { return ((Type *) this)[i]; }
197  Type &operator[](unsigned int i) { return ((Type *) this)[i]; }
198  operator Type *() { return (Type *) this; }
199  operator Type * const() const { return (Type * const) this; }
200 
202  void operator += (const CL_Vec3<Type>& vector) { x+= vector.x; y+= vector.y; z+= vector.z; }
203 
205  void operator += ( Type value) { x+= value; y+= value; z+= value; }
206 
208  CL_Vec3<Type> operator + (const CL_Vec3<Type>& vector) const {return CL_Vec3<Type>(vector.x + x, vector.y + y, vector.z + z);}
209 
211  CL_Vec3<Type> operator + (Type value) const {return CL_Vec3<Type>(value + x, value + y, value + z);}
212 
214  void operator -= (const CL_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  CL_Vec3<Type> operator - (const CL_Vec3<Type>& vector) const {return CL_Vec3<Type>(x - vector.x, y - vector.y, z - vector.z);}
221 
223  CL_Vec3<Type> operator - (Type value) const {return CL_Vec3<Type>(x - value, y - value, z - value);}
224 
226  void operator *= (const CL_Vec3<Type>& vector) { x*= vector.x; y*= vector.y; z*= vector.z; }
227 
229  void operator *= ( Type value) { x*= value; y*= value; z*= value; }
230 
232  CL_Vec3<Type> operator * (const CL_Vec3<Type>& vector) const {return CL_Vec3<Type>(vector.x * x, vector.y * y, vector.z * z);}
233 
235  CL_Vec3<Type> operator * (Type value) const {return CL_Vec3<Type>(value * x, value * y, value * z);}
236 
238  void operator /= (const CL_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  CL_Vec3<Type> operator / (const CL_Vec3<Type>& vector) const {return CL_Vec3<Type>(x / vector.x, y / vector.y, z / vector.z);}
245 
247  CL_Vec3<Type> operator / (Type value) const {return CL_Vec3<Type>(x / value, y / value, z / value);}
248 
250  CL_Vec3<Type> &operator = (const CL_Vec3<Type>& vector) { x = vector.x; y = vector.y; z = vector.z; return *this; }
251 
253  bool operator == (const CL_Vec3<Type>& vector) const {return ((x == vector.x) && (y == vector.y) && (z == vector.z));}
254 
256  bool operator != (const CL_Vec3<Type>& vector) const {return ((x != vector.x) || (y != vector.y) || (z != vector.z));}
258 };
259 
262 template<typename Type>
264 {
265  return CL_Vec3<Type>(
266  matrix[0*3+0]*v.x + matrix[0*3+1]*v.y + matrix[0*3+2]*v.z,
267  matrix[1*3+0]*v.x + matrix[1*3+1]*v.y + matrix[1*3+2]*v.z,
268  matrix[2*3+0]*v.x + matrix[2*3+1]*v.y + matrix[2*3+2]*v.z);
269 }
270 
273 template<typename Type>
275 {
276  return CL_Vec3<Type>(
277  matrix[0*3+0]*v.x + matrix[1*3+0]*v.y + matrix[2*3+0]*v.z,
278  matrix[0*3+1]*v.x + matrix[1*3+1]*v.y + matrix[2*3+1]*v.z,
279  matrix[0*3+2]*v.x + matrix[1*3+2]*v.y + matrix[2*3+2]*v.z);
280 }
281 
282 template<>
283 inline CL_Vec3<unsigned char>::CL_Vec3(const CL_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); }
284 
285 template<>
286 inline CL_Vec3<unsigned char>::CL_Vec3(const CL_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); }
287 
288 template<>
289 inline CL_Vec3<unsigned char>::CL_Vec3(const CL_Vec3<int> &copy) { x = (unsigned char) copy.x; y = (unsigned char) copy.y; z = (unsigned char) copy.z; }
290 
291 template<>
292 inline CL_Vec3<char>::CL_Vec3(const CL_Vec3<float> &copy) { x = (char) floor(copy.x +0.5f); y = (char) floor(copy.y + 0.5f); z = (char) floor(copy.z + 0.5f); }
293 
294 template<>
295 inline CL_Vec3<char>::CL_Vec3(const CL_Vec3<double> &copy) { x = (char) floor(copy.x+0.5); y = (char) floor(copy.y+0.5); z = (char) floor(copy.z + 0.5); }
296 
297 template<>
298 inline CL_Vec3<char>::CL_Vec3(const CL_Vec3<int> &copy) { x = (char) copy.x; y = (char) copy.y; z = (char) copy.z; }
299 
300 template<>
301 inline CL_Vec3<unsigned short>::CL_Vec3(const CL_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); }
302 
303 template<>
304 inline CL_Vec3<unsigned short>::CL_Vec3(const CL_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); }
305 
306 template<>
307 inline CL_Vec3<unsigned short>::CL_Vec3(const CL_Vec3<int> &copy) { x = (unsigned short) copy.x; y = (unsigned short) copy.y; z = (unsigned short) copy.z; }
308 
309 template<>
310 inline CL_Vec3<short>::CL_Vec3(const CL_Vec3<float> &copy) { x = (short) floor(copy.x +0.5f); y = (short) floor(copy.y + 0.5f); z = (short) floor(copy.z + 0.5f); }
311 
312 template<>
313 inline CL_Vec3<short>::CL_Vec3(const CL_Vec3<double> &copy) { x = (short) floor(copy.x+0.5); y = (short) floor(copy.y+0.5); z = (short) floor(copy.z + 0.5); }
314 
315 template<>
316 inline CL_Vec3<short>::CL_Vec3(const CL_Vec3<int> &copy) { x = (short) copy.x; y = (short) copy.y; z = (short) copy.z; }
317 
318 template<>
319 inline CL_Vec3<int>::CL_Vec3(const CL_Vec3<float> &copy) { x = (int) floor(copy.x +0.5f); y = (int) floor(copy.y + 0.5f); z = (int) floor(copy.z + 0.5f); }
320 
321 template<>
322 inline CL_Vec3<int>::CL_Vec3(const CL_Vec3<double> &copy) { x = (int) floor(copy.x+0.5); y = (int) floor(copy.y+0.5); z = (int) floor(copy.z + 0.5); }
323 
324 template<>
325 inline CL_Vec3<int>::CL_Vec3(const CL_Vec3<int> &copy) { x = (int) copy.x; y = (int) copy.y; z = (int) copy.z; }
326 
327 template<>
328 inline CL_Vec3<unsigned int>::CL_Vec3(const CL_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); }
329 
330 template<>
331 inline CL_Vec3<unsigned int>::CL_Vec3(const CL_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); }
332 
333 template<>
334 inline CL_Vec3<unsigned int>::CL_Vec3(const CL_Vec3<int> &copy) { x = (unsigned int) copy.x; y = (unsigned int) copy.y; z = (unsigned int) copy.z; }
335 
336 template<>
337 inline CL_Vec3<float>::CL_Vec3(const CL_Vec3<float> &copy) { x = (float) copy.x; y = (float) copy.y; z = (float) copy.z; }
338 
339 template<>
340 inline CL_Vec3<float>::CL_Vec3(const CL_Vec3<double> &copy) { x = (float) copy.x; y = (float) copy.y; z = (float) copy.z; }
341 
342 template<>
343 inline CL_Vec3<float>::CL_Vec3(const CL_Vec3<int> &copy) { x = (float) copy.x; y = (float) copy.y; z = (float) copy.z; }
344 
345 template<>
346 inline CL_Vec3<double>::CL_Vec3(const CL_Vec3<float> &copy) { x = (double) copy.x; y = (double) copy.y; z = (double) copy.z; }
347 
348 template<>
349 inline CL_Vec3<double>::CL_Vec3(const CL_Vec3<double> &copy) { x = (double) copy.x; y = (double) copy.y; z = (double) copy.z; }
350 
351 template<>
352 inline CL_Vec3<double>::CL_Vec3(const CL_Vec3<int> &copy) { x = (double) copy.x; y = (double) copy.y; z = (double) copy.z; }
353 
362