eigen_extensions.h
Go to the documentation of this file.
00001 #ifndef EIGEN_EXTENSIONS_H
00002 #define EIGEN_EXTENSIONS_H
00003 
00004 #include <Eigen/Eigen>
00005 //#define EIGEN_YES_I_KNOW_SPARSE_MODULE_IS_NOT_STABLE_YET
00006 #include <Eigen/Sparse>
00007 //#include <boost/filesystem.hpp>
00008 #include <stdint.h>
00009 #include <fstream>
00010 #include <iostream>
00011 //#include <gzstream/gzstream.h>
00012 
00013 namespace eigen_extensions {
00014 
00015   inline void stdToEig(const std::vector<double>& std, Eigen::VectorXd* eig)
00016   {
00017     eig->resize(std.size());
00018     for(size_t i = 0; i < std.size(); ++i)
00019       eig->coeffRef(i) = std[i];
00020   }
00021   
00022   inline double stdev(const Eigen::VectorXd& vec)
00023   {
00024     double mean = vec.sum() / (double)vec.rows();
00025     double total = 0;
00026     for(int i = 0; i < vec.rows(); ++i)
00027       total += (vec.coeffRef(i) - mean) * (vec.coeffRef(i) - mean);
00028     double var = total / (double)vec.rows();
00029     return sqrt(var);
00030   }
00031 
00032   template<class S, int T, int U>
00033   void serialize(const Eigen::Matrix<S, T, U>& mat, std::ostream& strm);
00034   
00035   template<class S, int T, int U>
00036   void deserialize(std::istream& strm, Eigen::Matrix<S, T, U>* mat);
00037 
00038   template<class S, int T, int U>
00039   void serializeASCII(const Eigen::Matrix<S, T, U>& mat, std::ostream& strm);
00040   
00041   template<class S, int T, int U>
00042   void deserializeASCII(std::istream& strm, Eigen::Matrix<S, T, U>* mat);  
00043 
00044   
00045   // -- Scalar serialization
00046   //    TODO: Can you name these {de,}serialize() and still have the right
00047   //    functions get called when serializing matrices?
00048   template<class T>
00049   void serializeScalar(T val, std::ostream& strm);
00050   
00051   template<class T>
00052   void deserializeScalar(std::istream& strm, T* val);
00053 
00054   template<class T>
00055   void serializeScalarASCII(T val, std::ostream& strm);
00056 
00057   template<class T>
00058   void deserializeScalarASCII(std::istream& strm, T* val);
00059 
00060 
00061   /************************************************************
00062    * Template implementations
00063    ************************************************************/
00064   
00065   template<class S, int T, int U>
00066   void serialize(const Eigen::Matrix<S, T, U>& mat, std::ostream& strm)
00067   {
00068     int bytes = sizeof(S);
00069     int rows = mat.rows();
00070     int cols = mat.cols();
00071     strm.write((char*)&bytes, sizeof(int));
00072     strm.write((char*)&rows, sizeof(int));
00073     strm.write((char*)&cols, sizeof(int));
00074     strm.write((const char*)mat.data(), sizeof(S) * rows * cols);
00075   }
00076   
00077   template<class S, int T, int U>
00078   void deserialize(std::istream& strm, Eigen::Matrix<S, T, U>* mat)
00079   {
00080     int bytes;
00081     int rows;
00082     int cols;
00083     strm.read((char*)&bytes, sizeof(int));
00084     strm.read((char*)&rows, sizeof(int));
00085     strm.read((char*)&cols, sizeof(int));
00086     assert(bytes == sizeof(S));
00087       
00088     S *buf = (S*) malloc(sizeof(S) * rows * cols);
00089     strm.read((char*)buf, sizeof(S) * rows * cols);
00090     *mat = Eigen::Map< Eigen::Matrix<S, T, U> >(buf, rows, cols);
00091     free(buf);    
00092   }
00093 
00094   template<class S, int T, int U>
00095   void serializeASCII(const Eigen::Matrix<S, T, U>& mat, std::ostream& strm)
00096   {
00097     int old_precision = strm.precision();
00098     strm.precision(16);
00099     strm << "% " << mat.rows() << " " << mat.cols() << std::endl;
00100     strm << mat << std::endl;
00101     strm.precision(old_precision);
00102   }
00103       
00104   template<class S, int T, int U>
00105   void deserializeASCII(std::istream& strm, Eigen::Matrix<S, T, U>* mat)
00106   {
00107     // -- Read the header.
00108     std::string line;
00109     while(line.length() == 0) getline(strm, line);
00110     assert(line[0] == '%');
00111     std::istringstream iss(line.substr(1));
00112     int rows;
00113     int cols;
00114     iss >> rows;
00115     iss >> cols;
00116     // -- Read in the data.
00117     *mat = Eigen::Matrix<S, T, U>(rows, cols);
00118     for(int y = 0; y < rows; ++y) {
00119       getline(strm, line);
00120       std::istringstream iss(line);
00121       for(int x = 0; x < cols; ++x) {
00122         iss >> mat->coeffRef(y, x);
00123       }
00124     }
00125   }
00126 
00127   template<class T>
00128   void serializeScalar(T val, std::ostream& strm)
00129   {
00130     strm.write((char*)&val, sizeof(T));
00131   }
00132   
00133   template<class T>
00134   void deserializeScalar(std::istream& strm, T* val)
00135   {
00136     strm.read((char*)val, sizeof(T));
00137   }
00138 
00139   template<class T>
00140   void serializeScalarASCII(T val, std::ostream& strm)
00141   {
00142           int old_precision = strm.precision();
00143           strm.precision(16);
00144           strm << "% " << val << std::endl;
00145           strm.precision(old_precision);
00146   }
00147 
00148   template<class T>
00149   void deserializeScalarASCII(std::istream& strm, T* val)
00150   {
00151           std::string line;
00152           while(line.length() == 0) getline(strm, line);
00153           assert(line[0] == '%');
00154           std::istringstream iss(line.substr(1));
00155           iss >> *val;
00156   }
00157   
00158 }
00159 
00160 #endif // EIGEN_EXTENSIONS_H


rtabmap
Author(s): Mathieu Labbe
autogenerated on Thu Jun 6 2019 21:59:19