Point Cloud Library (PCL)  1.15.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 #pragma once
39 
40 #include <pcl/field_traits.h>
41 #include <pcl/type_traits.h>
42 #include <pcl/for_each_type.h>
43 #include <pcl/common/concatenate.h>
44 #include <pcl/common/copy_point.h>
45 
46 #include <cstring> // for memcpy
47 
48 namespace pcl
49 {
50 
51 namespace detail
52 {
53 
54 /* CopyPointHelper and its specializations copy the contents of a source
55  * point to a target point. There are three cases:
56  *
57  * - Points have the same type.
58  * In this case a single `memcpy` is used.
59  *
60  * - Points have different types and one of the following is true:
61  * * both have RGB fields;
62  * * both have RGBA fields;
63  * * one or both have no RGB/RGBA fields.
64  * In this case we find the list of common fields and copy their
65  * contents one by one with `NdConcatenateFunctor`.
66  *
67  * - Points have different types and one of these types has RGB field, and
68  * the other has RGBA field.
69  * In this case we also find the list of common fields and copy their
70  * contents. In order to account for the fact that RGB and RGBA do not
71  * match we have an additional `memcpy` to copy the contents of one into
72  * another.
73  *
74  * An appropriate version of CopyPointHelper is instantiated during
75  * compilation time automatically, so there is absolutely no run-time
76  * overhead. */
77 
78 template <typename PointInT, typename PointOutT, typename Enable = void>
79 struct CopyPointHelper { };
80 
81 template <typename PointInT, typename PointOutT>
82 struct CopyPointHelper<PointInT, PointOutT, std::enable_if_t<std::is_same<PointInT, PointOutT>::value>>
83 {
84  void operator () (const PointInT& point_in, PointOutT& point_out) const
85  {
86  memcpy (&point_out, &point_in, sizeof (PointInT));
87  }
88 };
89 
90 template <typename PointInT, typename PointOutT>
91 struct CopyPointHelper<PointInT, PointOutT,
92  std::enable_if_t<boost::mpl::and_<boost::mpl::not_<std::is_same<PointInT, PointOutT>>,
93  boost::mpl::or_<boost::mpl::not_<pcl::traits::has_color<PointInT>>,
94  boost::mpl::not_<pcl::traits::has_color<PointOutT>>,
95  boost::mpl::and_<pcl::traits::has_field<PointInT, pcl::fields::rgb>,
96  pcl::traits::has_field<PointOutT, pcl::fields::rgb>>,
97  boost::mpl::and_<pcl::traits::has_field<PointInT, pcl::fields::rgba>,
98  pcl::traits::has_field<PointOutT, pcl::fields::rgba>>>>::value>>
99 {
100  void operator () (const PointInT& point_in, PointOutT& point_out) const
101  {
102  using FieldListInT = typename pcl::traits::fieldList<PointInT>::type;
103  using FieldListOutT = typename pcl::traits::fieldList<PointOutT>::type;
104  using FieldList = typename pcl::intersect<FieldListInT, FieldListOutT>::type;
105  pcl::for_each_type <FieldList> (pcl::NdConcatenateFunctor <PointInT, PointOutT> (point_in, point_out));
106  }
107 };
108 
109 template <typename PointInT, typename PointOutT>
110 struct CopyPointHelper<PointInT, PointOutT,
111  std::enable_if_t<boost::mpl::and_<boost::mpl::not_<std::is_same<PointInT, PointOutT>>,
112  boost::mpl::or_<boost::mpl::and_<pcl::traits::has_field<PointInT, pcl::fields::rgb>,
113  pcl::traits::has_field<PointOutT, pcl::fields::rgba>>,
114  boost::mpl::and_<pcl::traits::has_field<PointInT, pcl::fields::rgba>,
115  pcl::traits::has_field<PointOutT, pcl::fields::rgb>>>>::value>>
116 {
117  void operator () (const PointInT& point_in, PointOutT& point_out) const
118  {
119  using FieldListInT = typename pcl::traits::fieldList<PointInT>::type;
120  using FieldListOutT = typename pcl::traits::fieldList<PointOutT>::type;
121  using FieldList = typename pcl::intersect<FieldListInT, FieldListOutT>::type;
122  constexpr std::uint32_t offset_in = boost::mpl::if_<pcl::traits::has_field<PointInT, pcl::fields::rgb>,
123  pcl::traits::offset<PointInT, pcl::fields::rgb>,
124  pcl::traits::offset<PointInT, pcl::fields::rgba> >::type::value;
125  constexpr std::uint32_t offset_out = boost::mpl::if_<pcl::traits::has_field<PointOutT, pcl::fields::rgb>,
126  pcl::traits::offset<PointOutT, pcl::fields::rgb>,
127  pcl::traits::offset<PointOutT, pcl::fields::rgba> >::type::value;
128  pcl::for_each_type <FieldList> (pcl::NdConcatenateFunctor <PointInT, PointOutT> (point_in, point_out));
129  memcpy (reinterpret_cast<char*> (&point_out) + offset_out,
130  reinterpret_cast<const char*> (&point_in) + offset_in,
131  4);
132  }
133 };
134 
135 } // namespace detail
136 
137 template <typename PointInT, typename PointOutT> void
138 copyPoint (const PointInT& point_in, PointOutT& point_out)
139 {
141  copy (point_in, point_out);
142 }
143 
144 } // namespace pcl
145 
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
Helper functor structure for concatenate.
Definition: concatenate.h:50
typename boost::mpl::remove_if< Sequence1, boost::mpl::not_< boost::mpl::contains< Sequence2, boost::mpl::_1 > > >::type type