Point Cloud Library (PCL)  1.7.1
ply_parser.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2007-2012, Ares Lagae
6  * Copyright (c) 2010-2011, Willow Garage, Inc.
7  *
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * * Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  * * Redistributions in binary form must reproduce the above
17  * copyright notice, this list of conditions and the following
18  * disclaimer in the documentation and/or other materials provided
19  * with the distribution.
20  * * Neither the name of the copyright holder(s) nor the names of its
21  * contributors may be used to endorse or promote products derived
22  * from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  *
37  * $Id$
38  *
39  */
40 
41 #ifndef PCL_IO_PLY_PLY_PARSER_H
42 #define PCL_IO_PLY_PLY_PARSER_H
43 
44 #include <fstream>
45 #include <iostream>
46 #include <istream>
47 #include <sstream>
48 #include <string>
49 #include <vector>
50 
51 #ifdef BUILD_Maintainer
52 # if defined __GNUC__
53 # if __GNUC__ == 4 && __GNUC_MINOR__ > 3
54 # pragma GCC diagnostic ignored "-Weffc++"
55 # pragma GCC diagnostic ignored "-pedantic"
56 # else
57 # pragma GCC system_header
58 # endif
59 # elif defined _MSC_VER
60 # pragma warning(push, 1)
61 # endif
62 #endif
63 
64 #include <pcl/io/boost.h>
65 
66 #include <pcl/io/ply/ply.h>
67 #include <pcl/io/ply/io_operators.h>
68 #include <pcl/pcl_macros.h>
69 
70 namespace pcl
71 {
72  namespace io
73  {
74  namespace ply
75  {
76  /** Class ply_parser parses a PLY file and generates appropriate atomic
77  * parsers for the body.
78  * \author Ares Lagae as part of libply, Nizar Sallem
79  * Ported with agreement from the author under the terms of the BSD
80  * license.
81  */
82  class PCL_EXPORTS ply_parser
83  {
84  public:
85 
86  typedef boost::function<void (std::size_t, const std::string&)> info_callback_type;
87  typedef boost::function<void (std::size_t, const std::string&)> warning_callback_type;
88  typedef boost::function<void (std::size_t, const std::string&)> error_callback_type;
89 
90  typedef boost::function<void ()> magic_callback_type;
91  typedef boost::function<void (format_type, const std::string&)> format_callback_type;
92  typedef boost::function<void (const std::string&)> comment_callback_type;
93  typedef boost::function<void (const std::string&)> obj_info_callback_type;
94  typedef boost::function<bool ()> end_header_callback_type;
95 
96  typedef boost::function<void ()> begin_element_callback_type;
97  typedef boost::function<void ()> end_element_callback_type;
98  typedef boost::tuple<begin_element_callback_type, end_element_callback_type> element_callbacks_type;
99  typedef boost::function<element_callbacks_type (const std::string&, std::size_t)> element_definition_callback_type;
100 
101  template <typename ScalarType>
103  {
104  typedef boost::function<void (ScalarType)> type;
105  };
106 
107  template <typename ScalarType>
109  {
111  typedef boost::function<scalar_property_callback_type (const std::string&, const std::string&)> type;
112  };
113 
114  typedef boost::mpl::vector<int8, int16, int32, uint8, uint16, uint32, float32, float64> scalar_types;
115 
117  {
118  private:
119  template <typename T>
120  struct callbacks_element
121  {
122 // callbacks_element () : callback ();
123  typedef T scalar_type;
125  };
126 
127  typedef boost::mpl::inherit_linearly<
128  scalar_types,
129  boost::mpl::inherit<
130  boost::mpl::_1,
131  callbacks_element<boost::mpl::_2>
132  >
133  >::type callbacks;
134  callbacks callbacks_;
135 
136  public:
137  template <typename ScalarType>
139  get () const
140  {
141  return (static_cast<const callbacks_element<ScalarType>&> (callbacks_).callback);
142  }
143 
144  template <typename ScalarType>
146  get ()
147  {
148  return (static_cast<callbacks_element<ScalarType>&> (callbacks_).callback);
149  }
150 
151  template <typename ScalarType>
153  at (scalar_property_definition_callbacks_type& scalar_property_definition_callbacks);
154 
155  template <typename ScalarType>
157  at (const scalar_property_definition_callbacks_type& scalar_property_definition_callbacks);
158  };
159 
160  template <typename ScalarType>
161  friend typename scalar_property_definition_callback_type<ScalarType>::type&
162  at (scalar_property_definition_callbacks_type& scalar_property_definition_callbacks)
163  {
164  return (scalar_property_definition_callbacks.get<ScalarType> ());
165  }
166 
167 
168  template <typename ScalarType>
169  friend const typename scalar_property_definition_callback_type<ScalarType>::type&
170  at (const scalar_property_definition_callbacks_type& scalar_property_definition_callbacks)
171  {
172  return (scalar_property_definition_callbacks.get<ScalarType> ());
173  }
174 
175  template <typename SizeType, typename ScalarType>
177  {
178  typedef boost::function<void (SizeType)> type;
179  };
180 
181  template <typename SizeType, typename ScalarType>
183  {
184  typedef boost::function<void (ScalarType)> type;
185  };
186 
187  template <typename SizeType, typename ScalarType>
189  {
190  typedef boost::function<void ()> type;
191  };
192 
193  template <typename SizeType, typename ScalarType>
195  {
199  typedef boost::function<
200  boost::tuple<
204  > (const std::string&, const std::string&)> type;
205  };
206 
207  typedef boost::mpl::vector<uint8, uint16, uint32> size_types;
208 
210  {
211  private:
212  template <typename T> struct pair_with : boost::mpl::pair<T,boost::mpl::_> {};
213  template<typename Sequence1, typename Sequence2>
214 
215  struct sequence_product :
216  boost::mpl::fold<Sequence1, boost::mpl::vector0<>,
217  boost::mpl::joint_view<
218  boost::mpl::_1,boost::mpl::transform<Sequence2, pair_with<boost::mpl::_2> > > >
219  {};
220 
221  template <typename T>
222  struct callbacks_element
223  {
224  typedef typename T::first size_type;
225  typedef typename T::second scalar_type;
227  };
228 
229  typedef boost::mpl::inherit_linearly<sequence_product<size_types, scalar_types>::type, boost::mpl::inherit<boost::mpl::_1, callbacks_element<boost::mpl::_2> > >::type callbacks;
230  callbacks callbacks_;
231 
232  public:
233  template <typename SizeType, typename ScalarType>
235  get ()
236  {
237  return (static_cast<callbacks_element<boost::mpl::pair<SizeType, ScalarType> >&> (callbacks_).callback);
238  }
239 
240  template <typename SizeType, typename ScalarType>
242  get () const
243  {
244  return (static_cast<const callbacks_element<boost::mpl::pair<SizeType, ScalarType> >&> (callbacks_).callback);
245  }
246 
247  template <typename SizeType, typename ScalarType>
249  at (list_property_definition_callbacks_type& list_property_definition_callbacks);
250 
251  template <typename SizeType, typename ScalarType>
253  at (const list_property_definition_callbacks_type& list_property_definition_callbacks);
254  };
255 
256  template <typename SizeType, typename ScalarType>
257  friend typename list_property_definition_callback_type<SizeType, ScalarType>::type&
258  at (list_property_definition_callbacks_type& list_property_definition_callbacks)
259  {
260  return (list_property_definition_callbacks.get<SizeType, ScalarType> ());
261  }
262 
263  template <typename SizeType, typename ScalarType>
264  friend const typename list_property_definition_callback_type<SizeType, ScalarType>::type&
265  at (const list_property_definition_callbacks_type& list_property_definition_callbacks)
266  {
267  return (list_property_definition_callbacks.get<SizeType, ScalarType> ());
268  }
269 
270 
271  inline void
272  info_callback (const info_callback_type& info_callback);
273 
274  inline void
275  warning_callback (const warning_callback_type& warning_callback);
276 
277  inline void
278  error_callback (const error_callback_type& error_callback);
279 
280  inline void
281  magic_callback (const magic_callback_type& magic_callback);
282 
283  inline void
284  format_callback (const format_callback_type& format_callback);
285 
286  inline void
287  element_definition_callback (const element_definition_callback_type& element_definition_callback);
288 
289  inline void
290  scalar_property_definition_callbacks (const scalar_property_definition_callbacks_type& scalar_property_definition_callbacks);
291 
292  inline void
293  list_property_definition_callbacks (const list_property_definition_callbacks_type& list_property_definition_callbacks);
294 
295  inline void
296  comment_callback (const comment_callback_type& comment_callback);
297 
298  inline void
299  obj_info_callback (const obj_info_callback_type& obj_info_callback);
300 
301  inline void
302  end_header_callback (const end_header_callback_type& end_header_callback);
303 
304  typedef int flags_type;
305  enum flags { };
306 
308  flags_ (flags),
309  comment_callback_ (), obj_info_callback_ (), end_header_callback_ (),
310  line_number_ (0), current_element_ ()
311  {}
312 
313  bool parse (const std::string& filename);
314  //inline bool parse (const std::string& filename);
315 
316  private:
317 
318  struct property
319  {
320  property (const std::string& name) : name (name) {}
321  virtual ~property () {}
322  virtual bool parse (class ply_parser& ply_parser, format_type format, std::istream& istream) = 0;
323  std::string name;
324  };
325 
326  template <typename ScalarType>
327  struct scalar_property : public property
328  {
329  typedef ScalarType scalar_type;
330  typedef typename scalar_property_callback_type<scalar_type>::type callback_type;
331  scalar_property (const std::string& name, callback_type callback)
332  : property (name)
333  , callback (callback)
334  {}
335  bool parse (class ply_parser& ply_parser,
337  std::istream& istream)
338  {
339  return ply_parser.parse_scalar_property<scalar_type> (format, istream, callback);
340  }
341  callback_type callback;
342  };
343 
344  template <typename SizeType, typename ScalarType>
345  struct list_property : public property
346  {
347  typedef SizeType size_type;
348  typedef ScalarType scalar_type;
349  typedef typename list_property_begin_callback_type<size_type, scalar_type>::type begin_callback_type;
350  typedef typename list_property_element_callback_type<size_type, scalar_type>::type element_callback_type;
351  typedef typename list_property_end_callback_type<size_type, scalar_type>::type end_callback_type;
352  list_property (const std::string& name,
353  begin_callback_type begin_callback,
354  element_callback_type element_callback,
355  end_callback_type end_callback)
356  : property (name)
357  , begin_callback (begin_callback)
358  , element_callback (element_callback)
359  , end_callback (end_callback)
360  {}
361  bool parse (class ply_parser& ply_parser,
363  std::istream& istream)
364  {
365  return ply_parser.parse_list_property<size_type, scalar_type> (format,
366  istream,
367  begin_callback,
368  element_callback,
369  end_callback);
370  }
371  begin_callback_type begin_callback;
372  element_callback_type element_callback;
373  end_callback_type end_callback;
374  };
375 
376  struct element
377  {
378  element (const std::string& name,
379  std::size_t count,
380  const begin_element_callback_type& begin_element_callback,
381  const end_element_callback_type& end_element_callback)
382  : name (name)
383  , count (count)
384  , begin_element_callback (begin_element_callback)
385  , end_element_callback (end_element_callback)
386  , properties ()
387  {}
388  std::string name;
389  std::size_t count;
390  begin_element_callback_type begin_element_callback;
391  end_element_callback_type end_element_callback;
392  std::vector<boost::shared_ptr<property> > properties;
393  };
394 
395  flags_type flags_;
396 
397  info_callback_type info_callback_;
398  warning_callback_type warning_callback_;
399  error_callback_type error_callback_;
400 
401  magic_callback_type magic_callback_;
402  format_callback_type format_callback_;
403  element_definition_callback_type element_definition_callbacks_;
404  scalar_property_definition_callbacks_type scalar_property_definition_callbacks_;
405  list_property_definition_callbacks_type list_property_definition_callbacks_;
406  comment_callback_type comment_callback_;
407  obj_info_callback_type obj_info_callback_;
408  end_header_callback_type end_header_callback_;
409 
410  template <typename ScalarType> inline void
411  parse_scalar_property_definition (const std::string& property_name);
412 
413  template <typename SizeType, typename ScalarType> inline void
414  parse_list_property_definition (const std::string& property_name);
415 
416  template <typename ScalarType> inline bool
417  parse_scalar_property (format_type format,
418  std::istream& istream,
419  const typename scalar_property_callback_type<ScalarType>::type& scalar_property_callback);
420 
421  template <typename SizeType, typename ScalarType> inline bool
422  parse_list_property (format_type format,
423  std::istream& istream,
424  const typename list_property_begin_callback_type<SizeType, ScalarType>::type& list_property_begin_callback,
425  const typename list_property_element_callback_type<SizeType, ScalarType>::type& list_property_element_callback,
426  const typename list_property_end_callback_type<SizeType, ScalarType>::type& list_property_end_callback);
427 
428  std::size_t line_number_;
429  element* current_element_;
430  };
431  } // namespace ply
432  } // namespace io
433 } // namespace pcl
434 
435 /* inline bool pcl::io::ply::ply_parser::parse (const std::string& filename) */
436 /* { */
437 /* std::ifstream ifstream (filename.c_str ()); */
438 /* return (parse (ifstream)); */
439 /* } */
440 
442 {
443  info_callback_ = info_callback;
444 }
445 
447 {
448  warning_callback_ = warning_callback;
449 }
450 
452 {
453  error_callback_ = error_callback;
454 }
455 
457 {
458  magic_callback_ = magic_callback;
459 }
460 
462 {
463  format_callback_ = format_callback;
464 }
465 
467 {
468  element_definition_callbacks_ = element_definition_callback;
469 }
470 
472 {
473  scalar_property_definition_callbacks_ = scalar_property_definition_callbacks;
474 }
475 
477 {
478  list_property_definition_callbacks_ = list_property_definition_callbacks;
479 }
480 
482 {
483  comment_callback_ = comment_callback;
484 }
485 
487 {
488  obj_info_callback_ = obj_info_callback;
489 }
490 
492 {
493  end_header_callback_ = end_header_callback;
494 }
495 
496 template <typename ScalarType>
497 inline void pcl::io::ply::ply_parser::parse_scalar_property_definition (const std::string& property_name)
498 {
499  typedef ScalarType scalar_type;
500  typename scalar_property_definition_callback_type<scalar_type>::type& scalar_property_definition_callback =
501  scalar_property_definition_callbacks_.get<scalar_type> ();
502  typename scalar_property_callback_type<scalar_type>::type scalar_property_callback;
503  if (scalar_property_definition_callback)
504  {
505  scalar_property_callback = scalar_property_definition_callback (current_element_->name, property_name);
506  }
507  if (!scalar_property_callback)
508  {
509  if (warning_callback_)
510  {
511  warning_callback_ (line_number_,
512  "property '" + std::string (type_traits<scalar_type>::name ()) + " " +
513  property_name + "' of element '" + current_element_->name + "' is not handled");
514  }
515  }
516  current_element_->properties.push_back (boost::shared_ptr<property> (new scalar_property<scalar_type> (property_name, scalar_property_callback)));
517 }
518 
519 template <typename SizeType, typename ScalarType>
520 inline void pcl::io::ply::ply_parser::parse_list_property_definition (const std::string& property_name)
521 {
522  typedef SizeType size_type;
523  typedef ScalarType scalar_type;
524  typename list_property_definition_callback_type<size_type, scalar_type>::type& list_property_definition_callback =
525  list_property_definition_callbacks_.get<size_type, scalar_type> ();
526  typedef typename list_property_begin_callback_type<size_type, scalar_type>::type list_property_begin_callback_type;
527  typedef typename list_property_element_callback_type<size_type, scalar_type>::type list_property_element_callback_type;
528  typedef typename list_property_end_callback_type<size_type, scalar_type>::type list_property_end_callback_type;
529  boost::tuple<list_property_begin_callback_type, list_property_element_callback_type, list_property_end_callback_type> list_property_callbacks;
530  if (list_property_definition_callback)
531  {
532  list_property_callbacks = list_property_definition_callback (current_element_->name, property_name);
533  }
534  if (!boost::get<0> (list_property_callbacks) || !boost::get<1> (list_property_callbacks) || !boost::get<2> (list_property_callbacks))
535  {
536  if (warning_callback_)
537  {
538  warning_callback_ (line_number_,
539  "property 'list " + std::string (type_traits<size_type>::name ()) + " " +
540  std::string (type_traits<scalar_type>::name ()) + " " +
541  property_name + "' of element '" +
542  current_element_->name + "' is not handled");
543  }
544  }
545  current_element_->properties.push_back (boost::shared_ptr<property> (
546  new list_property<size_type, scalar_type> (
547  property_name,
548  boost::get<0> (list_property_callbacks),
549  boost::get<1> (list_property_callbacks),
550  boost::get<2> (list_property_callbacks))));
551 }
552 
553 template <typename ScalarType>
554 inline bool pcl::io::ply::ply_parser::parse_scalar_property (format_type format,
555  std::istream& istream,
556  const typename scalar_property_callback_type<ScalarType>::type& scalar_property_callback)
557 {
558  using namespace io_operators;
559  typedef ScalarType scalar_type;
560  if (format == ascii_format)
561  {
562  scalar_type value = std::numeric_limits<scalar_type>::quiet_NaN ();
563  char space = ' ';
564  istream >> value;
565  if (!istream.eof ())
566  istream >> space >> std::ws;
567  if (!istream || !isspace (space))
568  {
569  if (error_callback_)
570  error_callback_ (line_number_, "parse error");
571  return (false);
572  }
573  if (scalar_property_callback)
574  scalar_property_callback (value);
575  return (true);
576  }
577  else
578  {
579  scalar_type value = std::numeric_limits<scalar_type>::quiet_NaN ();
580  istream.read (reinterpret_cast<char*> (&value), sizeof (scalar_type));
581  if (!istream)
582  {
583  if (error_callback_)
584  error_callback_ (line_number_, "parse error");
585  return (false);
586  }
587  if (((format == binary_big_endian_format) && (host_byte_order == little_endian_byte_order)) ||
588  ((format == binary_little_endian_format) && (host_byte_order == big_endian_byte_order)))
589  swap_byte_order (value);
590  if (scalar_property_callback)
591  scalar_property_callback (value);
592  return (true);
593  }
594 }
595 
596 template <typename SizeType, typename ScalarType>
597 inline bool pcl::io::ply::ply_parser::parse_list_property (format_type format, std::istream& istream,
598  const typename list_property_begin_callback_type<SizeType, ScalarType>::type& list_property_begin_callback,
599  const typename list_property_element_callback_type<SizeType, ScalarType>::type& list_property_element_callback,
600  const typename list_property_end_callback_type<SizeType, ScalarType>::type& list_property_end_callback)
601 {
602  using namespace io_operators;
603  typedef SizeType size_type;
604  typedef ScalarType scalar_type;
605  if (format == ascii_format)
606  {
607  size_type size = std::numeric_limits<size_type>::infinity ();
608  char space = ' ';
609  istream >> size;
610  if (!istream.eof ())
611  {
612  istream >> space >> std::ws;
613  }
614  if (!istream || !isspace (space))
615  {
616  if (error_callback_)
617  {
618  error_callback_ (line_number_, "parse error");
619  }
620  return (false);
621  }
622  if (list_property_begin_callback)
623  {
624  list_property_begin_callback (size);
625  }
626  for (std::size_t index = 0; index < size; ++index)
627  {
628  scalar_type value = std::numeric_limits<scalar_type>::quiet_NaN ();
629  char space = ' ';
630  istream >> value;
631  if (!istream.eof ())
632  {
633  istream >> space >> std::ws;
634  }
635  if (!istream || !isspace (space))
636  {
637  if (error_callback_)
638  {
639  error_callback_ (line_number_, "parse error");
640  }
641  return (false);
642  }
643  if (list_property_element_callback)
644  {
645  list_property_element_callback (value);
646  }
647  }
648  if (list_property_end_callback)
649  {
650  list_property_end_callback ();
651  }
652  return (true);
653  }
654  else
655  {
656  size_type size = std::numeric_limits<size_type>::infinity ();
657  istream.read (reinterpret_cast<char*> (&size), sizeof (size_type));
658  if (((format == binary_big_endian_format) && (host_byte_order == little_endian_byte_order)) ||
659  ((format == binary_little_endian_format) && (host_byte_order == big_endian_byte_order)))
660  {
661  swap_byte_order (size);
662  }
663  if (!istream)
664  {
665  if (error_callback_)
666  {
667  error_callback_ (line_number_, "parse error");
668  }
669  return (false);
670  }
671  if (list_property_begin_callback)
672  {
673  list_property_begin_callback (size);
674  }
675  for (std::size_t index = 0; index < size; ++index) {
676  scalar_type value = std::numeric_limits<scalar_type>::quiet_NaN ();
677  istream.read (reinterpret_cast<char*> (&value), sizeof (scalar_type));
678  if (!istream) {
679  if (error_callback_) {
680  error_callback_ (line_number_, "parse error");
681  }
682  return (false);
683  }
684  if (((format == binary_big_endian_format) && (host_byte_order == little_endian_byte_order)) ||
685  ((format == binary_little_endian_format) && (host_byte_order == big_endian_byte_order)))
686  {
687  swap_byte_order (value);
688  }
689  if (list_property_element_callback)
690  {
691  list_property_element_callback (value);
692  }
693  }
694  if (list_property_end_callback)
695  {
696  list_property_end_callback ();
697  }
698  return (true);
699  }
700 }
701 
702 #ifdef BUILD_Maintainer
703 # if defined __GNUC__
704 # if __GNUC__ == 4 && __GNUC_MINOR__ > 3
705 # pragma GCC diagnostic warning "-Weffc++"
706 # pragma GCC diagnostic warning "-pedantic"
707 # endif
708 # elif defined _MSC_VER
709 # pragma warning(pop)
710 # endif
711 #endif
712 
713 #endif // PCL_IO_PLY_PLY_PARSER_H