Point Cloud Library (PCL)  1.14.1-dev
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 #pragma once
39 
40 #include <pcl/pcl_macros.h>
41 #include <pcl/io/file_io.h>
42 #include <pcl/PCLPointField.h>
43 #include <pcl/common/io.h>
44 #include <pcl/common/utils.h> // pcl::utils::ignore
45 
46 
47 namespace pcl
48 {
49  /** \brief Ascii Point Cloud Reader.
50  * Read any ASCII file by setting the separating characters and input point fields.
51  *
52  * \author Adam Stambler (adasta@gmail.com)
53  * \ingroup io
54  */
56  {
57  public:
59  ~ASCIIReader () override;
60  using FileReader::read;
61 
62  /* Load only the meta information (number of points, their types, etc),
63  * and not the points themselves, from a given FILE file. Useful for fast
64  * evaluation of the underlying data structure.
65  *
66  * Returns:
67  * * < 0 (-1) on error
68  * * > 0 on success
69  * \param[in] file_name the name of the file to load
70  * \param[out] cloud the resultant point cloud dataset (only the header will be filled)
71  * \param[out] origin the sensor acquisition origin (only for > FILE_V7 - null if not present)
72  * \param[out] orientation the sensor acquisition orientation (only for > FILE_V7 - identity if not present)
73  * \param[out] file_version the FILE version of the file (either FILE_V6 or FILE_V7)
74  * \param[out] data_type the type of data (binary data=1, ascii=0, etc)
75  * \param[out] data_idx the offset of cloud data within the file
76  * \param[in] offset the offset in the file where to expect the true header to begin.
77  * One usage example for setting the offset parameter is for reading
78  * data from a TAR "archive containing multiple files: TAR files always
79  * add a 512 byte header in front of the actual file, so set the offset
80  * to the next byte after the header (e.g., 513).
81  */
82  int
83  readHeader (const std::string &file_name, pcl::PCLPointCloud2 &cloud,
84  Eigen::Vector4f &origin, Eigen::Quaternionf &orientation,
85  int &file_version, int &data_type, unsigned int &data_idx, const int offset = 0) override ;
86 
87 
88  /** \brief Read a point cloud data from a FILE file and store it into a pcl/PCLPointCloud2.
89  * \param[in] file_name the name of the file containing the actual PointCloud data
90  * \param[out] cloud the resultant PointCloud message read from disk
91  * \param[out] origin the sensor acquisition origin (only for > FILE_V7 - null if not present)
92  * \param[out] orientation the sensor acquisition orientation (only for > FILE_V7 - identity if not present)
93  * \param[out] file_version the FILE version of the file (either FILE_V6 or FILE_V7)
94  * \param[in] offset the offset in the file where to expect the true header to begin.
95  * One usage example for setting the offset parameter is for reading
96  * data from a TAR "archive containing multiple files: TAR files always
97  * add a 512 byte header in front of the actual file, so set the offset
98  * to the next byte after the header (e.g., 513).
99  */
100  int
101  read (const std::string &file_name, pcl::PCLPointCloud2 &cloud,
102  Eigen::Vector4f &origin, Eigen::Quaternionf &orientation, int &file_version,
103  const int offset = 0) override;
104 
105  /** \brief Set the ascii file point fields.
106  */
107  template<typename PointT>
108  void setInputFields ();
109 
110  /** \brief Set the ascii file point fields using a list of fields.
111  * \param[in] fields is a list of point fields, in order, in the input ascii file
112  */
113  void
114  setInputFields (const std::vector<pcl::PCLPointField>& fields);
115 
116  /** \brief Set the Separating characters for the ascii point fields 2.
117  * \param[in] chars string of separating characters
118  * Sets the separating characters for the point fields. The
119  * default separating characters are " \n\t,"
120  */
121  void
122  setSepChars (const std::string &chars);
123 
124  /** \brief Set the extension of the ascii point file type.
125  * \param[in] ext extension (example : ".txt" or ".xyz" )
126  */
127  void
128  setExtension (const std::string &ext) { extension_ = ext; }
129 
130  protected:
131  std::string sep_chars_{", \n\r\t"};
132  std::string extension_{".txt"};
133  std::vector<pcl::PCLPointField> fields_;
134  std::string name_{"AsciiReader"};
135 
136 
137  /** \brief Parses token based on field type.
138  * \param[in] token string representation of point fields
139  * \param[in] field token point field type
140  * \param[out] data_target address that the point field data should be assigned to
141  * returns the size of the parsed point field in bytes
142  */
143  int
144  parse (const std::string& token, const pcl::PCLPointField& field, std::uint8_t* data_target);
145 
146  /** \brief Returns the size in bytes of a point field type.
147  * \param[in] type point field type
148  * returns the size of the type in bytes
149  */
150  std::uint32_t
151  typeSize (int type);
152  };
153 }
154 
155 #include <pcl/io/impl/ascii_io.hpp>
Ascii Point Cloud Reader.
Definition: ascii_io.h:56
int parse(const std::string &token, const pcl::PCLPointField &field, std::uint8_t *data_target)
Parses token based on field type.
int readHeader(const std::string &file_name, pcl::PCLPointCloud2 &cloud, Eigen::Vector4f &origin, Eigen::Quaternionf &orientation, int &file_version, int &data_type, unsigned int &data_idx, const int offset=0) override
Read a point cloud data header from a FILE file.
void setInputFields(const std::vector< pcl::PCLPointField > &fields)
Set the ascii file point fields using a list of fields.
std::vector< pcl::PCLPointField > fields_
Definition: ascii_io.h:133
int read(const std::string &file_name, pcl::PCLPointCloud2 &cloud, Eigen::Vector4f &origin, Eigen::Quaternionf &orientation, int &file_version, const int offset=0) override
Read a point cloud data from a FILE file and store it into a pcl/PCLPointCloud2.
void setExtension(const std::string &ext)
Set the extension of the ascii point file type.
Definition: ascii_io.h:128
~ASCIIReader() override
void setSepChars(const std::string &chars)
Set the Separating characters for the ascii point fields 2.
std::uint32_t typeSize(int type)
Returns the size in bytes of a point field type.
Point Cloud Data (FILE) file format reader interface.
Definition: file_io.h:57
virtual int read(const std::string &file_name, pcl::PCLPointCloud2 &cloud, Eigen::Vector4f &origin, Eigen::Quaternionf &orientation, int &file_version, const int offset=0)=0
Read a point cloud data from a FILE file and store it into a pcl/PCLPointCloud2.
Defines all the PCL and non-PCL macros used.
#define PCL_EXPORTS
Definition: pcl_macros.h:323