Lamp-Da 0.1
A compact lantern project
Loading...
Searching...
No Matches
vector_math.h
Go to the documentation of this file.
1
5#ifndef UTILS_VECTOR_MATH_H
6#define UTILS_VECTOR_MATH_H
7
8namespace lampda {
9namespace utils {
10
14struct vec2d
15{
16 float x;
17 float y;
18
20 vec2d() : x(0), y(0) {}
22 vec2d(const vec2d& other) = default;
24 vec2d(const float _x, const float _y) : x(_x), y(_y) {}
25
27 float dot(const vec2d& other) const { return this->x * other.x + this->y * other.y; }
29 vec2d multiply(const vec2d& other) const { return vec2d(this->x * other.x, this->y * other.y); }
31 vec2d multiply(const float mult) const { return vec2d(this->x * mult, this->y * mult); }
33 vec2d add(const vec2d& other) const { return vec2d(this->x + other.x, this->y + other.y); }
35 vec2d add(const float mult) const { return vec2d(this->x + mult, this->y + mult); }
36
38 vec2d& operator=(const vec2d& other)
39 {
40 // Guard self assignment
41 if (this == &other)
42 return *this;
43
44 this->x = other.x;
45 this->y = other.y;
46 return *this;
47 }
48};
49
53struct vec3d : public vec2d
54{
55 float z;
56
58 vec3d() : vec2d(), z(0) {}
60 vec3d(const float _x, const float _y, const float _z) : vec2d(_x, _y), z(_z) {}
62 vec3d(const vec2d& res, const float _z) : vec2d(res), z(_z) {}
63
65 float dot(const vec3d& other) const { return vec2d::dot(other) + this->z * other.z; }
67 vec3d multiply(const vec3d& other) const { return vec3d(vec2d::multiply(other), this->z * other.z); }
69 vec3d multiply(const float mult) const { return vec3d(vec2d::multiply(mult), this->z * mult); }
71 vec3d add(const vec3d& other) const { return vec3d(vec2d::add(other), this->z + other.z); }
73 vec3d add(const float mult) const { return vec3d(vec2d::add(mult), this->z + mult); }
74
76 vec3d& operator=(const vec3d& other)
77 {
78 // Guard self assignment
79 if (this == &other)
80 return *this;
81
82 this->x = other.x;
83 this->y = other.y;
84 this->z = other.z;
85 return *this;
86 }
87};
88
92struct vec4d : public vec3d
93{
94 float w;
95
97 vec4d() : vec3d(), w(0) {}
99 vec4d(const float _x, const float _y, const float _z, const float _w) : vec3d(_x, _y, _z), w(_w) {}
101 vec4d(const vec3d& res, const float _w) : vec3d(res), w(_w) {}
102
104 float dot(const vec4d& other) const { return vec3d::dot(other) + this->w * other.w; }
106 vec4d multiply(const vec4d& other) const { return vec4d(vec3d::multiply(other), this->w * other.w); }
108 vec4d multiply(const float mult) const { return vec4d(vec3d::multiply(mult), this->w * mult); }
110 vec4d add(const vec4d& other) const { return vec4d(vec3d::add(other), this->w + other.w); }
112 vec4d add(const float mult) const { return vec4d(vec3d::add(mult), this->w + mult); }
113
115 vec4d& operator=(const vec4d& other)
116 {
117 // Guard self assignment
118 if (this == &other)
119 return *this;
120
121 this->x = other.x;
122 this->y = other.y;
123 this->z = other.z;
124 this->w = other.w;
125 return *this;
126 }
127};
128
133{
134 // first line
135 float R11;
136 float R12;
137 float R13;
138
139 // second line
140 float R21;
141 float R22;
142 float R23;
143
144 // third line
145 float R31;
146 float R32;
147 float R33;
148
149 vec3d col1() const { return vec3d(R11, R21, R31); }
150 vec3d col2() const { return vec3d(R12, R22, R32); }
151 vec3d col3() const { return vec3d(R13, R23, R33); }
152
153 vec3d row1() const { return vec3d(R11, R12, R13); }
154 vec3d row2() const { return vec3d(R21, R22, R23); }
155 vec3d row3() const { return vec3d(R31, R32, R33); }
156
160 vec3d transform(const vec3d& vec) const;
161
165 RotationMatrix compose(const RotationMatrix& other) const;
166
170 void from_angles_XYZ(const float x_rad, const float y_rad, const float z_rad);
174 void from_angles_XYZ(const vec3d& angles_rad);
175
176private:
180 void from_angles_ZYX(const vec3d& angles_rad);
181};
182
187{
192
196 TransformationMatrix(const RotationMatrix& rot, const vec3d& trans);
198 TransformationMatrix(const vec3d& euler, const vec3d& trans);
199
205 vec3d transform(const vec3d& vec) const;
206};
207
208} // namespace utils
209} // namespace lampda
210
211#endif
Program scope.
Definition: control_fixed_modes.hpp:12
Represent an XYZ rotation matrix.
Definition: vector_math.h:133
float R11
row 1, index 1
Definition: vector_math.h:135
float R31
row 3, index 1
Definition: vector_math.h:145
float R32
row 3, index 2
Definition: vector_math.h:146
float R22
row 2, index 2
Definition: vector_math.h:141
vec3d col2() const
Access the matrix by columns.
Definition: vector_math.h:150
void from_angles_XYZ(const float x_rad, const float y_rad, const float z_rad)
create a rotation matrix from three angles in radian (XYZ)
Definition: vector_math.cpp:33
float R13
row 1, index 3
Definition: vector_math.h:137
vec3d transform(const vec3d &vec) const
transform a vector by this rotation
Definition: vector_math.cpp:7
vec3d col1() const
Access the matrix by columns.
Definition: vector_math.h:149
float R23
row 2, index 3
Definition: vector_math.h:142
float R33
row 3, index 3
Definition: vector_math.h:147
float R12
row 1, index 2
Definition: vector_math.h:136
vec3d row1() const
Access the matrix by row.
Definition: vector_math.h:153
RotationMatrix compose(const RotationMatrix &other) const
Compose this roation with another.
Definition: vector_math.cpp:16
vec3d col3() const
Access the matrix by columns.
Definition: vector_math.h:151
float R21
row 2, index 1
Definition: vector_math.h:140
vec3d row2() const
Access the matrix by row.
Definition: vector_math.h:154
vec3d row3() const
Access the matrix by row.
Definition: vector_math.h:155
Transform a vector from a space to another.
Definition: vector_math.h:187
vec3d transform(const vec3d &vec) const
Transform a vector by this transformation.
Definition: vector_math.cpp:76
TransformationMatrix()=default
Constructor.
vec3d translation
translation part of the matrix
Definition: vector_math.h:191
RotationMatrix rotation
Rotation part of the matrix.
Definition: vector_math.h:189
2d vector in any space
Definition: vector_math.h:15
vec2d(const float _x, const float _y)
Construct by parametersr.
Definition: vector_math.h:24
float dot(const vec2d &other) const
Dot product in 2D, representing the arcos of the angle between this vector and another.
Definition: vector_math.h:27
vec2d add(const vec2d &other) const
Coefficient wise addition.
Definition: vector_math.h:33
vec2d & operator=(const vec2d &other)
Copy operator.
Definition: vector_math.h:38
vec2d multiply(const vec2d &other) const
Coefficient wise multiplication.
Definition: vector_math.h:29
vec2d multiply(const float mult) const
Coefficient wise multiplication by a constant.
Definition: vector_math.h:31
vec2d add(const float mult) const
Coefficient wise addition by a constant.
Definition: vector_math.h:35
vec2d(const vec2d &other)=default
Construct by copy.
float x
x coordinate in 2D
Definition: vector_math.h:16
float y
y coordinate in 2D
Definition: vector_math.h:17
vec2d()
Default constructor.
Definition: vector_math.h:20
3d vector in any space
Definition: vector_math.h:54
vec3d add(const vec3d &other) const
Coefficient wise addition.
Definition: vector_math.h:71
vec3d(const vec2d &res, const float _z)
Construct by 2D vector and parameter.
Definition: vector_math.h:62
float dot(const vec3d &other) const
Dot product in 3D, representing the arcos of the angle between this vector and another.
Definition: vector_math.h:65
vec3d multiply(const vec3d &other) const
Coefficient wise multiplication.
Definition: vector_math.h:67
vec3d & operator=(const vec3d &other)
Copy operator.
Definition: vector_math.h:76
vec3d(const float _x, const float _y, const float _z)
Construct by parameters.
Definition: vector_math.h:60
vec3d multiply(const float mult) const
Coefficient wise multiplication by a constant.
Definition: vector_math.h:69
vec3d add(const float mult) const
Coefficient wise addition by a constant.
Definition: vector_math.h:73
vec3d()
Default constructor.
Definition: vector_math.h:58
float z
z coordinate of a 3D vector
Definition: vector_math.h:55
4d vector in any space
Definition: vector_math.h:93
vec4d(const float _x, const float _y, const float _z, const float _w)
Construct from parameters.
Definition: vector_math.h:99
float w
4d part of the coordinate
Definition: vector_math.h:94
vec4d & operator=(const vec4d &other)
Egality affectation.
Definition: vector_math.h:115
vec4d add(const float mult) const
Coefficient wise addition by a constant.
Definition: vector_math.h:112
vec4d()
Default constructor.
Definition: vector_math.h:97
float dot(const vec4d &other) const
Dot product, representing the angle arcos from this vector to another.
Definition: vector_math.h:104
vec4d(const vec3d &res, const float _w)
Construct from 3D and 4D part.
Definition: vector_math.h:101
vec4d multiply(const vec4d &other) const
Coefficient wise multiplication.
Definition: vector_math.h:106
vec4d multiply(const float mult) const
Coefficient wise multiplication by a constant.
Definition: vector_math.h:108
vec4d add(const vec4d &other) const
Coefficient wise addition.
Definition: vector_math.h:110