16#pragma warning(disable: 4804)
17#pragma warning(disable: 4146)
48 static constexpr Matrix3 scale( T s )
noexcept {
return Matrix3( { s, T(0), T(0) }, { T(0), s, T(0) }, { T(0), T(0), s } ); }
50 static constexpr Matrix3 scale( T sx, T sy, T sz )
noexcept {
return Matrix3( { sx, T(0), T(0) }, { T(0), sy, T(0) }, { T(0), T(0), sz } ); }
75 constexpr T
trace() const noexcept {
return x.x +
y.y +
z.z; }
77 constexpr T
normSq() const noexcept {
return x.lengthSq() +
y.lengthSq() +
z.lengthSq(); }
78 constexpr auto norm() const noexcept
86 constexpr T
det() const noexcept;
101 [[nodiscard]] friend constexpr
bool operator ==( const
Matrix3<T> & a, const
Matrix3<T> & b ) {
return a.x == b.x && a.y == b.y && a.z == b.z; }
112 if constexpr ( std::is_integral_v<T> )
113 return { b.x / a, b.y / a, b.z / a };
115 return b * ( 1 / a );
123 if constexpr ( std::is_integral_v<T> )
124 { a.
x /= b; a.
y /= b; a.
z /= b;
return a; }
126 return a *= ( 1 / b );
132 return {
dot( a.x, b ),
dot( a.y, b ),
dot( a.z, b ) };
139 for (
int i = 0; i < 3; ++i )
140 for (
int j = 0; j < 3; ++j )
141 res[i][j] =
dot( a[i], b.col(j) );
147 return s << mat.
x <<
'\n' << mat.
y <<
'\n' << mat.
z <<
'\n';
152 return s >> mat.
x >> mat.
y >> mat.
z;
163 return dot( a.x, b.x ) + dot( a.y, b.y ) + dot( a.z, b.z );
170 return { a.
x * b, a.
y * b, a.
z * b };
177 auto u = axis.normalized();
182 { c + u.x * u.x * oc, u.x * u.y * oc - u.z * s, u.x * u.z * oc + u.y * s },
183 { u.y * u.x * oc + u.z * s, c + u.y * u.y * oc, u.y * u.z * oc - u.x * s },
184 { u.z * u.x * oc - u.y * s, u.z * u.y * oc + u.x * s, c + u.z * u.z * oc }
189constexpr Matrix3<T> Matrix3<T>::rotation(
const Vector3<T> & from,
const Vector3<T> & to )
noexcept MR_REQUIRES_IF_SUPPORTED( std::floating_point<T> )
191 auto axis = cross( from, to );
192 if ( axis.lengthSq() > 0 )
193 return rotation( axis,
angle( from, to ) );
194 if (
dot( from, to ) >= 0 )
196 return rotation( cross( from, from.furthestBasisVector() ), T( PI ) );
200constexpr Matrix3<T> Matrix3<T>::rotationFromEuler(
const Vector3<T> & eulerAngles )
noexcept MR_REQUIRES_IF_SUPPORTED( std::is_floating_point_v<T> )
203 const auto cx = std::cos( eulerAngles.x );
204 const auto cy = std::cos( eulerAngles.y );
205 const auto cz = std::cos( eulerAngles.z );
206 const auto sx = std::sin( eulerAngles.x );
207 const auto sy = std::sin( eulerAngles.y );
208 const auto sz = std::sin( eulerAngles.z );
210 { cy * cz, cz * sx * sy - cx * sz, cx * cz * sy + sx * sz },
211 { cy * sz, cx * cz + sx * sy * sz, -cz * sx + cx * sy * sz },
212 { -sy, cy * sx, cx * cy }
217constexpr Matrix3<T> Matrix3<T>::approximateLinearRotationMatrixFromEuler(
const Vector3<T> & eulerAngles )
noexcept MR_REQUIRES_IF_SUPPORTED( std::is_floating_point_v<T> )
219 const auto alpha = eulerAngles.x;
220 const auto beta = eulerAngles.y;
221 const auto gamma = eulerAngles.z;
223 { T(1), -gamma, beta },
224 { gamma, T(1), -alpha },
225 { -beta, alpha, T(1) }
233 x.x * ( y.y * z.z - y.z * z.y )
234 - x.y * ( y.x * z.z - y.z * z.x )
235 + x.z * ( y.x * z.y - y.y * z.x );
241 auto det = this->det();
246 { y.y * z.z - y.z * z.y, x.z * z.y - x.y * z.z, x.y * y.z - x.z * y.y },
247 { y.z * z.x - y.x * z.z, x.x * z.z - x.z * z.x, x.z * y.x - x.x * y.z },
248 { y.x * z.y - y.y * z.x, x.y * z.x - x.x * z.y, x.x * y.y - x.y * y.x }
264constexpr Vector3<T> Matrix3<T>::toEulerAngles() const noexcept
MR_REQUIRES_IF_SUPPORTED( std::is_floating_point_v<T> )
268 std::atan2( z.y, z.z ),
269 std::atan2( -z.x, std::sqrt( z.y * z.y + z.z * z.z ) ),
270 std::atan2( y.x, x.x )
278 const auto a0 = col( 0 );
281 const auto r00 = a0.length();
282 const auto e0 = r00 > 0 ? a0 / r00 : Vector3<T>{};
283 const auto r01 =
dot( e0, a1 );
284 const auto r02 =
dot( e0, a2 );
286 const auto r11 = a1.length();
287 const auto e1 = r11 > 0 ? a1 / r11 : Vector3<T>{};
288 const auto r12 =
dot( e1, a2 );
289 a2 -= r02 * e0 + r12 * e1;
290 const auto r22 = a2.length();
291 const auto e2 = r22 > 0 ? a2 / r22 : Vector3<T>{};
#define MR_REQUIRES_IF_SUPPORTED(...)
Definition MRMacros.h:34
static constexpr Matrix3 scale(T sx, T sy, T sz) noexcept
returns a matrix that has its own scale along each axis
Definition MRMatrix3.h:50
Matrix3< T > outer(const Vector3< T > &a, const Vector3< T > &b)
x = a * b^T
Definition MRMatrix3.h:168
T ValueType
Definition MRMatrix3.h:25
static constexpr Matrix3 identity() noexcept
Definition MRMatrix3.h:46
friend constexpr auto operator*(T a, const Matrix3< T > &b) -> Matrix3< decltype(std::declval< T >() *std::declval< T >())>
Definition MRMatrix3.h:108
Matrix3 q
Definition MRMatrix3.h:96
constexpr Matrix3< T > inverse() const noexcept MR_REQUIRES_IF_SUPPORTED(!std constexpr Matrix3< T > transposed() const noexcept
computes inverse matrix
constexpr T det() const noexcept
computes determinant of the matrix
Vector3< T > x
rows, identity matrix by default
Definition MRMatrix3.h:29
constexpr const Vector3< T > & operator[](int row) const noexcept
row access
Definition MRMatrix3.h:68
friend constexpr Matrix3< T > & operator-=(Matrix3< T > &a, const Matrix3< T > &b)
Definition MRMatrix3.h:119
constexpr Matrix3(const Vector3< T > &x, const Vector3< T > &y, const Vector3< T > &z)
initializes matrix from its 3 rows
Definition MRMatrix3.h:38
T x
Definition MRVector3.h:39
constexpr T normSq() const noexcept
compute sum of squared matrix elements
Definition MRMatrix3.h:77
T y
Definition MRVector3.h:39
friend constexpr Matrix3< T > & operator*=(Matrix3< T > &a, T b)
Definition MRMatrix3.h:120
friend constexpr Matrix3< T > & operator+=(Matrix3< T > &a, const Matrix3< T > &b)
Definition MRMatrix3.h:118
MR_REQUIRES_IF_SUPPORTED(!std::is_same_v< T, U >) const expr explicit Matrix3(const Matrix3< U > &m)
Definition MRMatrix3.h:42
constexpr auto norm() const noexcept
Definition MRMatrix3.h:78
constexpr Vector3< T > col(int i) const noexcept
column access
Definition MRMatrix3.h:72
static constexpr Matrix3 scale(T s) noexcept
returns a matrix that scales uniformly
Definition MRMatrix3.h:48
constexpr T trace() const noexcept
computes trace of the matrix
Definition MRMatrix3.h:75
friend constexpr bool operator!=(const Matrix3< T > &a, const Matrix3< T > &b)
Definition MRMatrix3.h:102
static constexpr Matrix3 fromColumns(const Vector3< T > &x, const Vector3< T > &y, const Vector3< T > &z) noexcept
Definition MRMatrix3.h:65
constexpr Matrix3() noexcept
Definition MRMatrix3.h:33
friend std::istream & operator>>(std::istream &s, Matrix3 &mat)
Definition MRMatrix3.h:150
Vector3< T > y
Definition MRMatrix3.h:30
static constexpr Matrix3 zero() noexcept
Definition MRMatrix3.h:45
auto dot(const Matrix3< T > &a, const Matrix3< T > &b) -> decltype(dot(a.x, b.x))
double-dot product: x = a : b
Definition MRMatrix3.h:161
friend constexpr auto operator-(const Matrix3< T > &a, const Matrix3< T > &b) -> Matrix3< decltype(std::declval< T >() - std::declval< T >())>
Definition MRMatrix3.h:107
friend std::ostream & operator<<(std::ostream &s, const Matrix3 &mat)
Definition MRMatrix3.h:145
friend constexpr auto operator+(const Matrix3< T > &a, const Matrix3< T > &b) -> Matrix3< decltype(std::declval< T >()+std::declval< T >())>
NOTE: We use std::declval() in the operators below because libclang 18 in our binding generator is bu...
Definition MRMatrix3.h:106
friend constexpr auto operator/(Matrix3< T > b, T a) -> Matrix3< decltype(std::declval< T >()/std::declval< T >())>
Definition MRMatrix3.h:110
static constexpr Matrix3 static rotation(const Vector3< T > &axis, T angle) noexcept MR_REQUIRES_IF_SUPPORTED(std constexpr Matrix3 static rotation(const Vector3< T > &from, const Vector3< T > &to) noexcept MR_REQUIRES_IF_SUPPORTED(std constexpr Matrix3 static rotationFromEuler(const Vector3< T > &eulerAngles) noexcept MR_REQUIRES_IF_SUPPORTED(std constexpr Matrix3 static approximateLinearRotationMatrixFromEuler(const Vector3< T > &eulerAngles) noexcept MR_REQUIRES_IF_SUPPORTED(std constexpr Matrix fromRows)(const Vector3< T > &x, const Vector3< T > &y, const Vector3< T > &z) noexcept
creates matrix representing rotation around given axis on given angle
Definition MRMatrix3.h:62
Vector3< T > z
Definition MRMatrix3.h:31
T z
Definition MRVector3.h:39
friend constexpr Matrix3< T > & operator/=(Matrix3< T > &a, T b)
Definition MRMatrix3.h:121
static constexpr Matrix3 scale(const Vector3< T > &s) noexcept
Definition MRMatrix3.h:51
@ angle
Direction, normally Vector3f.
constexpr auto dot(A a, A b)
Definition MRImGuiVectorOperators.h:129
only for bindings generation
Definition MRCameraOrientationPlugin.h:8
returns 3 Euler angles, assuming this is a rotation matrix composed as follows: R=R(z)*R(y)*R(x)
Definition MRMatrix3.h:95
Definition MRMatrix3.h:24
Definition MRVector3.h:33