utils.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2010, Willow Garage, Inc.
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above
00014  *     copyright notice, this list of conditions and the following
00015  *     disclaimer in the documentation and/or other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of the Willow Garage nor the names of its
00018  *     contributors may be used to endorse or promote products derived
00019  *     from this software without specific prior written permission.
00020  *
00021  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  *  POSSIBILITY OF SUCH DAMAGE.
00033  *********************************************************************/
00034 
00035 /*
00036   Author: Daniel Hennes
00037  */
00038 
00039 #include "inverse_dynamics/utils.h"
00040 
00041 namespace utils {
00042 
00043   struct timeval starttic;
00044   
00045   void tic() {
00046     gettimeofday(&starttic, NULL);
00047   }
00048 
00049   void toc() {
00050     struct timeval end;
00051     long mtime, seconds, useconds;
00052     gettimeofday(&end, NULL);
00053     seconds  = end.tv_sec  - starttic.tv_sec;
00054     useconds = end.tv_usec - starttic.tv_usec;
00055     mtime = ((seconds) * 1000 + useconds/1000.0) + 0.5;
00056     printf("Elapsed time: %ld milliseconds\n", mtime);
00057   }
00058 
00059   bool writeMatrix(std::string fname, Eigen::MatrixXd * matrix) {
00060     std::ofstream file;
00061     std::ostream * out;
00062     file.open(fname.c_str());
00063     if (!file) {
00064       fprintf(stderr, "Error: can't open file %s.\n", fname.c_str());
00065       return false;
00066     }
00067     out = &file;
00068     *out << *matrix << std::endl;
00069     file.close();
00070     return true;
00071   }
00072 
00073   bool readMatrix(std::string fname, Eigen::MatrixXd * matrix) {
00074     // open file
00075     std::ifstream file;
00076     std::istream * in;
00077     file.open(fname.c_str());
00078     if (!file) {
00079       fprintf(stderr, "Error: can't open file %s.\n", fname.c_str());
00080       return false;
00081     }
00082     in = &file;
00083 
00084     std::vector<double> data;
00085     std::vector<double>::iterator it;
00086     size_t cols(0);
00087     size_t rows(0);
00088     for (; *in; rows++) {
00089       std::vector<double> row;
00090 
00091       std::string line;
00092       getline(*in, line);
00093       std::istringstream ls(line);
00094 
00095       double value;
00096       while (ls >> value) {
00097         row.push_back(value);
00098       }
00099     
00100       if (0 == row.size())
00101         break;
00102       
00103       // check if column number agrees      
00104       if (!cols) {
00105         cols = row.size();
00106       } else if (cols != row.size()) {
00107         fprintf(stderr, "Error reading matrix from \"%s\", line: %zu. Expected %zu columns but got %zu.\n", fname.c_str(), rows+1, cols, row.size());
00108         return false;
00109       }
00110 
00111       // add row
00112       for (it = row.begin(); it < row.end(); it++)
00113         data.push_back(*it); 
00114     }
00115 
00116     if (0 == rows)
00117       return false;
00118 
00119     // Eigen::MatrixXd m = *matrix;
00120     matrix->resize(rows, cols);
00121     int i = 0;
00122     for (it = data.begin(); it < data.end(); it++) {
00123       (*matrix)(i / cols, i % cols) = *it;
00124       i++;
00125     }
00126     file.close();
00127     return true;
00128   }
00129 
00130 } // end namespace utils


kelsey_sandbox
Author(s): kelsey
autogenerated on Wed Nov 27 2013 11:52:04