Jafar
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
ahpTools.hpp
Go to the documentation of this file.
00001 
00013 #include "jmath/misc.hpp"
00014 #include "rtslam/quatTools.hpp"
00015 
00016 #ifndef LANDMARKAHP_HPP_
00017 #define LANDMARKAHP_HPP_
00018 
00019 namespace jafar {
00020   namespace rtslam {
00025     namespace lmkAHP {
00026       using namespace ublas;
00027       using namespace jblas;
00028       using namespace quaternion;
00029 
00030 
00037       template<class AHP, class P0, class M>
00038       void split(const AHP & ahp, P0 & p0, M & m, double & rho) {
00039         p0 = project(ahp, range(0, 3));
00040         m = project(ahp, range(3, 6));
00041         rho = ahp(6);
00042       }
00043 
00044 
00053       template<class P0, class M>
00054       vec7 compose(const P0 & p0, const M & m, const double rho) {
00055         vec7 ahp;
00056         project(ahp, range(0, 3)) = p0;
00057         project(ahp, range(3, 6)) = m;
00058         ahp(6) = rho;
00059         return ahp;
00060       }
00061 
00062       template<class VF, class Vlf>
00063       vec fromFrame(const VF & F, const Vlf & ahpf) {
00064 
00065 
00066         // split non-trivial chunks of landmark state
00067         vec3 p0f(subrange(ahpf, 0, 3));
00068         vec3 mf(subrange(ahpf, 3, 6));
00069 
00070 
00071         // transformed landmark in global frame
00072         vec ahp(7);
00073 
00074         subrange(ahp, 0, 3) = eucFromFrame(F, p0f);
00075         subrange(ahp, 3, 6) = vecFromFrame(F, mf);
00076         ahp(6) = ahpf(6);
00077 
00078         return ahp;
00079       }
00080 
00081       template<class VF, class Vahpf, class Vahp, class MAHP_f, class MAHP_ahpf>
00082       void fromFrame(const VF & F, const Vahpf & ahpf, Vahp & ahp, MAHP_f & AHP_f, MAHP_ahpf & AHP_ahpf) {
00083         // split non-trivial chunks of landmark state
00084         vec3 p0f(subrange(ahpf, 0, 3));
00085         vec3 mf(subrange(ahpf, 3, 6));
00086         // destination chunks
00087         vec3 p0;
00088         vec3 m;
00089         mat JAC_33(3, 3), JAC_37(3, 7);
00090         // Jacobians
00091         AHP_f.clear();
00092         AHP_ahpf.clear();
00093         // transform p0
00094         eucFromFrame(F, p0f, p0, JAC_37, JAC_33);
00095         subrange(ahp, 0, 3) = p0;
00096         subrange(AHP_f, 0, 3, 0, 7) = JAC_37;
00097         subrange(AHP_ahpf, 0, 3, 0, 3) = JAC_33;
00098         // transform m
00099         vecFromFrame(F, mf, m, JAC_37, JAC_33);
00100         subrange(ahp, 3, 6) = m;
00101         subrange(AHP_f, 3, 6, 0, 7) = JAC_37;
00102         subrange(AHP_ahpf, 3, 6, 3, 6) = JAC_33;
00103         // transform rho
00104         ahp(6) = ahpf(6);
00105         AHP_ahpf(6, 6) = 1;
00106       }
00107 
00108       template<class VF, class Vahp>
00109       vec toFrame(const VF & F, const Vahp & ahp) {
00110         // split non-trivial chunks of landmark state
00111         vec3 p0(subrange(ahp, 0, 3));
00112         vec3 m(subrange(ahp, 3, 6));
00113         // transformed landmark in frame F
00114         vec ahpf(7);
00115         subrange(ahpf, 0, 3) = eucToFrame(F, p0);
00116         subrange(ahpf, 3, 6) = vecToFrame(F, m);
00117         ahpf(6) = ahp(6);
00118 
00119         return ahpf;
00120       }
00121 
00122       template<class VF, class Vahp, class Vahpf, class MAHPF_f, class MAHPF_ahp>
00123       void toFrame(const VF & F, const Vahp & ahp, Vahpf & ahpf, MAHPF_f & AHPF_f, MAHPF_ahp & AHPF_ahp) {
00124         // split non-trivial chunks of landmark state
00125         vec3 p0(subrange(ahp, 0, 3));
00126         vec3 m(subrange(ahp, 3, 6));
00127         // destination chunks
00128         vec3 p0f;
00129         vec3 mf;
00130         mat JAC_33(3, 3), JAC_37(3, 7);
00131         // Jacobians
00132         AHPF_f.clear();
00133         AHPF_ahp.clear();
00134         // transform p0
00135         eucToFrame(F, p0, p0f, JAC_37, JAC_33);
00136         subrange(ahpf, 0, 3) = p0f;
00137         subrange(AHPF_f, 0, 3, 0, 7) = JAC_37;
00138         subrange(AHPF_ahp, 0, 3, 0, 3) = JAC_33;
00139         // transform m
00140         vecToFrame(F, m, mf, JAC_37, JAC_33);
00141         subrange(ahpf, 3, 6) = mf;
00142         subrange(AHPF_f, 3, 6, 0, 7) = JAC_37;
00143         subrange(AHPF_ahp, 3, 6, 3, 6) = JAC_33;
00144         // transform rho
00145         ahpf(6) = ahp(6);
00146         AHPF_ahp(6, 6) = 1;
00147       }
00148 
00149 
00155       template<class VA>
00156       vec3 ahp2euc(const VA & ahp) {
00157         return subrange(ahp, 0, 3) + subrange(ahp, 3, 6) / ahp(6);
00158       }
00159 
00160 
00167       template<class VA, class VE, class ME_a>
00168       void ahp2euc(const VA & ahp, VE & euc, ME_a & EUC_ahp) {
00169         euc.assign(ahp2euc(ahp));
00170         // split non-trivial chunks of landmark state
00171         vec3 m(subrange(ahp, 3, 6));
00172         double rho = ahp(6);
00173         identity_mat I(3);
00174         subrange(EUC_ahp, 0, 3, 0, 3) = I;
00175         subrange(EUC_ahp, 0, 3, 3, 6) = I / rho;
00176         column(EUC_ahp, 6) = -m / rho / rho;
00177       }
00178 
00179 
00196       template<class VS, class VA>
00197       vec3 toBearingOnlyFrame(const VS & s, const VA & ahp) {
00198         vec3 p0, m;
00199         double rho;
00200         split(ahp, p0, m, rho);
00201         vec3 t = subrange(s, 0, 3);
00202         vec4 q = subrange(s, 3, 7);
00203         vec3 d = m - (t - p0) * rho;
00204         return rotateInv(q, d); // OK JS April 1 2010
00205       }
00206 
00207 
00227       template<class VS, class VA, class VV>
00228       void toBearingOnlyFrame(const VS & s, const VA & ahp, VV & v, double & dist) {
00229         vec3 p0, m;
00230         double rho;
00231         split(ahp, p0, m, rho);
00232         vec3 t = subrange(s, 0, 3);
00233         vec4 q = subrange(s, 3, 7);
00234         vec3 d = m - (t - p0) * rho;
00235         v = rotateInv(q, d); // OK JS April 1 2010
00236         dist = norm_2(v) / rho;
00237       }
00238 
00239 
00261       template<class VS, class VA, class VV, class MV_s, class MV_a>
00262       void toBearingOnlyFrame(const VS & s, const VA & ahp, VV & v, double & dist, MV_s & V_s, MV_a & V_ahp) {
00263         vec3 p0, m;
00264         double rho;
00265         mat34 V_q;
00266         mat33 V_d;
00267         // value
00268         split(ahp, p0, m, rho);
00269         vec3 t = subrange(s, 0, 3);
00270         vec4 q = subrange(s, 3, 7);
00271         vec3 d = m - (t - p0) * rho; //    before rotation
00272         rotateInv(q, d, v, V_q, V_d); //   obtain v
00273         dist = norm_2(v) / rho; //         obtain non-observable distance
00274         // Jacobians
00275         mat33 V_p0;
00276         V_p0 = V_d * rho;
00277         //        mat V_rho(3,1); // This comments only for Jacobian reference...
00278         //        V_m = V_d;
00279         //        V_rho = prod(V_d, (p0 - t));
00280         //        V_t = - V_d * rho;
00281         V_s.clear();
00282         V_ahp.clear();
00283         subrange(V_s, 0, 3, 0, 3) = -V_p0; //       dv / dt
00284         subrange(V_s, 0, 3, 3, 7) = V_q; //         dv / dq
00285         subrange(V_ahp, 0, 3, 0, 3) = V_p0; //      dv / dp0
00286         subrange(V_ahp, 0, 3, 3, 6) = V_d; //       dv / dm
00287         column(V_ahp, 6) = prod(V_d, (p0 - t)); //  dv / drho   // OK JS April 1 2010
00288       }
00289 
00290 
00304       template<class VS, class VLS>
00305       vec7 fromBearingOnlyFrame(const VS & s, const VLS & v, const double _rho) {
00306         vec3 t = subrange(s, 0, 3);
00307         vec4 q = subrange(s, 3, 7);
00308         vec7 ahp;
00309 
00310         subrange(ahp, 0, 3) = t;
00311         subrange(ahp, 3, 6) = rotate(q, v);
00312         ahp(6) = _rho * norm_2(v);
00313         return ahp; // OK JS April 1 2010
00314       }
00315 
00316 
00333       template<class VS, class VLS, class VA, class MA_s, class MA_v, class MA_rho>
00334       void fromBearingOnlyFrame(const VS & s, const VLS & v, const double _rho, VA & ahp, MA_s & AHP_s, MA_v & AHP_v,
00335           MA_rho & AHP_rho) {
00336         vec3 t = subrange(s, 0, 3);
00337         vec4 q = subrange(s, 3, 7);
00338         vec3 m;
00339         mat34 M_q;
00340         mat33 M_v;
00341         mat NV_v(1, 3);
00342 
00343         rotate(q, v, m, M_q, M_v);
00344         double nv = norm_2(v);
00345 
00346         subrange(ahp, 0, 3) = t; // p0  = t
00347         subrange(ahp, 3, 6) = m; // m   = R(q) * v
00348         ahp(6) = _rho * nv; //              rho = ||v|| * _rho
00349         ublasExtra::norm_2Jac<3>(v, NV_v); // drho / dv = d||v||/dv * _rho
00350 
00351         // Jacobians
00352         identity_mat I(3);
00353         AHP_s.clear();
00354         AHP_v.clear();
00355         AHP_rho.clear();
00356         subrange(AHP_s, 0, 3, 0, 3) = I; //             dp0 / dt
00357         subrange(AHP_s, 3, 6, 3, 7) = M_q; //            dm / dq
00358         subrange(AHP_v, 3, 6, 0, 3) = M_v; //            dm / dv
00359         subrange(AHP_v, 6, 7, 0, 3) = _rho * NV_v; //  drho / dv
00360         AHP_rho(6, 0) = nv; //                         drho / drho
00361       } // OK JS April 1 2010
00362 
00363       template<class VS, class VA, class MA_s>
00364       double linearityScore(const VS & senpose, const VA & ahp, const MA_s & AHP){
00365 
00366           vec3 euc = ahp2euc(ahp);
00367 
00368           vec3 hw = euc - subrange(senpose, 0, 3);
00369 
00370           double sigma_rho = sqrt(AHP(6,6));
00371 
00372           double sigma_d   = sigma_rho/(ahp(6)*ahp(6));
00373           double norm_hw   = norm_2(hw);
00374           double norm_m    = norm_2(subrange(ahp, 3, 6));
00375           double cos_a     = inner_prod(subrange(ahp, 3, 6) , hw) / (norm_hw*norm_m);
00376 
00377           double L = 4.0*sigma_d/norm_hw*jmath::abs(cos_a);
00378 
00379 //          std::cout << "rho="<<ahp(6)<<", sr="<<sigma_rho<<", sd="<<sigma_d<<", nh="<<norm_hw<<", nm="<<norm_m<<", cos="<<jmath::abs(cos_a)<<std::endl;
00380 //          std::cout << "linearity score: " << L << std::endl;
00381 
00382           return L;
00383       }
00384 
00385     } // namespace landmarkAHP
00386 
00387 
00388   }
00389 }
00390 
00391 #endif /* LANDMARKAHP_HPP_ */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on Wed Oct 15 2014 00:37:26 for Jafar by doxygen 1.7.6.1
LAAS-CNRS