34 #include <visp3/core/vpMath.h>
35 #include <visp3/vision/vpPose.h>
37 #define DEBUG_LEVEL1 0
38 #define DEBUG_LEVEL2 0
39 #define DEBUG_LEVEL3 0
41 #define SEUIL_RESIDUAL 0.0001
50 std::cout <<
"begin (Dementhon.cc)CalculSolutionDementhon() " << std::endl;
54 double normI3 = sqrt(I4[0] * I4[0] + I4[1] * I4[1] + I4[2] * I4[2]);
55 double normJ3 = sqrt(J4[0] * J4[0] + J4[1] * J4[1] + J4[2] * J4[2]);
57 if ((normI3 < 1e-10) || (normJ3 < 1e-10)) {
60 "Division by zero in Dementhon pose computation: normI or normJ = 0"));
63 double Z0 = 2.0 / (normI3 + normJ3);
66 for (
unsigned int i = 0; i < 3; i++) {
67 I3[i] = I4[i] / normI3;
68 J3[i] = J4[i] / normJ3;
80 cMo[0][3] = I4[3] * Z0;
85 cMo[1][3] = J4[3] * Z0;
93 std::cout <<
"end (Dementhon.cc)CalculSolutionDementhon() " << std::endl;
107 cdg[0] = cdg[1] = cdg[2] = 0.0;
108 for (std::list<vpPoint>::const_iterator it =
listP.begin(); it !=
listP.end(); ++it) {
114 for (
unsigned int i = 0; i < 3; i++)
120 for (std::list<vpPoint>::const_iterator it =
listP.begin(); it !=
listP.end(); ++it) {
130 for (
unsigned int i = 0; i <
npt; i++) {
131 A[i][0] = c3d[i].get_oX();
132 A[i][1] = c3d[i].get_oY();
133 A[i][2] = c3d[i].get_oZ();
140 std::cout <<
"A" << std::endl << A << std::endl;
141 std::cout <<
"A^+" << std::endl << Ap << std::endl;
148 for (
unsigned int i = 0; i <
npt; i++) {
149 xprim[i] = c3d[i].get_x();
150 yprim[i] = c3d[i].get_y();
157 calculSolutionDementhon(I4, J4, cMo);
160 for (
unsigned int i = 0; i <
npt; i++) {
162 z = cMo[2][0] * c3d[i].get_oX() + cMo[2][1] * c3d[i].get_oY() + cMo[2][2] * c3d[i].get_oZ() + cMo[2][3];
171 double res_old = 2.0 * res;
176 while ((cpt < ITER_MAX) && (res > SEUIL_RESIDUAL) && (res < res_old)) {
182 for (
unsigned int i = 0; i <
npt; i++) {
184 (cMo[2][0] * c3d[i].get_oX() + cMo[2][1] * c3d[i].get_oY() + cMo[2][2] * c3d[i].get_oZ()) / cMo[2][3];
186 xprim[i] = (1.0 + eps) * c3d[i].get_x();
187 yprim[i] = (1.0 + eps) * c3d[i].get_y();
192 calculSolutionDementhon(I4, J4, cMo);
194 for (
unsigned int i = 0; i <
npt; i++) {
196 z = cMo[2][0] * c3d[i].get_oX() + cMo[2][1] * c3d[i].get_oY() + cMo[2][2] * c3d[i].get_oZ() + cMo[2][3];
204 std::cout <<
"Pb z < 0 with cMo in Dementhon's loop" << std::endl;
210 std::cout <<
"it = " << cpt <<
" residu = " << res <<
" Theta U rotation: " <<
vpMath::deg(erc[0]) <<
" "
215 std::cout <<
"Divergence : res = " << res <<
" res_old = " << res_old << std::endl;
222 cMo[0][3] -= (cdg[0] * cMo[0][0] + cdg[1] * cMo[0][1] + cdg[2] * cMo[0][2]);
223 cMo[1][3] -= (cdg[0] * cMo[1][0] + cdg[1] * cMo[1][1] + cdg[2] * cMo[1][2]);
224 cMo[2][3] -= (cdg[0] * cMo[2][0] + cdg[1] * cMo[2][1] + cdg[2] * cMo[2][2]);
227 static void calculRTheta(
double s,
double c,
double &r,
double &theta)
229 if ((fabs(c) > EPS_DEM) || (fabs(s) > EPS_DEM)) {
230 r = sqrt(sqrt(s * s + c * c));
231 theta = atan2(s, c) / 2.0;
233 if (fabs(c) > fabs(s)) {
253 for (
unsigned int i = 0; i < 3; i++) {
258 double c = J0.sumSquare() - I0.sumSquare();
261 calculRTheta(s, c, r, theta);
262 double co = cos(theta);
263 double si = sin(theta);
267 I = I04 + U * r * co;
268 J = J04 + U * r * si;
272 std::cout <<
"I0 " << I04.
t() << std::endl;
273 std::cout <<
"J0 " << J04.
t() << std::endl;
274 std::cout <<
"I1 " << I.t() << std::endl;
275 std::cout <<
"J1 " << J.t() << std::endl;
278 calculSolutionDementhon(I, J, cMo1);
281 I = I04 - U * r * co;
282 J = J04 - U * r * si;
285 std::cout <<
"I2 " << I.t() << std::endl;
286 std::cout <<
"J2 " << J.t() << std::endl;
289 calculSolutionDementhon(I, J, cMo2);
298 std::cout <<
"begin vpPose::CalculArbreDementhon() " << std::endl;
304 for (
unsigned int i = 0; i <
npt; i++) {
306 z = cMo[2][0] * c3d[i].get_oX() + cMo[2][1] * c3d[i].get_oY() + cMo[2][2] * c3d[i].get_oZ() + cMo[2][3];
313 unsigned int cpt = 0;
315 double res_old = 2.0 * res_min;
318 while ((cpt < ITER_MAX) && (res_min > SEUIL_RESIDUAL) && (res_min < res_old)) {
325 for (
unsigned int i = 0; i <
npt; i++) {
327 (cMo[2][0] * c3d[i].get_oX() + cMo[2][1] * c3d[i].get_oY() + cMo[2][2] * c3d[i].get_oZ()) / cMo[2][3];
329 xprim[i] = (1.0 + eps) * c3d[i].get_x();
330 yprim[i] = (1.0 + eps) * c3d[i].get_y();
337 calculTwoSolutionsDementhonPlan(I04, J04, U, cMo1, cMo2);
342 for (
unsigned int i = 0; i <
npt; i++) {
344 z = cMo1[2][0] * c3d[i].get_oX() + cMo1[2][1] * c3d[i].get_oY() + cMo1[2][2] * c3d[i].get_oZ() + cMo1[2][3];
347 z = cMo2[2][0] * c3d[i].get_oX() + cMo2[2][1] * c3d[i].get_oY() + cMo2[2][2] * c3d[i].get_oZ() + cMo2[2][3];
352 if ((erreur1 == -1) && (erreur2 == -1)) {
355 std::cout <<
" End of loop since z < 0 for both solutions" << std::endl;
359 if ((erreur1 == 0) && (erreur2 == -1)) {
363 if ((erreur1 == -1) && (erreur2 == 0)) {
367 if ((erreur1 == 0) && (erreur2 == 0)) {
384 std::cout <<
"it = " << cpt <<
" cMo1 : residu: " << s <<
" Theta U rotation: " <<
vpMath::deg(erc[0]) <<
" "
387 std::cout <<
"Pb z < 0 with cMo1" << std::endl;
393 std::cout <<
"it = " << cpt <<
" cMo2 : residu: " << s <<
" Theta U rotation: " <<
vpMath::deg(erc[0]) <<
" "
396 std::cout <<
"Pb z < 0 with cMo2" << std::endl;
399 if (res_min > res_old) {
401 std::cout <<
"Divergence : res_min = " << res_min <<
" res_old = " << res_old << std::endl;
409 std::cout <<
"end vpPose::CalculArbreDementhon() return " << erreur << std::endl;
426 std::cout <<
"begin CCalculPose::PoseDementhonPlan()" << std::endl;
428 const double svdFactorUsedWhenFailure = 10.;
429 const double svdThresholdLimit = 1e-2;
430 const double lnOfSvdFactorUsed = std::log(svdFactorUsedWhenFailure);
431 const double logNOfSvdThresholdLimit = std::log(svdThresholdLimit)/lnOfSvdFactorUsed;
435 cdg[0] = cdg[1] = cdg[2] = 0.0;
436 for (std::list<vpPoint>::const_iterator it =
listP.begin(); it !=
listP.end(); ++it) {
442 for (
unsigned int i = 0; i < 3; i++)
448 for (std::list<vpPoint>::const_iterator it =
listP.begin(); it !=
listP.end(); ++it) {
458 for (
unsigned int i = 0; i <
npt; i++) {
459 A[i][0] = c3d[i].get_oX();
460 A[i][1] = c3d[i].get_oY();
461 A[i][2] = c3d[i].get_oZ();
466 bool isRankEqualTo3 =
false;
468 int nbMaxIter =
static_cast<int>(std::max(std::ceil(logNOfSvdThresholdLimit - logNofSvdThresh), 1.));
471 for(
int i = 0; i < nbMaxIter && !isRankEqualTo3; i++)
473 irank = A.
pseudoInverse(Ap, sv, svdThreshold, imA, imAt, kAt);
476 isRankEqualTo3 =
true;
480 isRankEqualTo3 =
false;
481 svdThreshold *= svdFactorUsedWhenFailure;
485 if (!isRankEqualTo3) {
486 std::stringstream errorMsg;
487 errorMsg <<
"In Dementhon planar, after ";
488 errorMsg << nbMaxIter;
489 errorMsg <<
" trials multiplying the svd threshold by ";
490 errorMsg << svdFactorUsedWhenFailure;
491 errorMsg <<
", rank (";
493 errorMsg <<
") is still not 3";
498 for (
unsigned int i = 0; i < 4; i++) {
503 std::cout <<
"A" << std::endl << A << std::endl;
504 std::cout <<
"A^+" << std::endl << Ap << std::endl;
505 std::cout <<
"U^T = " << U.
t() << std::endl;
512 for (
unsigned int i = 0; i <
npt; i++) {
513 xi[i] = c3d[i].get_x();
514 yi[i] = c3d[i].get_y();
522 calculTwoSolutionsDementhonPlan(I04, J04, U, cMo1, cMo2);
528 std::cout <<
"cMo Start Tree 1 : res " << res <<
" Theta U rotation: " <<
vpMath::deg(erc[0]) <<
" "
532 std::cout <<
"cMo Start Tree 2 : res " << res <<
" Theta U rotation: " <<
vpMath::deg(erc[0]) <<
" "
539 if ((erreur1 == -1) && (erreur2 == -1)) {
543 if ((erreur1 == 0) && (erreur2 == -1))
545 if ((erreur1 == -1) && (erreur2 == 0))
547 if ((erreur1 == 0) && (erreur2 == 0)) {
558 std::cout <<
"Pb z < 0 with Start Tree 1" << std::endl;
560 std::cout <<
"Pb z < 0 with Start Tree 2" << std::endl;
562 std::cout <<
" Tree 1 chosen " << std::endl;
564 std::cout <<
" Tree 2 chosen " << std::endl;
568 cMo[0][3] -= (cdg[0] * cMo[0][0] + cdg[1] * cMo[0][1] + cdg[2] * cMo[0][2]);
569 cMo[1][3] -= (cdg[0] * cMo[1][0] + cdg[1] * cMo[1][1] + cdg[2] * cMo[1][2]);
570 cMo[2][3] -= (cdg[0] * cMo[2][0] + cdg[1] * cMo[2][1] + cdg[2] * cMo[2][2]);
573 std::cout <<
"end CCalculPose::PoseDementhonPlan()" << std::endl;
587 double squared_error = 0;
593 for (
unsigned int i = 0; i <
npt; i++) {
595 double X = c3d[i].get_oX() * cMo[0][0] + c3d[i].get_oY() * cMo[0][1] + c3d[i].get_oZ() * cMo[0][2] + cMo[0][3];
596 double Y = c3d[i].get_oX() * cMo[1][0] + c3d[i].get_oY() * cMo[1][1] + c3d[i].get_oZ() * cMo[1][2] + cMo[1][3];
597 double Z = c3d[i].get_oX() * cMo[2][0] + c3d[i].get_oY() * cMo[2][1] + c3d[i].get_oZ() * cMo[2][2] + cMo[2][3];
604 return squared_error;
608 #undef SEUIL_RESIDUAL
Implementation of column vector and the associated operations.
static double dotProd(const vpColVector &a, const vpColVector &b)
static vpColVector cross(const vpColVector &a, const vpColVector &b)
error that can be emitted by ViSP classes.
@ divideByZeroError
Division by zero.
Implementation of an homogeneous matrix and operations on such kind of matrices.
void extract(vpRotationMatrix &R) const
static double sqr(double x)
static double deg(double rad)
Implementation of a matrix and operations on matrices.
vpMatrix pseudoInverse(double svThreshold=1e-6) const
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
double get_oX() const
Get the point oX coordinate in the object frame.
double get_oZ() const
Get the point oZ coordinate in the object frame.
void set_oY(double oY)
Set the point oY coordinate in the object frame.
void set_oZ(double oZ)
Set the point oZ coordinate in the object frame.
void set_oX(double oX)
Set the point oX coordinate in the object frame.
double get_oY() const
Get the point oY coordinate in the object frame.
double computeResidualDementhon(const vpHomogeneousMatrix &cMo)
Compute and return the residual corresponding to the sum of squared residuals in meter^2 for the pose...
unsigned int npt
Number of point used in pose computation.
double dementhonSvThresh
SVD threshold use for the pseudo-inverse computation in poseDementhonPlan.
void poseDementhonNonPlan(vpHomogeneousMatrix &cMo)
std::list< vpPoint > listP
Array of point (use here class vpPoint)
int calculArbreDementhon(vpMatrix &b, vpColVector &U, vpHomogeneousMatrix &cMo)
void poseDementhonPlan(vpHomogeneousMatrix &cMo)
Compute the pose using Dementhon approach for planar objects this is a direct implementation of the a...
Implementation of a rotation vector as axis-angle minimal representation.