vec3.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 "vec4.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 Vec3
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
84 Vec3() : x(0), y(0), z(0) { }
85 explicit Vec3(const Type &scalar) : x(scalar), y(scalar), z(scalar) { }
86 explicit Vec3(const Vec2<Type> &copy, const Type &p3) { x = copy.x; y = copy.y; z = p3; }
87 explicit Vec3(const Vec4<Type> &copy) { x = copy.x; y = copy.y; z = copy.z; }
88
89 Vec3(const Vec3<double> &copy);
90 Vec3(const Vec3<float> &copy);
91 Vec3(const Vec3<int> &copy);
92
93 explicit Vec3(const Type &p1, const Type &p2, const Type &p3) : x(p1), y(p2), z(p3) { }
94 explicit Vec3(const Type *array_xyz) : x(array_xyz[0]), y(array_xyz[1]), z(array_xyz[2]) { }
95
101 static Vec3<Type> normalize(const Vec3<Type>& vector);
102
106 static Type dot(const Vec3<Type>& vector1, const Vec3<Type>& vector2) { return vector1.x*vector2.x + vector1.y*vector2.y + vector1.z*vector2.z; }
107
113 static Vec3<Type> cross(const Vec3<Type>& vector1, const Vec3<Type>& vector2);
114
121 static Vec3<Type> rotate(const Vec3<Type>& vector, const Angle &angle, const Vec3<Type>& axis);
122
128 static Vec3<Type> round(const Vec3<Type>& vector);
129
133 static Vec3<Type> reflect(const Vec3<Type>& incident, const Vec3<Type>& normal);
134
140 static bool is_equal(const Vec3<Type> &first, const Vec3<Type> &second, Type epsilon)
141 {
142 Type diff_x = second.x - first.x; Type diff_y = second.y - first.y; Type diff_z = second.z - first.z;
143 return (diff_x >= -epsilon && diff_x <= epsilon && diff_y >= -epsilon && diff_y <= epsilon && diff_z >= -epsilon && diff_z <= epsilon );
144 }
145
148
149 public:
154 Type length() const;
155
160 Vec3<Type> &normalize();
161
168 Type dot(const Vec3<Type>& vector) const { return x*vector.x + y*vector.y + z*vector.z; }
169
175 Angle angle(const Vec3<Type>& vector) const;
176
182 Angle angle_normed(const Vec3<Type>& vector) const;
183
189 Type distance(const Vec3<Type>& vector) const;
190
196 Vec3<Type> &cross(const Vec3<Type>& vector);
197
203 Vec3<Type> &rotate(const Angle &angle, const Vec3<Type>& axis);
204
209 Vec3<Type> &round();
210
215 bool is_equal(const Vec3<Type> &other, Type epsilon) const { return Vec3<Type>::is_equal(*this, other, epsilon); }
216
220
221 public:
223 void operator += (const Vec3<Type>& vector) { x+= vector.x; y+= vector.y; z+= vector.z; }
224
226 void operator += ( Type value) { x+= value; y+= value; z+= value; }
227
229 void operator -= (const Vec3<Type>& vector) { x-= vector.x; y-= vector.y; z-= vector.z; }
230
232 void operator -= ( Type value) { x-= value; y-= value; z-= value; }
233
235 Vec3<Type> operator - () const {return Vec3<Type>(-x , -y, -z);}
236
238 void operator *= (const 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 void operator /= (const Vec3<Type>& vector) { x/= vector.x; y/= vector.y; z/= vector.z; }
245
247 void operator /= ( Type value) { x/= value; y/= value; z/= value; }
248
250 Vec3<Type> &operator = (const Vec3<Type>& vector) { x = vector.x; y = vector.y; z = vector.z; return *this; }
251
253 bool operator == (const Vec3<Type>& vector) const {return ((x == vector.x) && (y == vector.y) && (z == vector.z));}
254
256 bool operator != (const Vec3<Type>& vector) const {return ((x != vector.x) || (y != vector.y) || (z != vector.z));}
257
259 bool operator < (const Vec3<Type>& vector) const { return z < vector.z || (z == vector.z && (y < vector.y || (y == vector.y && x < vector.x))); }
261 };
262
264 template<typename Type>
265 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);}
266
268 template<typename Type>
269 Vec3<Type> operator + (Type s, const Vec3<Type>& v) {return Vec3<Type>(s + v.x, s + v.y, s + v.z);}
270
272 template<typename Type>
273 Vec3<Type> operator + (const Vec3<Type>& v, Type s) {return Vec3<Type>(v.x + s, v.y + s, v.z + s);}
274
276 template<typename Type>
277 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);}
278
280 template<typename Type>
281 Vec3<Type> operator - (Type s, const Vec3<Type>& v) {return Vec3<Type>(s - v.x, s - v.y, s - v.z);}
282
284 template<typename Type>
285 Vec3<Type> operator - (const Vec3<Type>& v, Type s) {return Vec3<Type>(v.x - s, v.y - s, v.z - s);}
286
288 template<typename Type>
289 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);}
290
292 template<typename Type>
293 Vec3<Type> operator * (Type s, const Vec3<Type>& v) {return Vec3<Type>(s * v.x, s * v.y, s * v.z);}
294
296 template<typename Type>
297 Vec3<Type> operator * (const Vec3<Type>& v, Type s) {return Vec3<Type>(v.x * s, v.y * s, v.z * s);}
298
300 template<typename Type>
301 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);}
302
304 template<typename Type>
305 Vec3<Type> operator / (Type s, const Vec3<Type>& v) {return Vec3<Type>(s / v.x, s / v.y, s / v.z);}
306
308 template<typename Type>
309 Vec3<Type> operator / (const Vec3<Type>& v, Type s) {return Vec3<Type>(v.x / s, v.y / s, v.z / s);}
310
313 template<typename Type>
314 Vec3<Type> operator * (const Vec3<Type>& v, const Mat3<Type>& matrix)
315 {
316 return Vec3<Type>(
317 matrix[0*3+0]*v.x + matrix[0*3+1]*v.y + matrix[0*3+2]*v.z,
318 matrix[1*3+0]*v.x + matrix[1*3+1]*v.y + matrix[1*3+2]*v.z,
319 matrix[2*3+0]*v.x + matrix[2*3+1]*v.y + matrix[2*3+2]*v.z);
320 }
321
324 template<typename Type>
325 Vec3<Type> operator * (const Mat3<Type>& matrix, const Vec3<Type>& v)
326 {
327 return Vec3<Type>(
328 matrix[0*3+0]*v.x + matrix[1*3+0]*v.y + matrix[2*3+0]*v.z,
329 matrix[0*3+1]*v.x + matrix[1*3+1]*v.y + matrix[2*3+1]*v.z,
330 matrix[0*3+2]*v.x + matrix[1*3+2]*v.y + matrix[2*3+2]*v.z);
331 }
332
333 template<>
334 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); }
335
336 template<>
337 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); }
338
339 template<>
340 inline Vec3<unsigned char>::Vec3(const Vec3<int> &copy) { x = (unsigned char) copy.x; y = (unsigned char) copy.y; z = (unsigned char) copy.z; }
341
342 template<>
343 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); }
344
345 template<>
346 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); }
347
348 template<>
349 inline Vec3<char>::Vec3(const Vec3<int> &copy) { x = (char) copy.x; y = (char) copy.y; z = (char) copy.z; }
350
351 template<>
352 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); }
353
354 template<>
355 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); }
356
357 template<>
358 inline Vec3<unsigned short>::Vec3(const Vec3<int> &copy) { x = (unsigned short) copy.x; y = (unsigned short) copy.y; z = (unsigned short) copy.z; }
359
360 template<>
361 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); }
362
363 template<>
364 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); }
365
366 template<>
367 inline Vec3<short>::Vec3(const Vec3<int> &copy) { x = (short) copy.x; y = (short) copy.y; z = (short) copy.z; }
368
369 template<>
370 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); }
371
372 template<>
373 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); }
374
375 template<>
376 inline Vec3<int>::Vec3(const Vec3<int> &copy) { x = (int) copy.x; y = (int) copy.y; z = (int) copy.z; }
377
378 template<>
379 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); }
380
381 template<>
382 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); }
383
384 template<>
385 inline Vec3<unsigned int>::Vec3(const Vec3<int> &copy) { x = (unsigned int) copy.x; y = (unsigned int) copy.y; z = (unsigned int) copy.z; }
386
387 template<>
388 inline Vec3<float>::Vec3(const Vec3<float> &copy) { x = (float) copy.x; y = (float) copy.y; z = (float) copy.z; }
389
390 template<>
391 inline Vec3<float>::Vec3(const Vec3<double> &copy) { x = (float) copy.x; y = (float) copy.y; z = (float) copy.z; }
392
393 template<>
394 inline Vec3<float>::Vec3(const Vec3<int> &copy) { x = (float) copy.x; y = (float) copy.y; z = (float) copy.z; }
395
396 template<>
397 inline Vec3<double>::Vec3(const Vec3<float> &copy) { x = (double) copy.x; y = (double) copy.y; z = (double) copy.z; }
398
399 template<>
400 inline Vec3<double>::Vec3(const Vec3<double> &copy) { x = (double) copy.x; y = (double) copy.y; z = (double) copy.z; }
401
402 template<>
403 inline Vec3<double>::Vec3(const Vec3<int> &copy) { x = (double) copy.x; y = (double) copy.y; z = (double) copy.z; }
404
405 template<typename Type>
406 inline Type Vec3<Type>::length() const {return (Type) floor(sqrt(float(x*x+y*y+z*z))+0.5f);}
407
408 template<>
409 inline double Vec3<double>::length() const {return sqrt(x*x+y*y+z*z);}
410
411 template<>
412 inline float Vec3<float>::length() const {return sqrt(x*x+y*y+z*z);}
413
414 template<typename Type>
415 inline Vec3<Type> &Vec3<Type>::normalize() { Type f = length(); if (f!=0) { x /= f; y /= f; z /= f; } return *this; }
416
417 template<typename Type>
418 inline Vec3<Type> Vec3<Type>::normalize(const Vec3<Type>& vector) { Vec3<Type> dest(vector); dest.normalize(); return dest; }
419
420 typedef Vec3<unsigned char> Vec3ub;
421 typedef Vec3<char> Vec3b;
422 typedef Vec3<unsigned short> Vec3us;
423 typedef Vec3<short> Vec3s;
424 typedef Vec3<unsigned int> Vec3ui;
425 typedef Vec3<int> Vec3i;
426 typedef Vec3<float> Vec3f;
427 typedef Vec3<double> Vec3d;
428
429 }
430
clan::Vec3::t
Type t
Definition: vec3.h:81
clan::Vec3::Vec3
Vec3(const Type &p1, const Type &p2, const Type &p3)
Definition: vec3.h:93
clan::Vec3::cross
static Vec3< Type > cross(const Vec3< Type > &vector1, const Vec3< Type > &vector2)
Calculate the cross product between two vectors.
clan::Vec3::Vec3
Vec3(const Type *array_xyz)
Definition: vec3.h:94
clan
Definition: clanapp.h:35
clan::Vec3::angle
Angle angle(const Vec3< Type > &vector) const
Calculate the angle between this vector and an other vector.
clan::Vec3::x
Type x
Definition: vec3.h:80
clan::Vec3f
Vec3< float > Vec3f
Definition: vec3.h:426
clan::Angle
Angle class.
Definition: angle.h:62
clan::Vec3ui
Vec3< unsigned int > Vec3ui
Definition: vec3.h:424
clan::Vec3::y
Type y
Definition: vec3.h:81
clan::Vec3::round
Vec3< Type > & round()
Rounds all components on this vector.
clan::Vec4::z
Type z
Definition: vec4.h:82
clan::Vec3::normalize
Vec3< Type > & normalize()
Normalizes this vector.
Definition: vec3.h:415
clan::Key::x
clan::Vec3::operator+=
void operator+=(const Vec3< Type > &vector)
+= operator.
Definition: vec3.h:223
clan::operator/
Vec2< Type > operator/(const Vec2< Type > &v1, const Vec2< Type > &v2)
/ operator.
Definition: vec2.h:312
clan::Vec3::distance
Type distance(const Vec3< Type > &vector) const
Calculate the distance between this vector and an other vector.
clan::Vec3::reflect
static Vec3< Type > reflect(const Vec3< Type > &incident, const Vec3< Type > &normal)
Calculate the reflection direction for an incident vector.
clan::Vec3ub
Vec3< unsigned char > Vec3ub
Definition: vec3.h:420
clan::Vec3::length
Type length() const
Returns the length (magnitude) of this vector.
Definition: vec3.h:406
clan::Vec3::Vec3
Vec3(const Vec4< Type > &copy)
Definition: vec3.h:87
clan::Vec3::operator/=
void operator/=(const Vec3< Type > &vector)
/= operator.
Definition: vec3.h:244
clan::Vec3::Vec3
Vec3()
Definition: vec3.h:84
clan::Vec3::u
Type u
Definition: vec3.h:82
clan::Vec3s
Vec3< short > Vec3s
Definition: vec3.h:423
clan::Vec2::x
Type x
Definition: vec2.h:80
clan::Vec3i
Vec3< int > Vec3i
Definition: vec3.h:425
clan::Vec3::operator-=
void operator-=(const Vec3< Type > &vector)
-= operator.
Definition: vec3.h:229
clan::Key::f
clan::FontWeight::normal
clan::operator+
Vec2< Type > operator+(const Vec2< Type > &v1, const Vec2< Type > &v2)
operator.
Definition: vec2.h:276
clan::Vec3::dot
static Type dot(const Vec3< Type > &vector1, const Vec3< Type > &vector2)
Dot products between two vectors.
Definition: vec3.h:106
clan::Vec3::r
Type r
Definition: vec3.h:80
clan::Key::y
clan::Vec3us
Vec3< unsigned short > Vec3us
Definition: vec3.h:422
clan::Vec3b
Vec3< char > Vec3b
Definition: vec3.h:421
clan::Vec2
2D vector
Definition: line.h:48
clan::Mat3
3D matrix
Definition: mat2.h:48
clan::StyleValueType::length
value is a keyword
clan::operator*
Vec2< Type > operator*(const Vec2< Type > &v1, const Vec2< Type > &v2)
operator.
Definition: vec2.h:300
clan::Vec3< double >
clan::Vec4::y
Type y
Definition: vec4.h:81
clan::Vec3::operator*=
void operator*=(const Vec3< Type > &vector)
*= operator.
Definition: vec3.h:238
clan::Vec3::Vec3
Vec3(const Vec2< Type > &copy, const Type &p3)
Definition: vec3.h:86
clan::Vec3::s
Type s
Definition: vec3.h:80
clan::Vec3::operator-
Vec3< Type > operator-() const
operator.
Definition: vec3.h:235
clan::Vec3::operator!=
bool operator!=(const Vec3< Type > &vector) const
!= operator.
Definition: vec3.h:256
clan::Vec3d
Vec3< double > Vec3d
Definition: vec3.h:427
clan::Vec3::operator==
bool operator==(const Vec3< Type > &vector) const
== operator.
Definition: vec3.h:253
clan::Vec3::Vec3
Vec3(const Type &scalar)
Definition: vec3.h:85
clan::Vec3::b
Type b
Definition: vec3.h:82
clan::Vec3::g
Type g
Definition: vec3.h:81
clan::Vec4::x
Type x
Definition: vec4.h:80
clan::Vec3::rotate
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);.
clan::Vec4
4D vector
Definition: size.h:47
clan::operator-
Vec2< Type > operator-(const Vec2< Type > &v1, const Vec2< Type > &v2)
operator.
Definition: vec2.h:288
clan::Vec3::is_equal
bool is_equal(const Vec3< Type > &other, Type epsilon) const
Returns true if equal within the bounds of an epsilon.
Definition: vec3.h:215
clan::Vec3::datatype
Type datatype
Definition: vec3.h:78
clan::Vec3::operator=
Vec3< Type > & operator=(const Vec3< Type > &vector)
= operator.
Definition: vec3.h:250
clan::Vec3::z
Type z
Definition: vec3.h:82
clan::Vec3::angle_normed
Angle angle_normed(const Vec3< Type > &vector) const
Calculate the angle between this vector and an other vector, where the vectors are unit vectors...
clan::Vec3::dot
Type dot(const Vec3< Type > &vector) const
Dot products this vector with an other vector.
Definition: vec3.h:168
clan::Vec2::y
Type y
Definition: vec2.h:81
clan::Vec3::is_equal
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:140
clan::Key::z