ascii_io.cpp
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Point Cloud Library (PCL) - www.pointclouds.org
00005  *  Copyright (c) 2012-, Open Perception, Inc.
00006  *
00007  *  All rights reserved.
00008  *
00009  *  Redistribution and use in source and binary forms, with or without
00010  *  modification, are permitted provided that the following conditions
00011  *  are met:
00012  *
00013  *   * Redistributions of source code must retain the above copyright
00014  *     notice, this list of conditions and the following disclaimer.
00015  *   * Redistributions in binary form must reproduce the above
00016  *     copyright notice, this list of conditions and the following
00017  *     disclaimer in the documentation and/or other materials provided
00018  *     with the distribution.
00019  *   * Neither the name of the copyright holder(s) nor the names of its
00020  *     contributors may be used to endorse or promote products derived
00021  *     from this software without specific prior written permission.
00022  *
00023  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00024  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00025  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00026  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00027  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00028  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00029  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00030  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00032  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00033  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00034  *  POSSIBILITY OF SUCH DAMAGE.
00035  *
00036  */
00037 
00038 #include <pcl/io/ascii_io.h>
00039 #include <istream>
00040 #include <fstream>
00041 #include <boost/filesystem.hpp>
00042 #include <boost/lexical_cast.hpp>
00043 #include <boost/cstdint.hpp>
00044 
00046 pcl::ASCIIReader::ASCIIReader ()
00047 {
00048   extension_ = ".txt";
00049   sep_chars_ = ", \n\r\t";
00050   name_ = "AsciiReader";
00051 
00052   {
00053     pcl::PCLPointField f;
00054     f.datatype = pcl::PCLPointField::FLOAT32;
00055     f.count    = 1;
00056     f.name     = "x";
00057     f.offset   = 0;
00058     fields_.push_back (f);
00059   }
00060 
00061   {
00062     pcl::PCLPointField f;
00063     f.datatype = pcl::PCLPointField::FLOAT32;
00064     f.count    = 1;
00065     f.name     = "y";
00066     f.offset   = 4;
00067     fields_.push_back (f);
00068   }
00069 
00070   {
00071     pcl::PCLPointField f;
00072     f.datatype = pcl::PCLPointField::FLOAT32;
00073     f.count    = 1;
00074     f.name     = "z";
00075     f.offset   = 8;
00076     fields_.push_back (f);
00077   }
00078 }
00079 
00081 pcl::ASCIIReader::~ASCIIReader ()
00082 {
00083 }
00084 
00086 int
00087 pcl::ASCIIReader::readHeader (const std::string& file_name,
00088   pcl::PCLPointCloud2& cloud, Eigen::Vector4f& origin,
00089   Eigen::Quaternionf& orientation, int& file_version, int& data_type,
00090   unsigned int& data_idx, const int offset)
00091 {
00092         (void)offset; //offset is not used for ascii file implementation
00093         
00094   boost::filesystem::path fpath = file_name;
00095 
00096   if (!boost::filesystem::exists (fpath))
00097   {
00098     PCL_ERROR ("[%s] File %s does not exist.\n", name_.c_str (), file_name.c_str ());
00099     return (-1);
00100   }
00101   if (boost::filesystem::extension (fpath) != extension_)
00102   {
00103     PCL_ERROR ("[%s] File does not have %s extension. \n", name_.c_str(), extension_.c_str());
00104     return -1;
00105   }
00106 
00107   cloud.fields = fields_;
00108   cloud.point_step = 0;
00109   for (int i = 0; i < fields_.size (); i++) 
00110     cloud.point_step += typeSize (cloud.fields[i].datatype);
00111 
00112   std::fstream ifile (file_name.c_str (), std::fstream::in);
00113   std::string line;
00114   int total = 0;
00115   while (std::getline (ifile, line))
00116     total++;
00117 
00118   origin = Eigen::Vector4f::Zero ();
00119   orientation = Eigen::Quaternionf ();
00120   cloud.width = total;
00121   cloud.height = 1;
00122   cloud.is_dense = true;
00123   file_version = -1;
00124   data_type = 0;
00125   data_idx  = 0;
00126   return (total);
00127 }
00128 
00130 int
00131 pcl::ASCIIReader::read (
00132     const std::string& file_name,
00133     pcl::PCLPointCloud2& cloud,
00134     Eigen::Vector4f& origin,
00135     Eigen::Quaternionf& orientation, int& file_version, const int offset)
00136 {
00137 
00138   int  data_type;
00139   unsigned int data_idx;
00140   if (this->readHeader (file_name, cloud, origin, orientation, file_version, data_type, data_idx, offset) < 0) 
00141     return (-1);
00142   cloud.data.resize (cloud.height * cloud.width * cloud.point_step);
00143 
00144   std::string line;
00145   std::fstream ifile (file_name.c_str (), std::fstream::in);
00146 
00147   int total=0;
00148 
00149   uint8_t* data = &cloud.data[0];
00150   while (std::getline (ifile, line))
00151   {
00152     boost::algorithm::trim (line);
00153     if (line.find_first_not_of ("#") != 0) 
00154       continue;   //skip comment lines
00155 
00156    std::vector<std::string> tokens;
00157    boost::algorithm::split (tokens, line,boost::algorithm::is_any_of (sep_chars_), boost::algorithm::token_compress_on);
00158 
00159    if (tokens.size () != fields_.size ()) 
00160      continue;
00161 
00162    uint32_t offset = 0;
00163    try
00164    {
00165      for (int i = 0; i < fields_.size (); i++) 
00166        offset += parse (tokens[i], fields_[i], data + offset);
00167    }
00168    catch (std::exception& /*e*/)
00169    {
00170      continue;
00171    }
00172    data += offset;
00173    total++;
00174   }
00175   cloud.data.resize (total * cloud.point_step);
00176   return (cloud.width * cloud.height);
00177 }
00178 
00180 void
00181 pcl::ASCIIReader::setInputFields (const std::vector<pcl::PCLPointField>& fields)
00182 {
00183         fields_ = fields;
00184 }
00185 
00187 void
00188 pcl::ASCIIReader::setSepChars (const std::string &chars)
00189 {
00190         sep_chars_ = chars;
00191 }
00192 
00194 int
00195 pcl::ASCIIReader::parse (
00196     const std::string& token,
00197     const pcl::PCLPointField& field,
00198     uint8_t* data_target)
00199 {
00200   switch (field.datatype)
00201   {
00202     case pcl::PCLPointField::INT8:
00203     {
00204       *(reinterpret_cast<int8_t*>(data_target)) = boost::lexical_cast<int8_t> (token);
00205       return (1);
00206     }
00207     case pcl::PCLPointField::UINT8:
00208     {
00209       *(reinterpret_cast<uint8_t*>(data_target)) = boost::lexical_cast<uint8_t> (token);
00210       return 1;
00211     }
00212     case pcl::PCLPointField::INT16:
00213     {
00214       *(reinterpret_cast<int16_t*>(data_target)) = boost::lexical_cast<int16_t> (token);
00215       return 2;
00216     }
00217     case pcl::PCLPointField::UINT16:
00218     {
00219       *(reinterpret_cast<uint16_t*>(data_target)) = boost::lexical_cast<uint16_t> (token);
00220       return 2;
00221     }
00222     case pcl::PCLPointField::INT32:
00223     {
00224       *(reinterpret_cast<int32_t*>(data_target)) = boost::lexical_cast<int32_t> (token);
00225       return 4;
00226     }
00227     case pcl::PCLPointField::UINT32:
00228     {
00229       *(reinterpret_cast<uint32_t*>(data_target)) = boost::lexical_cast<uint32_t> (token);
00230       return 4;
00231     }
00232     case pcl::PCLPointField::FLOAT32:
00233     {
00234       *(reinterpret_cast<float*>(data_target)) = boost::lexical_cast<float> (token);
00235       return 4;
00236     }
00237     case pcl::PCLPointField::FLOAT64:
00238     {
00239       *(reinterpret_cast<double*>(data_target)) = boost::lexical_cast<double> (token);
00240       return 8;
00241     }
00242   }
00243   return 0;
00244 }
00245 
00247 boost::uint32_t
00248 pcl::ASCIIReader::typeSize (int type)
00249 {
00250   switch (type)
00251   {
00252     case pcl::PCLPointField::INT8:
00253       return 1;
00254     case pcl::PCLPointField::UINT8:
00255       return 1;
00256     case pcl::PCLPointField::INT16:
00257       return 2;
00258     case pcl::PCLPointField::UINT16:
00259       return 2;
00260     case pcl::PCLPointField::INT32:
00261       return 4;
00262     case pcl::PCLPointField::UINT32:
00263       return 4;
00264     case pcl::PCLPointField::FLOAT32:
00265       return 4;
00266     case pcl::PCLPointField::FLOAT64:
00267       return 8;
00268   }
00269   return (0);
00270 }
00271 


pcl
Author(s): Open Perception
autogenerated on Wed Aug 26 2015 15:22:35