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> 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>
static 164 return (scalar_property_definition_callbacks.
get<ScalarType> ());
168 template <
typename ScalarType>
static 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<
201 list_property_begin_callback_type,
202 list_property_element_callback_type,
203 list_property_end_callback_type
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>
static 260 return (list_property_definition_callbacks.
get<SizeType, ScalarType> ());
263 template <
typename SizeType,
typename ScalarType>
static 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);
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 () {}
326 template <
typename ScalarType>
327 struct scalar_property :
public property
329 typedef ScalarType scalar_type;
331 scalar_property (
const std::string& name, callback_type callback)
333 , callback (callback)
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;
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)
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_;
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,
421 template <
typename SizeType,
typename ScalarType>
inline bool 423 std::istream& istream,
428 std::size_t line_number_;
429 element* current_element_;
443 info_callback_ = info_callback;
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;
501 scalar_property_definition_callbacks_.get<scalar_type> ();
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_,
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;
525 list_property_definition_callback_type& list_property_definition_callback = list_property_definition_callbacks_.get<size_type, scalar_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_,
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,
558 using namespace io_operators;
559 typedef ScalarType scalar_type;
570 catch (boost::bad_lexical_cast &)
572 value = std::numeric_limits<scalar_type>::quiet_NaN ();
576 istream >> space >> std::ws;
577 if (!istream || !isspace (space))
580 error_callback_ (line_number_,
"parse error");
583 if (scalar_property_callback)
584 scalar_property_callback (value);
589 scalar_type value = std::numeric_limits<scalar_type>::quiet_NaN ();
590 istream.read (reinterpret_cast<char*> (&value),
sizeof (scalar_type));
594 error_callback_ (line_number_,
"parse error");
600 if (scalar_property_callback)
601 scalar_property_callback (value);
606 template <
typename SizeType,
typename ScalarType>
607 inline bool pcl::io::ply::ply_parser::parse_list_property (
format_type format, std::istream& istream,
612 using namespace io_operators;
613 typedef SizeType size_type;
614 typedef ScalarType scalar_type;
617 size_type size = std::numeric_limits<size_type>::infinity ();
622 istream >> space >> std::ws;
624 if (!istream || !isspace (space))
628 error_callback_ (line_number_,
"parse error");
632 if (list_property_begin_callback)
634 list_property_begin_callback (size);
636 for (std::size_t index = 0; index < size; ++index)
646 catch (boost::bad_lexical_cast &)
648 value = std::numeric_limits<scalar_type>::quiet_NaN ();
653 istream >> space >> std::ws;
655 if (!istream || !isspace (space))
659 error_callback_ (line_number_,
"parse error");
663 if (list_property_element_callback)
665 list_property_element_callback (value);
668 if (list_property_end_callback)
670 list_property_end_callback ();
676 size_type size = std::numeric_limits<size_type>::infinity ();
677 istream.read (reinterpret_cast<char*> (&size),
sizeof (size_type));
687 error_callback_ (line_number_,
"parse error");
691 if (list_property_begin_callback)
693 list_property_begin_callback (size);
695 for (std::size_t index = 0; index < size; ++index) {
696 scalar_type value = std::numeric_limits<scalar_type>::quiet_NaN ();
697 istream.read (reinterpret_cast<char*> (&value),
sizeof (scalar_type));
699 if (error_callback_) {
700 error_callback_ (line_number_,
"parse error");
709 if (list_property_element_callback)
711 list_property_element_callback (value);
714 if (list_property_end_callback)
716 list_property_end_callback ();
722 #ifdef BUILD_Maintainer 723 # if defined __GNUC__ 724 # if __GNUC__ == 4 && __GNUC_MINOR__ > 3 725 # pragma GCC diagnostic warning "-Weffc++" 726 # pragma GCC diagnostic warning "-pedantic" 728 # elif defined _MSC_VER 729 # pragma warning(pop) 733 #endif // PCL_IO_PLY_PLY_PARSER_H
boost::function< bool()> end_header_callback_type
list_property_definition_callback_type< SizeType, ScalarType >::type & get()
boost::mpl::vector< int8, int16, int32, uint8, uint16, uint32, float32, float64 > scalar_types
static scalar_property_definition_callback_type< ScalarType >::type & at(scalar_property_definition_callbacks_type &scalar_property_definition_callbacks)
void error_callback(const error_callback_type &error_callback)
boost::function< void(const std::string &)> obj_info_callback_type
const scalar_property_definition_callback_type< ScalarType >::type & get() const
boost::function< void(std::size_t, const std::string &)> error_callback_type
boost::function< void(std::size_t, const std::string &)> info_callback_type
boost::function< void()> end_element_callback_type
void magic_callback(const magic_callback_type &magic_callback)
boost::function< void(std::size_t, const std::string &)> warning_callback_type
boost::function< void(format_type, const std::string &)> format_callback_type
contains standard typedefs and generic type traits
ply_parser(flags_type flags=0)
defines output operators for int8 and uint8
list_property_element_callback_type< SizeType, ScalarType >::type list_property_element_callback_type
static const scalar_property_definition_callback_type< ScalarType >::type & at(const scalar_property_definition_callbacks_type &scalar_property_definition_callbacks)
void warning_callback(const warning_callback_type &warning_callback)
boost::function< void()> begin_element_callback_type
Class ply_parser parses a PLY file and generates appropriate atomic parsers for the body...
void info_callback(const info_callback_type &info_callback)
void scalar_property_definition_callbacks(const scalar_property_definition_callbacks_type &scalar_property_definition_callbacks)
list_property_begin_callback_type< SizeType, ScalarType >::type list_property_begin_callback_type
void format_callback(const format_callback_type &format_callback)
list_property_end_callback_type< SizeType, ScalarType >::type list_property_end_callback_type
void list_property_definition_callbacks(const list_property_definition_callbacks_type &list_property_definition_callbacks)
static list_property_definition_callback_type< SizeType, ScalarType >::type & at(list_property_definition_callbacks_type &list_property_definition_callbacks)
boost::tuple< begin_element_callback_type, end_element_callback_type > element_callbacks_type
boost::function< element_callbacks_type(const std::string &, std::size_t)> element_definition_callback_type
void end_header_callback(const end_header_callback_type &end_header_callback)
boost::mpl::vector< uint8, uint16, uint32 > size_types
void obj_info_callback(const obj_info_callback_type &obj_info_callback)
boost::function< boost::tuple< list_property_begin_callback_type, list_property_element_callback_type, list_property_end_callback_type >const std::string &, const std::string &)> type
void comment_callback(const comment_callback_type &comment_callback)
boost::function< void(SizeType)> type
void swap_byte_order(char *bytes)
boost::function< void()> type
boost::function< void(ScalarType)> type
boost::function< void()> magic_callback_type
static const list_property_definition_callback_type< SizeType, ScalarType >::type & at(const list_property_definition_callbacks_type &list_property_definition_callbacks)
boost::function< void(const std::string &)> comment_callback_type
boost::function< void(ScalarType)> type
boost::function< scalar_property_callback_type(const std::string &, const std::string &)> type
scalar_property_callback_type< ScalarType >::type scalar_property_callback_type
void element_definition_callback(const element_definition_callback_type &element_definition_callback)