Point Cloud Library (PCL)  1.7.0
ascii_io.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2012-, Open Perception, Inc.
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of the copyright holder(s) nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  */
37 
38 #ifndef PCL_IO_ASCII_IO_H_
39 #define PCL_IO_ASCII_IO_H_
40 
41 #include <pcl/io/file_io.h>
42 #include <pcl/PCLPointField.h>
43 #include <pcl/common/io.h>
44 
45 
46 namespace pcl
47 {
48  /** \brief Ascii Point Cloud Reader.
49  * Read any ASCII file by setting the separating characters and input point fields.
50  *
51  * \author Adam Stambler (adasta@gmail.com)
52  * \ingroup io
53  */
54  class PCL_EXPORTS ASCIIReader : public FileReader
55  {
56  public:
57  ASCIIReader ();
58  virtual ~ASCIIReader ();
59  using FileReader::read;
60 
61  /* Load only the meta information (number of points, their types, etc),
62  * and not the points themselves, from a given FILE file. Useful for fast
63  * evaluation of the underlying data structure.
64  *
65  * Returns:
66  * * < 0 (-1) on error
67  * * > 0 on success
68  * \param[in] file_name the name of the file to load
69  * \param[out] cloud the resultant point cloud dataset (only the header will be filled)
70  * \param[out] origin the sensor acquisition origin (only for > FILE_V7 - null if not present)
71  * \param[out] orientation the sensor acquisition orientation (only for > FILE_V7 - identity if not present)
72  * \param[out] file_version the FILE version of the file (either FILE_V6 or FILE_V7)
73  * \param[out] data_type the type of data (binary data=1, ascii=0, etc)
74  * \param[out] data_idx the offset of cloud data within the file
75  * \param[in] offset the offset in the file where to expect the true header to begin.
76  * One usage example for setting the offset parameter is for reading
77  * data from a TAR "archive containing multiple files: TAR files always
78  * add a 512 byte header in front of the actual file, so set the offset
79  * to the next byte after the header (e.g., 513).
80  */
81  virtual int
82  readHeader (const std::string &file_name, pcl::PCLPointCloud2 &cloud,
83  Eigen::Vector4f &origin, Eigen::Quaternionf &orientation,
84  int &file_version, int &data_type, unsigned int &data_idx, const int offset = 0) ;
85 
86 
87  /** \brief Read a point cloud data from a FILE file and store it into a pcl/PCLPointCloud2.
88  * \param[in] file_name the name of the file containing the actual PointCloud data
89  * \param[out] cloud the resultant PointCloud message read from disk
90  * \param[out] origin the sensor acquisition origin (only for > FILE_V7 - null if not present)
91  * \param[out] orientation the sensor acquisition orientation (only for > FILE_V7 - identity if not present)
92  * \param[out] file_version the FILE version of the file (either FILE_V6 or FILE_V7)
93  * \param[in] offset the offset in the file where to expect the true header to begin.
94  * One usage example for setting the offset parameter is for reading
95  * data from a TAR "archive containing multiple files: TAR files always
96  * add a 512 byte header in front of the actual file, so set the offset
97  * to the next byte after the header (e.g., 513).
98  */
99  virtual int
100  read (const std::string &file_name, pcl::PCLPointCloud2 &cloud,
101  Eigen::Vector4f &origin, Eigen::Quaternionf &orientation, int &file_version,
102  const int offset = 0);
103 
104  /** \brief Set the ascii file point fields using a list of fields.
105  * \param[in] fields is a list of point fields, in order, in the input ascii file
106  */
107  void
108  setInputFields (const std::vector<pcl::PCLPointField>& fields);
109 
110 
111  /** \brief Set the ascii file point fields using a point type.
112  * \param[in] p a point type
113  */
114  template<typename PointT>
115  void setInputFields (const PointT p = PointT ());
116 
117 
118  /** \brief Set the Separting characters for the ascii point fields 2.
119  * \param[in] chars string of separating characters
120  * Sets the separating characters for the point fields. The
121  * default separating characters are " \n\t,"
122  */
123  void
124  setSepChars (const std::string &chars);
125 
126  /** \brief Set the extension of the ascii point file type.
127  * \param[in] ext extension (example : ".txt" or ".xyz" )
128  */
129  void
130  setExtension (const std::string &ext) { extension_ = ext; }
131 
132  protected:
133  std::string sep_chars_;
134  std::string extension_;
135  std::vector<pcl::PCLPointField> fields_;
136  std::string name_;
137 
138 
139  /** \brief Parses token based on field type.
140  * \param[in] token string representation of point fields
141  * \param[in] field token point field type
142  * \param[out] data_target address that the point field data should be assigned to
143  * returns the size of the parsed point field in bytes
144  */
145  int
146  parse (const std::string& token, const pcl::PCLPointField& field, uint8_t* data_target);
147 
148  /** \brief Returns the size in bytes of a point field type.
149  * \param[in] type point field type
150  * returns the size of the type in bytes
151  */
152  uint32_t
153  typeSize (int type);
154  };
155 }
156 
157 //////////////////////////////////////////////////////////////////////////////
158 template<typename PointT> void
160 {
161  (void) p;
162 
163  pcl::getFields<PointT> (fields_);
164 
165  // Remove empty fields and adjust offset
166  int offset =0;
167  for (std::vector<pcl::PCLPointField>::iterator field_iter = fields_.begin ();
168  field_iter != fields_.end (); field_iter++)
169  {
170  if (field_iter->name == "_")
171  field_iter = fields_.erase (field_iter);
172  field_iter->offset = offset;
173  offset += typeSize (field_iter->datatype);
174  }
175 }
176 
177 #endif // PCL_IO_ASCII_IO_H_