vec3.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 "vec4.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 Vec3
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 
85  Vec3() : x(0), y(0), z(0) { }
86  explicit Vec3(const Type &scalar) : x(scalar), y(scalar), z(scalar) { }
87  explicit Vec3(const Vec2<Type> &copy, const Type &p3) { x = copy.x; y = copy.y; z = p3; }
88  explicit Vec3(const Vec4<Type> &copy) { x = copy.x; y = copy.y; z = copy.z; }
89 
90  Vec3(const Vec3<double> &copy);
91  Vec3(const Vec3<float> &copy);
92  Vec3(const Vec3<int> &copy);
93 
94  explicit Vec3(const Type &p1, const Type &p2, const Type &p3) : x(p1), y(p2), z(p3) { }
95  explicit Vec3(const Type *array_xyz) : x(array_xyz[0]), y(array_xyz[1]), z(array_xyz[2]) { }
96 
102  static Vec3<Type> normalize(const Vec3<Type>& vector);
103 
107  static Type dot(const Vec3<Type>& vector1, const Vec3<Type>& vector2) { return vector1.x*vector2.x + vector1.y*vector2.y + vector1.z*vector2.z; }
108 
114  static Vec3<Type> cross(const Vec3<Type>& vector1, const Vec3<Type>& vector2);
115 
122  static Vec3<Type> rotate(const Vec3<Type>& vector, const Angle &angle, const Vec3<Type>& axis);
123 
129  static Vec3<Type> round(const Vec3<Type>& vector);
130 
134  static Vec3<Type> reflect(const Vec3<Type>& incident, const Vec3<Type>& normal);
135 
141  static bool is_equal(const Vec3<Type> &first, const Vec3<Type> &second, Type epsilon)
142  {
143  Type diff_x = second.x - first.x; Type diff_y = second.y - first.y; Type diff_z = second.z - first.z;
144  return (diff_x >= -epsilon && diff_x <= epsilon && diff_y >= -epsilon && diff_y <= epsilon && diff_z >= -epsilon && diff_z <= epsilon );
145  }
146 
149 
150 public:
155  Type length() const;
156 
162 
169  Type dot(const Vec3<Type>& vector) const { return x*vector.x + y*vector.y + z*vector.z; }
170 
176  Angle angle(const Vec3<Type>& vector) const;
177 
183  Angle angle_normed(const Vec3<Type>& vector) const;
184 
190  Type distance(const Vec3<Type>& vector) const;
191 
197  Vec3<Type> &cross(const Vec3<Type>& vector);
198 
204  Vec3<Type> &rotate(const Angle &angle, const Vec3<Type>& axis);
205 
210  Vec3<Type> &round();
211 
216  bool is_equal(const Vec3<Type> &other, Type epsilon) const { return Vec3<Type>::is_equal(*this, other, epsilon); }
217 
221 
222 public:
224  void operator += (const Vec3<Type>& vector) { x+= vector.x; y+= vector.y; z+= vector.z; }
225 
227  void operator += ( Type value) { x+= value; y+= value; z+= value; }
228 
230  void operator -= (const Vec3<Type>& vector) { x-= vector.x; y-= vector.y; z-= vector.z; }
231 
233  void operator -= ( Type value) { x-= value; y-= value; z-= value; }
234 
236  Vec3<Type> operator - () const {return Vec3<Type>(-x , -y, -z);}
237 
239  void operator *= (const Vec3<Type>& vector) { x*= vector.x; y*= vector.y; z*= vector.z; }
240 
242  void operator *= ( Type value) { x*= value; y*= value; z*= value; }
243 
245  void operator /= (const Vec3<Type>& vector) { x/= vector.x; y/= vector.y; z/= vector.z; }
246 
248  void operator /= ( Type value) { x/= value; y/= value; z/= value; }
249 
251  Vec3<Type> &operator = (const Vec3<Type>& vector) { x = vector.x; y = vector.y; z = vector.z; return *this; }
252 
254  bool operator == (const Vec3<Type>& vector) const {return ((x == vector.x) && (y == vector.y) && (z == vector.z));}
255 
257  bool operator != (const Vec3<Type>& vector) const {return ((x != vector.x) || (y != vector.y) || (z != vector.z));}
258 
260  bool operator < (const Vec3<Type>& vector) const { return z < vector.z || (z == vector.z && (y < vector.y || (y == vector.y && x < vector.x))); }
262 };
263 
265 template<typename Type>
266 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);}
267 
269 template<typename Type>
270 Vec3<Type> operator + (Type s, const Vec3<Type>& v) {return Vec3<Type>(s + v.x, s + v.y, s + v.z);}
271 
273 template<typename Type>
274 Vec3<Type> operator + (const Vec3<Type>& v, Type s) {return Vec3<Type>(v.x + s, v.y + s, v.z + s);}
275 
277 template<typename Type>
278 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);}
279 
281 template<typename Type>
282 Vec3<Type> operator - (Type s, const Vec3<Type>& v) {return Vec3<Type>(s - v.x, s - v.y, s - v.z);}
283 
285 template<typename Type>
286 Vec3<Type> operator - (const Vec3<Type>& v, Type s) {return Vec3<Type>(v.x - s, v.y - s, v.z - s);}
287 
289 template<typename Type>
290 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);}
291 
293 template<typename Type>
294 Vec3<Type> operator * (Type s, const Vec3<Type>& v) {return Vec3<Type>(s * v.x, s * v.y, s * v.z);}
295 
297 template<typename Type>
298 Vec3<Type> operator * (const Vec3<Type>& v, Type s) {return Vec3<Type>(v.x * s, v.y * s, v.z * s);}
299 
301 template<typename Type>
302 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);}
303 
305 template<typename Type>
306 Vec3<Type> operator / (Type s, const Vec3<Type>& v) {return Vec3<Type>(s / v.x, s / v.y, s / v.z);}
307 
309 template<typename Type>
310 Vec3<Type> operator / (const Vec3<Type>& v, Type s) {return Vec3<Type>(v.x / s, v.y / s, v.z / s);}
311 
314 template<typename Type>
315 Vec3<Type> operator * (const Vec3<Type>& v, const Mat3<Type>& matrix)
316 {
317  return Vec3<Type>(
318  matrix[0*3+0]*v.x + matrix[0*3+1]*v.y + matrix[0*3+2]*v.z,
319  matrix[1*3+0]*v.x + matrix[1*3+1]*v.y + matrix[1*3+2]*v.z,
320  matrix[2*3+0]*v.x + matrix[2*3+1]*v.y + matrix[2*3+2]*v.z);
321 }
322 
325 template<typename Type>
326 Vec3<Type> operator * (const Mat3<Type>& matrix, const Vec3<Type>& v)
327 {
328  return Vec3<Type>(
329  matrix[0*3+0]*v.x + matrix[1*3+0]*v.y + matrix[2*3+0]*v.z,
330  matrix[0*3+1]*v.x + matrix[1*3+1]*v.y + matrix[2*3+1]*v.z,
331  matrix[0*3+2]*v.x + matrix[1*3+2]*v.y + matrix[2*3+2]*v.z);
332 }
333 
334 template<>
335 inline Vec3<unsigned char>::Vec3(const 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); }
336 
337 template<>
338 inline Vec3<unsigned char>::Vec3(const 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); }
339 
340 template<>
341 inline Vec3<unsigned char>::Vec3(const Vec3<int> &copy) { x = (unsigned char) copy.x; y = (unsigned char) copy.y; z = (unsigned char) copy.z; }
342 
343 template<>
344 inline Vec3<char>::Vec3(const Vec3<float> &copy) { x = (char) floor(copy.x +0.5f); y = (char) floor(copy.y + 0.5f); z = (char) floor(copy.z + 0.5f); }
345 
346 template<>
347 inline Vec3<char>::Vec3(const Vec3<double> &copy) { x = (char) floor(copy.x+0.5); y = (char) floor(copy.y+0.5); z = (char) floor(copy.z + 0.5); }
348 
349 template<>
350 inline Vec3<char>::Vec3(const Vec3<int> &copy) { x = (char) copy.x; y = (char) copy.y; z = (char) copy.z; }
351 
352 template<>
353 inline Vec3<unsigned short>::Vec3(const 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); }
354 
355 template<>
356 inline Vec3<unsigned short>::Vec3(const 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); }
357 
358 template<>
359 inline Vec3<unsigned short>::Vec3(const Vec3<int> &copy) { x = (unsigned short) copy.x; y = (unsigned short) copy.y; z = (unsigned short) copy.z; }
360 
361 template<>
362 inline Vec3<short>::Vec3(const Vec3<float> &copy) { x = (short) floor(copy.x +0.5f); y = (short) floor(copy.y + 0.5f); z = (short) floor(copy.z + 0.5f); }
363 
364 template<>
365 inline Vec3<short>::Vec3(const Vec3<double> &copy) { x = (short) floor(copy.x+0.5); y = (short) floor(copy.y+0.5); z = (short) floor(copy.z + 0.5); }
366 
367 template<>
368 inline Vec3<short>::Vec3(const Vec3<int> &copy) { x = (short) copy.x; y = (short) copy.y; z = (short) copy.z; }
369 
370 template<>
371 inline Vec3<int>::Vec3(const Vec3<float> &copy) { x = (int) floor(copy.x +0.5f); y = (int) floor(copy.y + 0.5f); z = (int) floor(copy.z + 0.5f); }
372 
373 template<>
374 inline Vec3<int>::Vec3(const Vec3<double> &copy) { x = (int) floor(copy.x+0.5); y = (int) floor(copy.y+0.5); z = (int) floor(copy.z + 0.5); }
375 
376 template<>
377 inline Vec3<int>::Vec3(const Vec3<int> &copy) { x = (int) copy.x; y = (int) copy.y; z = (int) copy.z; }
378 
379 template<>
380 inline Vec3<unsigned int>::Vec3(const 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); }
381 
382 template<>
383 inline Vec3<unsigned int>::Vec3(const 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); }
384 
385 template<>
386 inline Vec3<unsigned int>::Vec3(const Vec3<int> &copy) { x = (unsigned int) copy.x; y = (unsigned int) copy.y; z = (unsigned int) copy.z; }
387 
388 template<>
389 inline Vec3<float>::Vec3(const Vec3<float> &copy) { x = (float) copy.x; y = (float) copy.y; z = (float) copy.z; }
390 
391 template<>
392 inline Vec3<float>::Vec3(const Vec3<double> &copy) { x = (float) copy.x; y = (float) copy.y; z = (float) copy.z; }
393 
394 template<>
395 inline Vec3<float>::Vec3(const Vec3<int> &copy) { x = (float) copy.x; y = (float) copy.y; z = (float) copy.z; }
396 
397 template<>
398 inline Vec3<double>::Vec3(const Vec3<float> &copy) { x = (double) copy.x; y = (double) copy.y; z = (double) copy.z; }
399 
400 template<>
401 inline Vec3<double>::Vec3(const Vec3<double> &copy) { x = (double) copy.x; y = (double) copy.y; z = (double) copy.z; }
402 
403 template<>
404 inline Vec3<double>::Vec3(const Vec3<int> &copy) { x = (double) copy.x; y = (double) copy.y; z = (double) copy.z; }
405 
406 template<typename Type>
407 inline Type Vec3<Type>::length() const {return (Type) floor(sqrt(float(x*x+y*y+z*z))+0.5f);}
408 
409 template<>
410 inline double Vec3<double>::length() const {return sqrt(x*x+y*y+z*z);}
411 
412 template<>
413 inline float Vec3<float>::length() const {return sqrt(x*x+y*y+z*z);}
414 
415 template<typename Type>
416 inline Vec3<Type> &Vec3<Type>::normalize() { Type f = length(); if (f!=0) { x /= f; y /= f; z /= f; } return *this; }
417 
418 template<typename Type>
419 inline Vec3<Type> Vec3<Type>::normalize(const Vec3<Type>& vector) { Vec3<Type> dest(vector); dest.normalize(); return dest; }
420 
426 typedef Vec3<int> Vec3i;
429 
430 }
431 
Type t
Definition: vec3.h:82
Vec3(const Type &p1, const Type &p2, const Type &p3)
Definition: vec3.h:94
static Vec3< Type > cross(const Vec3< Type > &vector1, const Vec3< Type > &vector2)
Calculate the cross product between two vectors.
Vec3(const Type *array_xyz)
Definition: vec3.h:95
Angle angle(const Vec3< Type > &vector) const
Calculate the angle between this vector and an other vector.
Type x
Definition: vec3.h:81
Vec3< float > Vec3f
Definition: vec3.h:427
Angle class.
Definition: angle.h:63
Vec3< unsigned int > Vec3ui
Definition: vec3.h:425
Type y
Definition: vec3.h:82
Vec3< Type > & round()
Rounds all components on this vector.
Type z
Definition: vec4.h:83
Vec3< Type > & normalize()
Normalizes this vector.
Definition: vec3.h:416
void operator+=(const Vec3< Type > &vector)
+= operator.
Definition: vec3.h:224
Vec2< Type > operator/(const Vec2< Type > &v1, const Vec2< Type > &v2)
/ operator.
Definition: vec2.h:314
Type distance(const Vec3< Type > &vector) const
Calculate the distance between this vector and an other vector.
static Vec3< Type > reflect(const Vec3< Type > &incident, const Vec3< Type > &normal)
Calculate the reflection direction for an incident vector.
Vec3< unsigned char > Vec3ub
Definition: vec3.h:421
Type length() const
Returns the length (magnitude) of this vector.
Definition: vec3.h:407
Vec3(const Vec4< Type > &copy)
Definition: vec3.h:88
void operator/=(const Vec3< Type > &vector)
/= operator.
Definition: vec3.h:245
Vec3()
Definition: vec3.h:85
Type u
Definition: vec3.h:83
Vec3< short > Vec3s
Definition: vec3.h:424
Type x
Definition: vec2.h:82
Vec3< int > Vec3i
Definition: vec3.h:426
void operator-=(const Vec3< Type > &vector)
-= operator.
Definition: vec3.h:230
Vec2< Type > operator+(const Vec2< Type > &v1, const Vec2< Type > &v2)
operator.
Definition: vec2.h:278
static Type dot(const Vec3< Type > &vector1, const Vec3< Type > &vector2)
Dot products between two vectors.
Definition: vec3.h:107
Type r
Definition: vec3.h:81
Vec3< unsigned short > Vec3us
Definition: vec3.h:423
Vec3< char > Vec3b
Definition: vec3.h:422
2D vector
Definition: line.h:49
3D matrix
Definition: mat2.h:49
Vec2< Type > operator*(const Vec2< Type > &v1, const Vec2< Type > &v2)
operator.
Definition: vec2.h:302
Type y
Definition: vec4.h:82
void operator*=(const Vec3< Type > &vector)
*= operator.
Definition: vec3.h:239
Vec3(const Vec2< Type > &copy, const Type &p3)
Definition: vec3.h:87
Type s
Definition: vec3.h:81
Vec3< Type > operator-() const
operator.
Definition: vec3.h:236
bool operator!=(const Vec3< Type > &vector) const
!= operator.
Definition: vec3.h:257
Vec3< double > Vec3d
Definition: vec3.h:428
bool operator==(const Vec3< Type > &vector) const
== operator.
Definition: vec3.h:254
Vec3(const Type &scalar)
Definition: vec3.h:86
Type b
Definition: vec3.h:83
Type g
Definition: vec3.h:82
Type x
Definition: vec4.h:81
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);.
4D vector
Definition: size.h:48
Vec2< Type > operator-(const Vec2< Type > &v1, const Vec2< Type > &v2)
operator.
Definition: vec2.h:290
bool is_equal(const Vec3< Type > &other, Type epsilon) const
Returns true if equal within the bounds of an epsilon.
Definition: vec3.h:216
Type datatype
Definition: vec3.h:79
Vec3< Type > & operator=(const Vec3< Type > &vector)
= operator.
Definition: vec3.h:251
Type z
Definition: vec3.h:83
Angle angle_normed(const Vec3< Type > &vector) const
Calculate the angle between this vector and an other vector, where the vectors are unit vectors...
Type dot(const Vec3< Type > &vector) const
Dot products this vector with an other vector.
Definition: vec3.h:169
Type y
Definition: vec2.h:83
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:141