27 #include <MathAlgo/SVD.hh>
28 #include <MathAlgo/Minpack.hh>
41 BIASERR(
"wrong matrix size "<<mat.
num_rows()<<
"x"
55 memcpy(this->
Data_, UVT.
GetData(), 9*
sizeof(ROTATION_MATRIX_TYPE));
61 int ErrorFunctionAverageRMatrix(
void *p,
int numerrors,
int numvars,
62 const double *x,
double *fvec,
int iflag)
64 BIASASSERT(numvars==4);
74 double error = 0, err;
75 for (
unsigned int j=0; j<pInstance->
pvecR_->size(); j++) {
76 for (
unsigned int row=0; row<3; row++) {
77 for (
unsigned int col=0; col<3; col++) {
78 err = fvec[9*j+3*col+row]
79 = R[row][col] - (*pInstance->
pvecR_)[j][row][col];
86 if (error<pInstance->bestErrorSoFar_) {
98 BIASERR(
"Empty vector given for average rotation matrix estimation!");
101 BIASWARN(
"Untested code for average rotation matrix computation so far!");
110 for (
unsigned int i = 1; i < vecR.size(); i++) {
114 weight = 1.0 / double(i+1);
115 RMatrix RDiff = result * vecR[i].Transpose();
119 angle *= -1.0 * weight;
120 RDiff.
Set(axis, angle);
121 result = RDiff * result;
126 if (RefineRotation) {
129 long int num_params = 4;
130 long int num_errors = 9 * vecR.size();
131 long int max_error_calls = 1000;
132 double epsilon = 0.02;
138 for (
unsigned int i = 0; i < 4; i++) qvec[i] = q[i];
143 (
void*)(
this), num_errors, num_params,
144 qvec, qresvec, epsilon,
145 1e-10, max_error_calls);
146 if ((LMResult != LM_OK) && (LMResult != LM_TOO_MANY_IT)) {
147 BIASERR(
"Error in Levenberg-Marquardt optimization (error code is "
150 cout <<
"Levenberg-Marquardt optimization finished with code "
virtual void EnforceConstraints()
Enforce orthonormality constraints and right-handed rotation with SVD.
long int LevenbergMarquardtExtended(minpack_funcder_mn fun, void *p, long int m, long int n, Vector< double > &InitialGuess, Vector< double > &Result, double Tolerance, long int MaxIter)
Solves an overdetermined system f(x) = 0 of m non-linear equations with n parameters using the extend...
std::vector< RMatrix > * pvecR_
computes and holds the singular value decomposition of a rectangular (not necessarily quadratic) Matr...
Subscript num_cols() const
void Set(const T &scalar)
set all elements to a scalat value
int SetFromQuaternion(const Quaternion< ROTATION_MATRIX_TYPE > &q)
Set rotation matrix from a quaternion.
int GetQuaternion(Quaternion< ROTATION_MATRIX_TYPE > &quat) const
Calculates quaternion representation for this rotation matrix.
ROTATION_MATRIX_TYPE Data_[9]
void Set(const Vector3< ROTATION_MATRIX_TYPE > &w, const ROTATION_MATRIX_TYPE phi)
Set from rotation axis w and angle phi (in rad)
const Matrix< double > & GetVT() const
return VT (=transposed(V))
Quaternion< double > bestQuaternionSoFar_
matrix class with arbitrary size, indexing is row major.
RMatrix GetAverageRMatrix(std::vector< RMatrix > &vecR, bool RefineRotation=true)
Compute average rotation by pairwise weighted averaging of matrices.
const Matrix< double > & GetU() const
return U U is a m x m orthogonal matrix
ROTATION_MATRIX_TYPE GetDeterminant() const
returns the Determinant |A| of this
Implements a 3D rotation matrix.
Subscript num_rows() const
int GetRotationAxisAngle(Vector3< ROTATION_MATRIX_TYPE > &axis, ROTATION_MATRIX_TYPE &angle) const
Calculates angle and rotation axis representation for this rotation matrix.