Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a family of "has field" meta-functions #462

Merged
merged 2 commits into from
Jan 23, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions common/include/pcl/point_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
#include <pcl/pcl_macros.h>
#include <bitset>
#include <pcl/register_point_struct.h>
#include <boost/mpl/contains.hpp>
#include <boost/mpl/fold.hpp>
#include <boost/mpl/vector.hpp>

/**
* \file pcl/point_types.h
Expand Down Expand Up @@ -666,6 +669,79 @@ namespace pcl
}
}
};

namespace traits
{

/** \brief Metafunction to check if a given point type has a given field.
*
* Example usage at run-time:
*
* \code
* bool curvature_available = pcl::traits::has_field<PointT, pcl::fields::curvature>::value;
* \endcode
*
* Example usage at compile-time:
*
* \code
* BOOST_MPL_ASSERT_MSG ((pcl::traits::has_field<PointT, pcl::fields::label>::value),
* POINT_TYPE_SHOULD_HAVE_LABEL_FIELD,
* (PointT));
* \endcode
*/
template <typename PointT, typename Field>
struct has_field : boost::mpl::contains<typename pcl::traits::fieldList<PointT>::type, Field>::type
{ };

/** Metafunction to check if a given point type has all given fields. */
template <typename PointT, typename Field>
struct has_all_fields : boost::mpl::fold<Field,
boost::mpl::bool_<true>,
boost::mpl::and_<boost::mpl::_1,
has_field<PointT, boost::mpl::_2> > >::type
{ };

/** Metafunction to check if a given point type has any of the given fields. */
template <typename PointT, typename Field>
struct has_any_field : boost::mpl::fold<Field,
boost::mpl::bool_<false>,
boost::mpl::or_<boost::mpl::_1,
has_field<PointT, boost::mpl::_2> > >::type
{ };

/** Metafunction to check if a given point type has x, y, and z fields. */
template <typename PointT>
struct has_xyz : has_all_fields<PointT, boost::mpl::vector<pcl::fields::x,
pcl::fields::y,
pcl::fields::z> >
{ };

/** Metafunction to check if a given point type has normal_x, normal_y, and
* normal_z fields. */
template <typename PointT>
struct has_normal : has_all_fields<PointT, boost::mpl::vector<pcl::fields::normal_x,
pcl::fields::normal_y,
pcl::fields::normal_z> >
{ };

/** Metafunction to check if a given point type has curvature field. */
template <typename PointT>
struct has_curvature : has_field<PointT, pcl::fields::curvature>
{ };

/** Metafunction to check if a given point type has either rgb or rgba field. */
template <typename PointT>
struct has_color : has_any_field<PointT, boost::mpl::vector<pcl::fields::rgb,
pcl::fields::rgba> >
{ };

/** Metafunction to check if a given point type has label field. */
template <typename PointT>
struct has_label : has_field<PointT, pcl::fields::label>
{ };

}

} // namespace pcl

#if defined _MSC_VER
Expand Down
31 changes: 31 additions & 0 deletions test/common/test_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1140,6 +1140,37 @@ TEST (PCL, IsSamePointType)
EXPECT_FALSE (status);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TEST (PCL, HasField)
{
// has_field
EXPECT_TRUE ((pcl::traits::has_field<pcl::Normal, pcl::fields::curvature>::value));
EXPECT_FALSE ((pcl::traits::has_field<pcl::PointXYZ, pcl::fields::curvature>::value));
// has_all_fields
EXPECT_TRUE ((pcl::traits::has_all_fields<pcl::PointXYZRGB, boost::mpl::vector<pcl::fields::x, pcl::fields::rgb> >::value));
EXPECT_FALSE ((pcl::traits::has_all_fields<pcl::PointXYZ, boost::mpl::vector<pcl::fields::x, pcl::fields::rgb> >::value));
// has_any_field
EXPECT_TRUE ((pcl::traits::has_any_field<pcl::PointXYZ, boost::mpl::vector<pcl::fields::x, pcl::fields::normal_x> >::value));
EXPECT_TRUE ((pcl::traits::has_any_field<pcl::Normal, boost::mpl::vector<pcl::fields::x, pcl::fields::normal_x> >::value));
EXPECT_FALSE ((pcl::traits::has_any_field<pcl::RGB, boost::mpl::vector<pcl::fields::x, pcl::fields::normal_x> >::value));
// has_xyz
EXPECT_TRUE ((pcl::traits::has_xyz<pcl::PointXYZ>::value));
EXPECT_FALSE ((pcl::traits::has_xyz<pcl::Normal>::value));
// has_normal
EXPECT_TRUE ((pcl::traits::has_normal<pcl::PointNormal>::value));
EXPECT_FALSE ((pcl::traits::has_normal<pcl::PointXYZ>::value));
// has_curvature
EXPECT_TRUE ((pcl::traits::has_curvature<pcl::PointNormal>::value));
EXPECT_FALSE ((pcl::traits::has_curvature<pcl::RGB>::value));
// has_color
EXPECT_TRUE ((pcl::traits::has_color<pcl::PointXYZRGB>::value));
EXPECT_TRUE ((pcl::traits::has_color<pcl::PointXYZRGBA>::value));
EXPECT_FALSE ((pcl::traits::has_color<pcl::PointXYZ>::value));
// has_label
EXPECT_TRUE ((pcl::traits::has_label<pcl::PointXYZL>::value));
EXPECT_FALSE ((pcl::traits::has_label<pcl::Normal>::value));
}

/* ---[ */
int
main (int argc, char** argv)
Expand Down