37#ifdef TRAX_OPEN_SOURCE
39# pragma warning( disable: 4127 )
41# include <Eigen/Dense>
51 template<
typename>
struct Vector;
54 template<
typename,
typename>
struct Frame;
61 template<
typename Valtype, const
unsigned short nCols, const
unsigned short nRows >
65 typedef Valtype value_type;
72 Matrix(
const Matrix& matrix );
73 Matrix( Matrix&& matrix )
noexcept;
74 explicit Matrix(
const std::initializer_list<Valtype>& elements );
75 ~Matrix()
noexcept =
default;
83 Matrix& operator=(
const Matrix& matrix )
noexcept;
85 Matrix& operator=( Matrix&& matrix )
noexcept;
87 Matrix& operator=(
const std::initializer_list<Valtype>& elements )
noexcept;
94 const Valtype& operator()(
unsigned short col,
unsigned short row )
const noexcept;
96 Valtype& operator()(
unsigned short col,
unsigned short row )
noexcept;
105 constexpr
unsigned short Cols() const noexcept;
109 constexpr
unsigned short Rows() const noexcept;
113 bool IsEqual( const Matrix& matrix, Valtype delta = 0 ) const noexcept;
117 bool operator==( const Matrix& matrix ) const noexcept;
121 bool operator!=( const Matrix& matrix ) const noexcept;
125 void operator*=( Valtype skalar ) noexcept;
129 void operator*=( const Matrix<Valtype,nCols,nCols>& matrix );
133 void operator/=( Valtype skalar ) noexcept;
137 void operator+=( const Matrix& matrix ) noexcept;
141 void operator-=( const Matrix& matrix ) noexcept;
146 const Valtype*
ptr() const noexcept;
151 Matrix<Valtype,nCols-1,nRows-1>
SubMatrix(
unsigned short c,
unsigned short r ) const;
153 std::unique_ptr<Valtype[]> m;
158 template<typename Valtype,const
unsigned short nCols> inline
159 constexpr
Vector<Valtype>
Column( const Matrix<Valtype,nCols,3>& m,
unsigned short idx ) noexcept;
163 template<typename Valtype,const
unsigned short nRows> inline
164 constexpr
Vector<Valtype>
Row( const Matrix<Valtype,3,nRows>& m,
unsigned short idx ) noexcept;
168 template<typename Valtype, const
unsigned short nCols, const
unsigned short nRows >
169 constexpr Matrix<Valtype,nRows,nCols>
Transposed( const Matrix<Valtype,nCols,nRows>& m ) noexcept;
174 template< typename Valtype,
176 const
unsigned short nCols,
177 const
unsigned short nRows > inline
178 auto operator*( Valtype skalar, const Matrix<Valtype2,nCols,nRows>& matrix ) noexcept -> Matrix<decltype(Valtype{}*Valtype2{}),nCols,nRows>;
190 template<
typename Valtype,
192 unsigned short nColsFirst_RowsSecond,
193 unsigned short nRowsFirst,
194 unsigned short nColsSecond >
inline
195 auto operator*(
const Matrix<Valtype,nColsFirst_RowsSecond,nRowsFirst>& first,
196 const Matrix<Valtype2,nColsSecond,nColsFirst_RowsSecond>& second )
noexcept -> Matrix<
decltype(Valtype{}*Valtype2{}),nColsSecond,nRowsFirst>;
199 template<
typename Valtype,
201 unsigned short nCols,
202 unsigned short nRows >
inline
203 Matrix<Valtype,nCols,nRows>&
operator*=( Matrix<Valtype,nCols,nRows>& first, Valtype2 skalar )
noexcept;
206 template<
typename Valtype,
208 unsigned short nCols,
209 unsigned short nRows >
inline
210 Matrix<Valtype,nCols,nRows>&
operator/=( Matrix<Valtype,nCols,nRows>& first, Valtype2 skalar )
noexcept;
213 template<
typename Valtype,
214 const unsigned short nCols,
215 const unsigned short nRows >
inline
217 const Matrix<Valtype,nCols,nRows>& first,
218 const Matrix<Valtype,nCols,nRows>& second )
noexcept;
221 template<
typename Valtype,
222 const unsigned short nCols,
223 const unsigned short nRows >
inline
225 const Matrix<Valtype,nCols,nRows>& first,
226 const Matrix<Valtype,nCols,nRows>& second )
noexcept;
229 template<
typename Valtype,
230 const unsigned short nCols,
231 const unsigned short nRows >
inline
232 constexpr Matrix<Valtype,nCols,nRows>&
operator+( Matrix<Valtype,nCols,nRows>& matrix )
noexcept;
235 template<
typename Valtype,
236 const unsigned short nCols,
237 const unsigned short nRows >
inline
238 Matrix<Valtype,nCols,nRows>&
operator-( Matrix<Valtype,nCols,nRows>& matrix )
noexcept;
246 template<
typename Valtype,
248 const unsigned short nCols,
249 const unsigned short nRows >
inline
250 void copy_column_major(
const Matrix<Valtype,nCols,nRows>& source, Valtype2* target )
noexcept;
252 template<
typename Valtype,
254 const unsigned short nCols,
255 const unsigned short nRows >
inline
256 void copy_column_major(
const Valtype2* source, Matrix<Valtype,nCols,nRows>& target )
noexcept;
258 template<
typename Valtype,
260 const unsigned short nCols,
261 const unsigned short nRows >
inline
262 void copy_row_major(
const Matrix<Valtype,nCols,nRows>& source, Valtype2* target )
noexcept;
264 template<
typename Valtype,
266 const unsigned short nCols,
267 const unsigned short nRows >
inline
268 void copy_row_major(
const Valtype2* source, Matrix<Valtype,nCols,nRows>& target )
noexcept;
276 template<
typename Valtype, const
unsigned short nColsAndRows>
277 class SquareMatrix :
public Matrix<Valtype,nColsAndRows,nColsAndRows>
280 using Basetype = Matrix<Valtype,nColsAndRows,nColsAndRows>;
287 SquareMatrix() noexcept(false) = default;
288 SquareMatrix( const SquareMatrix& ) = default;
289 SquareMatrix( const
Basetype& matrix );
290 SquareMatrix(
Basetype&& matrix ) noexcept;
291 explicit SquareMatrix( const
std::initializer_list<Valtype>& elements );
340 template<typename Valtype,typename Valtype2> constexpr
341 auto operator*( const SquareMatrix<Valtype,3>& m, const
Vector<Valtype2>& v ) noexcept ->
Vector<decltype(Valtype{}*Valtype2{})>;
345 template<
typename Valtype, const
unsigned short nColsAndRows >
constexpr
346 SquareMatrix<Valtype,nColsAndRows> Inverted(
const SquareMatrix<Valtype,nColsAndRows>& m );
350 template<
typename Valtype, const
unsigned short nColsAndRows >
constexpr
351 SquareMatrix<Valtype,nColsAndRows> Transposed(
const SquareMatrix<Valtype,nColsAndRows>& m );
356 template<
typename Valtype, const
unsigned short nColsAndRows >
constexpr
357 auto Determinant(
const SquareMatrix<Valtype,nColsAndRows>& m ) ->
decltype(pow<nColsAndRows>(Valtype{}));
363 template<
typename Valtype, const
unsigned short nColsAndRows >
constexpr
364 auto Adjungated(
const SquareMatrix<Valtype,nColsAndRows>& m,
unsigned short c,
unsigned short r ) ->
decltype(
pow<nColsAndRows-1>(Valtype{}));
367 template<
typename Valtype >
constexpr
368 auto Adjungated(
const SquareMatrix<Valtype,1>& m,
unsigned short c,
unsigned short r )
noexcept ->
decltype(pow<0>(Valtype{}));
373 template<
typename Valtype, const
unsigned short nColsAndRows >
374 SquareMatrix<Valtype,nColsAndRows>
AdjungatedMatrix(
const SquareMatrix<Valtype,nColsAndRows>& m );
377 template<
typename Valtype1,
typename Valtype2>
378 auto operator*(
const SquareMatrix<Valtype1,2>& m,
const Vector2D<Valtype2>& v )
noexcept -> Vector2D<
decltype(Valtype1{}*Valtype2{})>;
381 template<
typename Valtype>
class Rotation;
385 template<
typename Valtype>
386 class Transformation :
public SquareMatrix<Valtype,4>
398 Transformation() =
default;
399 Transformation(
const SquareMatrix<Valtype,4>& matrix );
400 Transformation( SquareMatrix<Valtype,4>&& matrix )
noexcept;
401 Transformation(
const Basetype& matrix );
402 Transformation( Basetype&& matrix )
noexcept;
404 template<
typename Valtype2>
406 template<
typename Valtype2,
typename ValtypeT2>
408 explicit Transformation(
const std::initializer_list<Valtype>& elements );
416 using SquareMatrix<Valtype,4>::operator=;
418 template<
typename Valtype2>
420 template<
typename Valtype2,
typename ValtypeT2>
425 using SquareMatrix<Valtype,4>::operator();
430 template<
typename Valtype2>
433 template<
typename Valtype2>
436 template<
typename Valtype2>
451 void CreateTranslation( Valtype tx, Valtype ty, Valtype tz )
noexcept;
464 void CreateRotation( Valtype rx, Valtype ry, Valtype rz );
477 void CreateScaling( Valtype sx, Valtype sy, Valtype sz )
noexcept;
484 template<
typename Valtype2>
498 Valtype LeftMargin = 0.0f,
499 Valtype TopMargin = 0.0f )
noexcept;
548 bool GeRotation( Transformation& R )
const noexcept;
553 bool GetScaling( Transformation& S )
const noexcept;
558 bool Dismantle( Transformation& T, Transformation& R, Transformation& S )
const noexcept;
566 template<
typename Valtype>
568 Valtype{1}, Valtype{0}, Valtype{0}, Valtype{0},
569 Valtype{0}, Valtype{1}, Valtype{0}, Valtype{0},
570 Valtype{0}, Valtype{0}, Valtype{1}, Valtype{0},
571 Valtype{0}, Valtype{0}, Valtype{0}, Valtype{1} };
574 template<
typename Valtype,
575 typename Valtype2 >
inline
576 auto operator*(
const Transformation<Valtype>& first,
577 const Transformation<Valtype2>& second )
noexcept -> Transformation<
decltype(Valtype{}*Valtype2{})>;
588 template<
typename Valtype>
589 bool Slerp( Transformation<Valtype>& out,
590 const Transformation<Valtype>& inA,
591 const Transformation<Valtype>& inB,
600 template<
typename Valtype,
typename ValtypeT>
601 Frame<Valtype,ValtypeT>
Invert(
const Frame<Valtype,ValtypeT> frame );
605 template<
typename Valtype>
606 class Rotation :
public SquareMatrix<Valtype,3>
617 Rotation() noexcept(false) = default;
618 Rotation( const SquareMatrix<Valtype,3>& matrix );
619 Rotation( SquareMatrix<Valtype,3>&& matrix ) noexcept;
621 Rotation(
Basetype&& matrix ) noexcept;
623 explicit Rotation( const
Vector<Valtype>& axis );
624 Rotation( Valtype q0, Valtype q1, Valtype q2, Valtype q3 ) noexcept;
625 template<typename ValtypeP>
626 explicit Rotation( const
Frame<ValtypeP,Valtype>& frame );
627 template<typename Valtype2>
629 explicit Rotation( Valtype r00, Valtype r10, Valtype r20,
630 Valtype r01, Valtype r11, Valtype r21,
631 Valtype r02, Valtype r12, Valtype r22 );
637 using SquareMatrix<Valtype,3>::operator=;
639 template<typename ValtypeP>
640 Rotation& operator=( const
Frame<ValtypeP,Valtype>& frame ) noexcept;
644 using SquareMatrix<Valtype,3>::operator();
654 void CreateFromAxis( Valtype rx, Valtype ry, Valtype rz ) noexcept;
660 void Rotate(
Vector<Valtype>& v ) const noexcept;
662 template<typename ValtypeP>
663 void Rotate(
Frame<ValtypeP,Valtype>& frame ) const noexcept;
671 Vector<Valtype> operator*( const
Vector<Valtype>& v ) const noexcept;
688 template<typename Valtype>
693 template<
typename Valtype,
694 typename Valtype2 >
inline
695 auto operator*(
const Rotation<Valtype>& first,
696 const Rotation<Valtype2>& second )
noexcept -> Rotation<
decltype(Valtype{}*Valtype2{})>;
699template<
typename Valtype, const
unsigned short nCols, const
unsigned short nRows >
700inline Matrix<Valtype,nCols,nRows>::Matrix()
701 : m{ std::make_unique<Valtype[]>(nCols*nRows) }
703 static_assert( nCols,
"Matrix: Number columes can not be zero" );
704 static_assert( nRows,
"Matrix: Number of rows can not be zero" );
707template<
typename Valtype, const
unsigned short nCols, const
unsigned short nRows >
708Matrix<Valtype,nCols,nRows>::Matrix(
const Matrix& matrix )
709 : m{
std::make_unique<Valtype[]>(nCols*nRows) }
711 for(
size_t Col = 0; Col < nCols; ++Col )
712 for(
size_t Row = 0;
Row < nRows; ++
Row )
713 m[nRows * Col +
Row] = matrix.m[nRows * Col +
Row];
716template<
typename Valtype, const
unsigned short nCols, const
unsigned short nRows >
717inline Matrix<Valtype,nCols,nRows>::Matrix( Matrix&& matrix )
noexcept
719 m = std::move(matrix.m);
722template<
typename Valtype,
unsigned short nCols,
unsigned short nRows>
723inline Matrix<Valtype,nCols,nRows>::Matrix(
const std::initializer_list<Valtype>& elements )
724 : m{
std::make_unique<Valtype[]>(nCols*nRows) }
726 static_assert( nCols,
"Matrix: Number columes can not be zero" );
727 static_assert( nRows,
"Matrix: Number of rows can not be zero" );
729 operator=( elements );
732template<
typename Valtype, const
unsigned short nCols, const
unsigned short nRows >
733Matrix<Valtype,nCols,nRows>& Matrix<Valtype,nCols,nRows>::operator=(
734 const Matrix& matrix )
noexcept
736 for(
size_t Col = 0; Col < nCols; ++Col )
737 for(
size_t Row = 0;
Row < nRows; ++
Row )
738 m[nRows * Col + Row] = matrix.m[nRows * Col + Row];
743template<
typename Valtype, const
unsigned short nCols, const
unsigned short nRows >
744inline Matrix<Valtype,nCols,nRows>& Matrix<Valtype,nCols,nRows>::operator=(
745 Matrix&& matrix )
noexcept
747 m = std::move(matrix.m);
751template<
typename Valtype,
unsigned short nCols,
unsigned short nRows>
752inline Matrix<Valtype,nCols,nRows>& Matrix<Valtype,nCols,nRows>::operator=(
753 const std::initializer_list<Valtype>& elements )
noexcept
755 auto it = elements.begin();
756 const auto end = elements.end();
759 for(
size_t Row = 0;
Row < nRows; ++
Row ){
760 for(
size_t Col = 0; Col < nCols; ++Col ){
761 m[nRows * Col +
Row] = (it != end) ? *it++ : Valtype{0};
767template<
typename Valtype, const
unsigned short nCols, const
unsigned short nRows >
768inline const Valtype& Matrix<Valtype,nCols,nRows>::operator()(
unsigned short col,
unsigned short row )
const noexcept{
770 return m[nRows *
static_cast<size_t>(col) + row];
773template<
typename Valtype, const
unsigned short nCols, const
unsigned short nRows >
774inline Valtype& Matrix<Valtype,nCols,nRows>::operator()(
unsigned short col,
unsigned short row )
noexcept{
776 return m[nRows *
static_cast<size_t>(col) + row];
779template<
typename Valtype, const
unsigned short nCols, const
unsigned short nRows >
781 for(
size_t Col = 0; Col < nCols; ++Col )
782 for(
size_t Row = 0;
Row < nRows; ++
Row )
783 m[nRows * Col +
Row] = 0;
786template<
typename Valtype, const
unsigned short nCols, const
unsigned short nRows >
791template<
typename Valtype, const
unsigned short nCols, const
unsigned short nRows >
796template<
typename Valtype, const
unsigned short nCols, const
unsigned short nRows >
798 for(
unsigned short x = 0; x < nCols; ++x )
799 for(
unsigned short y = 0; y < nRows; ++y )
806template<
typename Valtype, const
unsigned short nCols, const
unsigned short nRows >
808 return IsEqual( matrix, std::numeric_limits<Valtype>::epsilon() );
811template<
typename Valtype, const
unsigned short nCols, const
unsigned short nRows >
816template<
typename Valtype, const
unsigned short nCols, const
unsigned short nRows >
818 for(
size_t Col = 0; Col < nCols; ++Col )
819 for(
size_t Row = 0;
Row < nRows; ++
Row )
820 m[nRows * Col +
Row] *= skalar;
823template<
typename Valtype, const
unsigned short nCols, const
unsigned short nRows >
825 Matrix<Valtype,nCols,nRows> TempVal;
826 MatrixMultiply( *
this, matrix, TempVal );
830template<
typename Valtype, const
unsigned short nCols, const
unsigned short nRows >
832 for(
size_t Col = 0; Col < nCols; ++Col )
833 for(
size_t Row = 0;
Row < nRows; ++
Row )
834 m[nRows * Col +
Row] /= skalar;
837template<
typename Valtype, const
unsigned short nCols, const
unsigned short nRows >
839 for(
size_t Col = 0; Col < nCols; ++Col )
840 for(
size_t Row = 0;
Row < nRows; ++
Row )
841 m[nRows * Col +
Row] += matrix.m[nRows * Col +
Row];
844template<
typename Valtype, const
unsigned short nCols, const
unsigned short nRows >
846 for(
size_t Col = 0; Col < nCols; ++Col )
847 for(
size_t Row = 0;
Row < nRows; ++
Row )
848 m[nRows * Col +
Row] -= matrix.m[nRows * Col +
Row];
851template<
typename Valtype, const
unsigned short nCols, const
unsigned short nRows >
856template<
typename Valtype, const
unsigned short nCols, const
unsigned short nRows >
858 Matrix<Valtype,nCols-1,nRows-1> Result;
860 for( x = 0; x < c; ++x ){
862 for( y = 0; y < r; ++y )
863 Result( x, y ) = (*this)( x, y );
864 for( y++; y < nRows; ++y )
865 Result( x, y - 1u ) = (*this)( x, y );
867 for( x++; x < nCols; ++x ){
869 for( y = 0; y < r; ++y )
870 Result( x - 1u, y ) = (*this)( x, y );
871 for( y++; y < nRows; ++y )
872 Result( x - 1u, y - 1u ) = (*this)( x, y );
877template<
typename Valtype,const
unsigned short nCols>
882template<
typename Valtype,const
unsigned short nRows>
887template<
typename Valtype,
unsigned short nCols,
unsigned short nRows>
890 for(
unsigned short c = 0; c < nCols; ++c )
891 for(
unsigned short r = 0; r < nRows; ++r )
892 Result(r,c) = m(c,r);
897template<
typename Valtype,
typename Valtype2, const
unsigned short nCols, const
unsigned short nRows >
898inline auto operator*( Valtype scalar,
const Matrix<Valtype2,nCols,nRows>& matrix )
noexcept -> Matrix<
decltype(Valtype{}*Valtype2{}),nCols,nRows>{
901 Matrix<
decltype(Valtype{}*Valtype2{}),nCols,nRows> Retval;
902 for(
unsigned short c = 0; c < nCols; ++c )
903 for(
unsigned short r = 0; r < nRows; ++r )
904 Retval(c,r) = scalar * matrix(c,r);
918 unsigned short nColsFirst_RowsSecond,
919 unsigned short nRowsFirst,
920 unsigned short nColsSecond>
922 const Matrix<Valtype,nColsFirst_RowsSecond,nRowsFirst>& first,
923 const Matrix<Valtype2,nColsSecond,nColsFirst_RowsSecond>& second )
noexcept -> Matrix<
decltype(Valtype{}*Valtype2{}),nColsSecond,nRowsFirst>
925 using ResultType =
decltype(Valtype{}*Valtype2{});
927 Matrix<ResultType,nColsSecond,nRowsFirst> result;
928 for(
unsigned short x = 0; x < nColsSecond; ++x )
929 for(
unsigned short y = 0; y < nRowsFirst; ++y )
931 result(x,y) = ResultType{0};
932 for(
unsigned short z = 0; z < nColsFirst_RowsSecond; ++z )
933 result(x,y) += first(z,y) * second(x,z);
939template<
typename Valtype,
941 unsigned short nCols,
942 unsigned short nRows >
inline
943Matrix<Valtype,nCols,nRows>&
operator*=( Matrix<Valtype,nCols,nRows>& matrix, Valtype2 skalar )
noexcept{
944 for(
unsigned short c = 0; c < nCols; ++c )
945 for(
unsigned short r = 0; r < nRows; ++r )
946 matrix(c,r) *= skalar;
950template<
typename Valtype,
952 unsigned short nCols,
953 unsigned short nRows >
inline
954Matrix<Valtype,nCols,nRows>&
operator/=( Matrix<Valtype,nCols,nRows>& matrix, Valtype2 skalar )
noexcept{
955 for(
unsigned short c = 0; c < nCols; ++c )
956 for(
unsigned short r = 0; r < nRows; ++r )
957 matrix(c,r) /= skalar;
961template<
typename Valtype,
unsigned short nCols,
unsigned short nRows>
962inline Matrix<Valtype,nCols,nRows>
operator+(
const Matrix<Valtype,nCols,nRows>& first,
const Matrix<Valtype,nCols,nRows>& second)
noexcept{
963 Matrix<Valtype,nCols,nRows> result;
964 for(
unsigned short c = 0; c < nCols; ++c )
965 for(
unsigned short r = 0; r < nRows; ++r )
966 result(c,r) = first(c,r) + second(c,r);
970template<
typename Valtype,
unsigned short nCols,
unsigned short nRows>
971inline Matrix<Valtype,nCols,nRows>
operator-(
const Matrix<Valtype,nCols,nRows>& first,
const Matrix<Valtype,nCols,nRows>& second)
noexcept{
972 Matrix<Valtype,nCols,nRows> result;
973 for(
unsigned short c = 0; c < nCols; ++c )
974 for(
unsigned short r = 0; r < nRows; ++r )
975 result(c,r) = first(c,r) - second(c,r);
979template<
typename Valtype,
unsigned short nCols,
unsigned short nRows>
980inline constexpr Matrix<Valtype,nCols,nRows>&
operator+( Matrix<Valtype,nCols,nRows>& matrix )
noexcept{
984template<
typename Valtype,
unsigned short nCols,
unsigned short nRows>
985inline Matrix<Valtype,nCols,nRows>&
operator-( Matrix<Valtype,nCols,nRows>& matrix )
noexcept{
986 for(
unsigned short c = 0; c < nCols; ++c )
987 for(
unsigned short r = 0; r < nRows; ++r )
992template<
typename Valtype,
994 const unsigned short nCols,
995 const unsigned short nRows >
inline
996void copy_column_major(
const Matrix<Valtype,nCols,nRows>& source, Valtype2* target )
noexcept{
998 for(
unsigned short c = 0; c < nCols; ++c )
999 for(
unsigned short r = 0; r < nRows; ++r )
1000 target[c*nRows+r] =
static_cast<Valtype2
>(source(c,r));
1003template<
typename Valtype,
1005 const unsigned short nCols,
1006 const unsigned short nRows >
inline
1007void copy_column_major(
const Valtype2* source, Matrix<Valtype,nCols,nRows>& target )
noexcept{
1009 for(
unsigned short c = 0; c < nCols; ++c )
1010 for(
unsigned short r = 0; r < nRows; ++r )
1011 target(c,r) =
static_cast<Valtype
>(source[c*nRows+r]);
1014template<
typename Valtype,
1016 const unsigned short nCols,
1017 const unsigned short nRows >
inline
1018void copy_row_major(
const Matrix<Valtype,nCols,nRows>& source, Valtype2* target )
noexcept{
1020 for(
unsigned short r = 0; r < nRows; ++r )
1021 for(
unsigned short c = 0; c < nCols; ++c )
1022 target[r*nCols+c] =
static_cast<Valtype2
>(source(c,r));
1025template<
typename Valtype,
1027 const unsigned short nCols,
1028 const unsigned short nRows >
inline
1029void copy_row_major(
const Valtype2* source, Matrix<Valtype,nCols,nRows>& target )
noexcept{
1031 for(
unsigned short r = 0; r < nRows; ++r )
1032 for(
unsigned short c = 0; c < nCols; ++c )
1033 target(c,r) =
static_cast<Valtype
>(source[r*nCols+c]);
1036template<
typename Valtype,
unsigned short nColsAndRows>
1037inline SquareMatrix<Valtype,nColsAndRows>::SquareMatrix(
const Basetype& matrix )
1042template<
typename Valtype,
unsigned short nColsAndRows>
1043inline SquareMatrix<Valtype,nColsAndRows>::SquareMatrix( Basetype&& matrix ) noexcept
1044 : Basetype{std::move(matrix)}
1048template<
typename Valtype,
unsigned short nColsAndRows>
1049inline SquareMatrix<Valtype,nColsAndRows>::SquareMatrix(
const std::initializer_list<Valtype>& elements )
1050 : Basetype{elements}
1054template<
typename Valtype, const
unsigned short nColsAndRows>
1056 for(
unsigned short x = 0; x < nColsAndRows; ++x )
1057 for(
unsigned short y = 0; y < nColsAndRows; ++y )
1058 (*
this)(x,y) = (Valtype)((x == y) ? 1 : 0);
1061template<
typename Valtype, const
unsigned short nColsAndRows>
1063 for(
unsigned short x = 0; x < nColsAndRows; ++x )
1064 for(
unsigned short y = 0; y < nColsAndRows; ++y )
1065 if( !
common::Equals( (*
this)(x,y), (Valtype)((x == y) ? 1 : 0), epsilon_ ) )
1070template<
typename Valtype, const
unsigned short nColsAndRows>
1072 for(
unsigned short x = 0; x < nColsAndRows; ++x )
1073 for(
unsigned short y = 0; y < nColsAndRows; ++y )
1074 if( x != y && (*
this)(x,y) != Valtype{0} )
1080template<
typename Valtype,
unsigned short nColsAndRows>
1082 for(
unsigned short i = 0; i < nColsAndRows; ++i )
1083 for(
unsigned short j = 0; j < nColsAndRows; ++j ){
1093template<
typename Valtype, const
unsigned short nColsAndRows>
1095 for(
unsigned short x = 0; x < nColsAndRows; ++x )
1096 for(
unsigned short y = 0; y < x; ++y )
1097 std::swap( (*
this)( x, y ), (*
this)( y, x ) );
1102template<
typename Valtype, const
unsigned short nColsAndRows>
1105 if( d == Valtype{0} )
1106 throw std::logic_error(
"no inverse matrix" );
1112template<
typename Valtype, const
unsigned short nColsAndRows>
1115 for(
unsigned short z = 0; z < nColsAndRows; ++z )
1116 T += (*
this)( z, z );
1121template<
typename Valtype,
typename Valtype2>
constexpr
1124 m(0,0) * v.dx + m(1,0) * v.dy + m(2,0) * v.dz,
1125 m(0,1) * v.dx + m(1,1) * v.dy + m(2,1) * v.dz,
1126 m(0,2) * v.dx + m(1,2) * v.dy + m(2,2) * v.dz };
1129template<
typename Valtype,
unsigned short nColsAndRows>
1136template<
typename Valtype, const
unsigned short nColsAndRows >
constexpr
1139 for(
unsigned short c = 0; c < nColsAndRows; ++c )
1140 for(
unsigned short r = 0; r < nColsAndRows; ++r )
1141 Result(r,c) = m(c,r);
1146template<
typename Valtype, const
unsigned short nColsAndRows >
constexpr
1148 decltype(pow<nColsAndRows>(Valtype{})) D{0};
1149 for(
unsigned short z = 0; z < nColsAndRows; ++z )
1150 D += m( z, 0 ) * Adjungated( m, z, 0 );
1155template<
typename Valtype, const
unsigned short nColsAndRows >
constexpr
1156auto Adjungated(
const SquareMatrix<Valtype,nColsAndRows>& m,
unsigned short c,
unsigned short r ) ->
decltype(
pow<nColsAndRows-1>(Valtype{})){
1157 SquareMatrix<Valtype,nColsAndRows-1> sub{ m.SubMatrix( c, r ) };
1158 return (((r + c) % 2) ? -1 : 1 ) *
Determinant( sub );
1161template<
typename Valtype >
constexpr
1162auto Adjungated(
const SquareMatrix<Valtype,1>&,
unsigned short,
unsigned short )
noexcept ->
decltype(pow<0>(Valtype{})){
1166template<
typename Valtype, const
unsigned short nColsAndRows >
1169 for(
unsigned short x = 0; x < nColsAndRows; ++x )
1170 for(
unsigned short y = 0; y < nColsAndRows; ++y )
1171 Retval( x, y ) = Adjungated( m, x, y );
1176template<
typename Valtype1,
typename Valtype2>
1177inline auto operator*(
const SquareMatrix<Valtype1,2>& m,
const Vector2D<Valtype2>& v )
noexcept -> Vector2D<
decltype(Valtype1{}*Valtype2{})>{
1178 return { m(0,0) * v.dx + m(1,0) * v.dy, m(0,1) * v.dx + m(1,1) * v.dy };
1181template<
typename Valtype>
1182inline Transformation<Valtype>::Transformation(
const SquareMatrix<Valtype,4>& matrix )
1183 : SquareMatrix<Valtype,4>{matrix}
1187template<
typename Valtype>
1188inline Transformation<Valtype>::Transformation( SquareMatrix<Valtype,4>&& matrix ) noexcept
1189 : SquareMatrix<Valtype,4>{std::move(matrix)}
1193template<
typename Valtype>
1194inline Transformation<Valtype>::Transformation(
const Basetype& matrix )
1195 : SquareMatrix<Valtype,4>{matrix}
1199template<
typename Valtype>
1200inline Transformation<Valtype>::Transformation( Basetype&& matrix ) noexcept
1201 : SquareMatrix<Valtype,4>{std::move(matrix)}
1205template<
typename Valtype>
1206template<
typename Valtype2>
1207inline Transformation<Valtype>::Transformation(
const Rotation<Valtype2>& rot )
1208 : SquareMatrix<Valtype,4>{}
1213template<
typename Valtype>
1214template<
typename Valtype2,
typename ValtypeT2>
1215inline Transformation<Valtype>::Transformation(
const Frame<Valtype2,ValtypeT2>& frame )
1216 : SquareMatrix<Valtype,4>{}
1221template<
typename Valtype>
1222inline Transformation<Valtype>::Transformation(
const std::initializer_list<Valtype>& elements )
1223 : SquareMatrix<Valtype,4>{elements}
1227template<
typename Valtype>
1228template<
typename Valtype2>
1229Transformation<Valtype>& Transformation<Valtype>::operator=(
1230 const Rotation<Valtype2>& rot )
noexcept
1232 operator()(0,0) =
static_cast<Valtype
>(rot(0,0));
1233 operator()(1,0) =
static_cast<Valtype
>(rot(1,0));
1234 operator()(2,0) =
static_cast<Valtype
>(rot(2,0));
1235 operator()(3,0) = 0;
1237 operator()(0,1) =
static_cast<Valtype
>(rot(0,1));
1238 operator()(1,1) =
static_cast<Valtype
>(rot(1,1));
1239 operator()(2,1) =
static_cast<Valtype
>(rot(2,1));
1240 operator()(3,1) = 0;
1242 operator()(0,2) =
static_cast<Valtype
>(rot(0,2));
1243 operator()(1,2) =
static_cast<Valtype
>(rot(1,2));
1244 operator()(2,2) =
static_cast<Valtype
>(rot(2,2));
1245 operator()(3,2) = 0;
1247 operator()(0,3) = 0;
1248 operator()(1,3) = 0;
1249 operator()(2,3) = 0;
1250 operator()(3,3) = 1;
1255template<
typename Valtype>
1256template<
typename Valtype2,
typename ValtypeT2>
1257Transformation<Valtype>& Transformation<Valtype>::operator=(
1258 const Frame<Valtype2,ValtypeT2>& frame )
noexcept
1260 operator()( 0, 0 ) =
static_cast<Valtype
>(frame.T.dx/ValtypeT2{1});
1261 operator()( 0, 1 ) =
static_cast<Valtype
>(frame.T.dy/ValtypeT2{1});
1262 operator()( 0, 2 ) =
static_cast<Valtype
>(frame.T.dz/ValtypeT2{1});
1263 operator()( 0, 3 ) = 0;
1265 operator()( 1, 0 ) =
static_cast<Valtype
>(frame.N.dx/ValtypeT2{1});
1266 operator()( 1, 1 ) =
static_cast<Valtype
>(frame.N.dy/ValtypeT2{1});
1267 operator()( 1, 2 ) =
static_cast<Valtype
>(frame.N.dz/ValtypeT2{1});
1268 operator()( 1, 3 ) = 0;
1270 operator()( 2, 0 ) =
static_cast<Valtype
>(frame.B.dx/ValtypeT2{1});
1271 operator()( 2, 1 ) =
static_cast<Valtype
>(frame.B.dy/ValtypeT2{1});
1272 operator()( 2, 2 ) =
static_cast<Valtype
>(frame.B.dz/ValtypeT2{1});
1273 operator()( 2, 3 ) = 0;
1275 operator()( 3, 0 ) =
static_cast<Valtype
>(frame.P.x/Valtype2{1});
1276 operator()( 3, 1 ) =
static_cast<Valtype
>(frame.P.y/Valtype2{1});
1277 operator()( 3, 2 ) =
static_cast<Valtype
>(frame.P.z/Valtype2{1});
1278 operator()( 3, 3 ) = 1;
1283template<
typename Valtype>
1284template<
typename Valtype2>
1285inline PositionH<Valtype2> Transformation<Valtype>::operator*(
1286 const PositionH<Valtype2>& pos )
const noexcept
1289 (*this)(0,0) * pos.x + (*
this)(1,0) * pos.y + (*
this)(2,0) * pos.z + (*
this)(3,0) * pos.w,
1290 (*
this)(0,1) * pos.x + (*
this)(1,1) * pos.y + (*
this)(2,1) * pos.z + (*
this)(3,1) * pos.w,
1291 (*
this)(0,2) * pos.x + (*
this)(1,2) * pos.y + (*
this)(2,2) * pos.z + (*
this)(3,2) * pos.w,
1292 (*
this)(0,3) * pos.x + (*
this)(1,3) * pos.y + (*
this)(2,3) * pos.z + (*
this)(3,3) * pos.w
1296template<
typename Valtype>
1297template<
typename Valtype2>
1298inline Position<Valtype2> Transformation<Valtype>::operator*(
1299 const Position<Valtype2>& pos )
const noexcept
1301 PositionH<Valtype> HPos{ pos };
1304 return { Valtype2{HPos.x}, Valtype2{HPos.y}, Valtype2{HPos.z} };
1307template<
typename Valtype>
1308template<
typename Valtype2>
1309inline Vector<Valtype2> Transformation<Valtype>::operator*(
1310 const Vector<Valtype2>& dir )
const noexcept
1312 PositionH<Valtype> HPos{ dir };
1314 return { Valtype2{HPos.x}, Valtype2{HPos.y}, Valtype2{HPos.z} };
1317template<
typename Valtype>
1318inline void Transformation<Valtype>::CreateTranslation(
1319 const Vector<Valtype>& translation )
noexcept
1321 CreateTranslation( translation.dx, translation.dy, translation.dz );
1324template<
typename Valtype>
1325void Transformation<Valtype>::CreateTranslation(
1326 Valtype tx, Valtype ty, Valtype tz )
noexcept
1328 operator()(0,0) = 1;
1329 operator()(1,0) = 0;
1330 operator()(2,0) = 0;
1331 operator()(3,0) = tx;
1333 operator()(0,1) = 0;
1334 operator()(1,1) = 1;
1335 operator()(2,1) = 0;
1336 operator()(3,1) = ty;
1338 operator()(0,2) = 0;
1339 operator()(1,2) = 0;
1340 operator()(2,2) = 1;
1341 operator()(3,2) = tz;
1343 operator()(0,3) = 0;
1344 operator()(1,3) = 0;
1345 operator()(2,3) = 0;
1346 operator()(3,3) = 1;
1349template<
typename Valtype>
1350void Transformation<Valtype>::CreateRotation(
const Vector<Valtype>& rotation )
1355 Rotation<Valtype> R;
1356 R.CreateFromAxis(rotation);
1358 operator()(0,0) = R(0,0);
1359 operator()(1,0) = R(1,0);
1360 operator()(2,0) = R(2,0);
1361 operator()(3,0) = 0;
1363 operator()(0,1) = R(0,1);
1364 operator()(1,1) = R(1,1);
1365 operator()(2,1) = R(2,1);
1366 operator()(3,1) = 0;
1368 operator()(0,2) = R(0,2);
1369 operator()(1,2) = R(1,2);
1370 operator()(2,2) = R(2,2);
1371 operator()(3,2) = 0;
1373 operator()(0,3) = 0;
1374 operator()(1,3) = 0;
1375 operator()(2,3) = 0;
1376 operator()(3,3) = 1;
1379template<
typename Valtype>
1380inline void Transformation<Valtype>::CreateRotation( Valtype rx, Valtype ry, Valtype rz )
1382 CreateRotation( Vector<Valtype>( rx, ry, rz ) );
1385template<
typename Valtype>
1386inline void Transformation<Valtype>::CreateScaling(
const Vector<Valtype>& scaling )
noexcept
1388 CreateScaling( scaling.dx, scaling.dy, scaling.dz );
1391template<
typename Valtype>
1392void Transformation<Valtype>::CreateScaling( Valtype sx, Valtype sy, Valtype sz )
noexcept
1394 operator()(0,0) = sx;
1395 operator()(1,0) = 0;
1396 operator()(2,0) = 0;
1397 operator()(3,0) = 0;
1399 operator()(0,1) = 0;
1400 operator()(1,1) = sy;
1401 operator()(2,1) = 0;
1402 operator()(3,1) = 0;
1404 operator()(0,2) = 0;
1405 operator()(1,2) = 0;
1406 operator()(2,2) = sz;
1407 operator()(3,2) = 0;
1409 operator()(0,3) = 0;
1410 operator()(1,3) = 0;
1411 operator()(2,3) = 0;
1412 operator()(3,3) = 1;
1415template<
typename Valtype>
1416template<
typename Valtype2>
1418 assert( mirror.
T.IsNormal() );
1421 operator()(0,3) = Valtype{0};
1422 operator()(1,3) = Valtype{0};
1423 operator()(2,3) = Valtype{0};
1424 operator()(3,3) = Valtype{1};
1427template<
typename Valtype>
1452 *
this = { n.
dx, n.
dy, n.
dz, -n*e,
1454 -d.
dx, -d.
dy, -d.
dz, d*e,
1458template<
typename Valtype>
1465 Valtype TopMargin )
noexcept
1469 *
this = { Width / 2, 0, 0, LeftMargin + Width / 2,
1470 0, -Height / 2, 0, TopMargin + Height / 2,
1471 0, 0, MinZ - MaxZ, MinZ,
1475template<
typename Valtype>
1477 Valtype Fovy, Valtype Aspect, Valtype zn, Valtype zf )
noexcept
1486 Valtype w2 = n * tan( Fovy / 2 );
1487 Valtype h2 = w2 / Aspect;
1493 *
this = { n/w2, 0, 0, 0,
1495 0, 0, f/(f-n), -f*n/(f-n),
1501template<
typename Valtype>
1503 Valtype width, Valtype height, Valtype znear, Valtype zfar )
noexcept
1505 if( zfar <= znear ||
1516 *
this = { 2/w, 0, 0, 0,
1518 0, 0, -1/(f-n), -n/(f-n),
1524template<
typename Valtype>
1534 assert( (*
this)(0,3) == 0 && (*
this)(1,3) == 0 && (*
this)(2,3) == 0 );
1537 operator()(0,0) * v.dx + operator()(1,0) * v.dy + operator()(2,0) * v.dz,
1538 operator()(0,1) * v.dx + operator()(1,1) * v.dy + operator()(2,1) * v.dz,
1539 operator()(0,2) * v.dx + operator()(1,2) * v.dy + operator()(2,2) * v.dz };
1560template<
typename Valtype>
1562 Transformation& T )
const noexcept
1578 T(3,0) = operator()(3,0);
1579 T(3,1) = operator()(3,1);
1580 T(3,2) = operator()(3,2);
1583 T(0,3) = operator()(0,3);
1584 T(1,3) = operator()(1,3);
1585 T(2,3) = operator()(2,3);
1588template<
typename Valtype>
1590 Transformation& R )
const noexcept
1595 Valtype sx = std::sqrt( std::pow(
operator()(0,0), Valtype{2} ) + std::pow(
operator()(0,1), Valtype{2} ) + std::pow(
operator()(0,2), Valtype{2} ) );
1596 Valtype sy = std::sqrt( std::pow(
operator()(1,0), Valtype{2} ) + std::pow(
operator()(1,1), Valtype{2} ) + std::pow(
operator()(1,2), Valtype{2} ) );
1597 Valtype sz = std::sqrt( std::pow(
operator()(2,0), Valtype{2} ) + std::pow(
operator()(2,1), Valtype{2} ) + std::pow(
operator()(2,2), Valtype{2} ) );
1599 if( sx == 0 || sy == 0 || sz == 0 )
1609 R(0,0) = operator()(0,0) * isx;
1610 R(0,1) = operator()(0,1) * isx;
1611 R(0,2) = operator()(0,2) * isx;
1614 R(1,0) = operator()(1,0) * isy;
1615 R(1,1) = operator()(1,1) * isy;
1616 R(1,2) = operator()(1,2) * isy;
1619 R(2,0) = operator()(2,0) * isz;
1620 R(2,1) = operator()(2,1) * isz;
1621 R(2,2) = operator()(2,2) * isz;
1632template<
typename Valtype>
1634 Transformation& S )
const noexcept
1639 S(0,0) = std::sqrt( std::pow(
operator()(0,0), Valtype{2} ) + std::pow(
operator()(0,1), Valtype{2} ) + std::pow(
operator()(0,2), Valtype{2} ) );
1645 S(1,1) = std::sqrt( std::pow(
operator()(1,0), Valtype{2} ) + std::pow(
operator()(1,1), Valtype{2} ) + std::pow(
operator()(1,2), Valtype{2} ) );
1651 S(2,2) = std::sqrt( std::pow(
operator()(2,0), Valtype{2} ) + std::pow(
operator()(2,1), Valtype{2} ) + std::pow(
operator()(2,2), Valtype{2} ) );
1659 if( S(0,0) == 0 || S(1,1) == 0 || S(2,2) == 0 )
1665template<
typename Valtype>
1668 Transformation& S )
const noexcept
1676 Valtype isx = 1/S(0,0);
1677 Valtype isy = 1/S(1,1);
1678 Valtype isz = 1/S(2,2);
1681 R(0,0) = operator()(0,0) * isx;
1682 R(0,1) = operator()(0,1) * isx;
1683 R(0,2) = operator()(0,2) * isx;
1686 R(1,0) = operator()(1,0) * isy;
1687 R(1,1) = operator()(1,1) * isy;
1688 R(1,2) = operator()(1,2) * isy;
1691 R(2,0) = operator()(2,0) * isz;
1692 R(2,1) = operator()(2,1) * isz;
1693 R(2,2) = operator()(2,2) * isz;
1704template<
typename Valtype>
1707 if( !
common::Equals(
operator()(3,3), Valtype{1}, Valtype{0.001} ) )
1713template<
typename Valtype,
1714 typename Valtype2 >
inline
1718 return operator*(
static_cast<const Matrix<Valtype,4,4>&
>( first ),
static_cast<const Matrix<Valtype2,4,4>&
>( second ) );
1721template<
typename Valtype>
1761 S(0,0) = 1 + (S(0,0) - 1) * t;
1762 S(1,1) = 1 + (S(1,1) - 1) * t;
1763 S(2,2) = 1 + (S(2,2) - 1) * t;
1766 out = T * R * S * A;
1772template<
typename Valtype,
typename ValtypeT>
1781template<
typename Valtype>
1782inline Rotation<Valtype>::Rotation(
const SquareMatrix<Valtype,3>& matrix )
1783 : SquareMatrix<Valtype,3>{matrix}
1787template<
typename Valtype>
1788inline Rotation<Valtype>::Rotation( SquareMatrix<Valtype,3>&& matrix ) noexcept
1789 : SquareMatrix<Valtype,3>{std::move(matrix)}
1793template<
typename Valtype>
1794inline Rotation<Valtype>::Rotation(
const Basetype& matrix )
1795 : SquareMatrix<Valtype,3>{matrix}
1799template<
typename Valtype>
1800inline Rotation<Valtype>::Rotation( Basetype&& matrix ) noexcept
1801 : SquareMatrix<Valtype,3>{std::move(matrix)}
1805template<
typename Valtype>
1806inline Rotation<Valtype>::Rotation(
const Vector<Valtype>& axis )
1807 : SquareMatrix<Valtype,3>{}
1809 CreateFromAxis( axis );
1812template<
typename Valtype>
1813inline Rotation<Valtype>::Rotation( Valtype q0, Valtype q1, Valtype q2, Valtype q3 ) noexcept
1814 : SquareMatrix<Valtype,3>
1815{ 2*(q0*q0+q1*q1)-1, 2*(q1*q2-q0*q3), 2*(q1*q3+q0*q2),
1816 2*(q1*q2+q0*q3), 2*(q0*q0+q2*q2)-1, 2*(q2*q3-q0*q1),
1817 2*(q1*q3-q0*q2), 2*(q2*q3+q0*q1), 2*(q0*q0+q3*q3)-1 }
1821template<
typename Valtype>
1822template<
typename ValtypeP>
1823Rotation<Valtype>::Rotation(
const Frame<ValtypeP,Valtype>& frame )
1824 : SquareMatrix<Valtype,3>{}
1829template<
typename Valtype>
1830template<
typename Valtype2>
1831Rotation<Valtype>::Rotation(
const Transformation<Valtype2>& tr )
1832 : SquareMatrix<Valtype,3>{}
1834 Transformation<Valtype2> R;
1837 operator()(0,0) =
static_cast<Valtype
>(R(0,0));
1838 operator()(1,0) =
static_cast<Valtype
>(R(1,0));
1839 operator()(2,0) =
static_cast<Valtype
>(R(2,0));
1841 operator()(0,1) =
static_cast<Valtype
>(R(0,1));
1842 operator()(1,1) =
static_cast<Valtype
>(R(1,1));
1843 operator()(2,1) =
static_cast<Valtype
>(R(2,1));
1845 operator()(0,2) =
static_cast<Valtype
>(R(0,2));
1846 operator()(1,2) =
static_cast<Valtype
>(R(1,2));
1847 operator()(2,2) =
static_cast<Valtype
>(R(2,2));
1850template<
typename Valtype>
1851inline Rotation<Valtype>::Rotation(
1852 Valtype r00, Valtype r10, Valtype r20,
1853 Valtype r01, Valtype r11, Valtype r21,
1854 Valtype r02, Valtype r12, Valtype r22 )
1855 : SquareMatrix<Valtype,3>{ r00, r10, r20,
1861template<
typename Valtype>
1862template<
typename ValtypeP>
1863inline Rotation<Valtype>& Rotation<Valtype>::operator=(
const Frame<ValtypeP,Valtype>& frame )
noexcept
1865 operator()(0,0) = frame.T.dx;
1866 operator()(0,1) = frame.T.dy;
1867 operator()(0,2) = frame.T.dz;
1869 operator()(1,0) = frame.N.dx;
1870 operator()(1,1) = frame.N.dy;
1871 operator()(1,2) = frame.N.dz;
1873 operator()(2,0) = frame.B.dx;
1874 operator()(2,1) = frame.B.dy;
1875 operator()(2,2) = frame.B.dz;
1880template<
typename Valtype>
1936 SquareMatrix<Valtype,3> S{ 0, -u.
dz, u.
dy,
1940 SquareMatrix<Valtype,3> I{ 1, 0, 0,
1944 *
this = I + sin(theta) * S + (1 - cos(theta)) * S * S;
1947template<
typename Valtype>
1953template<
typename Valtype>
1954inline void Rotation<Valtype>::Rotate(
Vector<Valtype>& v )
const noexcept{
1958template<
typename Valtype>
1959template<
typename ValtypeP>
1960inline void Rotation<Valtype>::Rotate( Frame<ValtypeP,Valtype>& frame )
const noexcept{
1961 frame.T = operator*( frame.T );
1962 frame.N = operator*( frame.N );
1963 frame.B = operator*( frame.B );
1966template<
typename Valtype>
1967inline Position<Valtype> Rotation<Valtype>::operator*(
1968 const Position<Valtype>& p )
const noexcept
1970 return Position<Valtype>{
1971 (*this)(0,0) * p.x + (*
this)(1,0) * p.y + (*
this)(2,0) * p.z,
1972 (*
this)(0,1) * p.x + (*
this)(1,1) * p.y + (*
this)(2,1) * p.z,
1973 (*
this)(0,2) * p.x + (*
this)(1,2) * p.y + (*
this)(2,2) * p.z };
1976template<
typename Valtype>
1977inline Vector<Valtype> Rotation<Valtype>::operator*(
1978 const Vector<Valtype>& v )
const noexcept
1980 return Vector<Valtype>{
1981 (*this)(0,0) * v.dx + (*
this)(1,0) * v.dy + (*
this)(2,0) * v.dz,
1982 (*
this)(0,1) * v.dx + (*
this)(1,1) * v.dy + (*
this)(2,1) * v.dz,
1983 (*
this)(0,2) * v.dx + (*
this)(1,2) * v.dy + (*
this)(2,2) * v.dz };
2027template<
typename Valtype>
2030#ifdef TRAX_OPEN_SOURCE
2036 R(0,0) = (*this)(0,0); R(0,1) = (*this)(0,1); R(0,2) = (*this)(0,2);
2037 R(1,0) = (*this)(1,0); R(1,1) = (*this)(1,1); R(1,2) = (*this)(1,2);
2038 R(2,0) = (*this)(2,0); R(2,1) = (*this)(2,1); R(2,2) = (*this)(2,2);
2041 Eigen::Matrix3d A = R - Eigen::Matrix3d::Identity();
2044 Eigen::Vector3d b{ 0.0, 0.0, 0.0 };
2047 Eigen::JacobiSVD<Eigen::Matrix3d> svd{ A, Eigen::ComputeFullU | Eigen::ComputeFullV };
2048 Eigen::Vector3d x = svd.matrixV().col(2);
2050 Vector<Valtype> axis{
static_cast<Valtype
>(x(0)),
static_cast<Valtype
>(x(1)),
static_cast<Valtype
>(x(2)) };
2054 return Null<Valtype>;
2058template<
typename Valtype>
2063 Valtype cosangle =
common::Clamp( (trace-Valtype{1}) / 2,
static_cast<Valtype
>(-1),
static_cast<Valtype
>(+1) );
2064 return acos( cosangle );
2067template<
typename Valtype,
2068 typename Valtype2 >
inline
2072 return operator*(
static_cast<const Matrix<Valtype,3,3>&
>( first ),
static_cast<const Matrix<Valtype2,3,3>&
>( second ) );
Matrix template for arbitrary dimensions and value type.
Definition Matrix.h:63
Matrix< Valtype, nCols-1, nRows-1 > SubMatrix(unsigned short c, unsigned short r) const
void operator-=(const Matrix &matrix) noexcept
Matrix subtraction.
Definition Matrix.h:845
constexpr unsigned short Rows() const noexcept
constexpr unsigned short Cols() const noexcept
bool operator!=(const Matrix &matrix) const noexcept
Comparison for inequality.
Definition Matrix.h:812
void operator*=(Valtype skalar) noexcept
Matrix * Skalar multiplication.
Definition Matrix.h:817
void operator/=(Valtype skalar) noexcept
Matrix / Skalar division.
Definition Matrix.h:831
void operator+=(const Matrix &matrix) noexcept
Matrix addition.
Definition Matrix.h:838
const Valtype * ptr() const noexcept
bool operator==(const Matrix &matrix) const noexcept
Comparison for equality.
Definition Matrix.h:807
bool IsEqual(const Matrix &matrix, Valtype delta=0) const noexcept
void SetNull() noexcept
Sets all elements to 0.
Definition Matrix.h:780
Rotation matrix.
Definition Matrix.h:607
Valtype RotationAngle() const noexcept
Angle of the rotation described by this matrix.
Definition Matrix.h:2059
typename SquareMatrix< Valtype, 3 >::Basetype Basetype
Utmost base type of this Rotation.
Definition Matrix.h:609
Vector< Valtype > RotationAxis() const noexcept
Axis of the rotation described by this matrix.
Definition Matrix.h:2028
void CreateFromAxis(const Vector< Valtype > &axis)
This constructs a rotation with an axis according to the right-hand-rule. (Thumb points in vector dir...
Definition Matrix.h:1881
Square matrix with nColumns == nRows.
Definition Matrix.h:278
Valtype Trace() const noexcept
Definition Matrix.h:1113
void SetIdentity() noexcept
Definition Matrix.h:1055
SquareMatrix & Transpose() noexcept
Definition Matrix.h:1094
SquareMatrix & Invert()
Definition Matrix.h:1103
bool IsSymmetric(Valtype epsilon=0) const noexcept
Definition Matrix.h:1081
bool IsDiagonal() const noexcept
Definition Matrix.h:1071
Matrix< Valtype, nColsAndRows, nColsAndRows > Basetype
Utmost base type of this SquareMatrix.
Definition Matrix.h:280
bool IsIdentity(Valtype epsilon=0) const noexcept
Definition Matrix.h:1062
constexpr auto operator*(const Interval< Valtype > &i1, Valtype2 scalar) noexcept -> Interval< decltype(Valtype{} *Valtype2{})>
Interval operator.
Definition Interval.h:628
constexpr T pow(T val) noexcept
power function with templated integer exponent.
Definition Helpers.h:61
constexpr const T & Clamp(const T &v, const T &lo, const T &hi) noexcept
Clips a val to a specified range.
Definition Helpers.h:105
constexpr bool Equals(T a, T b, T epsilon) noexcept
Tests equality in the sense |a-b| < epsilon.
Definition Helpers.h:66
constexpr Interval< Valtype > & operator*=(Interval< Valtype > &i1, Valtype2 scalar) noexcept
Interval operator.
Definition Interval.h:638
constexpr Interval< Valtype > & operator/=(Interval< Valtype > &i1, Valtype2 scalar) noexcept
Interval operator.
Definition Interval.h:650
constexpr Interval< Valtype > operator+(const Interval< Valtype > &i1, const Interval< Valtype > &i2) noexcept
Interval operator.
Definition Interval.h:591
constexpr Interval< Valtype > operator-(const Interval< Valtype > &i1, Valtype l) noexcept
Interval operator.
Definition Interval.h:613
The namespace provides classes and methods for spatial computations.
Definition Box.h:32
Position< Valtype2 > operator*(const Frame< Valtype1, ValtypeT1 > &frame, const Position< Valtype2 > &p) noexcept
Frame operator.
Definition Frame.h:1089
Frame< Valtype, ValtypeT > Invert(const Frame< Valtype, ValtypeT > frame)
Inverts the frame, so that FI * F == Identity.
Definition Matrix.h:1773
constexpr Matrix< Valtype, nRows, nCols > Transposed(const Matrix< Valtype, nCols, nRows > &m) noexcept
Definition Matrix.h:888
constexpr auto Determinant(const SquareMatrix< Valtype, nColsAndRows > &m) -> decltype(pow< nColsAndRows >(Valtype{}))
Determinant of the matrix.
Definition Matrix.h:1147
const Rotation< Valtype > IdentityRotation
Rotation that leaves the transformed object unchanged.
Definition Matrix.h:689
constexpr Vector< Valtype > Row(const Matrix< Valtype, 3, nRows > &m, unsigned short idx) noexcept
Definition Matrix.h:883
const Transformation< Valtype > IdentityTransformation
Transformation that leaves the transformed object unchanged.
Definition Matrix.h:567
bool Slerp(Transformation< Valtype > &out, const Transformation< Valtype > &inA, const Transformation< Valtype > &inB, Valtype t)
Spherical interpolates two matrices weighting them by t.
Definition Matrix.h:1722
SquareMatrix< Valtype, nColsAndRows > AdjungatedMatrix(const SquareMatrix< Valtype, nColsAndRows > &m)
Matrix of adjungated values.
Definition Matrix.h:1167
constexpr SquareMatrix< Valtype, nColsAndRows > Inverted(const SquareMatrix< Valtype, nColsAndRows > &m)
Definition Matrix.h:1130
constexpr Position< Valtype > Origin3D
Origin of coordinate system.
Definition Position.h:143
constexpr Vector< Valtype > Column(const Matrix< Valtype, nCols, 3 > &m, unsigned short idx) noexcept
Definition Matrix.h:878
A Frame ("TNBFrame") describes a location in 3d space and an orientation using a right handed coordin...
Definition Frame.h:52
Implements a 4D - position in homogenous coordinates.
Definition PositionH.h:41
Implements a 3D - position in cartesian coordinates.
Definition Position.h:46
Implements a 2D - vector in cartesian coordinates.
Definition Vector2D.h:46
Implements a Vector bundle.
Definition VectorBundle.h:42
Position< Valtype > P
Base space postion.
Definition VectorBundle.h:43
Vector< ValtypeT > T
Tangent vector or x-axis.
Definition VectorBundle.h:44
Implements a 3D - vector in cartesian coordinates.
Definition Vector.h:48
Valtype dy
cartesian y component.
Definition Vector.h:52
Valtype dx
cartesian x component.
Definition Vector.h:51
Valtype dz
cartesian z component.
Definition Vector.h:53
auto Normalize() noexcept -> decltype(Valtype{}/Valtype{})
Normalizes the vector to length 1.
Definition Vector.h:487