41 #include "private/vpLevenbergMarquartd.h"
42 #include <visp3/vision/vpPose.h>
46 #define MINIMUM 0.000001
67 #define MIJ(m, i, j, s) ((m) + ((long)(i) * (long)(s)) + (long)(j))
70 #define MINIMUM 0.000001
81 static double XI[NBPTMAX], YI[NBPTMAX];
82 static double XO[NBPTMAX], YO[NBPTMAX], ZO[NBPTMAX];
84 void eval_function(
int npt,
double *xc,
double *f);
85 void fcn(
int m,
int n,
double *xc,
double *fvecc,
double *jac,
int ldfjac,
int iflag);
87 void eval_function(
int npt,
double *xc,
double *f)
90 const unsigned int sizeU = 3;
92 const unsigned int index_0 = 0;
93 const unsigned int index_1 = 1;
94 const unsigned int index_2 = 2;
95 const unsigned int index_3 = 3;
96 const unsigned int index_4 = 4;
97 const unsigned int index_5 = 5;
99 u[index_0] = xc[index_3];
100 u[index_1] = xc[index_4];
101 u[index_2] = xc[index_5];
105 for (i = 0; i < npt; ++i) {
106 double x = (rd[index_0][index_0] * XO[i]) + (rd[index_0][index_1] * YO[i]) + (rd[index_0][index_2] * ZO[i]) + xc[index_0];
107 double y = (rd[index_1][index_0] * XO[i]) + (rd[index_1][index_1] * YO[i]) + (rd[index_1][index_2] * ZO[i]) + xc[index_1];
108 double z = (rd[index_2][index_0] * XO[i]) + (rd[index_2][index_1] * YO[i]) + (rd[index_2][index_2] * ZO[i]) + xc[index_2];
109 f[i] = (x / z) - XI[i];
110 f[npt + i] = (y / z) - YI[i];
141 void fcn(
int m,
int n,
double *xc,
double *fvecc,
double *jac,
int ldfjac,
int iflag)
148 printf(
"pas assez de points\n");
153 const int flagFunc = 1, flagJacobian = 2;
154 if (iflag == flagFunc) {
155 eval_function(npt, xc, fvecc);
157 else if (iflag == flagJacobian) {
159 const unsigned int index_0 = 0;
160 const unsigned int index_1 = 1;
161 const unsigned int index_2 = 2;
162 const unsigned int index_3 = 3;
163 const unsigned int index_4 = 4;
164 const unsigned int index_5 = 5;
165 u[index_0] = xc[index_3];
166 u[index_1] = xc[index_4];
167 u[index_2] = xc[index_5];
169 rd.
buildFrom(u[index_0], u[index_1], u[index_2]);
173 double tt = sqrt((u[index_0] * u[index_0]) + (u[index_1] * u[index_1]) + (u[index_2] * u[index_2]));
175 u1 = u[index_0] / tt;
176 u2 = u[index_1] / tt;
177 u3 = u[index_2] / tt;
185 double mco = 1.0 - co;
188 for (
int i = 0; i < npt; ++i) {
194 double rx = (rd[index_0][index_0] * x) + (rd[index_0][index_1] * y) + (rd[index_0][index_2] * z) + xc[index_0];
195 double ry = (rd[index_1][index_0] * x) + (rd[index_1][index_1] * y) + (rd[index_1][index_2] * z) + xc[index_1];
196 double rz = (rd[index_2][index_0] * x) + (rd[index_2][index_1] * y) + (rd[index_2][index_2] * z) + xc[index_2];
201 double drxt = (((si * u1 * u3) + (co * u2)) * z) + (((si * u1 * u2) - (co * u3)) * y) + (((si * u1 * u1) - si) * x);
202 double drxu1 = (mco * u3 * z) + (mco * u2 * y) + (2. * mco * u1 * x);
203 double drxu2 = (si * z) + (mco * u1 * y);
204 double drxu3 = (mco * u1 * z) - (si * y);
206 double dryt = (((si * u2 * u3) - (co * u1)) * z) + (((si * u2 * u2) - si) * y) + (((co * u3) + (si * u1 * u2)) * x);
207 double dryu1 = (mco * u2 * x) - (si * z);
208 double dryu2 = (mco * u3 * z) + (2 * mco * u2 * y) + (mco * u1 * x);
209 double dryu3 = (mco * u2 * z) + (si * x);
211 double drzt = (((si * u3 * u3) - si) * z) + (((si * u2 * u3) + (co * u1)) * y) + (((si * u1 * u3) - (co * u2)) * x);
212 double drzu1 = (si * y) + (mco * u3 * x);
213 double drzu2 = (mco * u3 * y) - (si * x);
214 double drzu3 = (2 * mco * u3 * z) + (mco * u2 * y) + (mco * u1 * x);
219 double dxit = (drxt / rz) - ((rx * drzt) / (rz * rz));
221 double dyit = (dryt / rz) - ((ry * drzt) / (rz * rz));
223 double dxiu1 = (drxu1 / rz) - ((drzu1 * rx) / (rz * rz));
224 double dyiu1 = (dryu1 / rz) - ((drzu1 * ry) / (rz * rz));
226 double dxiu2 = (drxu2 / rz) - ((drzu2 * rx) / (rz * rz));
227 double dyiu2 = (dryu2 / rz) - ((drzu2 * ry) / (rz * rz));
229 double dxiu3 = (drxu3 / rz) - ((drzu3 * rx) / (rz * rz));
230 double dyiu3 = (dryu3 / rz) - ((drzu3 * ry) / (rz * rz));
236 *MIJ(jac, index_0, i, ldfjac) = 1. / rz;
237 *MIJ(jac, index_1, i, ldfjac) = 0.0;
238 *MIJ(jac, index_2, i, ldfjac) = -rx / (rz * rz);
240 *MIJ(jac, index_3, i, ldfjac) = (((u1 * dxit) + (((1. - (u1 * u1)) * dxiu1) / tt)) - ((u1 * u2 * dxiu2) / tt)) - ((u1 * u3 * dxiu3) / tt);
241 *MIJ(jac, index_4, i, ldfjac) = (((u2 * dxit) - ((u1 * u2 * dxiu1) / tt)) + (((1. - (u2 * u2)) * dxiu2) / tt)) - ((u2 * u3 * dxiu3) / tt);
243 *MIJ(jac, index_5, i, ldfjac) = (((u3 * dxit) - ((u1 * u3 * dxiu1) / tt)) - ((u2 * u3 * dxiu2) / tt)) + (((1. - (u3 * u3)) * dxiu3) / tt);
246 *MIJ(jac, index_3, i, ldfjac) = 0.0;
247 *MIJ(jac, index_4, i, ldfjac) = 0.0;
248 *MIJ(jac, index_5, i, ldfjac) = 0.0;
250 *MIJ(jac, index_0, npt + i, ldfjac) = 0.0;
251 *MIJ(jac, index_1, npt + i, ldfjac) = 1 / rz;
252 *MIJ(jac, index_2, npt + i, ldfjac) = -ry / (rz * rz);
254 *MIJ(jac, index_3, npt + i, ldfjac) =
255 (((u1 * dyit) + (((1 - (u1 * u1)) * dyiu1) / tt)) - ((u1 * u2 * dyiu2) / tt)) - ((u1 * u3 * dyiu3) / tt);
256 *MIJ(jac, index_4, npt + i, ldfjac) =
257 (((u2 * dyit) - ((u1 * u2 * dyiu1) / tt)) + (((1. - (u2 * u2)) * dyiu2) / tt)) - ((u2 * u3 * dyiu3) / tt);
258 *MIJ(jac, index_5, npt + i, ldfjac) =
259 (((u3 * dyit) - ((u1 * u3 * dyiu1) / tt)) - ((u2 * u3 * dyiu2) / tt)) + (((1. - (u3 * u3)) * dyiu3) / tt);
262 *MIJ(jac, index_3, npt + i, ldfjac) = 0.0;
263 *MIJ(jac, index_4, npt + i, ldfjac) = 0.0;
264 *MIJ(jac, index_5, npt + i, ldfjac) = 0.0;
273 const int n = NBR_PAR;
274 const int m =
static_cast<int>(2 *
npt);
276 const int lwa = (2 * NBPTMAX) + 50;
277 const int ldfjac = 2 * NBPTMAX;
278 int info, ipvt[NBR_PAR];
280 double f[ldfjac], sol[NBR_PAR];
281 double tol, jac[NBR_PAR][ldfjac], wa[lwa];
285 tol = std::numeric_limits<double>::epsilon();
294 const unsigned int val_3 = 3;
295 for (
unsigned int i = 0; i < val_3; ++i) {
296 sol[i] = cMo[i][val_3];
297 sol[i + val_3] = u[i];
302 std::list<vpPoint>::const_iterator listp_end =
listP.end();
303 for (std::list<vpPoint>::const_iterator it =
listP.begin(); it != listp_end; ++it) {
312 tst_lmder = lmder1(&fcn, m, n, sol, f, &jac[0][0], ldfjac, tol, &info, ipvt, lwa, wa);
313 if (tst_lmder == -1) {
314 std::cout <<
" in CCalculPose::PoseLowe(...) : ";
315 std::cout <<
"pb de minimization, returns FATAL_ERROR";
319 for (
unsigned int i = 0; i < val_3; ++i) {
320 u[i] = sol[i + val_3];
323 for (
unsigned int i = 0; i < val_3; ++i) {
324 cMo[i][val_3] = sol[i];
325 u[i] = sol[i + val_3];
Implementation of an homogeneous matrix and operations on such kind of matrices.
void extract(vpRotationMatrix &R) const
void insert(const vpRotationMatrix &R)
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_y() const
Get the point y coordinate in the image plane.
double get_oZ() const
Get the point oZ coordinate in the object frame.
double get_x() const
Get the point x coordinate in the image plane.
double get_oY() const
Get the point oY coordinate in the object frame.
unsigned int npt
Number of point used in pose computation.
std::list< vpPoint > listP
Array of point (use here class vpPoint)
void poseLowe(vpHomogeneousMatrix &cMo)
Compute the pose using the Lowe non linear approach it consider the minimization of a residual using ...
Implementation of a rotation matrix and operations on such kind of matrices.
vpRotationMatrix & buildFrom(const vpHomogeneousMatrix &M)
Implementation of a rotation vector as axis-angle minimal representation.