19 #ifndef SURGSIM_MATH_MATRIX_H 20 #define SURGSIM_MATH_MATRIX_H 25 #include <Eigen/Geometry> 35 typedef Eigen::Matrix<float, 2, 2, Eigen::RowMajor>
Matrix22f;
39 typedef Eigen::Matrix<float, 3, 3, Eigen::RowMajor>
Matrix33f;
43 typedef Eigen::Matrix<float, 4, 4, Eigen::RowMajor>
Matrix44f;
47 typedef Eigen::Matrix<double, 2, 2, Eigen::RowMajor>
Matrix22d;
51 typedef Eigen::Matrix<double, 3, 3, Eigen::RowMajor>
Matrix33d;
55 typedef Eigen::Matrix<double, 4, 4, Eigen::RowMajor>
Matrix44d;
59 typedef Eigen::Matrix<double, 6, 6, Eigen::RowMajor>
Matrix66d;
65 typedef Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>
Matrix;
73 template <
typename T,
int VOpt>
74 inline Eigen::Matrix<T, 3, 3>
makeRotationMatrix(
const T& angle,
const Eigen::Matrix<T, 3, 1, VOpt>& axis)
76 return Eigen::AngleAxis<T>(angle, axis).toRotationMatrix();
85 template <
typename T,
int VOpt>
88 Eigen::Matrix<T, 3, 3> result;
91 result(0, 1) = -vector(2);
92 result(0, 2) = vector(1);
94 result(1, 0) = vector(2);
96 result(1, 2) = -vector(0);
98 result(2, 0) = -vector(1);
99 result(2, 1) = vector(0);
112 template <
typename T,
int MOpt>
113 inline Eigen::Matrix<T, 3, 1>
skew(
const Eigen::Matrix<T, 3, 3, MOpt>& matrix)
115 Eigen::Matrix<T, 3, 3, MOpt> skewSymmetricPart = (matrix - matrix.transpose()) / 2.0;
116 return Eigen::Matrix<T, 3, 1>(skewSymmetricPart(2, 1), skewSymmetricPart(0, 2), skewSymmetricPart(1, 0));
126 template <
typename T,
int MOpt,
int VOpt>
128 T* angle, Eigen::Matrix<T, 3, 1, VOpt>* axis)
130 Eigen::AngleAxis<T> angleAxis(matrix);
131 *angle = angleAxis.angle();
132 *axis = angleAxis.axis();
141 template <
typename T,
int MOpt>
145 Eigen::AngleAxis<T> angleAxis(matrix);
146 return angleAxis.angle();
156 template <
class Matrix,
class SubMatrix>
157 void addSubMatrix(
const SubMatrix& subMatrix,
size_t blockIdRow,
size_t blockIdCol,
158 size_t blockSizeRow,
size_t blockSizeCol, Matrix* matrix)
160 matrix->block(blockSizeRow * blockIdRow, blockSizeCol * blockIdCol, blockSizeRow, blockSizeCol) += subMatrix;
170 template <
class Matrix,
class SubMatrix>
171 void addSubMatrix(
const SubMatrix& subMatrix,
const std::vector<size_t>& blockIds,
size_t blockSize, Matrix* matrix)
173 const size_t numBlocks = blockIds.size();
175 for (
size_t block0 = 0; block0 < numBlocks; block0++)
177 size_t blockId0 = blockIds[block0];
179 for (
size_t block1 = 0; block1 < numBlocks; block1++)
181 size_t blockId1 = blockIds[block1];
183 matrix->block(blockSize * blockId0, blockSize * blockId1, blockSize, blockSize) +=
184 subMatrix.block(blockSize * block0, blockSize * block1, blockSize, blockSize);
196 template <
class Matrix,
class SubMatrix>
197 void setSubMatrix(
const SubMatrix& subMatrix,
size_t blockIdRow,
size_t blockIdCol,
198 size_t blockSizeRow,
size_t blockSizeCol, Matrix* matrix)
200 matrix->block(blockSizeRow * blockIdRow, blockSizeCol * blockIdCol,
201 blockSizeRow, blockSizeCol) = subMatrix;
213 template <
class Matrix>
214 Eigen::Block<Matrix>
getSubMatrix(Matrix& matrix,
size_t blockIdRow,
size_t blockIdCol,
215 size_t blockSizeRow,
size_t blockSizeCol)
217 return matrix.block(blockSizeRow * blockIdRow, blockSizeCol * blockIdCol, blockSizeRow, blockSizeCol);
224 template <
class Derived>
225 void zeroRow(
size_t row, Eigen::DenseBase<Derived>* matrix)
227 matrix->middleRows(row, 1).setZero();
234 template <
class Derived>
235 void zeroColumn(
size_t column, Eigen::DenseBase<Derived>* matrix)
237 (*matrix).middleCols(column, 1).setZero();
243 #endif // SURGSIM_MATH_MATRIX_H Eigen::Matrix< T, 3, 3 > makeRotationMatrix(const T &angle, const Eigen::Matrix< T, 3, 1, VOpt > &axis)
Create a rotation matrix corresponding to the specified angle (in radians) and axis.
Definition: Matrix.h:74
Wraps glewInit() to separate the glew opengl definitions from the osg opengl definitions only imgui n...
Definition: AddRandomSphereBehavior.cpp:36
Eigen::Matrix< double, 2, 2, Eigen::RowMajor > Matrix22d
A 2x2 matrix of doubles.
Definition: Matrix.h:47
Eigen::Block< Matrix > getSubMatrix(Matrix &matrix, size_t blockIdRow, size_t blockIdCol, size_t blockSizeRow, size_t blockSizeCol)
Helper method to access a sub-matrix from a matrix, for the sake of clarity.
Definition: Matrix.h:214
void setSubMatrix(const SubMatrix &subMatrix, size_t blockIdRow, size_t blockIdCol, size_t blockSizeRow, size_t blockSizeCol, Matrix *matrix)
Helper method to set a sub-matrix into a matrix, for the sake of clarity.
Definition: Matrix.h:197
void zeroColumn(size_t column, Eigen::DenseBase< Derived > *matrix)
Helper method to zero a column of a matrix.
Definition: Matrix.h:235
Eigen::Matrix< double, 4, 4, Eigen::RowMajor > Matrix44d
A 4x4 matrix of doubles.
Definition: Matrix.h:55
T computeAngle(const Eigen::Matrix< T, 3, 3, MOpt > &matrix)
Get the angle corresponding to a quaternion's rotation, in radians.
Definition: Matrix.h:142
Eigen::Matrix< T, 3, 3 > makeSkewSymmetricMatrix(const Eigen::Matrix< T, 3, 1, VOpt > &vector)
Create a skew-symmetric matrix corresponding to the specified vector.
Definition: Matrix.h:86
Eigen::DiagonalMatrix< double, Eigen::Dynamic > DiagonalMatrix
A dynamic size diagonal matrix.
Definition: Matrix.h:62
void addSubMatrix(const SubMatrix &subMatrix, size_t blockIdRow, size_t blockIdCol, size_t blockSizeRow, size_t blockSizeCol, Matrix *matrix)
Helper method to add a sub-matrix into a matrix, for the sake of clarity.
Definition: Matrix.h:157
Eigen::Matrix< double, 6, 6, Eigen::RowMajor > Matrix66d
A 6x6 matrix of doubles.
Definition: Matrix.h:59
void zeroRow(size_t row, Eigen::DenseBase< Derived > *matrix)
Helper method to zero a row of a matrix.
Definition: Matrix.h:225
void computeAngleAndAxis(const Eigen::Matrix< T, 3, 3, MOpt > &matrix, T *angle, Eigen::Matrix< T, 3, 1, VOpt > *axis)
Get the angle (in radians) and axis corresponding to a rotation matrix.
Definition: Matrix.h:127
Eigen::Matrix< float, 4, 4, Eigen::RowMajor > Matrix44f
A 4x4 matrix of floats.
Definition: Matrix.h:43
Eigen::Matrix< float, 3, 3, Eigen::RowMajor > Matrix33f
A 3x3 matrix of floats.
Definition: Matrix.h:39
Eigen::Matrix< double, 3, 3, Eigen::RowMajor > Matrix33d
A 3x3 matrix of doubles.
Definition: Matrix.h:51
Eigen::Matrix< float, 2, 2, Eigen::RowMajor > Matrix22f
A 2x2 matrix of floats.
Definition: Matrix.h:35
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic > Matrix
A dynamic size matrix.
Definition: Matrix.h:65
Eigen::Matrix< T, 3, 1 > skew(const Eigen::Matrix< T, 3, 3, MOpt > &matrix)
Extract the unique vector from the skew-symmetric part of a given matrix.
Definition: Matrix.h:113