rotation_from_correspondences.hpp
Go to the documentation of this file.
00001 
00059 /*
00060  * rotation_from_correspondences.hpp
00061  *
00062  *  Created on: 28.04.2012
00063  *      Author: josh
00064  */
00065 
00066 #include <Eigen/SVD>
00067 #include <Eigen/LU>
00068 #include <Eigen/Dense>
00069 
00070 namespace pcl {
00071 
00072   RotationFromCorrespondences::RotationFromCorrespondences ()
00073   {
00074     reset();
00075   }
00076 
00077   RotationFromCorrespondences::~RotationFromCorrespondences ()
00078   {
00079   }
00080 
00081   inline void
00082   RotationFromCorrespondences::reset ()
00083   {
00084     no_of_samples_ = 0;
00085     accumulated_weight_ = 0.0;
00086     accumulated_weight2_=(0);
00087     covariance_.fill(0);
00088     var_.fill(0);
00089 
00090     va_.fill(0);
00091     vb_.fill(0);
00092     vA_.fill(0);
00093     vB_.fill(0);
00094   }
00095 
00096   Eigen::Vector3f elmwiseMul(const Eigen::Vector3f& n1, const Eigen::Vector3f& n2)
00097   {
00098     Eigen::Vector3f r;
00099     r(0)=n1(0)*std::abs(n2(0));
00100     r(1)=n1(1)*std::abs(n2(1));
00101     r(2)=n1(2)*std::abs(n2(2));
00102     return r;
00103   }
00104 
00105   template<typename M>
00106   M elmwiseDiv(const M& n1, const M& n2)
00107   {
00108     M r;
00109     for(int x=0; x<n1.rows(); x++)
00110       for(int y=0; y<n1.cols(); y++)
00111         if(std::abs(n2(x,y))>0.0000001f) r(x,y)=n1(x,y)/n2(x,y);
00112     return r;
00113   }
00114 
00115   Eigen::Vector3f elmwiseAbs(const Eigen::Vector3f& n1)
00116   {
00117     Eigen::Vector3f r;
00118     r(0)=std::abs(n1(0));
00119     r(1)=std::abs(n1(1));
00120     r(2)=std::abs(n1(2));
00121     return r;
00122   }
00123 
00124   inline void
00125   RotationFromCorrespondences::add (const Eigen::Vector3f& point, const Eigen::Vector3f& corresponding_point,
00126                                     const Eigen::Vector3f& n1, const Eigen::Vector3f& n2,
00127                                     const Eigen::Vector3f& cn1, const Eigen::Vector3f& cn2,
00128                                     float weight, float weight2)
00129   {
00130     if (weight==0.0f)
00131       return;
00132 
00133     ++no_of_samples_;
00134     accumulated_weight_ += weight;
00135     //float alpha = weight/accumulated_weight_;
00136 
00137     covariance_ +=  weight*(corresponding_point * point.transpose());
00138     var_ += weight*(corresponding_point * corresponding_point.transpose());
00139     static int i=0;
00140 
00141 
00142     accumulated_weight2_ += weight2;
00143     //va_ += weight*(elmwiseMul(corresponding_point,n1)+elmwiseMul(corresponding_point,n2));
00144     //vb_ += weight*(elmwiseMul(point,n1)+elmwiseMul(point,n2));
00145 
00146     va_ += weight*corresponding_point;
00147     vb_ += weight*point;
00148 /*
00149     //eigen composition
00150     Eigen::Matrix3f Q,V,M;
00151     Q.col(0)=corresponding_point;
00152     Q.col(1)=cn1;
00153     Q.col(2)=cn2;
00154     V.fill(0);
00155     V(0,0)=weight2;
00156     V(1,1)=cn1.norm(); //TODO: expect normalized vector + length?
00157     V(2,2)=cn2.norm();
00158     Q.col(0).normalize();
00159     Q.col(1).normalize();
00160     Q.col(2).normalize();
00161     M=V.inverse();//Q*V*Q.transpose();
00162     vA_ += weight*(M*(corresponding_point));
00163     std::cout<<"n0:\n"<<(corresponding_point)<<"\n";
00164     std::cout<<"n1:\n"<<(cn1)<<"\n";
00165     std::cout<<"n2:\n"<<(cn2)<<"\n";
00166     std::cout<<"delta:\n"<<(corresponding_point)<<"\nM:\n"<<M<<"\n";
00167     std::cout<<"res:\n"<<(M*(corresponding_point))<<"\n";
00168 
00169     Q.col(0)=point;
00170     Q.col(1)=n1;
00171     Q.col(2)=n2;
00172     V(1,1)=n1.norm();
00173     V(2,2)=n2.norm();
00174     Q.col(0).normalize();
00175     Q.col(1).normalize();
00176     Q.col(2).normalize();
00177     M=V.inverse();//Q*V*Q.transpose();
00178     vB_ += weight*(M*(point));
00179 */
00180     vA_ += weight2*corresponding_point;
00181     vB_ += weight2*point;
00182     ++i;
00183   }
00184 
00185   inline Eigen::Matrix3f
00186   RotationFromCorrespondences::getTransformation ()
00187   {
00188     //  if(no_of_samples_<2)
00189     //    return Eigen::Matrix3f::Identity();
00190 
00191     std::cout<<"accumulated_weight_\n"<<accumulated_weight_<<"\n";
00192     std::cout<<"C:\n"<<covariance_<<"\n";
00193     std::cout<<"vA:\n"<<(vA_*vB_.transpose())<<"\n";
00194     std::cout<<"vB:\n"<<(va_*vB_.transpose())<<"\n";
00195     std::cout<<"A:\n"<<(vA_*vB_.transpose())*accumulated_weight_/(accumulated_weight2_*accumulated_weight2_)-((va_*vB_.transpose())+(vA_*vb_.transpose()))/accumulated_weight2_<<"\n";
00196     std::cout<<"ab:\n"<<(va_*vb_.transpose())/(accumulated_weight_)<<"\n";
00197     //covariance_ -=  elmwiseDiv<Eigen::Vector3f>(va_,accumulated_weight2_)*elmwiseDiv<Eigen::Vector3f>(vb_,accumulated_weight2_).transpose();//elmwiseDiv<Eigen::Matrix3f>((va_*vb_.transpose()),accumulated_weight2_*accumulated_weight2_.transpose());
00198     //covariance_ +=  (vA_*vB_.transpose())*accumulated_weight_/(accumulated_weight2_*accumulated_weight2_)-((va_*vB_.transpose())+(vA_*vb_.transpose()))/accumulated_weight2_;
00199     covariance_ -=  (vA_*vB_.transpose())/(accumulated_weight2_);
00200     //    var_ -=  elmwiseDiv<Eigen::Matrix3f>((va_*va_.transpose()),accumulated_weight2_*accumulated_weight2_.transpose());
00201 
00202     Eigen::JacobiSVD<Eigen::Matrix<float, 3, 3> > svd (covariance_, Eigen::ComputeFullU | Eigen::ComputeFullV);
00203     const Eigen::Matrix<float, 3, 3>& u = svd.matrixU(),
00204         & v = svd.matrixV();
00205 
00206     std::cout<<"s:\n"<<svd.singularValues()<<"\n";
00207     std::cout<<"C:\n"<<covariance_<<"\n";
00208 
00209     Eigen::Matrix<float, 3, 3> s;
00210     s.setIdentity();
00211     if (u.determinant()*v.determinant() < 0.0f)
00212       s(2,2) = -1.0f;
00213 
00214     Eigen::Matrix<float, 3, 3> r = u * s * v.transpose();
00215 
00216     return r;
00217   }
00218 
00219 }  // END namespace


cob_3d_mapping_slam
Author(s): Joshua Hampp
autogenerated on Wed Aug 26 2015 11:04:51