point_traits.h
/*
* Software License Agreement (BSD License)
*
* Point Cloud Library (PCL) - www.pointclouds.org
* Copyright (c) 2010-2012, Willow Garage, Inc.
* Copyright (c) 2012-, Open Perception, Inc.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of the copyright holder(s) nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
#pragma once
#ifdef __GNUC__
#pragma GCC system_header
#endif
#include "pcl/pcl_macros.h"
#include <pcl/PCLPointField.h>
#include <boost/mpl/assert.hpp>
// This is required for the workaround at line 109
#ifdef _MSC_VER
#include <Eigen/Core>
#include <Eigen/src/StlSupport/details.h>
#endif
#include <type_traits>
namespace pcl
{
namespace deprecated
{
/** \class DeprecatedType
* \brief A dummy type to aid in template parameter deprecation
*/
struct T {};
}
namespace fields
{
// Tag types get put in this namespace
}
namespace traits
{
// Metafunction to return enum value representing a type
template<typename T> struct asEnum {};
template<> struct asEnum<std::int8_t> { static const std::uint8_t value = pcl::PCLPointField::INT8; };
template<> struct asEnum<std::uint8_t> { static const std::uint8_t value = pcl::PCLPointField::UINT8; };
template<> struct asEnum<std::int16_t> { static const std::uint8_t value = pcl::PCLPointField::INT16; };
template<> struct asEnum<std::uint16_t> { static const std::uint8_t value = pcl::PCLPointField::UINT16; };
template<> struct asEnum<std::int32_t> { static const std::uint8_t value = pcl::PCLPointField::INT32; };
template<> struct asEnum<std::uint32_t> { static const std::uint8_t value = pcl::PCLPointField::UINT32; };
template<> struct asEnum<float> { static const std::uint8_t value = pcl::PCLPointField::FLOAT32; };
template<> struct asEnum<double> { static const std::uint8_t value = pcl::PCLPointField::FLOAT64; };
// Metafunction to return type of enum value
template<int> struct asType {};
template<> struct asType<pcl::PCLPointField::INT8> { using type = std::int8_t; };
template<> struct asType<pcl::PCLPointField::UINT8> { using type = std::uint8_t; };
template<> struct asType<pcl::PCLPointField::INT16> { using type = std::int16_t; };
template<> struct asType<pcl::PCLPointField::UINT16> { using type = std::uint16_t; };
template<> struct asType<pcl::PCLPointField::INT32> { using type = std::int32_t; };
template<> struct asType<pcl::PCLPointField::UINT32> { using type = std::uint32_t; };
template<> struct asType<pcl::PCLPointField::FLOAT32> { using type = float; };
template<> struct asType<pcl::PCLPointField::FLOAT64> { using type = double; };
// Metafunction to decompose a type (possibly of array of any number of dimensions) into
// its scalar type and total number of elements.
template<typename T> struct decomposeArray
{
using type = std::remove_all_extents_t<T>;
static const std::uint32_t value = sizeof (T) / sizeof (type);
};
// For non-POD point types, this is specialized to return the corresponding POD type.
template<typename PointT>
struct POD
{
using type = PointT;
};
#ifdef _MSC_VER
/* Sometimes when calling functions like `copyPoint()` or `copyPointCloud`
* without explicitly specifying point types, MSVC deduces them to be e.g.
* `Eigen::internal::workaround_msvc_stl_support<pcl::PointXYZ>` instead of
* plain `pcl::PointXYZ`. Subsequently these types are passed to meta-
* functions like `has_field` or `fieldList` and make them choke. This hack
* makes use of the fact that internally `fieldList` always applies `POD` to
* its argument type. This specialization therefore allows to unwrap the
* contained point type. */
template<typename PointT>
struct POD<Eigen::internal::workaround_msvc_stl_support<PointT> >
{
using type = PointT;
};
#endif
// name
/* This really only depends on Tag, but we go through some gymnastics to avoid ODR violations.
We template it on the point type PointT to avoid ODR violations when registering multiple
point types with shared tags.
The dummy parameter is so we can partially specialize name on PointT and Tag but leave it
templated on dummy. Each specialization declares a static char array containing the tag
name. The definition of the static member would conflict when linking multiple translation
units that include the point type registration. But when the static member definition is
templated (on dummy), we sidestep the ODR issue.
*/
template<class PointT, typename Tag, int dummy = 0>
struct name : name<typename POD<PointT>::type, Tag, dummy>
{
// Contents of specialization:
// static const char value[];
// Avoid infinite compile-time recursion
BOOST_MPL_ASSERT_MSG((!std::is_same<PointT, typename POD<PointT>::type>::value),
POINT_TYPE_NOT_PROPERLY_REGISTERED, (PointT&));
};
// offset
template<class PointT, typename Tag>
struct offset : offset<typename POD<PointT>::type, Tag>
{
// Contents of specialization:
// static const std::size_t value;
// Avoid infinite compile-time recursion
BOOST_MPL_ASSERT_MSG((!std::is_same<PointT, typename POD<PointT>::type>::value),
POINT_TYPE_NOT_PROPERLY_REGISTERED, (PointT&));
};
// datatype
template<class PointT, typename Tag>
struct datatype : datatype<typename POD<PointT>::type, Tag>
{
// Contents of specialization:
// using type = ...;
// static const std::uint8_t value;
// static const std::uint32_t size;
// Avoid infinite compile-time recursion
BOOST_MPL_ASSERT_MSG((!std::is_same<PointT, typename POD<PointT>::type>::value),
POINT_TYPE_NOT_PROPERLY_REGISTERED, (PointT&));
};
// fields
template<typename PointT>
struct fieldList : fieldList<typename POD<PointT>::type>
{
// Contents of specialization:
// using type = boost::mpl::vector<...>;
// Avoid infinite compile-time recursion
BOOST_MPL_ASSERT_MSG((!std::is_same<PointT, typename POD<PointT>::type>::value),
POINT_TYPE_NOT_PROPERLY_REGISTERED, (PointT&));
};
} //namespace traits
// Return true if the PCLPointField matches the expected name and data type.
// Written as a struct to allow partially specializing on Tag.
template<typename PointT, typename Tag>
struct FieldMatches
{
bool operator() (const pcl::PCLPointField& field)
{
return (field.name == traits::name<PointT, Tag>::value &&
field.datatype == traits::datatype<PointT, Tag>::value &&
(field.count == traits::datatype<PointT, Tag>::size ||
field.count == 0 && traits::datatype<PointT, Tag>::size == 1 /* see bug #821 */));
}
};
/** \brief A helper functor that can copy a specific value if the given field exists.
*
* \note In order to actually copy the value an instance of this functor should be passed
* to a pcl::for_each_type loop. See the example below.
*
* \code
* PointInT p;
* bool exists;
* float value;
* using FieldList = typename pcl::traits::fieldList<PointInT>::type;
* pcl::for_each_type<FieldList> (pcl::CopyIfFieldExists<PointT, float> (p, "intensity", exists, value));
* \endcode
*/
template <typename PointInT, typename OutT>
struct CopyIfFieldExists
{
using Pod = typename traits::POD<PointInT>::type;
/** \brief Constructor.
* \param[in] pt the input point
* \param[in] field the name of the field
* \param[out] exists set to true if the field exists, false otherwise
* \param[out] value the copied field value
*/
CopyIfFieldExists (const PointInT &pt,
const std::string &field,
bool &exists,
OutT &value)
: pt_ (reinterpret_cast<const Pod&>(pt)), name_ (field), exists_ (exists), value_ (value)
{
exists_ = false;
}
/** \brief Constructor.
* \param[in] pt the input point
* \param[in] field the name of the field
* \param[out] value the copied field value
*/
CopyIfFieldExists (const PointInT &pt,
const std::string &field,
OutT &value)
: pt_ (reinterpret_cast<const Pod&>(pt)), name_ (field), exists_ (exists_tmp_), value_ (value)
{
}
/** \brief Operator. Data copy happens here. */
template <typename Key> inline void
operator() ()
{
if (name_ == pcl::traits::name<PointInT, Key>::value)
{
exists_ = true;
using T = typename pcl::traits::datatype<PointInT, Key>::type;
const std::uint8_t* data_ptr = reinterpret_cast<const std::uint8_t*>(&pt_) + pcl::traits::offset<PointInT, Key>::value;
value_ = static_cast<OutT> (*reinterpret_cast<const T*>(data_ptr));
}
}
private:
const Pod &pt_;
const std::string &name_;
bool &exists_;
// Bogus entry
bool exists_tmp_;
OutT &value_;
};
/** \brief A helper functor that can set a specific value in a field if the field exists.
*
* \note In order to actually set the value an instance of this functor should be passed
* to a pcl::for_each_type loop. See the example below.
*
* \code
* PointT p;
* using FieldList = typename pcl::traits::fieldList<PointT>::type;
* pcl::for_each_type<FieldList> (pcl::SetIfFieldExists<PointT, float> (p, "intensity", 42.0f));
* \endcode
*/
template <typename PointOutT, typename InT>
struct SetIfFieldExists
{
using Pod = typename traits::POD<PointOutT>::type;
/** \brief Constructor.
* \param[in] pt the input point
* \param[in] field the name of the field
* \param[out] value the value to set
*/
SetIfFieldExists (PointOutT &pt,
const std::string &field,
const InT &value)
: pt_ (reinterpret_cast<Pod&>(pt)), name_ (field), value_ (value)
{
}
/** \brief Operator. Data copy happens here. */
template <typename Key> inline void
operator() ()
{
if (name_ == pcl::traits::name<PointOutT, Key>::value)
{
using T = typename pcl::traits::datatype<PointOutT, Key>::type;
std::uint8_t* data_ptr = reinterpret_cast<std::uint8_t*>(&pt_) + pcl::traits::offset<PointOutT, Key>::value;
*reinterpret_cast<T*>(data_ptr) = static_cast<T> (value_);
}
}
private:
Pod &pt_;
const std::string &name_;
const InT &value_;
};
/** \brief Set the value at a specified field in a point
* \param[out] pt the point to set the value to
* \param[in] field_offset the offset of the field
* \param[in] value the value to set
*/
template <typename PointT, typename ValT> inline void
setFieldValue (PointT &pt, std::size_t field_offset, const ValT &value)
{
std::uint8_t* data_ptr = reinterpret_cast<std::uint8_t*>(&pt) + field_offset;
*reinterpret_cast<ValT*>(data_ptr) = value;
}
/** \brief Get the value at a specified field in a point
* \param[in] pt the point to get the value from
* \param[in] field_offset the offset of the field
* \param[out] value the value to retrieve
*/
template <typename PointT, typename ValT> inline void
getFieldValue (const PointT &pt, std::size_t field_offset, ValT &value)
{
const std::uint8_t* data_ptr = reinterpret_cast<const std::uint8_t*>(&pt) + field_offset;
value = *reinterpret_cast<const ValT*>(data_ptr);
}
template <typename ...> using void_t = void; // part of std in c++17
#ifdef DOXYGEN_ONLY
/**
* \brief Tests at compile time if type T has a custom allocator
*
* \see pcl::make_shared, PCL_MAKE_ALIGNED_OPERATOR_NEW
* \tparam T Type of the object to test
*/
template <typename T> struct has_custom_allocator;
#else
template <typename, typename = void_t<>> struct has_custom_allocator : std::false_type {};
template <typename T> struct has_custom_allocator<T, void_t<typename T::_custom_allocator_type_trait>> : std::true_type {};
#endif
}
pcl::getFieldValue
void getFieldValue(const PointT &pt, std::size_t field_offset, ValT &value)
Get the value at a specified field in a point.
Definition: point_traits.h:335
pcl::traits::asType< pcl::PCLPointField::INT16 >::type
std::int16_t type
Definition: point_traits.h:90
pcl::PCLPointField::count
std::uint32_t count
Definition: PCLPointField.h:20
pcl_macros.h
Defines all the PCL and non-PCL macros used.
pcl
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:45
pcl::traits::offset
Definition: point_traits.h:153
pcl::traits::asType< pcl::PCLPointField::INT8 >::type
std::int8_t type
Definition: point_traits.h:88
pcl::uint32_t
std::uint32_t uint32_t
Definition: pcl_macros.h:96
Eigen
Definition: bfgs.h:9
pcl::traits::decomposeArray
Definition: point_traits.h:99
pcl::traits::asType< pcl::PCLPointField::UINT32 >::type
std::uint32_t type
Definition: point_traits.h:93
pcl::traits::datatype
Definition: point_traits.h:165
pcl::traits::fieldList::BOOST_MPL_ASSERT_MSG
BOOST_MPL_ASSERT_MSG((!std::is_same< PointT, typename POD< PointT >::type >::value), POINT_TYPE_NOT_PROPERLY_REGISTERED,(PointT &))
pcl::int32_t
std::int32_t int32_t
Definition: pcl_macros.h:97
pcl::SetIfFieldExists::Pod
typename traits::POD< PointOutT >::type Pod
Definition: point_traits.h:285
pcl::traits::asType< pcl::PCLPointField::FLOAT32 >::type
float type
Definition: point_traits.h:94
pcl::FieldMatches::operator()
bool operator()(const pcl::PCLPointField &field)
Definition: point_traits.h:195
pcl::traits::asEnum
Definition: point_traits.h:76
pcl::PCLPointField::INT8
Definition: PCLPointField.h:22
pcl::int16_t
std::int16_t int16_t
Definition: pcl_macros.h:95
pcl::traits::decomposeArray::type
std::remove_all_extents_t< T > type
Definition: point_traits.h:101
pcl::uint16_t
std::uint16_t uint16_t
Definition: pcl_macros.h:94
pcl::PCLPointField::INT32
Definition: PCLPointField.h:26
pcl::PointXYZRGB
A point structure representing Euclidean xyz coordinates, and the RGB color.
Definition: point_types.hpp:623
pcl::PCLPointField::FLOAT32
Definition: PCLPointField.h:28
pcl::PCLPointField::UINT32
Definition: PCLPointField.h:27
pcl::PCLPointField::datatype
std::uint8_t datatype
Definition: PCLPointField.h:19
pcl::PCLPointField::UINT8
Definition: PCLPointField.h:23
pcl::traits::asType< pcl::PCLPointField::UINT8 >::type
std::uint8_t type
Definition: point_traits.h:89
pcl::PCLPointField::INT16
Definition: PCLPointField.h:24
pcl::SetIfFieldExists
A helper functor that can set a specific value in a field if the field exists.
Definition: point_traits.h:283
pcl::traits::decomposeArray::value
static const std::uint32_t value
Definition: point_traits.h:102
pcl::deprecated::T
Definition: point_traits.h:65
pcl::PCLPointField::UINT16
Definition: PCLPointField.h:25
pcl::traits::offset::BOOST_MPL_ASSERT_MSG
BOOST_MPL_ASSERT_MSG((!std::is_same< PointT, typename POD< PointT >::type >::value), POINT_TYPE_NOT_PROPERLY_REGISTERED,(PointT &))
pcl::CopyIfFieldExists::CopyIfFieldExists
CopyIfFieldExists(const PointInT &pt, const std::string &field, bool &exists, OutT &value)
Constructor.
Definition: point_traits.h:228
pcl::int8_t
std::int8_t int8_t
Definition: pcl_macros.h:93
pcl::PCLPointField::FLOAT64
Definition: PCLPointField.h:29
pcl::has_custom_allocator
Tests at compile time if type T has a custom allocator.
Definition: point_traits.h:351
pcl::CopyIfFieldExists::CopyIfFieldExists
CopyIfFieldExists(const PointInT &pt, const std::string &field, OutT &value)
Constructor.
Definition: point_traits.h:242
pcl::traits::name
Definition: point_traits.h:141
pcl::void_t
void void_t
Definition: point_traits.h:341
pcl::SetIfFieldExists::SetIfFieldExists
SetIfFieldExists(PointOutT &pt, const std::string &field, const InT &value)
Constructor.
Definition: point_traits.h:292
pcl::traits::POD::type
PointT type
Definition: point_traits.h:109
pcl::CopyIfFieldExists
A helper functor that can copy a specific value if the given field exists.
Definition: point_traits.h:218
pcl::traits::name::BOOST_MPL_ASSERT_MSG
BOOST_MPL_ASSERT_MSG((!std::is_same< PointT, typename POD< PointT >::type >::value), POINT_TYPE_NOT_PROPERLY_REGISTERED,(PointT &))
pcl::traits::asType< pcl::PCLPointField::INT32 >::type
std::int32_t type
Definition: point_traits.h:92
pcl::uint8_t
std::uint8_t uint8_t
Definition: pcl_macros.h:92
pcl::PCLPointField::name
std::string name
Definition: PCLPointField.h:16
pcl::CopyIfFieldExists::Pod
typename traits::POD< PointInT >::type Pod
Definition: point_traits.h:220
pcl::traits::asType
Definition: point_traits.h:87
pcl::CopyIfFieldExists::operator()
void operator()()
Operator.
Definition: point_traits.h:251
pcl::traits::asType< pcl::PCLPointField::UINT16 >::type
std::uint16_t type
Definition: point_traits.h:91
pcl::PCLPointField
Definition: PCLPointField.h:14
pcl::FieldMatches
Definition: point_traits.h:193
pcl::setFieldValue
void setFieldValue(PointT &pt, std::size_t field_offset, const ValT &value)
Set the value at a specified field in a point.
Definition: point_traits.h:323
pcl::SetIfFieldExists::operator()
void operator()()
Operator.
Definition: point_traits.h:301
pcl::traits::asType< pcl::PCLPointField::FLOAT64 >::type
double type
Definition: point_traits.h:95
pcl::traits::POD
Definition: point_traits.h:107
pcl::traits::fieldList
Definition: point_traits.h:179
pcl::traits::datatype::BOOST_MPL_ASSERT_MSG
BOOST_MPL_ASSERT_MSG((!std::is_same< PointT, typename POD< PointT >::type >::value), POINT_TYPE_NOT_PROPERLY_REGISTERED,(PointT &))