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,
30 const mrpt::poses::CPose3D& relativePose,
31 mrpt::optional_ref<mrpt::math::CMatrixFixed<double, 3, 12>> jacobian)
34 mrpt::math::CVectorFixedDouble<3> error;
35 const mrpt::math::TPoint3D& l = pairing.local;
37 const mrpt::math::TPoint3D g = relativePose.composePoint(l);
39 error[0] = g.x - pairing.global.x;
40 error[1] = g.y - pairing.global.y;
41 error[2] = g.z - pairing.global.z;
47 mrpt::math::CMatrixFixed<double, 3, 12>& J_aux = jacobian.value().get();
49 J_aux = (Eigen::Matrix<double, 3, 12>() <<
50 l.x, 0, 0, l.y, 0, 0, l.z, 0, 0, 1, 0, 0,
51 0, l.x, 0, 0, l.y, 0, 0, l.z, 0, 0, 1, 0,
52 0, 0, l.x, 0, 0, l.y, 0, 0, l.z, 0, 0, 1
63 const mrpt::poses::CPose3D& relativePose,
64 mrpt::optional_ref<mrpt::math::CMatrixFixed<double, 3, 12>> jacobian)
67 mrpt::math::CVectorFixedDouble<3> error;
71 const mrpt::math::TPoint3D l = pairing.
pt_local;
72 const mrpt::math::TPoint3D g = relativePose.composePoint(l);
75 const auto& u = lnGlob.director;
76 const auto q = (g - lnGlob.pBase);
78 const auto uq = mrpt::math::dotProduct<3, double>(u,
q);
80 error[0] =
q[0] - u.x * uq;
81 error[1] =
q[1] - u.y * uq;
82 error[2] =
q[2] - u.z * uq;
87 const Eigen::Matrix<double, 3, 3> J1 =
88 (Eigen::Matrix<double, 3, 3>() << 1 - mrpt::square(u.x), -u.x * u.y,
89 -u.x * u.z, -u.y * u.x, 1 - mrpt::square(u.y), -u.y * u.z,
90 -u.z * u.x, -u.z * u.y, 1 - mrpt::square(u.z))
94 Eigen::Matrix<double, 3, 12> J2 =
95 (Eigen::Matrix<double, 3, 12>() <<
96 l.x, 0, 0, l.y, 0, 0, l.z, 0, 0, 1, 0, 0,
97 0, l.x, 0, 0, l.y, 0, 0, l.z, 0, 0, 1, 0,
98 0, 0, l.x, 0, 0, l.y, 0, 0, l.z, 0, 0, 1
101 mrpt::math::CMatrixFixed<double, 3, 12>& J_aux = jacobian.value().get();
111 const mrpt::poses::CPose3D& relativePose,
112 mrpt::optional_ref<mrpt::math::CMatrixFixed<double, 3, 12>> jacobian)
115 mrpt::math::CVectorFixedDouble<3> error;
118 const mrpt::math::TPoint3D l = TPoint3D(p.x, p.y, p.z);
119 mrpt::math::TPoint3D g;
120 relativePose.composePoint(l, g);
121 double mod_n = mrpt::square(pl_aux.coefs[0]) +
122 mrpt::square(pl_aux.coefs[1]) +
123 mrpt::square(pl_aux.coefs[2]);
125 error[0] = -(pl_aux.coefs[0] / mod_n) *
126 (pl_aux.coefs[0] * g.x + pl_aux.coefs[1] * g.y +
127 pl_aux.coefs[2] * g.z + pl_aux.coefs[3]);
128 error[1] = -(pl_aux.coefs[1] / mod_n) *
129 (pl_aux.coefs[0] * g.x + pl_aux.coefs[1] * g.y +
130 pl_aux.coefs[2] * g.z + pl_aux.coefs[3]);
131 error[2] = -(pl_aux.coefs[2] / mod_n) *
132 (pl_aux.coefs[0] * g.x + pl_aux.coefs[1] * g.y +
133 pl_aux.coefs[2] * g.z + pl_aux.coefs[3]);
138 const Eigen::Matrix<double, 3, 3> J1 =
139 (Eigen::Matrix<double, 3, 3>()
140 << -mrpt::square(pl_aux.coefs[0]) / mod_n,
141 -pl_aux.coefs[0] * pl_aux.coefs[1] / mod_n,
142 -pl_aux.coefs[0] * pl_aux.coefs[2] / mod_n,
143 -pl_aux.coefs[1] * pl_aux.coefs[0] / mod_n,
144 -mrpt::square(pl_aux.coefs[1]) / mod_n,
145 -pl_aux.coefs[1] * pl_aux.coefs[2] / mod_n,
146 -pl_aux.coefs[2] * pl_aux.coefs[0] / mod_n,
147 -pl_aux.coefs[2] * pl_aux.coefs[1] / mod_n,
148 -mrpt::square(pl_aux.coefs[2]) / mod_n)
152 const Eigen::Matrix<double, 3, 12> J2 =
153 (Eigen::Matrix<double, 3, 12>() <<
154 l.x, 0, 0, l.y, 0, 0, l.z, 0, 0, 1, 0, 0,
155 0, l.x, 0, 0, l.y, 0, 0, l.z, 0, 0, 1, 0,
156 0, 0, l.x, 0, 0, l.y, 0, 0, l.z, 0, 0, 1
160 mrpt::math::CMatrixFixed<double, 3, 12>& J_aux = jacobian.value().get();
169 const mrpt::poses::CPose3D& relativePose,
170 mrpt::optional_ref<mrpt::math::CMatrixFixed<double, 4, 12>> jacobian)
173 mrpt::math::CVectorFixedDouble<4> error;
174 mrpt::math::TLine3D ln_aux;
175 mrpt::math::TPoint3D g;
177 const auto& p0 = pairing.
ln_local.pBase;
178 const auto& u0 = pairing.
ln_local.director;
179 const auto& p1 = pairing.
ln_global.pBase;
180 const auto& u1 = pairing.
ln_global.director;
182 relativePose.composePoint(p0, g);
183 ln_aux.pBase = mrpt::math::TPoint3D(g);
186 mrpt::math::CMatrixDouble44 aux;
187 relativePose.getHomogeneousMatrix(aux);
188 const Eigen::Matrix<double, 4, 4> T = aux.asEigen();
191 const Eigen::Matrix<double, 1, 4> U =
192 (Eigen::Matrix<double, 1, 4>() << u0[0], u0[1], u0[2], 1).finished();
193 const Eigen::Matrix<double, 1, 4> U_T = U * T;
194 ln_aux.director = {U_T[0], U_T[1], U_T[2]};
197 double alfa = getAngle(pairing.
ln_global, ln_aux) * 180 / (2 * 3.14159265);
208 const Eigen::Matrix<double, 1, 3> p_r2 =
209 (Eigen::Matrix<double, 1, 3>() << ln_aux.pBase.x - p1.x,
210 ln_aux.pBase.y - p1.y, ln_aux.pBase.z - p1.z)
213 const Eigen::Matrix<double, 1, 3> rv =
214 (Eigen::Matrix<double, 1, 3>() << u1[0], u1[1], u1[2]).finished();
217 const double tolerance = 0.01;
218 if (abs(alfa) < tolerance)
221 error[0] = mrpt::square(pairing.
ln_global.distance(ln_aux.pBase));
225 double mod_rv = rv * rv.transpose();
228 Eigen::Matrix<double, 1, 3> J1 =
229 2 * p_r2 - (2 / mod_rv) * (p_r2 * rv.transpose()) * rv;
232 const Eigen::Matrix<double, 3, 12> J2 =
233 (Eigen::Matrix<double, 3, 12>() <<
234 p0.x, 0, 0, p0.y, 0, 0, p0.z, 0, 0, 1, 0, 0,
235 0, p0.x, 0, 0, p0.y, 0, 0, p0.z, 0, 0, 1, 0,
236 0, 0, p0.x, 0, 0, p0.y, 0, 0, p0.z, 0, 0, 1
240 mrpt::math::CMatrixFixed<double, 4, 12>& J_auxp =
241 jacobian.value().get();
242 J_auxp.block<1, 12>(0, 0) = J1 * J2;
249 const double rw_x = U_T[1] * u1[2] - U_T[2] * u1[1];
250 const double rw_y = -(U_T[0] * u1[2] - U_T[2] * u1[0]);
251 const double rw_z = U_T[0] * u1[1] - U_T[1] * u1[0];
253 const Eigen::Matrix<double, 1, 3> r_w =
254 (Eigen::Matrix<double, 1, 3>() << rw_x, rw_y, rw_z).finished();
255 double aux_rw = r_w * r_w.transpose();
257 error[0] = p_r2.dot(r_w) / sqrt(aux_rw);
259 error[1] = U_T[0] - u1[0];
260 error[2] = U_T[1] - u1[1];
261 error[3] = U_T[2] - u1[2];
265 Eigen::Matrix<double, 1, 3> J1_1 = r_w / sqrt(aux_rw);
270 p_r2[0] * r_w[0] + p_r2[1] * r_w[1] + p_r2[2] * r_w[2];
271 const double Ax = -u1[2] * p_r2[1] + u1[1] * p_r2[2];
272 const double Ay = u1[2] * p_r2[0] - u1[0] * p_r2[2];
273 const double Az = -u1[1] * p_r2[0] + u1[0] * p_r2[1];
275 const double B = sqrt(aux_rw);
276 const double Bx = (-u1[2] * r_w[1] + u1[1] * r_w[2]) / B;
277 const double By = (u1[2] * r_w[0] + u1[0] * r_w[2]) / B;
278 const double Bz = (-u1[1] * r_w[0] + u1[0] * r_w[1]) / B;
281 std::cout <<
"\nA: " << A <<
"\nAx: " << Ax <<
"\nAy: " << Ay
282 <<
"\nAz: " << Az <<
"\nB: " << B <<
"\nBx: " << Bx
283 <<
"\nBy: " << By <<
"\nBz: " << Bz <<
"\n";
288 Eigen::Matrix<double, 1, 3> J1_2 =
289 (Eigen::Matrix<double, 1, 3>() <<
290 (Ax * B - A * Bx) / mrpt::square(B),
291 (Ay * B - A * By) / mrpt::square(B),
292 (Az * B - A * Bz) / mrpt::square(B)
298 Eigen::Matrix<double, 3, 6> J1_3 =
299 (Eigen::Matrix<double, 3, 6>() <<
307 Eigen::Matrix<double, 4, 6> J1;
308 J1.block<1, 3>(0, 0) = J1_1;
309 J1.block<1, 3>(0, 3) = J1_2;
310 J1.block<3, 6>(1, 0) = J1_3;
314 const Eigen::Matrix<double, 6, 12> J2 =
315 (Eigen::Matrix<double, 6, 12>() <<
316 p0.x, 0, 0, p0.y, 0, 0, p0.z, 0, 0, 1, 0, 0,
317 0, p0.x, 0, 0, p0.y, 0, 0, p0.z, 0, 0, 1, 0,
318 0, 0, p0.x, 0, 0, p0.y, 0, 0, p0.z, 0, 0, 1,
319 -u0[0], 0, 0, -u0[1], 0, 0, -u0[2], 0, 0, 0, 0, 0,
320 0, -u0[0], 0, 0, -u0[1], 0, 0, -u0[2], 0, 0, 0, 0,
321 0, 0, -u0[0], 0, 0, -u0[1], 0, 0, -u0[2], 0, 0, 0
325 mrpt::math::CMatrixFixed<double, 4, 12>& J_aux =
326 jacobian.value().get();
327 J_aux.block<4, 12>(0, 0) = J1 * J2;
329 std::cout <<
"\nJ1:\n" << J1 <<
"\nJ2:\n" << J2 <<
"\n";
339 const mrpt::poses::CPose3D& relativePose,
340 mrpt::optional_ref<mrpt::math::CMatrixFixed<double, 3, 12>> jacobian)
343 mrpt::math::CVectorFixedDouble<3> error;
345 const auto nl = pairing.
p_local.
plane.getNormalVector();
348 const auto p_oplus_nl = relativePose.rotateVector(nl);
350 for (
int i = 0; i < 3; i++) error[i] = p_oplus_nl[i] - ng[i];
363 mrpt::math::CMatrixFixed<double, 3, 12>& J_aux = jacobian.value().get();
364 J_aux = (Eigen::Matrix<double, 3, 12>() <<
365 nl.x, 0, 0, nl.y, 0, 0, nl.z, 0, 0, 0, 0, 0,
366 0, nl.x, 0, 0, nl.y, 0, 0, nl.z, 0, 0, 0, 0,
367 0, 0, nl.x, 0, 0, nl.y, 0, 0, nl.z, 0, 0, 0