15 #include <mrpt/math/CVectorFixed.h>
16 #include <mrpt/math/TPoint3D.h>
17 #include <mrpt/math/geometry.h>
18 #include <mrpt/math/ops_containers.h>
19 #include <mrpt/poses/CPose3D.h>
20 #include <mrpt/poses/Lie/SE.h>
22 #include <Eigen/Dense>
26 using namespace mrpt::math;
29 const mrpt::tfest::TMatchingPair& pairing,
const mrpt::poses::CPose3D& relativePose,
30 mrpt::optional_ref<mrpt::math::CMatrixFixed<double, 3, 12>> jacobian)
33 mrpt::math::CVectorFixedDouble<3> error;
34 const mrpt::math::TPoint3D& l = pairing.local;
36 const mrpt::math::TPoint3D g = relativePose.composePoint(l);
38 error[0] = g.x - pairing.global.x;
39 error[1] = g.y - pairing.global.y;
40 error[2] = g.z - pairing.global.z;
46 mrpt::math::CMatrixFixed<double, 3, 12>& J_aux = jacobian.value().get();
48 J_aux = (Eigen::Matrix<double, 3, 12>() <<
49 l.x, 0, 0, l.y, 0, 0, l.z, 0, 0, 1, 0, 0,
50 0, l.x, 0, 0, l.y, 0, 0, l.z, 0, 0, 1, 0,
51 0, 0, l.x, 0, 0, l.y, 0, 0, l.z, 0, 0, 1
62 mrpt::optional_ref<mrpt::math::CMatrixFixed<double, 3, 12>> jacobian)
65 mrpt::math::CVectorFixedDouble<3> error;
69 const mrpt::math::TPoint3D l = pairing.
pt_local;
70 const mrpt::math::TPoint3D g = relativePose.composePoint(l);
73 const auto& u = lnGlob.director;
74 const auto q = (g - lnGlob.pBase);
76 const auto uq = mrpt::math::dotProduct<3, double>(u,
q);
78 error[0] =
q[0] - u.x * uq;
79 error[1] =
q[1] - u.y * uq;
80 error[2] =
q[2] - u.z * uq;
85 const Eigen::Matrix<double, 3, 3> J1 =
86 (Eigen::Matrix<double, 3, 3>() << 1 - mrpt::square(u.x), -u.x * u.y, -u.x * u.z,
87 -u.y * u.x, 1 - mrpt::square(u.y), -u.y * u.z, -u.z * u.x, -u.z * u.y,
88 1 - mrpt::square(u.z))
92 Eigen::Matrix<double, 3, 12> J2 =
93 (Eigen::Matrix<double, 3, 12>() <<
94 l.x, 0, 0, l.y, 0, 0, l.z, 0, 0, 1, 0, 0,
95 0, l.x, 0, 0, l.y, 0, 0, l.z, 0, 0, 1, 0,
96 0, 0, l.x, 0, 0, l.y, 0, 0, l.z, 0, 0, 1
99 mrpt::math::CMatrixFixed<double, 3, 12>& J_aux = jacobian.value().get();
109 mrpt::optional_ref<mrpt::math::CMatrixFixed<double, 3, 12>> jacobian)
112 mrpt::math::CVectorFixedDouble<3> error;
115 const mrpt::math::TPoint3D l = TPoint3D(p.x, p.y, p.z);
116 mrpt::math::TPoint3D g;
117 relativePose.composePoint(l, g);
118 double mod_n = mrpt::square(pl_aux.coefs[0]) + mrpt::square(pl_aux.coefs[1]) +
119 mrpt::square(pl_aux.coefs[2]);
121 error[0] = -(pl_aux.coefs[0] / mod_n) * (pl_aux.coefs[0] * g.x + pl_aux.coefs[1] * g.y +
122 pl_aux.coefs[2] * g.z + pl_aux.coefs[3]);
123 error[1] = -(pl_aux.coefs[1] / mod_n) * (pl_aux.coefs[0] * g.x + pl_aux.coefs[1] * g.y +
124 pl_aux.coefs[2] * g.z + pl_aux.coefs[3]);
125 error[2] = -(pl_aux.coefs[2] / mod_n) * (pl_aux.coefs[0] * g.x + pl_aux.coefs[1] * g.y +
126 pl_aux.coefs[2] * g.z + pl_aux.coefs[3]);
131 const Eigen::Matrix<double, 3, 3> J1 =
132 (Eigen::Matrix<double, 3, 3>() << -mrpt::square(pl_aux.coefs[0]) / mod_n,
133 -pl_aux.coefs[0] * pl_aux.coefs[1] / mod_n, -pl_aux.coefs[0] * pl_aux.coefs[2] / mod_n,
134 -pl_aux.coefs[1] * pl_aux.coefs[0] / mod_n, -mrpt::square(pl_aux.coefs[1]) / mod_n,
135 -pl_aux.coefs[1] * pl_aux.coefs[2] / mod_n, -pl_aux.coefs[2] * pl_aux.coefs[0] / mod_n,
136 -pl_aux.coefs[2] * pl_aux.coefs[1] / mod_n, -mrpt::square(pl_aux.coefs[2]) / mod_n)
140 const Eigen::Matrix<double, 3, 12> J2 =
141 (Eigen::Matrix<double, 3, 12>() <<
142 l.x, 0, 0, l.y, 0, 0, l.z, 0, 0, 1, 0, 0,
143 0, l.x, 0, 0, l.y, 0, 0, l.z, 0, 0, 1, 0,
144 0, 0, l.x, 0, 0, l.y, 0, 0, l.z, 0, 0, 1
148 mrpt::math::CMatrixFixed<double, 3, 12>& J_aux = jacobian.value().get();
157 mrpt::optional_ref<mrpt::math::CMatrixFixed<double, 4, 12>> jacobian)
160 mrpt::math::CVectorFixedDouble<4> error;
161 mrpt::math::TLine3D ln_aux;
162 mrpt::math::TPoint3D g;
164 const auto& p0 = pairing.
ln_local.pBase;
165 const auto& u0 = pairing.
ln_local.director;
166 const auto& p1 = pairing.
ln_global.pBase;
167 const auto& u1 = pairing.
ln_global.director;
169 relativePose.composePoint(p0, g);
170 ln_aux.pBase = mrpt::math::TPoint3D(g);
173 mrpt::math::CMatrixDouble44 aux;
174 relativePose.getHomogeneousMatrix(aux);
175 const Eigen::Matrix<double, 4, 4> T = aux.asEigen();
178 const Eigen::Matrix<double, 1, 4> U =
179 (Eigen::Matrix<double, 1, 4>() << u0[0], u0[1], u0[2], 1).finished();
180 const Eigen::Matrix<double, 1, 4> U_T = U * T;
181 ln_aux.director = {U_T[0], U_T[1], U_T[2]};
184 double alfa = getAngle(pairing.
ln_global, ln_aux) * 180 / (2 * 3.14159265);
195 const Eigen::Matrix<double, 1, 3> p_r2 =
196 (Eigen::Matrix<double, 1, 3>() << ln_aux.pBase.x - p1.x, ln_aux.pBase.y - p1.y,
197 ln_aux.pBase.z - p1.z)
200 const Eigen::Matrix<double, 1, 3> rv =
201 (Eigen::Matrix<double, 1, 3>() << u1[0], u1[1], u1[2]).finished();
204 const double tolerance = 0.01;
205 if (abs(alfa) < tolerance)
208 error[0] = mrpt::square(pairing.
ln_global.distance(ln_aux.pBase));
212 double mod_rv = rv * rv.transpose();
215 Eigen::Matrix<double, 1, 3> J1 = 2 * p_r2 - (2 / mod_rv) * (p_r2 * rv.transpose()) * rv;
218 const Eigen::Matrix<double, 3, 12> J2 =
219 (Eigen::Matrix<double, 3, 12>() <<
220 p0.x, 0, 0, p0.y, 0, 0, p0.z, 0, 0, 1, 0, 0,
221 0, p0.x, 0, 0, p0.y, 0, 0, p0.z, 0, 0, 1, 0,
222 0, 0, p0.x, 0, 0, p0.y, 0, 0, p0.z, 0, 0, 1
226 mrpt::math::CMatrixFixed<double, 4, 12>& J_auxp = jacobian.value().get();
227 J_auxp.block<1, 12>(0, 0) = J1 * J2;
234 const double rw_x = U_T[1] * u1[2] - U_T[2] * u1[1];
235 const double rw_y = -(U_T[0] * u1[2] - U_T[2] * u1[0]);
236 const double rw_z = U_T[0] * u1[1] - U_T[1] * u1[0];
238 const Eigen::Matrix<double, 1, 3> r_w =
239 (Eigen::Matrix<double, 1, 3>() << rw_x, rw_y, rw_z).finished();
240 double aux_rw = r_w * r_w.transpose();
242 error[0] = p_r2.dot(r_w) / sqrt(aux_rw);
244 error[1] = U_T[0] - u1[0];
245 error[2] = U_T[1] - u1[1];
246 error[3] = U_T[2] - u1[2];
250 Eigen::Matrix<double, 1, 3> J1_1 = r_w / sqrt(aux_rw);
254 const double A = p_r2[0] * r_w[0] + p_r2[1] * r_w[1] + p_r2[2] * r_w[2];
255 const double Ax = -u1[2] * p_r2[1] + u1[1] * p_r2[2];
256 const double Ay = u1[2] * p_r2[0] - u1[0] * p_r2[2];
257 const double Az = -u1[1] * p_r2[0] + u1[0] * p_r2[1];
259 const double B = sqrt(aux_rw);
260 const double Bx = (-u1[2] * r_w[1] + u1[1] * r_w[2]) / B;
261 const double By = (u1[2] * r_w[0] + u1[0] * r_w[2]) / B;
262 const double Bz = (-u1[1] * r_w[0] + u1[0] * r_w[1]) / B;
265 std::cout <<
"\nA: " << A <<
"\nAx: " << Ax <<
"\nAy: " << Ay
266 <<
"\nAz: " << Az <<
"\nB: " << B <<
"\nBx: " << Bx
267 <<
"\nBy: " << By <<
"\nBz: " << Bz <<
"\n";
272 Eigen::Matrix<double, 1, 3> J1_2 =
273 (Eigen::Matrix<double, 1, 3>() <<
274 (Ax * B - A * Bx) / mrpt::square(B),
275 (Ay * B - A * By) / mrpt::square(B),
276 (Az * B - A * Bz) / mrpt::square(B)
282 Eigen::Matrix<double, 3, 6> J1_3 =
283 (Eigen::Matrix<double, 3, 6>() <<
291 Eigen::Matrix<double, 4, 6> J1;
292 J1.block<1, 3>(0, 0) = J1_1;
293 J1.block<1, 3>(0, 3) = J1_2;
294 J1.block<3, 6>(1, 0) = J1_3;
298 const Eigen::Matrix<double, 6, 12> J2 =
299 (Eigen::Matrix<double, 6, 12>() <<
300 p0.x, 0, 0, p0.y, 0, 0, p0.z, 0, 0, 1, 0, 0,
301 0, p0.x, 0, 0, p0.y, 0, 0, p0.z, 0, 0, 1, 0,
302 0, 0, p0.x, 0, 0, p0.y, 0, 0, p0.z, 0, 0, 1,
303 -u0[0], 0, 0, -u0[1], 0, 0, -u0[2], 0, 0, 0, 0, 0,
304 0, -u0[0], 0, 0, -u0[1], 0, 0, -u0[2], 0, 0, 0, 0,
305 0, 0, -u0[0], 0, 0, -u0[1], 0, 0, -u0[2], 0, 0, 0
309 mrpt::math::CMatrixFixed<double, 4, 12>& J_aux = jacobian.value().get();
310 J_aux.block<4, 12>(0, 0) = J1 * J2;
312 std::cout <<
"\nJ1:\n" << J1 <<
"\nJ2:\n" << J2 <<
"\n";
322 mrpt::optional_ref<mrpt::math::CMatrixFixed<double, 3, 12>> jacobian)
325 mrpt::math::CVectorFixedDouble<3> error;
327 const auto nl = pairing.
p_local.
plane.getNormalVector();
330 const auto p_oplus_nl = relativePose.rotateVector(nl);
332 for (
int i = 0; i < 3; i++) error[i] = p_oplus_nl[i] - ng[i];
345 mrpt::math::CMatrixFixed<double, 3, 12>& J_aux = jacobian.value().get();
346 J_aux = (Eigen::Matrix<double, 3, 12>() <<
347 nl.x, 0, 0, nl.y, 0, 0, nl.z, 0, 0, 0, 0, 0,
348 0, nl.x, 0, 0, nl.y, 0, 0, nl.z, 0, 0, 0, 0,
349 0, 0, nl.x, 0, 0, nl.y, 0, 0, nl.z, 0, 0, 0