43 #ifdef BUILD_Maintainer
45 # pragma GCC system_header
46 # elif defined _MSC_VER
47 # pragma warning(push, 1)
51 #include <pcl/io/boost.h>
96 template <
typename ScalarType>
99 using type = std::function<void (ScalarType)>;
102 template <
typename ScalarType>
109 using scalar_types = boost::mpl::vector<int8, int16, int32, uint8, uint16, uint32, float32, float64>;
114 template <
typename T>
115 struct callbacks_element
118 using scalar_type = T;
122 using callbacks = boost::mpl::inherit_linearly<
126 callbacks_element<boost::mpl::_2>
129 callbacks callbacks_;
132 template <
typename ScalarType>
136 return (
static_cast<const callbacks_element<ScalarType>&
> (callbacks_).callback);
139 template <
typename ScalarType>
143 return (
static_cast<callbacks_element<ScalarType>&
> (callbacks_).callback);
146 template <
typename ScalarType>
150 template <
typename ScalarType>
155 template <
typename ScalarType>
static
156 typename scalar_property_definition_callback_type<ScalarType>::type&
159 return (scalar_property_definition_callbacks.
get<ScalarType> ());
163 template <
typename ScalarType>
static
164 const typename scalar_property_definition_callback_type<ScalarType>::type&
167 return (scalar_property_definition_callbacks.
get<ScalarType> ());
170 template <
typename SizeType,
typename ScalarType>
173 using type = std::function<void (SizeType)>;
176 template <
typename SizeType,
typename ScalarType>
179 using type = std::function<void (ScalarType)>;
182 template <
typename SizeType,
typename ScalarType>
185 using type = std::function<void ()>;
188 template <
typename SizeType,
typename ScalarType>
194 using type = std::function<std::tuple<
198 > (
const std::string&,
const std::string&)>;
201 using size_types = boost::mpl::vector<uint8, uint16, uint32>;
206 template <
typename T>
struct pair_with : boost::mpl::pair<T,boost::mpl::_> {};
207 template<
typename Sequence1,
typename Sequence2>
209 struct sequence_product :
210 boost::mpl::fold<Sequence1, boost::mpl::vector0<>,
211 boost::mpl::joint_view<
212 boost::mpl::_1,boost::mpl::transform<Sequence2, pair_with<boost::mpl::_2> > > >
215 template <
typename T>
216 struct callbacks_element
218 using size_type =
typename T::first;
219 using scalar_type =
typename T::second;
223 using callbacks = boost::mpl::inherit_linearly<sequence_product<size_types, scalar_types>::type, boost::mpl::inherit<boost::mpl::_1, callbacks_element<boost::mpl::_2> > >::type;
224 callbacks callbacks_;
227 template <
typename SizeType,
typename ScalarType>
231 return (
static_cast<callbacks_element<boost::mpl::pair<SizeType, ScalarType>
>&> (callbacks_).callback);
234 template <
typename SizeType,
typename ScalarType>
238 return (
static_cast<const callbacks_element<boost::mpl::pair<SizeType, ScalarType>
>&> (callbacks_).callback);
241 template <
typename SizeType,
typename ScalarType>
245 template <
typename SizeType,
typename ScalarType>
250 template <
typename SizeType,
typename ScalarType>
static
251 typename list_property_definition_callback_type<SizeType, ScalarType>::type&
254 return (list_property_definition_callbacks.
get<SizeType, ScalarType> ());
257 template <
typename SizeType,
typename ScalarType>
static
258 const typename list_property_definition_callback_type<SizeType, ScalarType>::type&
261 return (list_property_definition_callbacks.
get<SizeType, ScalarType> ());
266 info_callback (
const info_callback_type& info_callback);
269 warning_callback (
const warning_callback_type& warning_callback);
272 error_callback (
const error_callback_type& error_callback);
275 magic_callback (
const magic_callback_type& magic_callback);
278 format_callback (
const format_callback_type& format_callback);
281 element_definition_callback (
const element_definition_callback_type& element_definition_callback);
284 scalar_property_definition_callbacks (
const scalar_property_definition_callbacks_type& scalar_property_definition_callbacks);
287 list_property_definition_callbacks (
const list_property_definition_callbacks_type& list_property_definition_callbacks);
290 comment_callback (
const comment_callback_type& comment_callback);
293 obj_info_callback (
const obj_info_callback_type& obj_info_callback);
296 end_header_callback (
const end_header_callback_type& end_header_callback);
302 line_number_ (0), current_element_ ()
305 bool parse (
const std::string& filename);
312 property (
const std::string& name) : name (name) {}
313 virtual ~property () {}
314 virtual bool parse (
class ply_parser& ply_parser,
format_type format, std::istream& istream) = 0;
318 template <
typename ScalarType>
319 struct scalar_property :
public property
321 using scalar_type = ScalarType;
323 scalar_property (
const std::string& name, callback_type callback)
325 , callback (callback)
327 bool parse (
class ply_parser& ply_parser,
329 std::istream& istream)
override
331 return ply_parser.parse_scalar_property<scalar_type> (
format, istream, callback);
333 callback_type callback;
336 template <
typename SizeType,
typename ScalarType>
337 struct list_property :
public property
339 using size_type = SizeType;
340 using scalar_type = ScalarType;
344 list_property (
const std::string& name,
345 begin_callback_type begin_callback,
346 element_callback_type element_callback,
347 end_callback_type end_callback)
349 , begin_callback (begin_callback)
350 , element_callback (element_callback)
351 , end_callback (end_callback)
353 bool parse (
class ply_parser& ply_parser,
355 std::istream& istream)
override
357 return ply_parser.parse_list_property<size_type, scalar_type> (
format,
363 begin_callback_type begin_callback;
364 element_callback_type element_callback;
365 end_callback_type end_callback;
370 element (
const std::string& name,
372 const begin_element_callback_type& begin_element_callback,
373 const end_element_callback_type& end_element_callback)
376 , begin_element_callback (begin_element_callback)
377 , end_element_callback (end_element_callback)
381 begin_element_callback_type begin_element_callback;
382 end_element_callback_type end_element_callback;
383 std::vector<std::shared_ptr<property>> properties;
386 info_callback_type info_callback_ = [](std::size_t,
const std::string&){};
387 warning_callback_type warning_callback_ = [](std::size_t,
const std::string&){};
388 error_callback_type error_callback_ = [](std::size_t,
const std::string&){};
390 magic_callback_type magic_callback_ = [](){};
391 format_callback_type format_callback_ = [](
format_type,
const std::string&){};
392 comment_callback_type comment_callback_ = [](
const std::string&){};
393 obj_info_callback_type obj_info_callback_ = [](
const std::string&){};
394 end_header_callback_type end_header_callback_ = [](){
return true;};
396 element_definition_callback_type element_definition_callbacks_ =
397 [](
const std::string&, std::size_t)
399 return std::make_tuple([](){}, [](){});
401 scalar_property_definition_callbacks_type scalar_property_definition_callbacks_;
402 list_property_definition_callbacks_type list_property_definition_callbacks_;
404 template <
typename ScalarType>
inline void
405 parse_scalar_property_definition (
const std::string& property_name);
407 template <
typename SizeType,
typename ScalarType>
inline void
408 parse_list_property_definition (
const std::string& property_name);
410 template <
typename ScalarType>
inline bool
412 std::istream& istream,
413 const typename scalar_property_callback_type<ScalarType>::type& scalar_property_callback);
415 template <
typename SizeType,
typename ScalarType>
inline bool
417 std::istream& istream,
418 const typename list_property_begin_callback_type<SizeType, ScalarType>::type& list_property_begin_callback,
419 const typename list_property_element_callback_type<SizeType, ScalarType>::type& list_property_element_callback,
420 const typename list_property_end_callback_type<SizeType, ScalarType>::type& list_property_end_callback);
422 std::size_t line_number_;
423 element* current_element_;
442 warning_callback_ = warning_callback;
447 error_callback_ = error_callback;
452 magic_callback_ = magic_callback;
457 format_callback_ = format_callback;
462 element_definition_callbacks_ = element_definition_callback;
467 scalar_property_definition_callbacks_ = scalar_property_definition_callbacks;
472 list_property_definition_callbacks_ = list_property_definition_callbacks;
477 comment_callback_ = comment_callback;
482 obj_info_callback_ = obj_info_callback;
487 end_header_callback_ = end_header_callback;
490 template <
typename ScalarType>
491 inline void pcl::io::ply::ply_parser::parse_scalar_property_definition (
const std::string& property_name)
493 using scalar_type = ScalarType;
495 scalar_property_definition_callbacks_.get<scalar_type> ();
497 if (scalar_property_definition_callback)
499 scalar_property_callback = scalar_property_definition_callback (current_element_->name, property_name);
501 if (!scalar_property_callback)
503 if (warning_callback_)
505 warning_callback_ (line_number_,
506 "property '" + std::string (type_traits<scalar_type>::name ()) +
" " +
507 property_name +
"' of element '" + current_element_->name +
"' is not handled");
510 current_element_->properties.emplace_back (
new scalar_property<scalar_type> (property_name, scalar_property_callback));
513 template <
typename SizeType,
typename ScalarType>
514 inline void pcl::io::ply::ply_parser::parse_list_property_definition (
const std::string& property_name)
516 using size_type = SizeType;
517 using scalar_type = ScalarType;
519 list_property_definition_callback_type& list_property_definition_callback = list_property_definition_callbacks_.get<size_type, scalar_type> ();
523 std::tuple<list_property_begin_callback_type, list_property_element_callback_type, list_property_end_callback_type> list_property_callbacks;
524 if (list_property_definition_callback)
526 list_property_callbacks = list_property_definition_callback (current_element_->name, property_name);
528 if (!std::get<0> (list_property_callbacks) || !std::get<1> (list_property_callbacks) || !std::get<2> (list_property_callbacks))
530 if (warning_callback_)
532 warning_callback_ (line_number_,
533 "property 'list " + std::string (type_traits<size_type>::name ()) +
" " +
534 std::string (type_traits<scalar_type>::name ()) +
" " +
535 property_name +
"' of element '" +
536 current_element_->name +
"' is not handled");
539 current_element_->properties.emplace_back (
new list_property<size_type, scalar_type> (
541 std::get<0> (list_property_callbacks),
542 std::get<1> (list_property_callbacks),
543 std::get<2> (list_property_callbacks)));
546 template <
typename ScalarType>
548 std::istream& istream,
549 const typename scalar_property_callback_type<ScalarType>::type& scalar_property_callback)
551 using namespace io_operators;
552 using scalar_type = ScalarType;
561 value =
static_cast<scalar_type
> (boost::lexical_cast<typename pcl::io::ply::type_traits<scalar_type>::parse_type> (value_s));
563 catch (boost::bad_lexical_cast &)
565 value = std::numeric_limits<scalar_type>::quiet_NaN ();
569 istream >> space >> std::ws;
570 if (!istream || !isspace (space))
573 error_callback_ (line_number_,
"parse error");
576 if (scalar_property_callback)
577 scalar_property_callback (value);
580 scalar_type value = std::numeric_limits<scalar_type>::quiet_NaN ();
581 istream.read (
reinterpret_cast<char*
> (&value),
sizeof (scalar_type));
585 error_callback_ (line_number_,
"parse error");
591 if (scalar_property_callback)
592 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 using size_type = SizeType;
604 using scalar_type = ScalarType;
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)
634 value =
static_cast<scalar_type
> (boost::lexical_cast<typename pcl::io::ply::type_traits<scalar_type>::parse_type> (value_s));
636 catch (boost::bad_lexical_cast &)
638 value = std::numeric_limits<scalar_type>::quiet_NaN ();
643 istream >> space >> std::ws;
645 if (!istream || !isspace (space))
649 error_callback_ (line_number_,
"parse error");
653 if (list_property_element_callback)
655 list_property_element_callback (value);
658 if (list_property_end_callback)
660 list_property_end_callback ();
664 size_type size = std::numeric_limits<size_type>::infinity ();
665 istream.read (
reinterpret_cast<char*
> (&size),
sizeof (size_type));
675 error_callback_ (line_number_,
"parse error");
679 if (list_property_begin_callback)
681 list_property_begin_callback (size);
683 for (std::size_t index = 0; index < size; ++index) {
684 scalar_type value = std::numeric_limits<scalar_type>::quiet_NaN ();
685 istream.read (
reinterpret_cast<char*
> (&value),
sizeof (scalar_type));
687 if (error_callback_) {
688 error_callback_ (line_number_,
"parse error");
697 if (list_property_element_callback)
699 list_property_element_callback (value);
702 if (list_property_end_callback)
704 list_property_end_callback ();
709 #ifdef BUILD_Maintainer
710 # if defined __GNUC__
711 # if __GNUC__ == 4 && __GNUC_MINOR__ > 3
712 # pragma GCC diagnostic warning "-Weffc++"
713 # pragma GCC diagnostic warning "-pedantic"
715 # elif defined _MSC_VER
716 # pragma warning(pop)