00001
00002 #ifndef VSLAM_DATA_DUMP_H_INCLUDED
00003 #define VSLAM_DATA_DUMP_H_INCLUDED
00004
00005 #include "utils.h"
00006 #include <boost/graph/adjacency_list.hpp>
00007 #include <boost/graph/depth_first_search.hpp>
00008 #include "vslam_utils/csv_matrix_io.h"
00009 #include <boost/filesystem.hpp>
00010 #define VSLAM_DUMP_DELIM ';'
00011
00012 namespace vslam{
00013
00014
00015
00016
00017
00018 template<class PointsIt, class TrajectoryIt>
00019 void readTrajectory(std::istream & in, PointsIt points, TrajectoryIt trajectory){
00020 using namespace vslam_utils;
00021 int num_pt, traj_sz;
00022 std::string useless;
00023 in >> useless;
00024 in >> num_pt;
00025 Vector3d vec;
00026 Matrix3d mat;
00027 for(int i=0; i < num_pt; i++){
00028 readVec(in, vec);
00029 *points=vec;
00030 ++points;
00031 }
00032 in >> useless;
00033 in >> traj_sz;
00034 for(int i=0; i < traj_sz; i++){
00035 readMatrix(in, mat);
00036 readVec(in, vec);
00037 *trajectory=SE3(mat, vec);
00038 ++trajectory;
00039 }
00040 }
00041
00042 template<class PointsIt, class TrajectoryIt>
00043 void writeTrajectory(std::ostream & out, PointsIt points_begin, PointsIt points_end,
00044 TrajectoryIt trajectory_begin, TrajectoryIt trajectory_end, int id){
00045 using namespace vslam_utils;
00046 writePoints(out, points_begin, points_end);
00047 out << "trajectory" << endl;
00048 out << count_if(trajectory_begin, trajectory_end, truePred<SE3>) << endl;
00049 while(trajectory_begin != trajectory_end){
00050 writeMatrix(out, trajectory_begin->rotation_matrix());
00051 writeDelim(out);
00052 writeMatrix(out, trajectory_begin->translation());
00053 ++trajectory_begin;
00054 out << endl;
00055 }
00056 }
00057
00058
00059
00060
00061
00062
00063
00064
00065 namespace details{
00066 ImageMatch preparePoseArrow(const SE3& T_cw){
00067 Vector3d vec1, vec2;
00068 vec1 = T_cw * Vector3d(0,0,0);
00069 vec2 = T_cw * Vector3d(0,0,1);
00070 return (make_pair(vec1, vec2-vec1));
00071 }
00072
00073 template<class G> class DrawGraphDFSVisitor : public boost::default_dfs_visitor{
00074 protected:
00075 ImageMatchVec& line_segments;
00076 ImageMatchVec& pose_arrows;
00077 public:
00078 DrawGraphDFSVisitor(ImageMatchVec& _line_segments, ImageMatchVec& _pose_arrows) :
00079 line_segments(_line_segments), pose_arrows(_pose_arrows) {}
00080
00081 void examine_edge(typename graph_traits<G>::edge_descriptor e, const G& g){
00082 typename graph_traits<G>::vertex_descriptor v1, v2;
00083 v1 = source(e, g);
00084 v2 = target(e, g);
00085 typename boost::property_map<G,vertex_seq_t>::const_type seq_pmap =
00086 get(vertex_seq, g);
00087 if(seq_pmap[v1] < seq_pmap[v2]){
00088 SE3 T_v2_w = get(vertex_transform, g, v2).inverse();
00089 SE3 T_v1_w = get(vertex_transform, g, v1).inverse();
00090 line_segments.push_back(
00091 make_pair(T_v1_w * Vector3d::Zero(), T_v2_w * Vector3d::Zero()));
00092 }
00093 }
00094
00095 void discover_vertex(typename graph_traits<G>::vertex_descriptor v, const G& g){
00096 pose_arrows.push_back(preparePoseArrow(get(vertex_transform, g, v).inverse()));
00097 }
00098 };
00099 }
00100
00101 template<class G> ImageMatchVec votrack(G& g){
00102 vector<Vector3d> poses;
00103
00104 int last_seq=-1;
00105 while(true){
00106 int min_seq=10000;
00107 typename graph_traits<G>::vertex_iterator vertex_it, vertex_it_end, tmp_it;
00108 tie(vertex_it, vertex_it_end) = vertices(g);
00109
00110 while(vertex_it!=vertex_it_end){
00111 int seq = get(vertex_seq, g, *vertex_it);
00112 if( seq > last_seq && seq < min_seq){
00113 min_seq = seq;
00114 tmp_it=vertex_it;
00115 }
00116 ++vertex_it;
00117 }
00118 if(min_seq==10000) break;
00119 last_seq=min_seq;
00120 poses.push_back(get(vertex_transform, g, *tmp_it).inverse()*Vector3d(0,0,0));
00121 }
00122
00123 ImageMatchVec line_segments;
00124 if(poses.size() >= 2){
00125 vector<Vector3d>::const_iterator it = poses.begin() + 1;
00126 while(it!=poses.end()){
00127 line_segments.push_back(make_pair(*(it-1), *it));
00128 ++it;
00129 }
00130 }
00131 return line_segments;
00132 }
00133
00134
00135
00136
00137
00138
00139 template<class G> void drawGraph(G& g){
00140 using namespace vslam_utils;
00141 ImageMatchVec line_segments, pose_arrows, line_segments2;
00142 details::DrawGraphDFSVisitor<G> vis(line_segments, pose_arrows);
00143 boost::depth_first_search(g, boost::visitor(vis));
00144 boost::filesystem::path p(debug_output_path.native());
00145 p /= "scene.csv";
00146 std::ofstream f(p.c_str());
00147 writeVectorPairs(f, line_segments.begin(), line_segments.end());
00148 writeVectorPairs(f, pose_arrows.begin(), pose_arrows.end());
00149
00150 line_segments2 = votrack(g);
00151 writeVectorPairs(f, line_segments2.begin(), line_segments2.end());
00152 }
00153
00154 template<class TrIt> void writeTrajectory(
00155 std::ostream& out, TrIt trajectory_begin, TrIt trajectory_end){
00156 using namespace vslam_utils;
00157 TrIt tr_it = trajectory_begin + 1;
00158 ImageMatchVec tmp;
00159 ImageMatchVec tmp2;
00160 tmp2.push_back(preparePoseArrow(*trajectory_begin));
00161 while(tr_it!=trajectory_end){
00162 SE3 T_c1w = (tr_it-1)->inverse();
00163 SE3 T_c2w = tr_it->inverse();
00164 tmp.push_back(make_pair(T_c1w*Vector3d::Zero(), T_c2w*Vector3d::Zero()));
00165 tmp2.push_back(preparePoseArrow(tr_it->inverse()));
00166 ++tr_it;
00167 }
00168 writeVectorPairs(out, tmp.begin(), tmp.end());
00169 writeVectorPairs(out, tmp2.begin(), tmp2.end());
00170 }
00171
00172
00173
00174
00175 template<class PtIt> void displayMatches(
00176 std::ostream& out, PtIt points_begin, PtIt points_end, const SE3& T_cw){
00177 using namespace vslam_utils;
00178 PtIt pt_it = points_begin;
00179 ImageMatchVec tmp;
00180 while(pt_it != points_end){
00181 tmp.push_back(make_pair(T_cw*Vector3d::Zero(), T_cw*(*pt_it)));
00182 ++pt_it;
00183 }
00184 writeVectorPairs(out, tmp.begin(), tmp.end());
00185 }
00186
00187 template<class PtIt> void displayScene(std::ostream& out, PtIt points_begin, PtIt points_end){
00188 writePoints(out, points_begin, points_end);
00189 }
00190 }
00191
00192
00193 #endif