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