vec3.h Source File - ClanLib SDK
Description: vec3.h Source File

vec3.h
1/*
2** ClanLib SDK
3** Copyright (c) 1997-2020 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#pragma once
32
33#include <cmath>
34#include "vec2.h"
35#include "vec4.h"
36
37namespace clan
38{
41
42 template<typename Type>
43 class Vec2;
44
45 template<typename Type>
46 class Vec3;
47
48 template<typename Type>
49 class Vec4;
50
51 template<typename Type>
52 class Mat2;
53
54 template<typename Type>
55 class Mat3;
56
57 template<typename Type>
58 class Mat4;
59
60 template<typename Type>
61 class Sizex;
62
63 template<typename Type>
64 class Pointx;
65
66 class Angle;
67
73 template<typename Type>
74 class Vec3
75 {
76 public:
77 typedef Type datatype;
78
79 union { Type x; Type s; Type r; };
80 union { Type y; Type t; Type g; };
81 union { Type z; Type u; Type b; };
82
83 Vec3() : x(0), y(0), z(0) { }
84 explicit Vec3(const Type &scalar) : x(scalar), y(scalar), z(scalar) { }
85 explicit Vec3(const Vec2<Type> &copy, const Type &p3) { x = copy.x; y = copy.y; z = p3; }
86 explicit Vec3(const Vec4<Type> &copy) { x = copy.x; y = copy.y; z = copy.z; }
87
88 Vec3(const Vec3<double> &copy);
89 Vec3(const Vec3<float> &copy);
90 Vec3(const Vec3<int> &copy);
91
92 explicit Vec3(const Type &p1, const Type &p2, const Type &p3) : x(p1), y(p2), z(p3) { }
93 explicit Vec3(const Type *array_xyz) : x(array_xyz[0]), y(array_xyz[1]), z(array_xyz[2]) { }
94
100 static Vec3<Type> normalize(const Vec3<Type>& vector);
101
105 static Type dot(const Vec3<Type>& vector1, const Vec3<Type>& vector2) { return vector1.x*vector2.x + vector1.y*vector2.y + vector1.z*vector2.z; }
106
112 static Vec3<Type> cross(const Vec3<Type>& vector1, const Vec3<Type>& vector2);
113
120 static Vec3<Type> rotate(const Vec3<Type>& vector, const Angle &angle, const Vec3<Type>& axis);
121
127 static Vec3<Type> round(const Vec3<Type>& vector);
128
132 static Vec3<Type> reflect(const Vec3<Type>& incident, const Vec3<Type>& normal);
133
139 static bool is_equal(const Vec3<Type> &first, const Vec3<Type> &second, Type epsilon)
140 {
141 Type diff_x = second.x - first.x; Type diff_y = second.y - first.y; Type diff_z = second.z - first.z;
142 return (diff_x >= -epsilon && diff_x <= epsilon && diff_y >= -epsilon && diff_y <= epsilon && diff_z >= -epsilon && diff_z <= epsilon);
143 }
144
149 Type length() const;
150
155 Vec3<Type> &normalize();
156
163 Type dot(const Vec3<Type>& vector) const { return x*vector.x + y*vector.y + z*vector.z; }
164
170 Angle angle(const Vec3<Type>& vector) const;
171
177 Angle angle_normed(const Vec3<Type>& vector) const;
178
184 Type distance(const Vec3<Type>& vector) const;
185
191 Vec3<Type> &cross(const Vec3<Type>& vector);
192
198 Vec3<Type> &rotate(const Angle &angle, const Vec3<Type>& axis);
199
204 Vec3<Type> &round();
205
210 bool is_equal(const Vec3<Type> &other, Type epsilon) const { return Vec3<Type>::is_equal(*this, other, epsilon); }
211
213 void operator += (const Vec3<Type>& vector) { x += vector.x; y += vector.y; z += vector.z; }
214
216 void operator += (Type value) { x += value; y += value; z += value; }
217
219 void operator -= (const Vec3<Type>& vector) { x -= vector.x; y -= vector.y; z -= vector.z; }
220
222 void operator -= (Type value) { x -= value; y -= value; z -= value; }
223
225 Vec3<Type> operator - () const { return Vec3<Type>(-x, -y, -z); }
226
228 void operator *= (const Vec3<Type>& vector) { x *= vector.x; y *= vector.y; z *= vector.z; }
229
231 void operator *= (Type value) { x *= value; y *= value; z *= value; }
232
234 void operator /= (const Vec3<Type>& vector) { x /= vector.x; y /= vector.y; z /= vector.z; }
235
237 void operator /= (Type value) { x /= value; y /= value; z /= value; }
238
240 Vec3<Type> &operator = (const Vec3<Type>& vector) { x = vector.x; y = vector.y; z = vector.z; return *this; }
241
243 bool operator == (const Vec3<Type>& vector) const { return ((x == vector.x) && (y == vector.y) && (z == vector.z)); }
244
246 bool operator != (const Vec3<Type>& vector) const { return ((x != vector.x) || (y != vector.y) || (z != vector.z)); }
247
249 bool operator < (const Vec3<Type>& vector) const { return z < vector.z || (z == vector.z && (y < vector.y || (y == vector.y && x < vector.x))); }
250 };
251
253 template<typename Type>
254 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); }
255
257 template<typename Type>
258 Vec3<Type> operator + (Type s, const Vec3<Type>& v) { return Vec3<Type>(s + v.x, s + v.y, s + v.z); }
259
261 template<typename Type>
262 Vec3<Type> operator + (const Vec3<Type>& v, Type s) { return Vec3<Type>(v.x + s, v.y + s, v.z + s); }
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
302 template<typename Type>
303 Vec3<Type> operator * (const Vec3<Type>& v, const Mat3<Type>& matrix)
304 {
305 return Vec3<Type>(
306 matrix[0 * 3 + 0] * v.x + matrix[0 * 3 + 1] * v.y + matrix[0 * 3 + 2] * v.z,
307 matrix[1 * 3 + 0] * v.x + matrix[1 * 3 + 1] * v.y + matrix[1 * 3 + 2] * v.z,
308 matrix[2 * 3 + 0] * v.x + matrix[2 * 3 + 1] * v.y + matrix[2 * 3 + 2] * v.z);
309 }
310
313 template<typename Type>
314 Vec3<Type> operator * (const Mat3<Type>& matrix, const Vec3<Type>& v)
315 {
316 return Vec3<Type>(
317 matrix[0 * 3 + 0] * v.x + matrix[1 * 3 + 0] * v.y + matrix[2 * 3 + 0] * v.z,
318 matrix[0 * 3 + 1] * v.x + matrix[1 * 3 + 1] * v.y + matrix[2 * 3 + 1] * v.z,
319 matrix[0 * 3 + 2] * v.x + matrix[1 * 3 + 2] * v.y + matrix[2 * 3 + 2] * v.z);
320 }
321
322 template<>
323 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); }
324
325 template<>
326 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); }
327
328 template<>
329 inline Vec3<unsigned char>::Vec3(const Vec3<int> &copy) { x = (unsigned char)copy.x; y = (unsigned char)copy.y; z = (unsigned char)copy.z; }
330
331 template<>
332 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); }
333
334 template<>
335 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); }
336
337 template<>
338 inline Vec3<char>::Vec3(const Vec3<int> &copy) { x = (char)copy.x; y = (char)copy.y; z = (char)copy.z; }
339
340 template<>
341 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); }
342
343 template<>
344 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); }
345
346 template<>
347 inline Vec3<unsigned short>::Vec3(const Vec3<int> &copy) { x = (unsigned short)copy.x; y = (unsigned short)copy.y; z = (unsigned short)copy.z; }
348
349 template<>
350 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); }
351
352 template<>
353 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); }
354
355 template<>
356 inline Vec3<short>::Vec3(const Vec3<int> &copy) { x = (short)copy.x; y = (short)copy.y; z = (short)copy.z; }
357
358 template<>
359 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); }
360
361 template<>
362 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); }
363
364 template<>
365 inline Vec3<int>::Vec3(const Vec3<int> &copy) { x = (int)copy.x; y = (int)copy.y; z = (int)copy.z; }
366
367 template<>
368 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); }
369
370 template<>
371 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); }
372
373 template<>
374 inline Vec3<unsigned int>::Vec3(const Vec3<int> &copy) { x = (unsigned int)copy.x; y = (unsigned int)copy.y; z = (unsigned int)copy.z; }
375
376 template<>
377 inline Vec3<float>::Vec3(const Vec3<float> &copy) { x = (float)copy.x; y = (float)copy.y; z = (float)copy.z; }
378
379 template<>
380 inline Vec3<float>::Vec3(const Vec3<double> &copy) { x = (float)copy.x; y = (float)copy.y; z = (float)copy.z; }
381
382 template<>
383 inline Vec3<float>::Vec3(const Vec3<int> &copy) { x = (float)copy.x; y = (float)copy.y; z = (float)copy.z; }
384
385 template<>
386 inline Vec3<double>::Vec3(const Vec3<float> &copy) { x = (double)copy.x; y = (double)copy.y; z = (double)copy.z; }
387
388 template<>
389 inline Vec3<double>::Vec3(const Vec3<double> &copy) { x = (double)copy.x; y = (double)copy.y; z = (double)copy.z; }
390
391 template<>
392 inline Vec3<double>::Vec3(const Vec3<int> &copy) { x = (double)copy.x; y = (double)copy.y; z = (double)copy.z; }
393
394 template<typename Type>
395 inline Type Vec3<Type>::length() const { return (Type)floor(sqrt(float(x*x + y*y + z*z)) + 0.5f); }
396
397 template<>
398 inline double Vec3<double>::length() const { return sqrt(x*x + y*y + z*z); }
399
400 template<>
401 inline float Vec3<float>::length() const { return sqrt(x*x + y*y + z*z); }
402
403 template<typename Type>
404 inline Vec3<Type> &Vec3<Type>::normalize() { Type f = length(); if (f != 0) { x /= f; y /= f; z /= f; } return *this; }
405
406 template<typename Type>
407 inline Vec3<Type> Vec3<Type>::normalize(const Vec3<Type>& vector) { Vec3<Type> dest(vector); dest.normalize(); return dest; }
408
409 typedef Vec3<unsigned char> Vec3ub;
410 typedef Vec3<char> Vec3b;
411 typedef Vec3<unsigned short> Vec3us;
412 typedef Vec3<short> Vec3s;
413 typedef Vec3<unsigned int> Vec3ui;
414 typedef Vec3<int> Vec3i;
415 typedef Vec3<float> Vec3f;
416 typedef Vec3<double> Vec3d;
417
419}
clan::Angle
Angle class.
Definition angle.h:60
clan::Mat3
3D matrix
Definition vec4.h:55
clan::Vec2
2D vector
Definition vec4.h:43
clan::Vec2::y
Type y
Definition vec2.h:81
clan::Vec2::x
Type x
Definition vec2.h:80
clan::Vec3
3D vector
Definition vec4.h:46
clan::Vec3::operator!=
bool operator!=(const Vec3< Type > &vector) const
!= operator.
Definition vec3.h:246
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:210
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:139
clan::Vec3::operator-
Vec3< Type > operator-() const
operator.
Definition vec3.h:225
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:93
clan::Vec3::s
Type s
Definition vec3.h:79
clan::Vec3::Vec3
Vec3()
Definition vec3.h:83
clan::Vec3::r
Type r
Definition vec3.h:79
clan::Vec3::dot
static Type dot(const Vec3< Type > &vector1, const Vec3< Type > &vector2)
Dot products between two vectors.
Definition vec3.h:105
clan::Vec3::operator=
Vec3< Type > & operator=(const Vec3< Type > &vector)
= operator.
Definition vec3.h:240
clan::Vec3::rotate
Vec3< Type > & rotate(const Angle &angle, const Vec3< Type > &axis)
Rotate this vector around an axis. Same as glRotate[f|d](angle, a);.
clan::Vec3::reflect
static Vec3< Type > reflect(const Vec3< Type > &incident, const Vec3< Type > &normal)
Calculate the reflection direction for an incident vector.
clan::Vec3::Vec3
Vec3(const Vec3< int > &copy)
clan::Vec3::Vec3
Vec3(const Vec3< double > &copy)
clan::Vec3::round
static Vec3< Type > round(const Vec3< Type > &vector)
Rounds all components on a vector.
clan::Vec3::round
Vec3< Type > & round()
Rounds all components on this vector.
clan::Vec3::Vec3
Vec3(const Type &p1, const Type &p2, const Type &p3)
Definition vec3.h:92
clan::Vec3::Vec3
Vec3(const Type &scalar)
Definition vec3.h:84
clan::Vec3::z
Type z
Definition vec3.h:81
clan::Vec3::y
Type y
Definition vec3.h:80
clan::Vec3::g
Type g
Definition vec3.h:80
clan::Vec3::t
Type t
Definition vec3.h:80
clan::Vec3::operator<
bool operator<(const Vec3< Type > &vector) const
< operator.
Definition vec3.h:249
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::operator-=
void operator-=(const Vec3< Type > &vector)
-= operator.
Definition vec3.h:219
clan::Vec3::operator+=
void operator+=(const Vec3< Type > &vector)
+= operator.
Definition vec3.h:213
clan::Vec3::operator/=
void operator/=(const Vec3< Type > &vector)
/= operator.
Definition vec3.h:234
clan::Vec3::cross
Vec3< Type > & cross(const Vec3< Type > &vector)
Calculate the cross product between this vector and an other vector.
clan::Vec3::Vec3
Vec3(const Vec4< Type > &copy)
Definition vec3.h:86
clan::Vec3::Vec3
Vec3(const Vec2< Type > &copy, const Type &p3)
Definition vec3.h:85
clan::Vec3::u
Type u
Definition vec3.h:81
clan::Vec3::distance
Type distance(const Vec3< Type > &vector) const
Calculate the distance between this vector and an other vector.
clan::Vec3::x
Type x
Definition vec3.h:79
clan::Vec3::b
Type b
Definition vec3.h:81
clan::Vec3::datatype
Type datatype
Definition vec3.h:77
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::Vec3::operator==
bool operator==(const Vec3< Type > &vector) const
== operator.
Definition vec3.h:243
clan::Vec3::angle
Angle angle(const Vec3< Type > &vector) const
Calculate the angle between this vector and an other vector.
clan::Vec3::dot
Type dot(const Vec3< Type > &vector) const
Dot products this vector with an other vector.
Definition vec3.h:163
clan::Vec3::operator*=
void operator*=(const Vec3< Type > &vector)
*= operator.
Definition vec3.h:228
clan::Vec3::Vec3
Vec3(const Vec3< float > &copy)
clan::Vec4
4D vector
Definition vec4.h:75
clan::Vec4::z
Type z
Definition vec4.h:81
clan::Vec4::y
Type y
Definition vec4.h:80
clan::Vec4::x
Type x
Definition vec4.h:79
clan::operator/
Vec2< Type > operator/(const Vec2< Type > &v1, const Vec2< Type > &v2)
/ operator.
Definition vec2.h:302
clan::Vec3::normalize
static Vec3< Type > normalize(const Vec3< Type > &vector)
Normalizes a vector.
Definition vec3.h:407
clan::Vec3ub
Vec3< unsigned char > Vec3ub
Definition vec3.h:409
clan::Vec3::length
Type length() const
Returns the length (magnitude) of this vector.
Definition vec3.h:395
clan::Vec3::normalize
Vec3< Type > & normalize()
Normalizes this vector.
Definition vec3.h:404
clan::Vec3us
Vec3< unsigned short > Vec3us
Definition vec3.h:411
clan::Vec3ui
Vec3< unsigned int > Vec3ui
Definition vec3.h:413
clan::Vec3f
Vec3< float > Vec3f
Definition vec3.h:415
clan::operator-
Vec2< Type > operator-(const Vec2< Type > &v1, const Vec2< Type > &v2)
operator.
Definition vec2.h:278
clan::operator+
Vec2< Type > operator+(const Vec2< Type > &v1, const Vec2< Type > &v2)
operator.
Definition vec2.h:266
clan::Vec3d
Vec3< double > Vec3d
Definition vec3.h:416
clan::Vec3b
Vec3< char > Vec3b
Definition vec3.h:410
clan::operator*
Vec2< Type > operator*(const Vec2< Type > &v1, const Vec2< Type > &v2)
operator.
Definition vec2.h:290
clan::Vec3s
Vec3< short > Vec3s
Definition vec3.h:412
clan::Vec3i
Vec3< int > Vec3i
Definition vec3.h:414
clan::FontWeight::normal
@ normal
clan
Definition clanapp.h:36
clan::StyleValueType::length
@ length
value is a keyword
clan::StyleValueType::angle
@ angle
value is a color
clan::Key::s
@ s
clan::Key::y
@ y
clan::Key::f
@ f
clan::Key::x
@ x
clan::Key::v
@ v
clan::Key::z
@ z