test_cylinder.cpp
Go to the documentation of this file.
00001 
00060 #include <gtest/gtest.h>
00061 #include <cob_3d_mapping_common/cylinder.h>
00062 #include <cob_3d_mapping_common/ros_msg_conversions.h>
00063 #include <pcl/common/eigen.h>
00064 #include <pcl/io/pcd_io.h>
00065 
00066 using namespace cob_3d_mapping;
00067 
00068 struct CylinderData
00069 {
00070   CylinderData(unsigned int id,
00071               std::vector<float> color,
00072               Eigen::Vector3f sym_axis,
00073               Eigen::Vector3f x_axis,
00074               Eigen::Vector3f origin,
00075               double r,
00076               std::vector<std::vector<Eigen::Vector3f> > contours,
00077               std::vector<bool> holes)
00078   {
00079     id_ = id;
00080     color_ = color;
00081     sym_axis_ = sym_axis.normalized();
00082     origin_ = origin;
00083     r_ = r;
00084     if (sym_axis_[2] < 0 )
00085     {
00086       sym_axis_ = sym_axis_ * -1;
00087     }
00088     contours_3d_cyl_ = contours;
00089     holes_ = holes;
00090     Eigen::Affine3f t;
00091     pcl::getTransformationFromTwoUnitVectorsAndOrigin(sym_axis_, x_axis.cross(sym_axis_), origin_, t);
00092     std::cout << x_axis.cross(sym_axis_) << std::endl;
00093     pose_ = t.inverse();
00094     for(unsigned int i = 0; i < contours_3d_cyl_.size(); i++)
00095     {
00096       std::vector<Eigen::Vector3f> c_3d;
00097       for(unsigned int j = 0; j < contours_3d_cyl_[i].size(); j++)
00098       {
00099         Eigen::Vector3f p_3d = pose_ * contours_3d_cyl_[i][j];
00100         c_3d.push_back(p_3d);
00101       }
00102       contours_3d_.push_back(c_3d);
00103     }
00104   }
00105 
00106   unsigned int id_;
00107   std::vector<float> color_;
00108   Eigen::Vector3f sym_axis_;
00109   Eigen::Vector3f origin_;
00110   double r_;
00111   std::vector<std::vector<Eigen::Vector2f> > contours_2d_;
00112   std::vector<std::vector<Eigen::Vector3f> > contours_3d_cyl_;
00113   std::vector<std::vector<Eigen::Vector3f> > contours_3d_;
00114   std::vector<bool> holes_;
00115   Eigen::Affine3f pose_;
00116   std::vector<unsigned int> merge_candidates_;
00117 };
00118 
00119 
00120 class CylinderTest : public ::testing::Test
00121 {
00122 protected:
00123   virtual void
00124   SetUp ()
00125   {
00126     std::vector<std::vector<Eigen::Vector3f> > contours;
00127     Eigen::Vector3f p1(-1,-1,0), p2(0,-1,1), p3(1,-1,0), p4(1,1,0), p5(0,1,1), p6(-1,1,0);
00128     std::vector<Eigen::Vector3f> c;
00129     c.push_back(p1);
00130     c.push_back(p2);
00131     c.push_back(p3);
00132     c.push_back(p4);
00133     c.push_back(p5);
00134     c.push_back(p6);
00135     contours.push_back(c);
00136     std::vector<bool> holes;
00137     holes.push_back(false);
00138     CylinderData* cd1 = new CylinderData(0, std::vector<float>(4,1), Eigen::Vector3f(0,1,0), Eigen::Vector3f(1,0,0), Eigen::Vector3f(0,0,0), 1.0, contours, holes);
00139     cd_.push_back(cd1);
00140 
00141     contours.clear();
00142     c.clear();
00143     holes.clear();
00144     p1 = Eigen::Vector3f(-0.71,-1,0.71);
00145     //p2 = Eigen::Vector3f(1,-1,1);
00146     p3 = Eigen::Vector3f(0.71,-1,0.71);
00147     p4 = Eigen::Vector3f(0.71,1,0.71);
00148     //p5 = Eigen::Vector3f(1,1,1);
00149     p6 = Eigen::Vector3f(-0.71,1,0.71);
00150     c.push_back(p1);
00151     c.push_back(p2);
00152     c.push_back(p3);
00153     c.push_back(p4);
00154     c.push_back(p5);
00155     c.push_back(p6);
00156     contours.push_back(c);
00157     holes.push_back(false);
00158     CylinderData* cd2 = new CylinderData(0, std::vector<float>(4,1), Eigen::Vector3f(0,1,0), Eigen::Vector3f(1,0,0), Eigen::Vector3f(1,0,0), 1.0, contours, holes);
00159     cd_.push_back(cd2);
00160   }
00161 
00162   std::vector<CylinderData*> cd_;
00163 };
00164 
00165 /*TEST_F(CylinderTest, radiusFromCloud)
00166 {
00167   boost::shared_ptr<pcl::PointCloud<pcl::PointXYZRGB> > pc(new pcl::PointCloud<pcl::PointXYZRGB>);
00168   pcl::io::loadPCDFile("cylinder00000.pcd", *pc);
00169   std::vector<int> indices;
00170   for( unsigned int i = 0; i < pc->size(); i++)
00171     indices.push_back(i);
00172   Eigen::Vector3f origin(0, 0.5, 4);
00173   Eigen::Vector3f sym_axis(0, 1, 0);
00174   double r = radiusFromCloud(pc, indices, origin, sym_axis);
00175   EXPECT_NEAR(r, 0.25, 0.01);
00176 }*/
00177 
00178 TEST_F(CylinderTest, creationWorks)
00179 {
00180   for(unsigned int i = 0; i < cd_.size(); i++)
00181   {
00182     cob_3d_mapping::Cylinder c(cd_[i]->id_, cd_[i]->origin_, cd_[i]->sym_axis_, cd_[i]->r_, cd_[i]->contours_3d_, cd_[i]->holes_, cd_[i]->color_);
00183     std::cout << "Pose " << c.pose_.matrix() << std::endl;
00184     std::cout << "Pose Inverse " << c.pose_.inverse().matrix() << std::endl;
00185     std::vector<std::vector<Eigen::Vector3f> > c_3d = c.getContours3D();
00186     std::cout << "Actual 3D contours" << std::endl;
00187     for(unsigned int j = 0; j < c_3d.size(); j++)
00188     {
00189       for(unsigned int k = 0; k < c_3d[j].size(); k++)
00190       {
00191         std::cout << cd_[i]->contours_3d_[j][k](0) << "," << cd_[i]->contours_3d_[j][k](1) << "," << cd_[i]->contours_3d_[j][k](2) << std::endl;
00192       }
00193     }
00194     std::cout << "Actual 3D contours in cyl coords" << std::endl;
00195     for(unsigned int j = 0; j < c_3d.size(); j++)
00196     {
00197       for(unsigned int k = 0; k < c_3d[j].size(); k++)
00198       {
00199         std::cout << cd_[i]->contours_3d_cyl_[j][k](0) << "," << cd_[i]->contours_3d_cyl_[j][k](1) << "," << cd_[i]->contours_3d_cyl_[j][k](2) << std::endl;
00200       }
00201     }
00202     std::cout << "Computed 3D contours" << std::endl;
00203     for(unsigned int j = 0; j < c_3d.size(); j++)
00204     {
00205       for(unsigned int k = 0; k < c_3d[j].size(); k++)
00206       {
00207         std::cout << c_3d[j][k](0) << "," << c_3d[j][k](1) << "," << c_3d[j][k](2) << std::endl;
00208       }
00209     }
00210     std::cout << "Computed 2D contours" << std::endl;
00211     for(unsigned int j = 0; j < c.contours_.size(); j++)
00212     {
00213       for(unsigned int k = 0; k < c.contours_[j].size(); k++)
00214       {
00215         std::cout << c.contours_[j][k](0) << "," << c.contours_[j][k](1) << std::endl;
00216       }
00217     }
00218     /*EXPECT_NEAR(pd_[i]->normal_.dot(p.normal_), 1.0, 0.001);
00219     EXPECT_NEAR(pd_[i]->d_, p.d_, 0.001);
00220     EXPECT_NEAR((p.pose_.rotation() * Eigen::Vector3f(0,0,1)).dot(pd_[i]->normal_), 1.0, 0.001);
00221     EXPECT_NEAR((p.pose_.translation() - pd_[i]->origin_).norm(), 0, 0.001);
00222     std::vector<std::vector<Eigen::Vector3f> > c_3d = p.getContours3D();
00223     //std::cout << "c of p " << i << ":" << std::endl;
00224     for(unsigned int j = 0; j < c_3d.size(); j++)
00225     {
00226       for(unsigned int k = 0; k < c_3d[j].size(); k++)
00227       {
00228         EXPECT_NEAR((pd_[i]->contours_3d_[j][k] - c_3d[j][k]).norm(), 0, 0.001);
00229         //std::cout << c_3d[j][k](0) << "," << c_3d[j][k](1) << "," << c_3d[j][k](2) << std::endl;
00230         //std::cout << pd_[i]->contours_3d_[j][k](0) << "," << pd_[i]->contours_3d_[j][k](1) << "," << pd_[i]->contours_3d_[j][k](2) << std::endl;
00231       }
00232     }*/
00233   }
00234 }
00235 
00236 
00237 int main(int argc, char **argv)
00238 {
00239   testing::InitGoogleTest(&argc, argv);
00240   return RUN_ALL_TESTS();
00241 }
00242 


cob_3d_mapping_common
Author(s): Georg Arbeiter
autogenerated on Wed Aug 26 2015 11:02:19