Point Cloud Library (PCL)  1.9.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/pcl_macros.h>
40 #include <pcl/TextureMesh.h>
41 #include <pcl/PolygonMesh.h>
42 #include <pcl/io/file_io.h>
43 
44 namespace pcl
45 {
47  {
48  public:
49  /** \brief empty constructor */
50  MTLReader ();
51 
52  /** \brief empty destructor */
53  virtual ~MTLReader() {}
54 
55  /** \brief Read a MTL file given its full path.
56  * \param[in] filename full path to MTL file
57  * \return 0 on success < 0 else.
58  */
59  int
60  read (const std::string& filename);
61 
62  /** \brief Read a MTL file given an OBJ file full path and the MTL file name.
63  * \param[in] obj_file_name full path to OBJ file
64  * \param[in] mtl_file_name MTL file name
65  * \return 0 on success < 0 else.
66  */
67  int
68  read (const std::string& obj_file_name, const std::string& mtl_file_name);
69 
70  std::vector<pcl::TexMaterial>::const_iterator
71  getMaterial (const std::string& material_name) const;
72 
73  /// materials array
74  std::vector<pcl::TexMaterial> materials_;
75 
76  private:
77  /// converts CIE XYZ to RGB
78  inline void
79  cie2rgb (const Eigen::Vector3f& xyz, pcl::TexMaterial::RGB& rgb) const;
80  /// fill a pcl::TexMaterial::RGB from a split line containing CIE x y z values
81  int
82  fillRGBfromXYZ (const std::vector<std::string>& split_line, pcl::TexMaterial::RGB& rgb);
83  /// fill a pcl::TexMaterial::RGB from a split line containing r g b values
84  int
85  fillRGBfromRGB (const std::vector<std::string>& split_line, pcl::TexMaterial::RGB& rgb);
86  /// matrix to convert CIE to RGB
87  Eigen::Matrix3f xyz_to_rgb_matrix_;
88 
90  };
91 
93  {
94  public:
95  /** \brief empty constructor */
96  OBJReader() {}
97  /** \brief empty destructor */
99  /** \brief Read a point cloud data header from a FILE file.
100  *
101  * Load only the meta information (number of points, their types, etc),
102  * and not the points themselves, from a given FILE file. Useful for fast
103  * evaluation of the underlying data structure.
104  *
105  * \param[in] file_name the name of the file containing the actual PointCloud data
106  * \param[out] cloud the resultant PointCloud message read from disk
107  * \param[out] origin the sensor acquisition origin always null
108  * \param[out] orientation the sensor acquisition orientation always identity
109  * \param[out] file_version always 0
110  * \param data_type
111  * \param data_idx
112  * \param[in] offset the offset in the file where to expect the true header to begin.
113  * One usage example for setting the offset parameter is for reading
114  * data from a TAR "archive containing multiple files: TAR files always
115  * add a 512 byte header in front of the actual file, so set the offset
116  * to the next byte after the header (e.g., 513).
117  *
118  * \return 0 on success.
119  */
120  int
121  readHeader (const std::string &file_name, pcl::PCLPointCloud2 &cloud,
122  Eigen::Vector4f &origin, Eigen::Quaternionf &orientation,
123  int &file_version, int &data_type, unsigned int &data_idx,
124  const int offset) override;
125 
126  /** \brief Read a point cloud data from a FILE file and store it into a
127  * pcl/PCLPointCloud2.
128  * \param[in] file_name the name of the file containing the actual PointCloud data
129  * \param[out] cloud the resultant PointCloud message read from disk
130  * \param[out] origin the sensor acquisition origin always null
131  * \param[out] orientation the sensor acquisition orientation always identity
132  * \param[out] file_version always 0
133  * \param[in] offset the offset in the file where to expect the true header to begin.
134  * One usage example for setting the offset parameter is for reading
135  * data from a TAR "archive containing multiple files: TAR files always
136  * add a 512 byte header in front of the actual file, so set the offset
137  * to the next byte after the header (e.g., 513).
138  *
139  * \return 0 on success.
140  */
141  int
142  read (const std::string &file_name, pcl::PCLPointCloud2 &cloud,
143  Eigen::Vector4f &origin, Eigen::Quaternionf &orientation,
144  int &file_version, const int offset = 0) override;
145 
146 
147  /** \brief Read a point cloud data from a FILE file and store it into a
148  * pcl/PCLPointCloud2.
149  * \param[in] file_name the name of the file containing the actual PointCloud data
150  * \param[out] cloud the resultant PointCloud message read from disk
151  * \param[in] offset the offset in the file where to expect the true header to begin.
152  * One usage example for setting the offset parameter is for reading
153  * data from a TAR "archive containing multiple files: TAR files always
154  * add a 512 byte header in front of the actual file, so set the offset
155  * to the next byte after the header (e.g., 513).
156  *
157  * \return 0 on success.
158  */
159  int
160  read (const std::string &file_name, pcl::PCLPointCloud2 &cloud, const int offset = 0);
161 
162  /** \brief Read a point cloud data from a FILE file and store it into a
163  * pcl/TextureMesh.
164  * \param[in] file_name the name of the file containing data
165  * \param[out] mesh the resultant TextureMesh read from disk
166  * \param[out] origin the sensor origin always null
167  * \param[out] orientation the sensor orientation always identity
168  * \param[out] file_version always 0
169  * \param[in] offset the offset in the file where to expect the true
170  * header to begin.
171  *
172  * \return 0 on success.
173  */
174  int
175  read (const std::string &file_name, pcl::TextureMesh &mesh,
176  Eigen::Vector4f &origin, Eigen::Quaternionf &orientation,
177  int &file_version, const int offset = 0);
178 
179  /** \brief Read a point cloud data from a FILE file and store it into a
180  * pcl/TextureMesh.
181  * \param[in] file_name the name of the file containing data
182  * \param[out] mesh the resultant TextureMesh read from disk
183  * \param[in] offset the offset in the file where to expect the true
184  * header to begin.
185  *
186  * \return 0 on success.
187  */
188  int
189  read (const std::string &file_name, pcl::TextureMesh &mesh, const int offset = 0);
190 
191  /** \brief Read a point cloud data from a FILE file and store it into a
192  * pcl/PolygonMesh.
193  * \param[in] file_name the name of the file containing data
194  * \param[out] mesh the resultant PolygonMesh read from disk
195  * \param[out] origin the sensor origin always null
196  * \param[out] orientation the sensor orientation always identity
197  * \param[out] file_version always 0
198  * \param[in] offset the offset in the file where to expect the true
199  * header to begin.
200  *
201  * \return 0 on success.
202  */
203  int
204  read (const std::string &file_name, pcl::PolygonMesh &mesh,
205  Eigen::Vector4f &origin, Eigen::Quaternionf &orientation,
206  int &file_version, const int offset = 0);
207 
208  /** \brief Read a point cloud data from a FILE file and store it into a
209  * pcl/PolygonMesh.
210  * \param[in] file_name the name of the file containing data
211  * \param[out] mesh the resultant PolygonMesh read from disk
212  * \param[in] offset the offset in the file where to expect the true
213  * header to begin.
214  *
215  * \return 0 on success.
216  */
217  int
218  read (const std::string &file_name, pcl::PolygonMesh &mesh, const int offset = 0);
219 
220  /** \brief Read a point cloud data from any FILE file, and convert it to the given
221  * template format.
222  * \param[in] file_name the name of the file containing the actual PointCloud data
223  * \param[out] cloud the resultant PointCloud message read from disk
224  * \param[in] offset the offset in the file where to expect the true header to begin.
225  * One usage example for setting the offset parameter is for reading
226  * data from a TAR "archive containing multiple files: TAR files always
227  * add a 512 byte header in front of the actual file, so set the offset
228  * to the next byte after the header (e.g., 513).
229  */
230  template<typename PointT> inline int
231  read (const std::string &file_name, pcl::PointCloud<PointT> &cloud,
232  const int offset =0)
233  {
234  pcl::PCLPointCloud2 blob;
235  int file_version;
236  int res = read (file_name, blob, cloud.sensor_origin_, cloud.sensor_orientation_,
237  file_version, offset);
238  if (res < 0)
239  return (res);
240 
241  pcl::fromPCLPointCloud2 (blob, cloud);
242  return (0);
243  }
244 
245  private:
246  /// Usually OBJ files come MTL files where texture materials are stored
247  std::vector<pcl::MTLReader> companions_;
248  };
249 
250  namespace io
251  {
252  /** \brief Load any OBJ file into a templated PointCloud type.
253  * \param[in] file_name the name of the file to load
254  * \param[out] cloud the resultant templated point cloud
255  * \param[out] origin the sensor acquisition origin, null
256  * \param[out] orientation the sensor acquisition orientation, identity
257  * \ingroup io
258  */
259  inline int
260  loadOBJFile (const std::string &file_name, pcl::PCLPointCloud2 &cloud,
261  Eigen::Vector4f &origin, Eigen::Quaternionf &orientation)
262  {
263  pcl::OBJReader p;
264  int obj_version;
265  return (p.read (file_name, cloud, origin, orientation, obj_version));
266  }
267 
268  /** \brief Load an OBJ file into a PCLPointCloud2 blob type.
269  * \param[in] file_name the name of the file to load
270  * \param[out] cloud the resultant templated point cloud
271  * \return 0 on success < 0 on error
272  *
273  * \ingroup io
274  */
275  inline int
276  loadOBJFile (const std::string &file_name, pcl::PCLPointCloud2 &cloud)
277  {
278  pcl::OBJReader p;
279  return (p.read (file_name, cloud));
280  }
281 
282  /** \brief Load any OBJ file into a templated PointCloud type
283  * \param[in] file_name the name of the file to load
284  * \param[out] cloud the resultant templated point cloud
285  * \ingroup io
286  */
287  template<typename PointT> inline int
288  loadOBJFile (const std::string &file_name, pcl::PointCloud<PointT> &cloud)
289  {
290  pcl::OBJReader p;
291  return (p.read (file_name, cloud));
292  }
293 
294  /** \brief Load any OBJ file into a PolygonMesh type.
295  * \param[in] file_name the name of the file to load
296  * \param[out] mesh the resultant mesh
297  * \return 0 on success < 0 on error
298  *
299  * \ingroup io
300  */
301  inline int
302  loadOBJFile (const std::string &file_name, pcl::PolygonMesh &mesh)
303  {
304  pcl::OBJReader p;
305  return (p.read (file_name, mesh));
306  }
307 
308  /** \brief Load any OBJ file into a TextureMesh type.
309  * \param[in] file_name the name of the file to load
310  * \param[out] mesh the resultant mesh
311  * \return 0 on success < 0 on error
312  *
313  * \ingroup io
314  */
315  inline int
316  loadOBJFile (const std::string &file_name, pcl::TextureMesh &mesh)
317  {
318  pcl::OBJReader p;
319  return (p.read (file_name, mesh));
320  }
321 
322  /** \brief Saves a TextureMesh in ascii OBJ format.
323  * \param[in] file_name the name of the file to write to disk
324  * \param[in] tex_mesh the texture mesh to save
325  * \param[in] precision the output ASCII precision
326  * \ingroup io
327  */
328  PCL_EXPORTS int
329  saveOBJFile (const std::string &file_name,
330  const pcl::TextureMesh &tex_mesh,
331  unsigned precision = 5);
332 
333  /** \brief Saves a PolygonMesh in ascii PLY format.
334  * \param[in] file_name the name of the file to write to disk
335  * \param[in] mesh the polygonal mesh to save
336  * \param[in] precision the output ASCII precision default 5
337  * \ingroup io
338  */
339  PCL_EXPORTS int
340  saveOBJFile (const std::string &file_name,
341  const pcl::PolygonMesh &mesh,
342  unsigned precision = 5);
343 
344  }
345 }
void fromPCLPointCloud2(const pcl::PCLPointCloud2 &msg, pcl::PointCloud< PointT > &cloud, const MsgFieldMap &field_map)
Convert a PCLPointCloud2 binary data blob into a pcl::PointCloud<T> object using a field_map...
Definition: conversions.h:168
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:45
virtual ~MTLReader()
empty destructor
Definition: obj_io.h:53
void read(std::istream &stream, Type &value)
Function for reading data from a stream.
Definition: region_xy.h:46
PCL_EXPORTS int saveOBJFile(const std::string &file_name, const pcl::PolygonMesh &mesh, unsigned precision=5)
Saves a PolygonMesh in ascii PLY format.
#define PCL_MAKE_ALIGNED_OPERATOR_NEW
Macro to signal a class requires a custom allocator.
Definition: pcl_macros.h:344
~OBJReader()
empty destructor
Definition: obj_io.h:98
Eigen::Vector4f sensor_origin_
Sensor acquisition pose (origin/translation).
Definition: point_cloud.h:434
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:231
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.
std::vector< pcl::TexMaterial > materials_
materials array
Definition: obj_io.h:74
Point Cloud Data (FILE) file format reader interface.
Definition: file_io.h:55
int loadOBJFile(const std::string &file_name, pcl::TextureMesh &mesh)
Load any OBJ file into a TextureMesh type.
Definition: obj_io.h:316
Eigen::Quaternionf sensor_orientation_
Sensor acquisition pose (rotation).
Definition: point_cloud.h:436
PointCloud represents the base class in PCL for storing collections of 3D points. ...
OBJReader()
empty constructor
Definition: obj_io.h:96
#define PCL_EXPORTS
Definition: pcl_macros.h:226
Defines all the PCL and non-PCL macros used.