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

vec4.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 "vec3.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 Vec4
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 union { Type w; Type v; Type a; };
83
84 Vec4() : x(0), y(0), z(0), w(0) { }
85 explicit Vec4(const Type &scalar) : x(scalar), y(scalar), z(scalar), w(scalar) { }
86 explicit Vec4(const Vec2<Type> &copy, const Type &p3, const Type &p4) : x(copy.x), y(copy.y), z(p3), w(p4) {}
87 explicit Vec4(const Vec2<Type> &copy, const Vec2<Type> &copy34) : x(copy.x), y(copy.y), z(copy34.x), w(copy34.y) {}
88 explicit Vec4(const Vec3<Type> &copy, const Type &p4) : x(copy.x), y(copy.y), z(copy.z), w(p4) {}
89 explicit Vec4(const Type &p1, const Type &p2, const Type &p3, const Type &p4) : x(p1), y(p2), z(p3), w(p4) { }
90 explicit Vec4(const Type &p1, const Type &p2, const Vec2<Type> &copy34) : x(p1), y(p2), z(copy34.x), w(copy34.y) { }
91 explicit Vec4(const Type *array_xyzw) : x(array_xyzw[0]), y(array_xyzw[1]), z(array_xyzw[2]), w(array_xyzw[3]) { }
92
93 Vec4(const Vec4<Type> &copy) = default;
94
95 template<typename OtherType, typename std::enable_if_t<std::is_integral<Type>::value && !std::is_integral<OtherType>::value, int> = 0>
96 Vec4(const Vec4<OtherType>& copy) : x(static_cast<Type>(std::floor(copy.x + 0.5f))), y(static_cast<Type>(std::floor(copy.y + 0.5f))), z(static_cast<Type>(std::floor(copy.z + 0.5f))), w(static_cast<Type>(std::floor(copy.z + 0.5f))) {}
97
98 template<typename OtherType, typename std::enable_if_t<!std::is_integral<Type>::value || std::is_integral<OtherType>::value, int> = 0>
99 Vec4(const Vec4<OtherType>& copy) : x(static_cast<Type>(copy.x)), y(static_cast<Type>(copy.y)), z(static_cast<Type>(copy.z)), w(static_cast<Type>(copy.z)) {}
100
106 static Vec4<Type> normalize3(const Vec4<Type> &vector);
107
113 static Vec4<Type> normalize4(const Vec4<Type> &vector);
114
122 static Type dot3(const Vec4<Type>& vector1, const Vec4<Type>& vector2) { return vector1.x*vector2.x + vector1.y*vector2.y + vector1.z*vector2.z; }
123
131 static Type dot4(const Vec4<Type>& vector1, const Vec4<Type>& vector2) { return vector1.x*vector2.x + vector1.y*vector2.y + vector1.z*vector2.z + vector1.w*vector2.w; }
132
138 static Vec4<Type> cross3(const Vec4<Type>& vector1, const Vec4<Type>& vector2);
139
149 static Vec4<Type> rotate3(const Vec4<Type>& vector, const Angle &angle, const Vec4<Type>& axis);
150
157 static Vec4<Type> round(const Vec4<Type>& vector);
158
164 static bool is_equal(const Vec4<Type> &first, const Vec4<Type> &second, Type epsilon)
165 {
166 Type diff_x = second.x - first.x; Type diff_y = second.y - first.y; Type diff_z = second.z - first.z; Type diff_w = second.w - first.w;
167 return (diff_x >= -epsilon && diff_x <= epsilon && diff_y >= -epsilon && diff_y <= epsilon && diff_z >= -epsilon && diff_z <= epsilon && diff_w >= -epsilon && diff_w <= epsilon);
168 }
169
170 void set_xy(const Vec2<Type> &new_v) { x = new_v.x; y = new_v.y; }
171 void set_zw(const Vec2<Type> &new_v) { z = new_v.x; w = new_v.y; }
172
178 Type length3() const;
179
185 Type length4() const;
186
191 Vec4<Type> &normalize3();
192
197 Vec4<Type> &normalize4();
198
205 Type dot3(const Vec4<Type>& vector) const { return x*vector.x + y*vector.y + z*vector.z; }
206
213 Type dot4(const Vec4<Type>& vector) const { return x*vector.x + y*vector.y + z*vector.z + w*vector.w; }
214
220 Angle angle3(const Vec4<Type>& vector) const;
221
227 Type distance3(const Vec4<Type>& vector) const;
228
234 Type distance4(const Vec4<Type>& vector) const;
235
242 Vec4<Type> &cross3(const Vec4<Type>& vector);
243
252 Vec4<Type> &rotate3(const Angle &angle, const Vec4<Type>& axis);
253
259 Vec4<Type> &round();
260
265 bool is_equal(const Vec4<Type> &other, Type epsilon) const { return Vec4<Type>::is_equal(*this, other, epsilon); }
266
268 void operator += (const 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 void operator -= (const Vec4<Type>& vector) { x -= vector.x; y -= vector.y; z -= vector.z; w -= vector.w; }
275
277 void operator -= (Type value) { x -= value; y -= value; z -= value; w -= value; }
278
280 Vec4<Type> operator - () const { return Vec4<Type>(-x, -y, -z, -w); }
281
283 void operator *= (const Vec4<Type>& vector) { x *= vector.x; y *= vector.y; z *= vector.z; w *= vector.w; }
284
286 void operator *= (Type value) { x *= value; y *= value; z *= value; w *= value; }
287
289 void operator /= (const Vec4<Type>& vector) { x /= vector.x; y /= vector.y; z /= vector.z; w /= vector.w; }
290
292 void operator /= (Type value) { x /= value; y /= value; z /= value; w /= value; }
293
295 Vec4<Type> &operator = (const Vec4<Type>& vector) = default;
296
298 bool operator == (const Vec4<Type>& vector) const { return ((x == vector.x) && (y == vector.y) && (z == vector.z) && (w == vector.w)); }
299
301 bool operator != (const Vec4<Type>& vector) const { return ((x != vector.x) || (y != vector.y) || (z != vector.z) || (w != vector.w)); }
302
304 bool operator < (const Vec4<Type>& vector) const { return w < vector.w || (w == vector.w && (z < vector.z || (z == vector.z && (y < vector.y || (y == vector.y && x < vector.x))))); }
305 };
306
308 template<typename Type>
309 Vec4<Type> operator + (const Vec4<Type>& v1, const Vec4<Type>& v2) { return Vec4<Type>(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z, v1.w + v2.w); }
310
312 template<typename Type>
313 Vec4<Type> operator + (Type s, const Vec4<Type>& v) { return Vec4<Type>(s + v.x, s + v.y, s + v.z, s + v.w); }
314
316 template<typename Type>
317 Vec4<Type> operator + (const Vec4<Type>& v, Type s) { return Vec4<Type>(v.x + s, v.y + s, v.z + s, v.w + s); }
318
320 template<typename Type>
321 Vec4<Type> operator - (const Vec4<Type>& v1, const Vec4<Type>& v2) { return Vec4<Type>(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z, v1.w - v2.w); }
322
324 template<typename Type>
325 Vec4<Type> operator - (Type s, const Vec4<Type>& v) { return Vec4<Type>(s - v.x, s - v.y, s - v.z, s - v.w); }
326
328 template<typename Type>
329 Vec4<Type> operator - (const Vec4<Type>& v, Type s) { return Vec4<Type>(v.x - s, v.y - s, v.z - s, v.w - s); }
330
332 template<typename Type>
333 Vec4<Type> operator * (const Vec4<Type>& v1, const Vec4<Type>& v2) { return Vec4<Type>(v1.x * v2.x, v1.y * v2.y, v1.z * v2.z, v1.w * v2.w); }
334
336 template<typename Type>
337 Vec4<Type> operator * (Type s, const Vec4<Type>& v) { return Vec4<Type>(s * v.x, s * v.y, s * v.z, s * v.w); }
338
340 template<typename Type>
341 Vec4<Type> operator * (const Vec4<Type>& v, Type s) { return Vec4<Type>(v.x * s, v.y * s, v.z * s, v.w * s); }
342
344 template<typename Type>
345 Vec4<Type> operator / (const Vec4<Type>& v1, const Vec4<Type>& v2) { return Vec4<Type>(v1.x / v2.x, v1.y / v2.y, v1.z / v2.z, v1.w / v2.w); }
346
348 template<typename Type>
349 Vec4<Type> operator / (Type s, const Vec4<Type>& v) { return Vec4<Type>(s / v.x, s / v.y, s / v.z, s / v.w); }
350
352 template<typename Type>
353 Vec4<Type> operator / (const Vec4<Type>& v, Type s) { return Vec4<Type>(v.x / s, v.y / s, v.z / s, v.w / s); }
354
355 template<typename Type>
356 Vec4<Type> operator * (const Vec4<Type>& v, const Mat4<Type>& matrix)
357 {
358 return Vec4<Type>(
359 matrix[0 * 4 + 0] * v.x + matrix[0 * 4 + 1] * v.y + matrix[0 * 4 + 2] * v.z + matrix[0 * 4 + 3] * v.w,
360 matrix[1 * 4 + 0] * v.x + matrix[1 * 4 + 1] * v.y + matrix[1 * 4 + 2] * v.z + matrix[1 * 4 + 3] * v.w,
361 matrix[2 * 4 + 0] * v.x + matrix[2 * 4 + 1] * v.y + matrix[2 * 4 + 2] * v.z + matrix[2 * 4 + 3] * v.w,
362 matrix[3 * 4 + 0] * v.x + matrix[3 * 4 + 1] * v.y + matrix[3 * 4 + 2] * v.z + matrix[3 * 4 + 3] * v.w);
363 }
364
365 template<typename Type>
366 Vec4<Type> operator * (const Mat4<Type>& matrix, const Vec4<Type>& v)
367 {
368 return Vec4<Type>(
369 matrix[0 * 4 + 0] * v.x + matrix[1 * 4 + 0] * v.y + matrix[2 * 4 + 0] * v.z + matrix[3 * 4 + 0] * v.w,
370 matrix[0 * 4 + 1] * v.x + matrix[1 * 4 + 1] * v.y + matrix[2 * 4 + 1] * v.z + matrix[3 * 4 + 1] * v.w,
371 matrix[0 * 4 + 2] * v.x + matrix[1 * 4 + 2] * v.y + matrix[2 * 4 + 2] * v.z + matrix[3 * 4 + 2] * v.w,
372 matrix[0 * 4 + 3] * v.x + matrix[1 * 4 + 3] * v.y + matrix[2 * 4 + 3] * v.z + matrix[3 * 4 + 3] * v.w);
373 }
374
375 template<typename Type>
376 inline Type Vec4<Type>::length3() const { return (Type)floor(sqrt(float(x*x + y*y + z*z)) + 0.5f); }
377
378 template<>
379 inline double Vec4<double>::length3() const { return sqrt(x*x + y*y + z*z); }
380
381 template<>
382 inline float Vec4<float>::length3() const { return sqrt(x*x + y*y + z*z); }
383
384 template<typename Type>
385 inline Type Vec4<Type>::length4() const { return (Type)floor(sqrt(float(x*x + y*y + z*z + w*w)) + 0.5f); }
386
387 template<>
388 inline double Vec4<double>::length4() const { return sqrt(x*x + y*y + z*z + w*w); }
389
390 template<>
391 inline float Vec4<float>::length4() const { return sqrt(x*x + y*y + z*z + w*w); }
392
393 typedef Vec4<unsigned char> Vec4ub;
394 typedef Vec4<char> Vec4b;
395 typedef Vec4<unsigned short> Vec4us;
396 typedef Vec4<short> Vec4s;
397 typedef Vec4<unsigned int> Vec4ui;
398 typedef Vec4<int> Vec4i;
399 typedef Vec4<float> Vec4f;
400 typedef Vec4<double> Vec4d;
401
403}
clan::Angle
Angle class.
Definition angle.h:60
clan::Mat2
2D matrix
Definition mat2.h:59
clan::Mat3
3D matrix
Definition mat3.h:60
clan::Mat4
4D matrix
Definition mat4.h:78
clan::Pointx
2D (x,y) point structure.
Definition point.h:52
clan::Sizex
2D (width,height) size structure.
Definition size.h:55
clan::Vec2
2D vector
Definition vec2.h:76
clan::Vec2::y
Type y
Definition vec2.h:81
clan::Vec2::x
Type x
Definition vec2.h:80
clan::Vec3
3D vector
Definition vec3.h:75
clan::Vec4
4D vector
Definition vec4.h:75
clan::Vec4< unsigned char >::u
unsigned char u
Definition vec4.h:81
clan::Vec4< unsigned char >::s
unsigned char s
Definition vec4.h:79
clan::Vec4::operator!=
bool operator!=(const Vec4< Type > &vector) const
!= operator.
Definition vec4.h:301
clan::Vec4< unsigned char >::a
unsigned char a
Definition vec4.h:82
clan::Vec4< unsigned char >::r
unsigned char r
Definition vec4.h:79
clan::Vec4::operator=
Vec4< Type > & operator=(const Vec4< Type > &vector)=default
= operator.
clan::Vec4< unsigned char >::z
unsigned char z
Definition vec4.h:81
clan::Vec4::rotate3
static Vec4< Type > rotate3(const Vec4< Type > &vector, const Angle &angle, const Vec4< Type > &axis)
Rotate a vector around an axis. Same as glRotate[f|d](angle, a);.
clan::Vec4::Vec4
Vec4(const Vec2< Type > &copy, const Type &p3, const Type &p4)
Definition vec4.h:86
clan::Vec4::Vec4
Vec4()
Definition vec4.h:84
clan::Vec4::operator*=
void operator*=(const Vec4< Type > &vector)
*= operator.
Definition vec4.h:283
clan::Vec4::normalize3
Vec4< Type > & normalize3()
Normalizes this vector (not taking into account the w ordinate)
clan::Vec4::is_equal
static bool is_equal(const Vec4< Type > &first, const Vec4< Type > &second, Type epsilon)
Returns true if equal within the bounds of an epsilon.
Definition vec4.h:164
clan::Vec4::dot3
Type dot3(const Vec4< Type > &vector) const
Dot products this vector with an other vector (not taking into account the w ordinate).
Definition vec4.h:205
clan::Vec4< unsigned char >::y
unsigned char y
Definition vec4.h:80
clan::Vec4::Vec4
Vec4(const Type &scalar)
Definition vec4.h:85
clan::Vec4::operator==
bool operator==(const Vec4< Type > &vector) const
== operator.
Definition vec4.h:298
clan::Vec4::cross3
static Vec4< Type > cross3(const Vec4< Type > &vector1, const Vec4< Type > &vector2)
Calculate the cross product between two vectors (not taking into account the w ordinate).
clan::Vec4::Vec4
Vec4(const Vec4< Type > &copy)=default
clan::Vec4< unsigned char >::x
unsigned char x
Definition vec4.h:79
clan::Vec4::set_xy
void set_xy(const Vec2< Type > &new_v)
Definition vec4.h:170
clan::Vec4< unsigned char >::v
unsigned char v
Definition vec4.h:82
clan::Vec4::operator-
Vec4< Type > operator-() const
operator.
Definition vec4.h:280
clan::Vec4::angle3
Angle angle3(const Vec4< Type > &vector) const
Calculate the angle between this vector and an other vector (not taking into account the w ordinate).
clan::Vec4::normalize4
Vec4< Type > & normalize4()
Normalizes this vector (taking into account the w ordinate)
clan::Vec4::round
static Vec4< Type > round(const Vec4< Type > &vector)
Rounds all components on a vector.
clan::Vec4::distance3
Type distance3(const Vec4< Type > &vector) const
Calculate the distance between this vector and an other vector (not taking into account the w ordinat...
clan::Vec4::rotate3
Vec4< Type > & rotate3(const Angle &angle, const Vec4< Type > &axis)
Rotate this vector around an axis. Same as glRotate[f|d](angle, a);.
clan::Vec4::normalize4
static Vec4< Type > normalize4(const Vec4< Type > &vector)
Normalizes a vector (taking into account the w ordinate)
clan::Vec4::operator+=
void operator+=(const Vec4< Type > &vector)
+= operator.
Definition vec4.h:268
clan::Vec4::Vec4
Vec4(const Vec3< Type > &copy, const Type &p4)
Definition vec4.h:88
clan::Vec4::Vec4
Vec4(const Vec4< OtherType > &copy)
Definition vec4.h:96
clan::Vec4::Vec4
Vec4(const Type &p1, const Type &p2, const Type &p3, const Type &p4)
Definition vec4.h:89
clan::Vec4::distance4
Type distance4(const Vec4< Type > &vector) const
Calculate the distance between this vector and an other vector (taking into account the w ordinate).
clan::Vec4::Vec4
Vec4(const Vec2< Type > &copy, const Vec2< Type > &copy34)
Definition vec4.h:87
clan::Vec4::operator<
bool operator<(const Vec4< Type > &vector) const
< operator.
Definition vec4.h:304
clan::Vec4< unsigned char >::w
unsigned char w
Definition vec4.h:82
clan::Vec4::round
Vec4< Type > & round()
Rounds all components on this vector.
clan::Vec4::dot4
Type dot4(const Vec4< Type > &vector) const
Dot products this vector with an other vector (taking into account the w ordinate).
Definition vec4.h:213
clan::Vec4::dot3
static Type dot3(const Vec4< Type > &vector1, const Vec4< Type > &vector2)
Dot products between two vectors (not taking into account the w ordinate).
Definition vec4.h:122
clan::Vec4::is_equal
bool is_equal(const Vec4< Type > &other, Type epsilon) const
Returns true if equal within the bounds of an epsilon.
Definition vec4.h:265
clan::Vec4::Vec4
Vec4(const Type *array_xyzw)
Definition vec4.h:91
clan::Vec4::operator/=
void operator/=(const Vec4< Type > &vector)
/= operator.
Definition vec4.h:289
clan::Vec4::datatype
Type datatype
Definition vec4.h:77
clan::Vec4::operator-=
void operator-=(const Vec4< Type > &vector)
-= operator.
Definition vec4.h:274
clan::Vec4::Vec4
Vec4(const Type &p1, const Type &p2, const Vec2< Type > &copy34)
Definition vec4.h:90
clan::Vec4::dot4
static Type dot4(const Vec4< Type > &vector1, const Vec4< Type > &vector2)
Dot products between two vectors (taking into account the w ordinate).
Definition vec4.h:131
clan::Vec4< unsigned char >::b
unsigned char b
Definition vec4.h:81
clan::Vec4::cross3
Vec4< Type > & cross3(const Vec4< Type > &vector)
Calculate the cross product between this vector and an other vector (not taking into account the w or...
clan::Vec4< unsigned char >::g
unsigned char g
Definition vec4.h:80
clan::Vec4::normalize3
static Vec4< Type > normalize3(const Vec4< Type > &vector)
Normalizes a vector (not taking into account the w ordinate)
clan::Vec4::set_zw
void set_zw(const Vec2< Type > &new_v)
Definition vec4.h:171
clan::Vec4< unsigned char >::t
unsigned char t
Definition vec4.h:80
clan::operator/
Vec2< Type > operator/(const Vec2< Type > &v1, const Vec2< Type > &v2)
/ operator.
Definition vec2.h:306
clan::Vec4ub
Vec4< unsigned char > Vec4ub
Definition vec4.h:393
clan::Vec4::length4
Type length4() const
Returns the length (magnitude) of this vector (taking into account the w ordinate).
Definition vec4.h:385
clan::Vec4ui
Vec4< unsigned int > Vec4ui
Definition vec4.h:397
clan::Vec4b
Vec4< char > Vec4b
Definition vec4.h:394
clan::Vec4f
Vec4< float > Vec4f
Definition vec4.h:399
clan::operator-
Vec2< Type > operator-(const Vec2< Type > &v1, const Vec2< Type > &v2)
operator.
Definition vec2.h:282
clan::Vec4::length3
Type length3() const
Returns the length (magnitude) of this vector (not taking into account the w ordinate).
Definition vec4.h:376
clan::operator+
Vec2< Type > operator+(const Vec2< Type > &v1, const Vec2< Type > &v2)
operator.
Definition vec2.h:270
clan::Vec4s
Vec4< short > Vec4s
Definition vec4.h:396
clan::Vec4i
Vec4< int > Vec4i
Definition vec4.h:398
clan::Vec4d
Vec4< double > Vec4d
Definition vec4.h:400
clan::operator*
Vec2< Type > operator*(const Vec2< Type > &v1, const Vec2< Type > &v2)
operator.
Definition vec2.h:294
clan::Vec4us
Vec4< unsigned short > Vec4us
Definition vec4.h:395
clan
Definition clanapp.h:36
clan::StyleValueType::angle
@ angle
value is a color
Definition style_value_type.h:44
clan::Key::s
@ s
Definition keys.h:99
clan::Key::v
@ v
Definition keys.h:102