41 #ifndef PCL_IO_PLY_PLY_PARSER_H
42 #define PCL_IO_PLY_PLY_PARSER_H
51 #ifdef BUILD_Maintainer
53 # if __GNUC__ == 4 && __GNUC_MINOR__ > 3
54 # pragma GCC diagnostic ignored "-Weffc++"
55 # pragma GCC diagnostic ignored "-pedantic"
57 # pragma GCC system_header
59 # elif defined _MSC_VER
60 # pragma warning(push, 1)
64 #include <pcl/io/boost.h>
66 #include <pcl/io/ply/ply.h>
67 #include <pcl/io/ply/io_operators.h>
68 #include <pcl/pcl_macros.h>
101 template <
typename ScalarType>
104 typedef boost::function<void (ScalarType)>
type;
107 template <
typename ScalarType>
111 typedef boost::function<scalar_property_callback_type (const std::string&, const std::string&)>
type;
114 typedef boost::mpl::vector<int8, int16, int32, uint8, uint16, uint32, float32, float64>
scalar_types;
119 template <
typename T>
120 struct callbacks_element
123 typedef T scalar_type;
127 typedef boost::mpl::inherit_linearly<
131 callbacks_element<boost::mpl::_2>
134 callbacks callbacks_;
137 template <
typename ScalarType>
141 return (
static_cast<const callbacks_element<ScalarType>&
> (callbacks_).callback);
144 template <
typename ScalarType>
148 return (
static_cast<callbacks_element<ScalarType>&
> (callbacks_).callback);
151 template <
typename ScalarType>
155 template <
typename ScalarType>
160 template <
typename ScalarType>
161 friend typename scalar_property_definition_callback_type<ScalarType>::type&
164 return (scalar_property_definition_callbacks.
get<ScalarType> ());
168 template <
typename ScalarType>
169 friend const typename scalar_property_definition_callback_type<ScalarType>::type&
172 return (scalar_property_definition_callbacks.
get<ScalarType> ());
175 template <
typename SizeType,
typename ScalarType>
178 typedef boost::function<void (SizeType)>
type;
181 template <
typename SizeType,
typename ScalarType>
184 typedef boost::function<void (ScalarType)>
type;
187 template <
typename SizeType,
typename ScalarType>
190 typedef boost::function<void ()>
type;
193 template <
typename SizeType,
typename ScalarType>
199 typedef boost::function<
204 > (
const std::string&,
const std::string&)>
type;
207 typedef boost::mpl::vector<uint8, uint16, uint32>
size_types;
212 template <
typename T>
struct pair_with : boost::mpl::pair<T,boost::mpl::_> {};
213 template<
typename Sequence1,
typename Sequence2>
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> > > >
221 template <
typename T>
222 struct callbacks_element
224 typedef typename T::first size_type;
225 typedef typename T::second scalar_type;
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_;
233 template <
typename SizeType,
typename ScalarType>
237 return (
static_cast<callbacks_element<boost::mpl::pair<SizeType, ScalarType>
>&> (callbacks_).callback);
240 template <
typename SizeType,
typename ScalarType>
244 return (
static_cast<const callbacks_element<boost::mpl::pair<SizeType, ScalarType>
>&> (callbacks_).callback);
247 template <
typename SizeType,
typename ScalarType>
251 template <
typename SizeType,
typename ScalarType>
256 template <
typename SizeType,
typename ScalarType>
257 friend typename list_property_definition_callback_type<SizeType, ScalarType>::type&
260 return (list_property_definition_callbacks.
get<SizeType, ScalarType> ());
263 template <
typename SizeType,
typename ScalarType>
264 friend const typename list_property_definition_callback_type<SizeType, ScalarType>::type&
267 return (list_property_definition_callbacks.
get<SizeType, ScalarType> ());
272 info_callback (
const info_callback_type& info_callback);
275 warning_callback (
const warning_callback_type& warning_callback);
278 error_callback (
const error_callback_type& error_callback);
281 magic_callback (
const magic_callback_type& magic_callback);
284 format_callback (
const format_callback_type& format_callback);
287 element_definition_callback (
const element_definition_callback_type& element_definition_callback);
290 scalar_property_definition_callbacks (
const scalar_property_definition_callbacks_type& scalar_property_definition_callbacks);
293 list_property_definition_callbacks (
const list_property_definition_callbacks_type& list_property_definition_callbacks);
296 comment_callback (
const comment_callback_type& comment_callback);
299 obj_info_callback (
const obj_info_callback_type& obj_info_callback);
302 end_header_callback (
const end_header_callback_type& end_header_callback);
309 comment_callback_ (), obj_info_callback_ (), end_header_callback_ (),
310 line_number_ (0), current_element_ ()
313 bool parse (
const std::string& filename);
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;
326 template <
typename ScalarType>
327 struct scalar_property :
public property
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)
333 , callback (callback)
335 bool parse (
class ply_parser& ply_parser,
337 std::istream& istream)
339 return ply_parser.parse_scalar_property<scalar_type> (
format, istream, callback);
341 callback_type callback;
344 template <
typename SizeType,
typename ScalarType>
345 struct list_property :
public property
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)
357 , begin_callback (begin_callback)
358 , element_callback (element_callback)
359 , end_callback (end_callback)
361 bool parse (
class ply_parser& ply_parser,
363 std::istream& istream)
365 return ply_parser.parse_list_property<size_type, scalar_type> (
format,
371 begin_callback_type begin_callback;
372 element_callback_type element_callback;
373 end_callback_type end_callback;
378 element (
const std::string& name,
380 const begin_element_callback_type& begin_element_callback,
381 const end_element_callback_type& end_element_callback)
384 , begin_element_callback (begin_element_callback)
385 , end_element_callback (end_element_callback)
390 begin_element_callback_type begin_element_callback;
391 end_element_callback_type end_element_callback;
392 std::vector<boost::shared_ptr<property> > properties;
397 info_callback_type info_callback_;
398 warning_callback_type warning_callback_;
399 error_callback_type error_callback_;
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_;
410 template <
typename ScalarType>
inline void
411 parse_scalar_property_definition (
const std::string& property_name);
413 template <
typename SizeType,
typename ScalarType>
inline void
414 parse_list_property_definition (
const std::string& property_name);
416 template <
typename ScalarType>
inline bool
418 std::istream& istream,
419 const typename scalar_property_callback_type<ScalarType>::type& scalar_property_callback);
421 template <
typename SizeType,
typename ScalarType>
inline bool
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);
428 std::size_t line_number_;
429 element* current_element_;
448 warning_callback_ = warning_callback;
453 error_callback_ = error_callback;
458 magic_callback_ = magic_callback;
463 format_callback_ = format_callback;
468 element_definition_callbacks_ = element_definition_callback;
473 scalar_property_definition_callbacks_ = scalar_property_definition_callbacks;
478 list_property_definition_callbacks_ = list_property_definition_callbacks;
483 comment_callback_ = comment_callback;
488 obj_info_callback_ = obj_info_callback;
493 end_header_callback_ = end_header_callback;
496 template <
typename ScalarType>
497 inline void pcl::io::ply::ply_parser::parse_scalar_property_definition (
const std::string& property_name)
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)
505 scalar_property_callback = scalar_property_definition_callback (current_element_->name, property_name);
507 if (!scalar_property_callback)
509 if (warning_callback_)
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");
516 current_element_->properties.push_back (boost::shared_ptr<property> (
new scalar_property<scalar_type> (property_name, scalar_property_callback)));
519 template <
typename SizeType,
typename ScalarType>
520 inline void pcl::io::ply::ply_parser::parse_list_property_definition (
const std::string& property_name)
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)
532 list_property_callbacks = list_property_definition_callback (current_element_->name, property_name);
534 if (!boost::get<0> (list_property_callbacks) || !boost::get<1> (list_property_callbacks) || !boost::get<2> (list_property_callbacks))
536 if (warning_callback_)
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");
545 current_element_->properties.push_back (boost::shared_ptr<property> (
546 new list_property<size_type, scalar_type> (
548 boost::get<0> (list_property_callbacks),
549 boost::get<1> (list_property_callbacks),
550 boost::get<2> (list_property_callbacks))));
553 template <
typename ScalarType>
555 std::istream& istream,
556 const typename scalar_property_callback_type<ScalarType>::type& scalar_property_callback)
558 using namespace io_operators;
559 typedef ScalarType scalar_type;
562 scalar_type value = std::numeric_limits<scalar_type>::quiet_NaN ();
566 istream >> space >> std::ws;
567 if (!istream || !isspace (space))
570 error_callback_ (line_number_,
"parse error");
573 if (scalar_property_callback)
574 scalar_property_callback (value);
579 scalar_type value = std::numeric_limits<scalar_type>::quiet_NaN ();
580 istream.read (reinterpret_cast<char*> (&value),
sizeof (scalar_type));
584 error_callback_ (line_number_,
"parse error");
590 if (scalar_property_callback)
591 scalar_property_callback (value);
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)
602 using namespace io_operators;
603 typedef SizeType size_type;
604 typedef ScalarType scalar_type;
607 size_type size = std::numeric_limits<size_type>::infinity ();
612 istream >> space >> std::ws;
614 if (!istream || !isspace (space))
618 error_callback_ (line_number_,
"parse error");
622 if (list_property_begin_callback)
624 list_property_begin_callback (size);
626 for (std::size_t index = 0; index < size; ++index)
628 scalar_type value = std::numeric_limits<scalar_type>::quiet_NaN ();
633 istream >> space >> std::ws;
635 if (!istream || !isspace (space))
639 error_callback_ (line_number_,
"parse error");
643 if (list_property_element_callback)
645 list_property_element_callback (value);
648 if (list_property_end_callback)
650 list_property_end_callback ();
656 size_type size = std::numeric_limits<size_type>::infinity ();
657 istream.read (reinterpret_cast<char*> (&size),
sizeof (size_type));
667 error_callback_ (line_number_,
"parse error");
671 if (list_property_begin_callback)
673 list_property_begin_callback (size);
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));
679 if (error_callback_) {
680 error_callback_ (line_number_,
"parse error");
689 if (list_property_element_callback)
691 list_property_element_callback (value);
694 if (list_property_end_callback)
696 list_property_end_callback ();
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"
708 # elif defined _MSC_VER
709 # pragma warning(pop)
713 #endif // PCL_IO_PLY_PLY_PARSER_H