Point Cloud Library (PCL)  1.14.1-dev
obj_io.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2010, Willow Garage, Inc.
5  * Copyright (c) 2013, Open Perception, Inc.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/or other materials provided
17  * with the distribution.
18  * * Neither the name of the copyright holder(s) nor the names of its
19  * contributors may be used to endorse or promote products derived
20  * from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *
35  */
36 
37 #pragma once
38 
39 #include <pcl/memory.h>
40 #include <pcl/TextureMesh.h>
41 #include <pcl/io/file_io.h>
42 
43 namespace pcl
44 {
45  struct PolygonMesh;
46 
48  {
49  public:
50  /** \brief empty constructor */
52 
53  /** \brief empty destructor */
54  virtual ~MTLReader() = default;
55 
56  /** \brief Read a MTL file given its full path.
57  * \param[in] filename full path to MTL file
58  * \return 0 on success < 0 else.
59  */
60  int
61  read (const std::string& filename);
62 
63  /** \brief Read a MTL file given an OBJ file full path and the MTL file name.
64  * \param[in] obj_file_name full path to OBJ file
65  * \param[in] mtl_file_name MTL file name
66  * \return 0 on success < 0 else.
67  */
68  int
69  read (const std::string& obj_file_name, const std::string& mtl_file_name);
70 
71  std::vector<pcl::TexMaterial>::const_iterator
72  getMaterial (const std::string& material_name) const;
73 
74  /// materials array
75  std::vector<pcl::TexMaterial> materials_;
76 
77  private:
78  /// converts CIE XYZ to RGB
79  inline void
80  cie2rgb (const Eigen::Vector3f& xyz, pcl::TexMaterial::RGB& rgb) const;
81  /// fill a pcl::TexMaterial::RGB from a split line containing CIE x y z values
82  int
83  fillRGBfromXYZ (const std::vector<std::string>& split_line, pcl::TexMaterial::RGB& rgb);
84  /// fill a pcl::TexMaterial::RGB from a split line containing r g b values
85  int
86  fillRGBfromRGB (const std::vector<std::string>& split_line, pcl::TexMaterial::RGB& rgb);
87  /// matrix to convert CIE to RGB
88  Eigen::Matrix3f xyz_to_rgb_matrix_;
89 
91  };
92 
94  {
95  public:
96  /** \brief empty constructor */
97  OBJReader() = default;
98  /** \brief empty destructor */
99  ~OBJReader() override = default;
100  /** \brief Read a point cloud data header from a FILE file.
101  *
102  * Load only the meta information (number of points, their types, etc),
103  * and not the points themselves, from a given FILE file. Useful for fast
104  * evaluation of the underlying data structure.
105  *
106  * \param[in] file_name the name of the file containing the actual PointCloud data
107  * \param[out] cloud the resultant PointCloud message read from disk
108  * \param[out] origin the sensor acquisition origin always null
109  * \param[out] orientation the sensor acquisition orientation always identity
110  * \param[out] file_version always 0
111  * \param data_type
112  * \param data_idx
113  * \param[in] offset the offset in the file where to expect the true header to begin.
114  * One usage example for setting the offset parameter is for reading
115  * data from a TAR "archive containing multiple files: TAR files always
116  * add a 512 byte header in front of the actual file, so set the offset
117  * to the next byte after the header (e.g., 513).
118  *
119  * \return 0 on success.
120  */
121  int
122  readHeader (const std::string &file_name, pcl::PCLPointCloud2 &cloud,
123  Eigen::Vector4f &origin, Eigen::Quaternionf &orientation,
124  int &file_version, int &data_type, unsigned int &data_idx,
125  const int offset) override;
126 
127  /** \brief Read a point cloud data from a FILE file and store it into a
128  * pcl/PCLPointCloud2.
129  * \param[in] file_name the name of the file containing the actual PointCloud data
130  * \param[out] cloud the resultant PointCloud message read from disk
131  * \param[out] origin the sensor acquisition origin always null
132  * \param[out] orientation the sensor acquisition orientation always identity
133  * \param[out] file_version always 0
134  * \param[in] offset the offset in the file where to expect the true header to begin.
135  * One usage example for setting the offset parameter is for reading
136  * data from a TAR "archive containing multiple files: TAR files always
137  * add a 512 byte header in front of the actual file, so set the offset
138  * to the next byte after the header (e.g., 513).
139  *
140  * \return 0 on success.
141  */
142  int
143  read (const std::string &file_name, pcl::PCLPointCloud2 &cloud,
144  Eigen::Vector4f &origin, Eigen::Quaternionf &orientation,
145  int &file_version, const int offset = 0) override;
146 
147 
148  /** \brief Read a point cloud data from a FILE file and store it into a
149  * pcl/PCLPointCloud2.
150  * \param[in] file_name the name of the file containing the actual PointCloud data
151  * \param[out] cloud the resultant PointCloud message read from disk
152  * \param[in] offset the offset in the file where to expect the true header to begin.
153  * One usage example for setting the offset parameter is for reading
154  * data from a TAR "archive containing multiple files: TAR files always
155  * add a 512 byte header in front of the actual file, so set the offset
156  * to the next byte after the header (e.g., 513).
157  *
158  * \return 0 on success.
159  */
160  int
161  read (const std::string &file_name, pcl::PCLPointCloud2 &cloud, const int offset = 0);
162 
163  /** \brief Read a point cloud data from a FILE file and store it into a
164  * pcl/TextureMesh.
165  * \param[in] file_name the name of the file containing data
166  * \param[out] mesh the resultant TextureMesh read from disk
167  * \param[out] origin the sensor origin always null
168  * \param[out] orientation the sensor orientation always identity
169  * \param[out] file_version always 0
170  * \param[in] offset the offset in the file where to expect the true
171  * header to begin.
172  *
173  * \return 0 on success.
174  */
175  int
176  read (const std::string &file_name, pcl::TextureMesh &mesh,
177  Eigen::Vector4f &origin, Eigen::Quaternionf &orientation,
178  int &file_version, const int offset = 0);
179 
180  /** \brief Read a point cloud data from a FILE file and store it into a
181  * pcl/TextureMesh.
182  * \param[in] file_name the name of the file containing data
183  * \param[out] mesh the resultant TextureMesh read from disk
184  * \param[in] offset the offset in the file where to expect the true
185  * header to begin.
186  *
187  * \return 0 on success.
188  */
189  int
190  read (const std::string &file_name, pcl::TextureMesh &mesh, const int offset = 0);
191 
192  /** \brief Read a point cloud data from a FILE file and store it into a
193  * pcl/PolygonMesh.
194  * \param[in] file_name the name of the file containing data
195  * \param[out] mesh the resultant PolygonMesh read from disk
196  * \param[out] origin the sensor origin always null
197  * \param[out] orientation the sensor orientation always identity
198  * \param[out] file_version always 0
199  * \param[in] offset the offset in the file where to expect the true
200  * header to begin.
201  *
202  * \return 0 on success.
203  */
204  int
205  read (const std::string &file_name, pcl::PolygonMesh &mesh,
206  Eigen::Vector4f &origin, Eigen::Quaternionf &orientation,
207  int &file_version, const int offset = 0);
208 
209  /** \brief Read a point cloud data from a FILE file and store it into a
210  * pcl/PolygonMesh.
211  * \param[in] file_name the name of the file containing data
212  * \param[out] mesh the resultant PolygonMesh read from disk
213  * \param[in] offset the offset in the file where to expect the true
214  * header to begin.
215  *
216  * \return 0 on success.
217  */
218  int
219  read (const std::string &file_name, pcl::PolygonMesh &mesh, const int offset = 0);
220 
221  /** \brief Read a point cloud data from any FILE file, and convert it to the given
222  * template format.
223  * \param[in] file_name the name of the file containing the actual PointCloud data
224  * \param[out] cloud the resultant PointCloud message read from disk
225  * \param[in] offset the offset in the file where to expect the true header to begin.
226  * One usage example for setting the offset parameter is for reading
227  * data from a TAR "archive containing multiple files: TAR files always
228  * add a 512 byte header in front of the actual file, so set the offset
229  * to the next byte after the header (e.g., 513).
230  */
231  template<typename PointT> inline int
232  read (const std::string &file_name, pcl::PointCloud<PointT> &cloud,
233  const int offset =0)
234  {
235  pcl::PCLPointCloud2 blob;
236  int file_version;
237  int res = read (file_name, blob, cloud.sensor_origin_, cloud.sensor_orientation_,
238  file_version, offset);
239  if (res < 0)
240  return (res);
241 
242  pcl::fromPCLPointCloud2 (blob, cloud);
243  return (0);
244  }
245 
246  private:
247  /// Usually OBJ files come MTL files where texture materials are stored
248  std::vector<pcl::MTLReader> companions_;
249  };
250 
251  namespace io
252  {
253  /** \brief Load any OBJ file into a templated PointCloud type.
254  * \param[in] file_name the name of the file to load
255  * \param[out] cloud the resultant templated point cloud
256  * \param[out] origin the sensor acquisition origin, null
257  * \param[out] orientation the sensor acquisition orientation, identity
258  * \ingroup io
259  */
260  inline int
261  loadOBJFile (const std::string &file_name, pcl::PCLPointCloud2 &cloud,
262  Eigen::Vector4f &origin, Eigen::Quaternionf &orientation)
263  {
264  pcl::OBJReader p;
265  int obj_version;
266  return (p.read (file_name, cloud, origin, orientation, obj_version));
267  }
268 
269  /** \brief Load an OBJ file into a PCLPointCloud2 blob type.
270  * \param[in] file_name the name of the file to load
271  * \param[out] cloud the resultant templated point cloud
272  * \return 0 on success < 0 on error
273  *
274  * \ingroup io
275  */
276  inline int
277  loadOBJFile (const std::string &file_name, pcl::PCLPointCloud2 &cloud)
278  {
279  pcl::OBJReader p;
280  return (p.read (file_name, cloud));
281  }
282 
283  /** \brief Load any OBJ file into a templated PointCloud type
284  * \param[in] file_name the name of the file to load
285  * \param[out] cloud the resultant templated point cloud
286  * \ingroup io
287  */
288  template<typename PointT> inline int
289  loadOBJFile (const std::string &file_name, pcl::PointCloud<PointT> &cloud)
290  {
291  pcl::OBJReader p;
292  return (p.read (file_name, cloud));
293  }
294 
295  /** \brief Load any OBJ file into a PolygonMesh type.
296  * \param[in] file_name the name of the file to load
297  * \param[out] mesh the resultant mesh
298  * \return 0 on success < 0 on error
299  *
300  * \ingroup io
301  */
302  inline int
303  loadOBJFile (const std::string &file_name, pcl::PolygonMesh &mesh)
304  {
305  pcl::OBJReader p;
306  return (p.read (file_name, mesh));
307  }
308 
309  /** \brief Load any OBJ file into a TextureMesh type.
310  * \param[in] file_name the name of the file to load
311  * \param[out] mesh the resultant mesh
312  * \return 0 on success < 0 on error
313  *
314  * \ingroup io
315  */
316  inline int
317  loadOBJFile (const std::string &file_name, pcl::TextureMesh &mesh)
318  {
319  pcl::OBJReader p;
320  return (p.read (file_name, mesh));
321  }
322 
323  /** \brief Saves a TextureMesh in ascii OBJ format.
324  * \param[in] file_name the name of the file to write to disk
325  * \param[in] tex_mesh the texture mesh to save
326  * \param[in] precision the output ASCII precision
327  * \return 0 on success, else a negative number
328  * \ingroup io
329  */
330  PCL_EXPORTS int
331  saveOBJFile (const std::string &file_name,
332  const pcl::TextureMesh &tex_mesh,
333  unsigned precision = 5);
334 
335  /** \brief Saves a PolygonMesh in ascii OBJ format.
336  * \param[in] file_name the name of the file to write to disk
337  * \param[in] mesh the polygonal mesh to save
338  * \param[in] precision the output ASCII precision default 5
339  * \ingroup io
340  */
341  PCL_EXPORTS int
342  saveOBJFile (const std::string &file_name,
343  const pcl::PolygonMesh &mesh,
344  unsigned precision = 5);
345 
346  }
347 }
Point Cloud Data (FILE) file format reader interface.
Definition: file_io.h:57
virtual ~MTLReader()=default
empty destructor
int read(const std::string &filename)
Read a MTL file given its full path.
std::vector< pcl::TexMaterial >::const_iterator getMaterial(const std::string &material_name) const
MTLReader()
empty constructor
std::vector< pcl::TexMaterial > materials_
materials array
Definition: obj_io.h:75
int read(const std::string &obj_file_name, const std::string &mtl_file_name)
Read a MTL file given an OBJ file full path and the MTL file name.
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.
~OBJReader() override=default
empty destructor
int read(const std::string &file_name, pcl::TextureMesh &mesh, Eigen::Vector4f &origin, Eigen::Quaternionf &orientation, int &file_version, const int offset=0)
Read a point cloud data from a FILE file and store it into a pcl/TextureMesh.
int read(const std::string &file_name, pcl::PolygonMesh &mesh, Eigen::Vector4f &origin, Eigen::Quaternionf &orientation, int &file_version, const int offset=0)
Read a point cloud data from a FILE file and store it into a pcl/PolygonMesh.
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) override
Read a point cloud data header from a FILE file.
int read(const std::string &file_name, pcl::PCLPointCloud2 &cloud, const int offset=0)
Read a point cloud data from a FILE file and store it into a pcl/PCLPointCloud2.
int read(const std::string &file_name, pcl::PolygonMesh &mesh, const int offset=0)
Read a point cloud data from a FILE file and store it into a pcl/PolygonMesh.
int read(const std::string &file_name, pcl::PointCloud< PointT > &cloud, const int offset=0)
Read a point cloud data from any FILE file, and convert it to the given template format.
Definition: obj_io.h:232
OBJReader()=default
empty constructor
int read(const std::string &file_name, pcl::TextureMesh &mesh, const int offset=0)
Read a point cloud data from a FILE file and store it into a pcl/TextureMesh.
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition: point_cloud.h:173
Eigen::Quaternionf sensor_orientation_
Sensor acquisition pose (rotation).
Definition: point_cloud.h:408
Eigen::Vector4f sensor_origin_
Sensor acquisition pose (origin/translation).
Definition: point_cloud.h:406
#define PCL_MAKE_ALIGNED_OPERATOR_NEW
Macro to signal a class requires a custom allocator.
Definition: memory.h:86
PCL_EXPORTS int saveOBJFile(const std::string &file_name, const pcl::TextureMesh &tex_mesh, unsigned precision=5)
Saves a TextureMesh in ascii OBJ format.
int loadOBJFile(const std::string &file_name, pcl::PCLPointCloud2 &cloud, Eigen::Vector4f &origin, Eigen::Quaternionf &orientation)
Load any OBJ file into a templated PointCloud type.
Definition: obj_io.h:261
Defines functions, macros and traits for allocating and using memory.
void read(std::istream &stream, Type &value)
Function for reading data from a stream.
Definition: region_xy.h:46
void fromPCLPointCloud2(const pcl::PCLPointCloud2 &msg, pcl::PointCloud< PointT > &cloud, const MsgFieldMap &field_map, const std::uint8_t *msg_data)
Convert a PCLPointCloud2 binary data blob into a pcl::PointCloud<T> object using a field_map.
Definition: conversions.h:229
#define PCL_EXPORTS
Definition: pcl_macros.h:325