22 #include <Geometry/RMatrix.hh>
23 #include <Base/Geometry/RMatrixBase.hh>
24 #include <Base/Math/Random.hh>
25 #include <Base/Math/Vector3.hh>
38 vec[0] = sin(phi) * cos(theta);
40 vec[2] = cos(phi) * cos(theta);
45 printf(
"%s = [ % 1.6f % 1.6f % 1.6f ; % 1.6f % 1.6f % 1.6f ; % 1.6f % 1.6f % 1.6f ]\n",
46 name.c_str(), mat[0][0], mat[0][1], mat[0][2],
47 mat[1][0], mat[1][1], mat[1][2], mat[2][0], mat[2][1], mat[2][2]);
70 int main(
int argc,
char *argv[])
73 const int numMatrix = argc > 1 ? std::max(1, atoi(argv[1])) : 100;
74 const int numVector = argc > 2 ? std::max(1, atoi(argv[2])) : 100;
75 const double errorStd = argc > 3 ? std::min(1.0, fabs(atof(argv[3]))) : 0.01;
76 const double matrixErrorThreshold = 1;
77 const double matrixCheckThreshold = 1e-10;
79 cout <<
"\nUsage : TestRMatrix [<num matrices>] [<num vectors>] [<error sigma>]\n\n"
80 <<
"Starting tests with " << numMatrix <<
" rotation matrices and "
81 << numVector <<
" 3d vectors with error sigma " << errorStd <<
" ...\n";
84 vector<Vector3<double> > vecs3d(numVector);
85 for (
int vnum = 0; vnum < numVector; vnum++)
86 computeRandomVec(random, vecs3d[vnum]);
87 double meanVectorError = 0, stdVectorError = 0;
93 double meanMatrixError = 0, stdMatrixError = 0;
94 int numMatrixFailed = 0;
95 for (
int mnum = 0; mnum < numMatrix; mnum++)
98 computeRandomVec(random, rx);
99 int min_id = (fabs(rx[1]) < fabs(rx[0])) ? 1 : 0;
100 if (fabs(rx[2]) < fabs(rx[min_id])) min_id = 2;
109 for (
int k = 0; k < 3; k++) {
115 BIASASSERT(Rgt.
Check(matrixCheckThreshold));
118 for (
int k = 0; k < 3; k++) {
128 for (
int k = 0; k < 3; k++) {
129 R[0][k] = Rbase[0][k] = rx[k];
130 R[1][k] = Rbase[1][k] = ry[k];
131 R[2][k] = Rbase[2][k] = rz[k];
135 const bool checkR = R.
Check(matrixCheckThreshold);
136 const bool checkRbase = Rbase.
Check(matrixCheckThreshold);
140 meanMatrixError += error;
141 stdMatrixError += error * error;
142 if (error > matrixErrorThreshold || !checkR || !checkRbase) {
144 cout <<
"Test " << (mnum+1) <<
" / " << numMatrix <<
" failed : \n";
146 cout <<
"-> R is not orthonormal : "
149 cout <<
"-> Rbase is not orthonormal : "
151 if (error > matrixErrorThreshold)
152 cout <<
"-> Difference " << error <<
" is too large!\n";
153 printMatrix3x3(R,
"-> R");
154 printMatrix3x3(Rbase,
"-> Rbase");
155 cout <<
"--------------------------------------------------\n";
159 for (
int vnum = 0; vnum < numVector; vnum++) {
160 error = (R*vecs3d[vnum] - Rbase*vecs3d[vnum]).NormL2();
161 meanVectorError += error;
162 stdVectorError += error * error;
167 meanMatrixError /= numMatrix;
168 stdMatrixError = sqrt(stdMatrixError / numMatrix -
169 meanMatrixError * meanMatrixError);
170 meanVectorError /= numMatrix * numVector;
171 stdVectorError = sqrt(stdVectorError / (numMatrix * numVector) -
172 meanVectorError * meanVectorError);
173 cout <<
"\nResults : " << numMatrixFailed <<
" / " << numMatrix
174 <<
" tests failed (" << numMatrixFailed / (0.01 * numMatrix) <<
" %)\n";
175 cout <<
"-> average difference between RMatrix and RMatrixBase : "
176 << meanMatrixError <<
" +- " << stdMatrixError <<
"\n"
177 <<
"-> average difference between rotated unit vectors : "
178 << meanVectorError <<
" +- " << stdVectorError <<
"\n\n";
virtual void EnforceConstraints()
Enforce orthonormality constraints and right-handed rotation with SVD.
void SetZero()
set all values to 0
bool Check(const double eps=std::numeric_limits< double >::epsilon(), const bool verbose=false) const
Check if this is a rotation matrix, i.e.
double GetUniformDistributed(const double min, const double max)
on succesive calls return uniform distributed random variable between min and max ...
virtual void EnforceConstraints()
Enforce orthonormality constraint on rotation matrix and sets determinant to +1.
std::string GetCheckFailureReason(const double eps=std::numeric_limits< double >::epsilon()) const
Return the reason for the constraint check failure in a human readable string.
void CrossProduct(const Vector3< T > &argvec, Vector3< T > &destvec) const
cross product of two vectors destvec = this x argvec
T GetDeterminant() const
returns the Determinant |A| of this
Implements a 3D rotation matrix.
double GetNormalDistributed(const double mean, const double sigma)
on succesive calls return normal distributed random variable with mean and standard deviation sigma ...
Vector3< T > & Normalize()
normalize this vector to length 1
class for producing random numbers from different distributions