Point Cloud Library (PCL)  1.9.1-dev
copy_point.hpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2014-, Open Perception, Inc.
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of the copyright holder(s) nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  */
37 
38 #ifndef PCL_COMMON_IMPL_COPY_POINT_HPP_
39 #define PCL_COMMON_IMPL_COPY_POINT_HPP_
40 
41 #include <pcl/point_types.h>
42 #include <pcl/point_traits.h>
43 #include <pcl/for_each_type.h>
44 #include <pcl/common/concatenate.h>
45 
46 namespace pcl
47 {
48 
49  namespace detail
50  {
51 
52  /* CopyPointHelper and its specializations copy the contents of a source
53  * point to a target point. There are three cases:
54  *
55  * - Points have the same type.
56  * In this case a single `memcpy` is used.
57  *
58  * - Points have different types and one of the following is true:
59  * * both have RGB fields;
60  * * both have RGBA fields;
61  * * one or both have no RGB/RGBA fields.
62  * In this case we find the list of common fields and copy their
63  * contents one by one with `NdConcatenateFunctor`.
64  *
65  * - Points have different types and one of these types has RGB field, and
66  * the other has RGBA field.
67  * In this case we also find the list of common fields and copy their
68  * contents. In order to account for the fact that RGB and RGBA do not
69  * match we have an additional `memcpy` to copy the contents of one into
70  * another.
71  *
72  * An appropriate version of CopyPointHelper is instantiated during
73  * compilation time automatically, so there is absolutely no run-time
74  * overhead. */
75 
76  template <typename PointInT, typename PointOutT, typename Enable = void>
77  struct CopyPointHelper { };
78 
79  template <typename PointInT, typename PointOutT>
80  struct CopyPointHelper<PointInT, PointOutT, std::enable_if_t<std::is_same<PointInT, PointOutT>::value>>
81  {
82  void operator () (const PointInT& point_in, PointOutT& point_out) const
83  {
84  memcpy (&point_out, &point_in, sizeof (PointInT));
85  }
86  };
87 
88  template <typename PointInT, typename PointOutT>
89  struct CopyPointHelper<PointInT, PointOutT,
90  std::enable_if_t<boost::mpl::and_<boost::mpl::not_<std::is_same<PointInT, PointOutT>>,
91  boost::mpl::or_<boost::mpl::not_<pcl::traits::has_color<PointInT>>,
92  boost::mpl::not_<pcl::traits::has_color<PointOutT>>,
93  boost::mpl::and_<pcl::traits::has_field<PointInT, pcl::fields::rgb>,
94  pcl::traits::has_field<PointOutT, pcl::fields::rgb>>,
95  boost::mpl::and_<pcl::traits::has_field<PointInT, pcl::fields::rgba>,
96  pcl::traits::has_field<PointOutT, pcl::fields::rgba>>>>::value>>
97  {
98  void operator () (const PointInT& point_in, PointOutT& point_out) const
99  {
100  using FieldListInT = typename pcl::traits::fieldList<PointInT>::type;
101  using FieldListOutT = typename pcl::traits::fieldList<PointOutT>::type;
102  using FieldList = typename pcl::intersect<FieldListInT, FieldListOutT>::type;
103  pcl::for_each_type <FieldList> (pcl::NdConcatenateFunctor <PointInT, PointOutT> (point_in, point_out));
104  }
105  };
106 
107  template <typename PointInT, typename PointOutT>
108  struct CopyPointHelper<PointInT, PointOutT,
109  std::enable_if_t<boost::mpl::and_<boost::mpl::not_<std::is_same<PointInT, PointOutT>>,
110  boost::mpl::or_<boost::mpl::and_<pcl::traits::has_field<PointInT, pcl::fields::rgb>,
111  pcl::traits::has_field<PointOutT, pcl::fields::rgba>>,
112  boost::mpl::and_<pcl::traits::has_field<PointInT, pcl::fields::rgba>,
113  pcl::traits::has_field<PointOutT, pcl::fields::rgb>>>>::value>>
114  {
115  void operator () (const PointInT& point_in, PointOutT& point_out) const
116  {
117  using FieldListInT = typename pcl::traits::fieldList<PointInT>::type;
118  using FieldListOutT = typename pcl::traits::fieldList<PointOutT>::type;
119  using FieldList = typename pcl::intersect<FieldListInT, FieldListOutT>::type;
120  const uint32_t offset_in = boost::mpl::if_<pcl::traits::has_field<PointInT, pcl::fields::rgb>,
123  const uint32_t offset_out = boost::mpl::if_<pcl::traits::has_field<PointOutT, pcl::fields::rgb>,
126  pcl::for_each_type <FieldList> (pcl::NdConcatenateFunctor <PointInT, PointOutT> (point_in, point_out));
127  memcpy (reinterpret_cast<char*> (&point_out) + offset_out,
128  reinterpret_cast<const char*> (&point_in) + offset_in,
129  4);
130  }
131  };
132 
133  }
134 
135 }
136 
137 template <typename PointInT, typename PointOutT> void
138 pcl::copyPoint (const PointInT& point_in, PointOutT& point_out)
139 {
141  copy (point_in, point_out);
142 }
143 
144 #endif //PCL_COMMON_IMPL_COPY_POINT_HPP_
145 
typename boost::mpl::remove_if< Sequence1, boost::mpl::not_< boost::mpl::contains< Sequence2, boost::mpl::_1 > > >::type type
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:45
Defines all the PCL implemented PointT point type structures.
Helper functor structure for concatenate.
Definition: concatenate.h:51
void copyPoint(const PointInT &point_in, PointOutT &point_out)
Copy the fields of a source point into a target point.
Definition: copy_point.hpp:138