slam.cpp
Go to the documentation of this file.
00001 
00059 /*
00060  * slam.cpp
00061  *
00062  *  Created on: 27.05.2012
00063  *      Author: josh
00064  */
00065 
00066 
00067 
00068 #define DEBUG_
00069 
00070 #include <ros/ros.h>
00071 #include <rosbag/bag.h>
00072 #include <rosbag/view.h>
00073 
00074 #include <pcl/point_types.h>
00075 #include <pcl/point_cloud.h>
00076 #include <pcl/io/pcd_io.h>
00077 #include <pcl/registration/transforms.h>
00078 
00079 #include <cob_3d_mapping_slam/curved_polygons/debug_interface.h>
00080 
00081 #include <cob_3d_mapping_slam/slam/context.h>
00082 #include <cob_3d_mapping_slam/slam/node.h>
00083 
00084 #include <cob_3d_mapping_slam/dof/tflink.h>
00085 #include <cob_3d_mapping_slam/dof/dof_uncertainty.h>
00086 #include <cob_3d_mapping_slam/dof/dof_variance.h>
00087 
00088 #include <cob_3d_mapping_slam/slam/dummy/objctxt.h>
00089 #include <cob_3d_mapping_slam/slam/dummy/registration.h>
00090 #include <cob_3d_mapping_slam/slam/dummy/key.h>
00091 #include <cob_3d_mapping_slam/slam/dummy/robot.h>
00092 
00093 #include <cob_3d_mapping_slam/curved_polygons/objctxt.h>
00094 #include <cob_3d_mapping_slam/curved_polygons/key.h>
00095 
00096 #include <cob_3d_mapping_msgs/ShapeArray.h>
00097 #include <cob_3d_mapping_msgs/CurvedPolygon_Array.h>
00098 #include "gnuplot_i.hpp"
00099 #include <gtest/gtest.h>
00100 #include <boost/random/mersenne_twister.hpp>
00101 #include <boost/random/uniform_real.hpp>
00102 #include <boost/random/variate_generator.hpp>
00103 
00104 #define CYCLES 100
00105 static const char *BAGFILES[][512]={
00106                                     //Test 1
00107                                     {//"test/cp_rgbd_dataset_freiburg2_desk_validation.bag",
00108                                      //"test/cp_rotate_real_cups4.bag",
00109                                      //"test/cp2_rot_real_cups1.bag",
00110                                      //"test/cp2_static_room.bag",
00111                                      //"test/cp3_rot_real_cups.bag",
00112                                      //"test/cp3_rgbd_dataset_freiburg2_dishes.bag",
00113                                      //"test/cp3_rgbd_dataset_freiburg2_rpy.bag",
00114                                      //"test/cp4_moving.bag",
00115                                      //"test/cp4_room.bag",
00116                                      "test/cp4_rgbd_dataset_freiburg2_rpy2.bag",
00117                                      //"test/cp4_rgbd_dataset_freiburg2_dishes.bag",
00118                                      //"test/cp4_cylinder.bag",
00119                                      //"test/cp4_office3.bag",
00120                                      //"test/cp3_rgbd_dataset_freiburg2_xyz.bag",
00121                                      //"test/cp3_rgbd_dataset_freiburg1_plant.bag",
00122                                      //"test/cp2_rgbd_dataset_freiburg2_dishes.bag",
00123                                      //"test/cp2_freiburg.bag",
00124                                      //"test/2012-06-14-09-04-44.bag",
00125                                      //"test/cp_rot_sim_kitchen.bag",
00126                                      //"test/cp_tr_real_cups.bag",
00127                                      //"test/cp_rotate_real_wall.bag",
00128                                      (const char*)0}
00129 };
00130 
00131 static const char *GROUNDTRUTH[][512]={
00132                                        {
00133                                         //"test/rgbd_dataset_freiburg2_dishes-groundtruth.txt",
00134                                         "test/rgbd_dataset_freiburg2_rpy-groundtruth.txt",
00135                                         //"test/rgbd_dataset_freiburg2_xyz-groundtruth.txt",
00136                                         //"test/rgbd_dataset_freiburg2_dishes-groundtruth.txt",
00137                                         0
00138                                        }
00139 };
00140 
00141 
00142 template<typename Matrix>
00143 float MATRIX_DISTANCE(const Matrix &a, const Matrix &b, const float thr=0.05) {
00144   Matrix c=a-b;
00145   float d=c.norm();
00146 
00147   if(d>thr) {
00148     std::cout<<"A\n"<<a<<"\n";
00149     std::cout<<"B\n"<<b<<"\n";
00150   }
00151 
00152   EXPECT_NEAR(d,0,thr);
00153   return d;
00154 }
00155 
00156 
00157 #if 0
00158 //TEST(Slam,merging)
00159 void test1()
00160 {
00161 
00162   cob_3d_mapping_msgs::feature ft;
00163   cob_3d_mapping_msgs::polyline_point pt;
00164   cob_3d_mapping_msgs::CurvedPolygon s;
00165 
00166   {
00167     s.weight = 100;
00168     s.parameter[0] = 2;
00169     s.parameter[1] = 0;
00170     s.parameter[2] = -0.01;
00171     s.parameter[3] = 0;
00172     s.parameter[4] = -0.01;
00173     s.parameter[5] = 0;
00174     s.ID = 0;
00175     s.energy = "";
00176     s.features.clear();
00177     pt.edge_prob = 1;
00178 
00179     pt.x = -1;
00180     pt.y = -1;
00181     s.polyline.push_back(pt);
00182     pt.x = 1;
00183     pt.y = -1;
00184     s.polyline.push_back(pt);
00185     pt.x = 1;
00186     pt.y = 1;
00187     s.polyline.push_back(pt);
00188     pt.x = -1;
00189     pt.y = 1;
00190     s.polyline.push_back(pt);
00191 
00192     ft.ID = 1; //nearest point
00193     ft.x = 0;
00194     ft.y = 0;
00195     ft.z = 2;
00196     s.features.push_back(ft);
00197 
00198     ft.ID = 2; //form
00199     ft.x = 0;
00200     ft.y = 0;
00201     ft.z = 0;
00202     s.features.push_back(ft);
00203 
00204     ft.ID = 3; //nearest point
00205     ft.x = 0;
00206     ft.y = 0;
00207     ft.z = 2;
00208     s.features.push_back(ft);
00209 
00210     ft.ID = 4; //curv
00211     ft.x = 1;
00212     ft.y = 0;
00213     ft.z = 0;
00214     s.features.push_back(ft);
00215 
00216     ft.ID = 5; //curv
00217     ft.x = 1;
00218     ft.y = 0;
00219     ft.z = 0;
00220     s.features.push_back(ft);
00221   }
00222   Slam_CurvedPolygon::ex_curved_polygon xp1 = s;
00223   EXPECT_FALSE( xp1.invalid() );
00224 
00225   {
00226     s.parameter[0] = 2;
00227     s.parameter[1] = 0;
00228     s.parameter[2] = 0;
00229     s.parameter[3] = 0;
00230     s.parameter[4] = 0;
00231     s.parameter[5] = 0;
00232 
00233     s.ID = 1;
00234     s.features.clear();
00235 
00236     for(size_t i=0; i<s.polyline.size(); i++)
00237     {
00238       s.polyline[i].x += 0.5;
00239     }
00240 
00241     ft.ID = 1; //nearest point
00242     ft.x = 0.5;
00243     ft.y = 0;
00244     ft.z = 2;
00245     s.features.push_back(ft);
00246 
00247     ft.ID = 2; //form
00248     ft.x = 0;
00249     ft.y = 0;
00250     ft.z = 0;
00251     s.features.push_back(ft);
00252 
00253     ft.ID = 3; //nearest point
00254     ft.x = 0.5;
00255     ft.y = 0;
00256     ft.z = 2;
00257     s.features.push_back(ft);
00258 
00259     ft.ID = 4; //curv
00260     ft.x = 1;
00261     ft.y = 0;
00262     ft.z = 0;
00263     s.features.push_back(ft);
00264 
00265     ft.ID = 5; //curv
00266     ft.x = 1;
00267     ft.y = 0;
00268     ft.z = 0;
00269     s.features.push_back(ft);
00270   }
00271 
00272   Slam_CurvedPolygon::ex_curved_polygon xp2 = s;
00273   EXPECT_FALSE( xp2.invalid() );
00274 
00275   Eigen::Vector3f t;
00276   t(0) = 0;
00277   t(1) = 0;
00278   t(2) = 0;
00279   Eigen::Matrix3f R = Eigen::Matrix3f::Identity();
00280   //  R(0,0) = 0; R(0,2) = 1;
00281   //  R(2,2) = 0; R(2,0) = 1;
00282   xp2.transform(R, t, 0.1,0.1);
00283 
00284   std::cout<<xp1.getFeatures()[5].v_<<"\n\n";
00285   std::cout<<xp1.getFeatures()[6].v_<<"\n\n";
00286   std::cout<<xp1.getFeatures()[7].v_<<"\n\n";
00287   std::cout<<xp1.getFeatures()[8].v_<<"\n\n";
00288   std::cout<<"----------\n";
00289   std::cout<<xp2.getFeatures()[5].v_<<"\n\n";
00290   std::cout<<xp2.getFeatures()[6].v_<<"\n\n";
00291   std::cout<<xp2.getFeatures()[7].v_<<"\n\n";
00292   std::cout<<xp2.getFeatures()[8].v_<<"\n\n";
00293   std::cout<<"----------\n";
00294 
00295   std::cout<<"can "<<(!xp1.canMerge(xp2)?"not ":" ")<<"merge\n";
00296 
00297   xp1 += xp2;
00298 
00299   std::cout<<xp1.getFeatures()[5].v_<<"\n\n";
00300   std::cout<<xp1.getFeatures()[6].v_<<"\n\n";
00301   std::cout<<xp1.getFeatures()[7].v_<<"\n\n";
00302   std::cout<<xp1.getFeatures()[8].v_<<"\n\n";
00303 
00304   Eigen::Vector3f r;
00305   r(2) = 2;
00306 
00307   r(0) = -1.5;
00308   r(1) = 1;
00309   EXPECT_NEAR( (xp1.getFeatures()[5].v_-r).norm(), 0, 0.05 );
00310   r(0) = 1;
00311   r(1) = 1;
00312   EXPECT_NEAR( (xp1.getFeatures()[6].v_-r).norm(), 0, 0.05 );
00313   r(0) = 1;
00314   r(1) = -1;
00315   EXPECT_NEAR( (xp1.getFeatures()[7].v_-r).norm(), 0, 0.05 );
00316   r(0) = -1.5;
00317   r(1) = -1;
00318   EXPECT_NEAR( (xp1.getFeatures()[8].v_-r).norm(), 0, 0.05 );
00319 }
00320 #endif
00321 
00322 struct SOdomotry_Data
00323 {
00324   double timestamp;
00325   Eigen::Vector3f t;
00326   Eigen::Quaternionf q;
00327 };
00328 
00329 #include <visualization_msgs/Marker.h>
00330 #include <nav_msgs/Odometry.h>
00331 #ifdef VIS_
00332 #include "pcl/visualization/pcl_visualizer.h"
00333 #include <pcl/visualization/cloud_viewer.h>
00334 #endif
00335 
00336 
00337 TEST(Slam,dummy_registration)
00338 //void t4()
00339 {
00340   //setup slam
00341   typedef DOF6::DOF6_Source<DOF6::TFLinkvf,DOF6::DOF6_Uncertainty<Dummy::RobotParameters,float> > DOF6;
00342   typedef Slam::Node<Dummy::OBJCTXT<DOF6> > Node;
00343 
00344   Slam::Context<Slam_CurvedPolygon::KEY<DOF6>, Node> ctxt(.60,.30);
00345 
00346   for(int i=0; i<6; i++)
00347   {
00348     ROS_INFO("new frame");
00349     ctxt.startFrame(i);
00350     ctxt.finishFrame();
00351 
00352     //check path
00353     Eigen::Matrix3f tmp_rot = Eigen::Matrix3f::Identity();
00354     Eigen::Matrix3f tmp_rot2 = Eigen::Matrix3f::Identity();
00355     Eigen::Vector3f tmp_tr  = Eigen::Vector3f::Zero();
00356     const Slam::SWAY<Node> *n = &ctxt.getPath().getLocal();
00357     while(n)
00358     {
00359 
00360       tmp_tr = tmp_rot2*tmp_tr + n->link_.getTranslation();
00361       tmp_rot = ((Eigen::Matrix3f)n->link_.getRotation())*tmp_rot;
00362       tmp_rot2= ((Eigen::Matrix3f)n->link_.getRotation());
00363 
00364       std::cout<<"ROTn\n"<<(::DOF6::EulerAnglesf)n->link_.getRotation()<<"\n";
00365       std::cout<<"TRn\n"<<n->link_.getTranslation()<<"\n";
00366 
00367       std::cout<<"con\n";
00368       size_t next_id = n->id_-1;
00369       const Slam::SWAY<Node> *n2 = NULL;
00370       for(size_t i=0; i<n->node_->getConnections().size(); i++)
00371         if(n->node_->getConnections()[i].id_ == next_id) {
00372           n2 = &n->node_->getConnections()[i];
00373           break;
00374         }
00375       n = n2;
00376     }
00377     std::cout<<"ROT1\n"<<(::DOF6::EulerAnglesf)tmp_rot<<"\n";
00378     std::cout<<"TR1\n"<<tmp_tr<<"\n";
00379   }
00380   //exit(0);
00381 }
00382 
00383 
00384 TEST(Slam,bag_run)
00385 //void t3()
00386 {
00387 #ifdef VIS_
00388   pcl::visualization::CloudViewer viewer("abc");
00389 #endif
00390   pcl::PointCloud<pcl::PointXYZRGB>::Ptr rgb(new pcl::PointCloud<pcl::PointXYZRGB>);
00391 
00392   int bg_file=0;
00393   while(BAGFILES[0][bg_file]) {
00394 
00395     std::vector<SOdomotry_Data> odos;
00396     if(GROUNDTRUTH[0][bg_file]) {
00397       std::ifstream infile(GROUNDTRUTH[0][bg_file]);
00398 
00399       std::string line;
00400       while (std::getline(infile, line))
00401       {
00402         if(line.size()<1 || line[0]=='#') continue;
00403 
00404         std::istringstream iss(line);
00405         SOdomotry_Data odo;
00406         if (!(iss >> odo.timestamp >> odo.t(0) >> odo.t(1) >> odo.t(2) >> odo.q.x() >> odo.q.y() >> odo.q.z() >> odo.q.w())) {
00407           ROS_ERROR("parsing groundtruth");
00408           ROS_ASSERT(0);
00409           break;
00410         } // error
00411 
00412         odos.push_back(odo);
00413       }
00414 
00415     }
00416 
00417     rosbag::Bag bag, bag_out;
00418     //try
00419     {
00420       //read file
00421       bag.    open(BAGFILES[0][bg_file],                         rosbag::bagmode::Read);
00422       bag_out.open(std::string(BAGFILES[0][bg_file])+".odo.bag", rosbag::bagmode::Write);
00423 
00424       //setup topics
00425       std::vector<std::string> topics;
00426       topics.push_back(std::string("curved_polygons"));
00427       topics.push_back(std::string("/curved_polygons"));
00428       topics.push_back(std::string("shapes_array"));
00429       topics.push_back(std::string("/shapes_array"));
00430 
00431       rosbag::View view(bag, rosbag::TopicQuery(topics));
00432 
00433       cob_3d_marker::MarkerContainer marker_cont;
00434       visualization_msgs::Marker marker_text, marker_points, marker_planes, marker_cor1, marker_cor2, marker_del, marker_map, marker_map2;
00435       visualization_msgs::Marker *pmarkers[]={
00436                                              &marker_text, &marker_points, &marker_planes, &marker_cor1, &marker_cor2, &marker_del, &marker_map, &marker_map2
00437       };
00438       marker_text.header.frame_id = "/openni_rgb_frame";
00439       marker_text.pose.position.x = 0;
00440       marker_text.pose.position.y = 0;
00441       marker_text.pose.position.z = 0;
00442       marker_text.pose.orientation.x = 0.0;
00443       marker_text.pose.orientation.y = 0.0;
00444       marker_text.pose.orientation.z = 0.0;
00445       marker_text.pose.orientation.w = 1.0;
00446       marker_text.action = visualization_msgs::Marker::ADD;
00447       marker_text.type = visualization_msgs::Marker::LINE_LIST;
00448       marker_text.scale.x = marker_text.scale.y = marker_text.scale.z = 0.005;
00449       marker_text.color.r = marker_text.color.g = marker_text.color.b =  marker_text.color.a = 1;
00450 
00451       marker_map = marker_del = marker_cor1 =  marker_cor2 = marker_points = marker_planes = marker_text;
00452       marker_text.type = visualization_msgs::Marker::TEXT_VIEW_FACING;
00453       marker_text.scale.x = marker_text.scale.y = marker_text.scale.z = 0.35;
00454       marker_points.color.g = marker_points.color.b =  0;
00455       marker_planes.color.g = marker_planes.color.r =  0;
00456       marker_cor2.color.b = marker_cor2.color.r =  0;
00457       marker_del.action = visualization_msgs::Marker::DELETE;
00458       marker_map.type = visualization_msgs::Marker::TRIANGLE_LIST;
00459       marker_map.scale.x = marker_map.scale.y = marker_map.scale.z = 1;
00460       marker_map2 = marker_map;
00461       marker_cor2.scale.x = marker_cor2.scale.y = marker_cor2.scale.z = 0.01;
00462 
00463       marker_text.id = 0;
00464       marker_points.id = 1;
00465       marker_planes.id = 2;
00466       marker_cor1.id = 3;
00467       marker_cor2.id = 4;
00468       marker_map.id = 5;
00469       marker_map2.id = 6;
00470 
00471       //setup slam
00472       typedef DOF6::DOF6_Source<DOF6::TFLinkvf,DOF6::DOF6_Uncertainty<Dummy::RobotParameters,float> > DOF6;
00473       typedef Slam::Node<Slam_CurvedPolygon::OBJCTXT<DOF6> > Node;
00474 
00475       Slam::Context<Slam_CurvedPolygon::KEY<DOF6>, Node> ctxt(.60,.30);
00476       //Slam::Context<Slam_CurvedPolygon::KEY<DOF6>, Node> ctxt(.30,.15);
00477 
00478       Eigen::Vector3f last_tr = Eigen::Vector3f::Zero();
00479       Eigen::Matrix3f last_rot= Eigen::Matrix3f::Identity();
00480 
00481       Eigen::Vector3f last_odo_tr = Eigen::Vector3f::Zero();
00482       Eigen::Vector3f first_odo_tr = Eigen::Vector3f::Zero();
00483       Eigen::Matrix3f first_odo_rot= Eigen::Matrix3f::Identity();
00484 
00485       //load data
00486       int ctr=0;
00487       ros::Time last, start;
00488       bool first=true;
00489       std::vector<cob_3d_mapping_msgs::ShapeArray> memory_sa;
00490       BOOST_FOREACH(rosbag::MessageInstance const m, view)
00491       {
00492 
00493         cob_3d_mapping_msgs::ShapeArray::ConstPtr sa = m.instantiate<cob_3d_mapping_msgs::ShapeArray>();
00494         if (sa != NULL)
00495         {
00496 //          if(sa->header.stamp.toSec()<1311867723.916822)
00497 //            continue;
00498           memory_sa.push_back(*sa);
00499           continue;
00500         }
00501 
00502         geometry_msgs::Point line_p, line_p0;
00503         line_p0.x = line_p0.y = line_p0.z = 0;
00504 
00505         cob_3d_mapping_msgs::CurvedPolygon_Array::ConstPtr s = m.instantiate<cob_3d_mapping_msgs::CurvedPolygon_Array>();
00506         if (s != NULL)
00507         {
00508           for(size_t i=0; i<sizeof(pmarkers)/sizeof(pmarkers[0]); i++)
00509             pmarkers[i]->header.frame_id = s->header.frame_id;
00510 
00511 //          ROS_INFO("time %f",s->header.stamp.toSec());
00512           if(s->header.stamp.toSec()<1311867724.916822)
00513             continue;
00514 
00515           //          if(!(std::abs((s->stamp-start).toSec()-0)<0.001 || first || std::abs((s->stamp-start).toSec()-3.15)<0.001 || std::abs((s->stamp-start).toSec()-3.7)<0.1))
00516           //            continue;
00517 
00518           if(last!=s->header.stamp) {
00519             marker_cor1.header.stamp = marker_cor2.header.stamp = marker_points.header.stamp = marker_planes.header.stamp = marker_text.header.stamp = s->header.stamp;
00520 
00521             ROS_INFO("new frame");
00522 
00523             last=s->header.stamp;
00524             Debug::Interface::get().setTime(ros::Time::now().toSec());
00525             //Debug::Interface::get().setTime((last-start).toSec());
00526             if(first) {
00527               first=false;
00528               start = s->header.stamp;
00529             }
00530             else
00531               ctxt.finishFrame();
00532 
00533             ROS_INFO("timestamp %f (%d)", (last-start).toSec(), ctr);
00534             ctr++;
00535 
00536             std_msgs::ColorRGBA line_c;
00537             pcl::PointXYZRGB p;
00538             Eigen::Vector3f from,to,temp;
00539             while(Debug::Interface::get().getArrow(from,to,p.r,p.g,p.b))
00540             {
00541               line_p.x = from(0);
00542               line_p.y = from(1);
00543               line_p.z = from(2);
00544               line_c.r=p.r/255.f;
00545               line_c.g=p.g/255.f;
00546               line_c.b=p.b/255.f;
00547               line_c.a=1;
00548               //p.r>100?marker_cor1.points.push_back(line_p):marker_cor2.points.push_back(line_p);
00549               marker_cor2.points.push_back(line_p);
00550               marker_cor2.colors.push_back(line_c);
00551               line_p.x = to(0);
00552               line_p.y = to(1);
00553               line_p.z = to(2);
00554               //p.r>100?marker_cor1.points.push_back(line_p):marker_cor2.points.push_back(line_p);
00555               marker_cor2.points.push_back(line_p);
00556               marker_cor2.colors.push_back(line_c);
00557               temp = to-from;
00558               //              from -= temp*0.1;
00559               //              temp *= 1.2;
00560               for(float ms=0; ms<=1; ms+=0.01)
00561               {
00562                 p.x=ms*temp(0)+from(0);
00563                 p.y=ms*temp(1)+from(1);
00564                 p.z=ms*temp(2)+from(2);
00565                 if(p.getVector3fMap().squaredNorm()<100)
00566                   rgb->push_back(p);
00567               }
00568             }
00569 
00570             //OUTPUT--------------
00571 
00572             //check path
00573             Eigen::Matrix3f tmp_rot = Eigen::Matrix3f::Identity();
00574             Eigen::Matrix3f tmp_rot2 = Eigen::Matrix3f::Identity();
00575             Eigen::Vector3f tmp_tr  = Eigen::Vector3f::Zero();
00576             const Slam::SWAY<Node> *n = &ctxt.getPath().getLocal();
00577             while(n)
00578             {
00579 
00580               tmp_tr = tmp_rot2*tmp_tr + n->link_.getTranslation();
00581               tmp_rot = ((Eigen::Matrix3f)n->link_.getRotation())*tmp_rot;
00582               tmp_rot2= ((Eigen::Matrix3f)n->link_.getRotation());
00583 
00584               std::cout<<"ROTn\n"<<(::DOF6::EulerAnglesf)n->link_.getRotation()<<"\n";
00585               std::cout<<"TRn\n"<<n->link_.getTranslation()<<"\n";
00586 
00587               std::cout<<"con\n";
00588               size_t next_id = n->id_-1;
00589               const Slam::SWAY<Node> *n2 = NULL;
00590               for(size_t i=0; i<n->node_->getConnections().size(); i++)
00591                 if(n->node_->getConnections()[i].id_ == next_id) {
00592                   n2 = &n->node_->getConnections()[i];
00593                   break;
00594                 }
00595               n = n2;
00596             }
00597             std::cout<<"ROT1\n"<<(::DOF6::EulerAnglesf)tmp_rot<<"\n";
00598             std::cout<<"TR1\n"<<tmp_tr<<"\n";
00599 
00600             {
00601 //              DOF6 tf;
00602 //              slam.getPath().getTF(tf, );
00603 //              std::cout<<"CMP\n"<<
00604             }
00605 
00606             Eigen::Quaternionf quat((Eigen::Matrix3f)tmp_rot.inverse());
00607 
00608             nav_msgs::Odometry odo;
00609             odo.header.frame_id = s->header.frame_id;
00610             odo.header.stamp = last;
00611             odo.pose.pose.position.x = -tmp_tr(0);
00612             odo.pose.pose.position.y = -tmp_tr(1);
00613             odo.pose.pose.position.z = -tmp_tr(2);
00614             odo.pose.pose.orientation.x = quat.x();
00615             odo.pose.pose.orientation.y = quat.y();
00616             odo.pose.pose.orientation.z = quat.z();
00617             odo.pose.pose.orientation.w = quat.w();
00618 
00619             bag_out.write("odometry", last, odo);
00620 
00621             while(odos.size()>0 && odos.front().timestamp<last.toSec())
00622               odos.erase(odos.begin());
00623             if(odos.size()>0)
00624             {
00625               if(start == s->header.stamp)
00626               {
00627                 first_odo_tr  = odos.front().t;
00628                 first_odo_rot = odos.front().q;
00629               }
00630 
00631               odos.front().t -= first_odo_tr;
00632               odos.front().t = first_odo_rot.inverse()*odos.front().t;
00633               odos.front().q = first_odo_rot.inverse()*odos.front().q;
00634 
00635               std::cout<<"ROT2\n"<<(::DOF6::EulerAnglesf)tmp_rot<<"\n";
00636               std::cout<<"TR2\n"<<tmp_tr<<"\n";
00637 
00638               std::cout<<"ODO ROT2\n"<<(::DOF6::EulerAnglesf)odos.front().q.toRotationMatrix().inverse()<<"\n";
00639               std::cout<<"ODO TR2\n"<<-odos.front().t<<"\n";
00640 
00641               odo.header.stamp = ros::Time(odos.front().timestamp);
00642               odo.pose.pose.position.x = odos.front().t(0);
00643               odo.pose.pose.position.y = odos.front().t(1);
00644               odo.pose.pose.position.z = odos.front().t(2);
00645 
00646               ROS_INFO("odometry tr jump with length %f", (odos.front().t-last_odo_tr).norm());
00647               last_odo_tr = odos.front().t;
00648               odo.pose.pose.orientation.x = odos.front().q.x();
00649               odo.pose.pose.orientation.y = odos.front().q.y();
00650               odo.pose.pose.orientation.z = odos.front().q.z();
00651               odo.pose.pose.orientation.w = odos.front().q.w();
00652 
00653               bag_out.write("groundtruth", ros::Time(odos.front().timestamp), odo);
00654             }
00655 
00656 #ifdef EXTRA_MARKER
00657             marker_cont << cob_3d_marker::MarkerClean();
00658             marker_cont.clear();
00659             marker_cont<<new cob_3d_marker::MarkerList_Line(2)<<new cob_3d_marker::MarkerList_Arrow(3)<<new cob_3d_marker::MarkerList_Text(4)<<new cob_3d_marker::MarkerList_Text(5);
00660 #endif
00661 
00662             tmp_rot = Eigen::Matrix3f::Identity();
00663             tmp_rot2 = Eigen::Matrix3f::Identity();
00664             tmp_tr  = Eigen::Vector3f::Zero();
00665             n = &ctxt.getPath().getLocal();
00666             while(n)
00667             {
00668 
00669               tmp_tr = tmp_rot2*tmp_tr + n->link_.getTranslation();
00670               tmp_rot = ((Eigen::Matrix3f)n->link_.getRotation())*tmp_rot;
00671               tmp_rot2= ((Eigen::Matrix3f)n->link_.getRotation());
00672 
00673               for(size_t i=0; i<n->node_->getContext().getObjs().size(); i++)
00674               {
00675                 //                if( n->node_->getContext().getObjs()[i]->getUsedCounter()<7)
00676                 //                  continue;
00677 
00678                 ::std_msgs::ColorRGBA col;
00679 
00680                 unsigned int rnd=(((i+1)*11)<<12)+(i+3)*i*7;//rand();
00681                 ROS_ASSERT(n->node_->getContext().getObjs()[i]->getUsedCounter() <= n->node_->getContext().getObjs()[i]->getCreationCounter());
00682                 col.a = std::log(n->node_->getContext().getObjs()[i]->getUsedCounter())/(std::log(n->node_->getContext().getObjs()[i]->getCreationCounter())+0.1f);
00683                 if(!pcl_isfinite(col.a))
00684                 {
00685                   col.a = 1.f;
00686                   ROS_ERROR("color not finite %d %d",n->node_->getContext().getObjs()[i]->getUsedCounter(),n->node_->getContext().getObjs()[i]->getCreationCounter());
00687                 }
00688 
00689                 col.r = ((rnd>>0)&0xff)/255.f;
00690                 col.g = ((rnd>>8)&0xff)/255.f;
00691                 col.b = ((rnd>>16)%0xff)/255.f;
00692 
00693                 {
00694                   std::vector<std::vector<Eigen::Vector3f> > pts;
00695                   n->node_->getContext().getObjs()[i]->getData().getControlPoints(pts);
00696                   for(size_t j=0; j<pts.size(); j++)
00697                   {
00698                     for(size_t k=0; k<pts[j].size()-1; k++)
00699                     {
00700                       Eigen::Vector3f v=tmp_rot*pts[j][k]+tmp_tr;
00701                     line_p.x = v(0);
00702                     line_p.y = v(1);
00703                     line_p.z = v(2);
00704                     line_c=col;
00705                     marker_cor1.points.push_back(line_p);
00706                     marker_cor1.colors.push_back(line_c);
00707 
00708                     v=tmp_rot*pts[j][k+1]+tmp_tr;
00709                     line_p.x = v(0);
00710                     line_p.y = v(1);
00711                     line_p.z = v(2);
00712                     marker_cor1.points.push_back(line_p);
00713                     marker_cor1.colors.push_back(line_c);
00714 
00715                     if(j+1!=pts.size()) {
00716                       v=tmp_rot*pts[j][k]+tmp_tr;
00717                     line_p.x = v(0);
00718                     line_p.y = v(1);
00719                     line_p.z = v(2);
00720                       line_c=col;
00721                       marker_cor1.points.push_back(line_p);
00722                       marker_cor1.colors.push_back(line_c);
00723                       v=tmp_rot*pts[j+1][k]+tmp_tr;
00724                       line_p.x = v(0);
00725                       line_p.y = v(1);
00726                       line_p.z = v(2);
00727                       line_c=col;
00728                       marker_cor1.points.push_back(line_p);
00729                       marker_cor1.colors.push_back(line_c);
00730                     }
00731 
00732 //                    v=tmp_rot*pts[j][k]+tmp_tr;
00733 //                    line_p.x = v(0);
00734 //                    line_p.y = v(1);
00735 //                    line_p.z = v(2);
00736 //                    marker_map.points.push_back(line_p);
00737 //                    marker_map.colors.push_back(col);
00738 //                    line_p.x = v(0)+0.03f;
00739 //                    line_p.y = v(1);
00740 //                    line_p.z = v(2);
00741 //                    marker_map.points.push_back(line_p);
00742 //                    marker_map.colors.push_back(col);
00743 //                    line_p.x = v(0);
00744 //                    line_p.y = v(1)+0.03f;
00745 //                    line_p.z = v(2);
00746 //                    marker_map.points.push_back(line_p);
00747 //                    marker_map.colors.push_back(col);
00748                   }
00749                   }
00750                 }
00751 
00752                 std::vector<Eigen::Vector3f> tris;
00753                 n->node_->getContext().getObjs()[i]->getData().getTriangles(tris);
00754                 ROS_ASSERT(tris.size()%3==0);
00755 
00756                 for(size_t j=0; j<tris.size(); j++)
00757                 {
00758                   //Eigen::Vector3f v=((Eigen::Matrix3f)n->link_.getRotation())*tris[j]+n->link_.getTranslation();
00759                   //Eigen::Vector3f v=tmp_rot.inverse()*(tris[j]-tmp_tr);
00760                   Eigen::Vector3f v=tmp_rot*tris[j]+tmp_tr;
00761                   line_p.x = v(0);
00762                   line_p.y = v(1);
00763                   line_p.z = v(2);
00764                   marker_map.points.push_back(line_p);
00765                   marker_map.colors.push_back(col);
00766                 }
00767 
00768 #ifdef EXTRA_MARKER
00769                 marker_cont<<n->node_->getContext().getObjs()[i]->getData().getSurface();
00770 #endif
00771               }
00772 
00773               size_t next_id = n->id_-1;
00774               const Slam::SWAY<Node> *n2 = NULL;
00775               for(size_t i=0; i<n->node_->getConnections().size(); i++)
00776                 if(n->node_->getConnections()[i].id_ == next_id) {
00777                   n2 = &n->node_->getConnections()[i];
00778                   break;
00779                 }
00780               n = n2;
00781               //break;
00782             }
00783 
00784 #ifdef EXTRA_MARKER
00785             marker_cont>>new cob_3d_marker::MarkerBagfile(&bag_out,"/markers2",last);
00786 #endif
00787 
00788             while(memory_sa.size()>0 && memory_sa.front().header.stamp<last)
00789             {
00790               bag_out.write("shapes_array", last, memory_sa.front());
00791               ROS_INFO("shapes_array %d",memory_sa.front().shapes.size());
00792               memory_sa.erase(memory_sa.begin());
00793             }
00794             for(int i=0; i<7; i++)
00795             {
00796               marker_del.id = i;
00797               if(i<5) bag_out.write("/markers", last, marker_del);
00798               else if(i<6) bag_out.write("/map", last, marker_del);
00799               else bag_out.write("/input", last, marker_del);
00800             }
00801             bag_out.write("/markers", last, marker_points);
00802             bag_out.write("/markers", last, marker_planes);
00803             bag_out.write("/input", last, marker_map2);
00804             bag_out.write("/markers", last, marker_cor1);
00805             bag_out.write("/markers", last, marker_cor2);
00806             bag_out.write("/map", last, marker_map);
00807             marker_points.points.clear();
00808             marker_planes.points.clear();
00809             marker_cor1.points.clear();
00810             marker_cor2.points.clear();
00811             marker_cor1.colors.clear();
00812             marker_cor2.colors.clear();
00813             marker_map.points.clear();
00814             marker_map.colors.clear();
00815             marker_map2.points.clear();
00816             marker_map2.colors.clear();
00817             //OUTPUT-------------------
00818 
00819 
00820             if((last_tr-tmp_tr).norm()>0.2 || ((::DOF6::EulerAnglesf)(last_rot.inverse()*tmp_rot)).getVector().squaredNorm()>0.15f*0.15f) {
00821               ROS_INFO("BIG JUMP");
00822               ROS_ERROR("BIG JUMP");
00823 
00824               marker_text.text = "BIG JUMP";
00825               bag_out.write("/markers", last, marker_text);
00826               //ASSERT_TRUE(false);
00827               //while(1) getchar();
00828             }
00829             last_tr=tmp_tr;
00830             last_rot=tmp_rot;
00831 
00832             rgb->width=1;
00833             rgb->height=rgb->size();
00834             if(rgb->size())
00835             {
00836               //              Eigen::Quaternionf q((Eigen::Matrix3f)tmp_rot.inverse());
00837               //              pcl::transformPointCloud(*rgb,*rgb, -tmp_tr, q);
00838 #ifdef VIS_
00839               viewer.showCloud(rgb);
00840 #endif
00841               std::cerr<<"press any key\n";
00842               //              if((last-start).toSec()>31.8)
00843               if(getchar()=='q')
00844               {
00845                 bag_out.close();
00846                 return;
00847               }
00848               rgb.reset(new pcl::PointCloud<pcl::PointXYZRGB>);
00849             }
00850 
00851             ctxt.startFrame(s->header.stamp.toSec());
00852           }
00853 
00854           for(size_t k=0; k<s->polygons.size(); k++)
00855           {
00856 
00857             if(s->polygons[k].weight<50) continue;
00858 
00859             Slam_CurvedPolygon::ex_curved_polygon xp = s->polygons[k];
00860 
00861             if(xp.invalid() || xp.getNearestPoint().squaredNorm()>20*20)
00862               continue;
00863 
00864             {
00865               std::vector<Eigen::Vector3f> tris;
00866               xp.getTriangles(tris);
00867               ROS_ASSERT(tris.size()%3==0);
00868 
00869               ::std_msgs::ColorRGBA col;
00870               size_t i=marker_map2.points.size();
00871               unsigned int rnd=(((i+1)*11)<<12)+(i+3)*i*7;//rand();
00872               col.a = 1.f;
00873               col.r = ((rnd>>0)&0xff)/255.f;
00874               col.g = ((rnd>>8)&0xff)/255.f;
00875               col.b = ((rnd>>16)%0xff)/255.f;
00876               for(size_t j=0; j<tris.size(); j++)
00877               {
00878                 Eigen::Vector3f v=tris[j];
00879                 line_p.x = v(0);
00880                 line_p.y = v(1);
00881                 line_p.z = v(2);
00882                 marker_map2.points.push_back(line_p);
00883                 marker_map2.colors.push_back(col);
00884               }
00885             }
00886 
00887             //        if(    std::abs(xp.getNearestPoint().norm()-1.2)>0.2
00888             //            || std::abs(xp.getNearestPoint()(0))>0.4
00889             //            || std::abs(xp.getNearestPoint()(1))>0.4
00890             //        ) continue;
00891 
00892             //                        if(s->polyline.size()<10)
00893             //                          continue;
00894             //                if(xp.getEnergy()<5)
00895             //                  continue;
00896             //        if(!xp.isPlane())
00897             //          continue;
00898             //        if(xp.getNearestPoint().norm()>4)
00899             //          continue;
00900 
00901             ctxt += xp;//s->polygons[k];
00902 
00903 #ifdef VIS_
00904             line_p.x = xp.getNearestPoint()(0);
00905             line_p.y = xp.getNearestPoint()(1);
00906             line_p.z = xp.getNearestPoint()(2);
00907             if(xp.isPlane())
00908             {
00909               marker_planes.points.push_back(line_p0);
00910               marker_planes.points.push_back(line_p);
00911             }
00912             else
00913             {
00914               marker_points.points.push_back(line_p0);
00915               marker_points.points.push_back(line_p);
00916             }
00917 
00918             pcl::PointXYZRGB p;
00919             std::cerr<<"param: "
00920                 <<s->polygons[k].parameter[0]<<" "
00921                 <<s->polygons[k].parameter[1]<<" "
00922                 <<s->polygons[k].parameter[2]<<" "
00923                 <<s->polygons[k].parameter[3]<<" "
00924                 <<s->polygons[k].parameter[4]<<" "
00925                 <<s->polygons[k].parameter[5]<<"\n";
00926             for(float ms=0; ms<=1; ms+=0.01)
00927             {
00928               p.r=p.g=p.b=0;
00929               if(!xp.isPlane())
00930                 p.r=255;
00931               else
00932                 p.b=255;
00933               p.x=ms*xp.getNearestPoint()(0);
00934               p.y=ms*xp.getNearestPoint()(1);
00935               p.z=ms*xp.getNearestPoint()(2);
00936               if(p.getVector3fMap().squaredNorm()<100)
00937                 rgb->push_back(p);
00938             }
00939 
00940             Eigen::Vector3f temp;
00941             temp=xp.getFeatures()[1].v_;
00942             temp.normalize();
00943             temp*=0.1;
00944             for(float ms=0; ms<=1; ms+=0.01)
00945             {
00946               p.r=p.g=p.b=0;
00947               p.g=255;
00948               p.b=255;
00949               p.x=ms*temp(0)+xp.getNearestPoint()(0);
00950               p.y=ms*temp(1)+xp.getNearestPoint()(1);
00951               p.z=ms*temp(2)+xp.getNearestPoint()(2);
00952               if(p.getVector3fMap().squaredNorm()<100)
00953                 rgb->push_back(p);
00954             }
00955             temp=xp.getFeatures()[3].v_;
00956             temp.normalize();
00957             temp*=0.1;
00958             for(float ms=0; ms<=1; ms+=0.01)
00959             {
00960               p.r=p.g=p.b=0;
00961               p.r=255;
00962               p.b=255;
00963               p.x=ms*temp(0)+xp.getNearestPoint()(0);
00964               p.y=ms*temp(1)+xp.getNearestPoint()(1);
00965               p.z=ms*temp(2)+xp.getNearestPoint()(2);
00966               if(p.getVector3fMap().squaredNorm()<100)
00967                 rgb->push_back(p);
00968             }
00969             temp=xp.getFeatures()[4].v_;
00970             temp.normalize();
00971             temp*=0.1;
00972             for(float ms=0; ms<=1; ms+=0.01)
00973             {
00974               p.r=p.g=p.b=0;
00975               p.r=255;
00976               p.g=255;
00977               p.x=ms*temp(0)+xp.getNearestPoint()(0);
00978               p.y=ms*temp(1)+xp.getNearestPoint()(1);
00979               p.z=ms*temp(2)+xp.getNearestPoint()(2);
00980               if(p.getVector3fMap().squaredNorm()<100)
00981                 rgb->push_back(p);
00982             }
00983 #endif
00984           }
00985         }
00986 
00987       }
00988 
00989       ctxt.finishFrame();
00990 
00991       bag.close();
00992     }
00993     /*catch(...) {
00994       bag_out.close();
00995       std::cout<<"failed to load: "<<BAGFILES[0][bg_file]<<"\n";
00996       ASSERT_TRUE(false);
00997     }*/
00998 
00999     bg_file++;
01000   }
01001 }
01002 
01003 int main(int argc, char **argv){
01004   ros::Time::init();
01005 
01006   testing::InitGoogleTest(&argc, argv);
01007   return RUN_ALL_TESTS();
01008 }


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