debug.cpp
Go to the documentation of this file.
00001 
00033 #include <opencv/cv.h>
00034 #include <iostream>
00035 #include <string>
00036 #include <sstream>
00037 #include <fstream>
00038 #include <vector>
00039 
00040 #include "debug.h"
00041 
00042 #include "geometry_msgs/Point.h"
00043 
00044 // -------------------------------------------------------------------------
00045 
00046 void debug::saveTscene(const cv::Mat &rTo, 
00047   const std::vector<geometry_msgs::Point> &points3d,
00048   const std::string &filename)
00049 {
00050   fstream f(filename.c_str(), ios::out);
00051 
00052         f << "#VRML V2.0 utf8" << endl << endl;
00053 
00054         // axes and viewpoint
00055         f << "Viewpoint { position 0 0 -3 " << endl
00056                 << "orientation 1 0 0 -3.1415926535897931 # towards 0 0 0  tilt -180 degrees" << endl
00057                 << "fieldOfView 0.78539816339744828 }" << endl << endl;
00058   
00059   cv::Mat wTr = cv::Mat::eye(4,4,CV_64F);
00060         saveRef(f, wTr);
00061         saveRef(f, wTr * rTo);
00062         saveRectangle(f, wTr * rTo, 1.0, 1.82);
00063         savePoints(f, points3d);
00064 
00065         f.close();
00066 }
00067 
00068 // -------------------------------------------------------------------------
00069 
00070 void debug::savePoints(fstream &f, 
00071   const std::vector<geometry_msgs::Point> &wP3d)
00072 {
00073   for(unsigned int i = 0; i < wP3d.size(); ++i)
00074   {
00075     double x = wP3d[i].x;
00076     double y = wP3d[i].y;
00077     double z = wP3d[i].z;
00078     
00079     f << "Transform {" << endl
00080       << "translation " << x << " " << y << " " << z << endl
00081             << "  children[" << endl
00082                   << "    Shape{" << endl
00083                   << "      geometry Sphere { radius 0.01 }" << endl
00084                   << "    }" << endl
00085                   << "  ]" << endl
00086                   << "}" << endl << endl;
00087   }
00088 }
00089     
00090 // -------------------------------------------------------------------------
00091 
00092 void debug::saveRectangle(fstream &f, const cv::Mat &wTr, float width, 
00093   float height)
00094 {
00095   const float w2 = width / 2.f;
00096   const float h2 = height / 2.f;
00097   
00098   cv::Mat rP = (cv::Mat_<double>(4, 4) << 
00099     -w2,  w2, w2, -w2,
00100     -h2, -h2, h2,  h2,
00101       0,   0,  0,   0,
00102       1,   1,  1,   1);
00103 
00104   cv::Mat wP = wTr * rP; 
00105   
00106   double x[4], y[4], z[4];
00107   stringstream s[4];
00108   
00109   for(int i = 0; i < 4; ++i)
00110   {
00111     x[i] = wP.at<double>(0,i) / wP.at<double>(3,i);
00112     y[i] = wP.at<double>(1,i) / wP.at<double>(3,i);
00113     z[i] = wP.at<double>(2,i) / wP.at<double>(3,i);
00114     
00115     s[i] << x[i] << " " << y[i] << " " << z[i];
00116   }
00117   
00118   // lines
00119   f << "Shape {" << endl
00120                 << "geometry IndexedLineSet {" << endl
00121                 << "color Color {" << endl
00122         <<            "color [" << endl
00123                        << "1.0 1.0 1.0," << endl
00124                        << "1.0 1.0 1.0," << endl
00125                        << "1.0 1.0 1.0," << endl
00126                        << "1.0 1.0 1.0," << endl
00127                     << "]" << endl
00128                 << "}" << endl
00129                 << "coord Coordinate {" << endl
00130                     << "point [" << endl
00131                       << s[0].str() << "," << endl
00132                       << s[1].str() << "," << endl
00133                       << s[2].str() << "," << endl
00134                       << s[3].str() << "," << endl
00135                     << "]" << endl
00136                 << "}" << endl
00137                 << "coordIndex [" << endl
00138                     << "0, 1, 2, 3, 0" << endl
00139                 << "]" << endl
00140             << "}" << endl
00141         << "}" << endl << endl;
00142   
00143   /* // plane
00144   // front
00145   f << "Shape {" << endl << "geometry IndexedFaceSet {"
00146   << endl << "coord Coordinate { point [" << endl;
00147   for(short i = 0; i < 4; i++)
00148   {
00149     double x = wP.at<double>(0,i) / wP.at<double>(3,i);
00150     double y = wP.at<double>(1,i) / wP.at<double>(3,i);
00151     double z = wP.at<double>(2,i) / wP.at<double>(3,i);
00152     
00153     f << x << " " << y << " " << z << ",";
00154   }
00155   f << " ] }";
00156   f << "coordIndex [0,  1, 2, 3, 0] } }" << endl;
00157 
00158   // and back
00159   f << "Shape {" << endl << "geometry IndexedFaceSet {"
00160     << endl << "coord Coordinate { point [" << endl;
00161   for(short i = 0; i < 4; i++)
00162   {
00163     double x = wP.at<double>(0,i) / wP.at<double>(3,i);
00164     double y = wP.at<double>(1,i) / wP.at<double>(3,i);
00165     double z = wP.at<double>(2,i) / wP.at<double>(3,i);
00166     
00167     f << x << " " << y << " " << z << ",";
00168   }
00169   f << " ] }";
00170   f << "coordIndex [3, 2, 1, 0, 3] } }" << endl;
00171   */
00172   
00173 }
00174 
00175 // -------------------------------------------------------------------------
00176 
00177 void debug::saveRef(fstream &f, const cv::Mat &wTr)
00178 {
00179   const float L = 0.5; // axis length;
00180 
00181   cv::Mat rP = (cv::Mat_<double>(4, 4) << 
00182     0, L, 0, 0,
00183     0, 0, L, 0,
00184     0, 0, 0, L,
00185     1, 1, 1, 1);
00186 
00187   cv::Mat wP = wTr * rP;
00188   
00189   double ox = wP.at<double>(0,0) / wP.at<double>(3,0);
00190   double oy = wP.at<double>(1,0) / wP.at<double>(3,0);
00191   double oz = wP.at<double>(2,0) / wP.at<double>(3,0);
00192   
00193   double rx = wP.at<double>(0,1) / wP.at<double>(3,1);
00194   double ry = wP.at<double>(1,1) / wP.at<double>(3,1);
00195   double rz = wP.at<double>(2,1) / wP.at<double>(3,1);
00196   
00197   double gx = wP.at<double>(0,2) / wP.at<double>(3,2);
00198   double gy = wP.at<double>(1,2) / wP.at<double>(3,2);
00199   double gz = wP.at<double>(2,2) / wP.at<double>(3,2);
00200   
00201   double bx = wP.at<double>(0,3) / wP.at<double>(3,3);
00202   double by = wP.at<double>(1,3) / wP.at<double>(3,3);
00203   double bz = wP.at<double>(2,3) / wP.at<double>(3,3);
00204 
00205   stringstream os, rs, gs, bs;
00206   os << ox << " " << oy << " " << oz;
00207   rs << rx << " " << ry << " " << rz;
00208   gs << gx << " " << gy << " " << gz;
00209   bs << bx << " " << by << " " << bz;
00210   
00211   f << "Shape {" << endl
00212                 << "geometry IndexedLineSet {" << endl
00213                 << "color Color {" << endl
00214         <<            "color [" << endl
00215                        << "1.0 0.0 0.0," << endl
00216                         << "1.0 0.0 0.0," << endl
00217                         << "0.0 1.0 0.0," << endl
00218                         << "0.0 1.0 0.0," << endl
00219                         << "0.0 0.0 1.0," << endl
00220                         << "0.0 0.0 1.0," << endl
00221                     << "]" << endl
00222                 << "}" << endl
00223                 << "coord Coordinate {" << endl
00224                     << "point [" << endl
00225                       << os.str() << "," << endl
00226                       << rs.str() << "," << endl
00227                       << os.str() << "," << endl
00228                       << gs.str() << "," << endl
00229                       << os.str() << "," << endl
00230                       << bs.str() << "," << endl
00231                     << "]" << endl
00232                 << "}" << endl
00233                 << "coordIndex [" << endl
00234                     << "0, 1, -1," << endl
00235                     << "2, 3, -1," << endl
00236                     << "4, 5, -1," << endl
00237                 << "]" << endl
00238             << "}" << endl
00239         << "}" << endl << endl;
00240 }
00241 
00242 
00243 // -------------------------------------------------------------------------
00244 


re_vision
Author(s): Dorian Galvez-Lopez
autogenerated on Sun Jan 5 2014 11:31:00