accumulators.h
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Point Cloud Library (PCL) - www.pointclouds.org
00005  *  Copyright (c) 2014-, Open Perception, Inc.
00006  *
00007  *  All rights reserved.
00008  *
00009  *  Redistribution and use in source and binary forms, with or without
00010  *  modification, are permitted provided that the following conditions
00011  *  are met:
00012  *
00013  *   * Redistributions of source code must retain the above copyright
00014  *     notice, this list of conditions and the following disclaimer.
00015  *   * Redistributions in binary form must reproduce the above
00016  *     copyright notice, this list of conditions and the following
00017  *     disclaimer in the documentation and/or other materials provided
00018  *     with the distribution.
00019  *   * Neither the name of the copyright holder(s) nor the names of its
00020  *     contributors may be used to endorse or promote products derived
00021  *     from this software without specific prior written permission.
00022  *
00023  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00024  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00025  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00026  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00027  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00028  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00029  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00030  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00032  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00033  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00034  *  POSSIBILITY OF SUCH DAMAGE.
00035  *
00036  */
00037 
00038 #ifndef PCL18_BACKPORTS_ACCUMULATORS_H
00039 #define PCL18_BACKPORTS_ACCUMULATORS_H
00040 
00041 #ifdef BACKPORT_PCL_CENTROID
00042 
00043 #include <map>
00044 #include <vector>
00045 
00046 #include <boost/mpl/filter_view.hpp>
00047 #include <boost/fusion/include/mpl.hpp>
00048 #include <boost/fusion/include/for_each.hpp>
00049 #include <boost/fusion/include/as_vector.hpp>
00050 #include <boost/fusion/include/filter_if.hpp>
00051 
00052 #include <pcl18_backports/point_types.h>
00053 
00054 namespace pcl
00055 {
00056 namespace detail
00057 {
00058 /* Below are several helper accumulator structures that are used by the
00059      * `CentroidPoint` class. Each of them is capable of accumulating
00060      * information from a particular field(s) of a point. The points are
00061      * inserted via `add()` and extracted via `get()` functions. Note that the
00062      * accumulators are not templated on point type, so in principle it is
00063      * possible to insert and extract points of different types. It is the
00064      * responsibility of the user to make sure that points have corresponding
00065      * fields. */
00066 
00067 struct AccumulatorXYZ
00068 {
00069   // Requires that point type has x, y, and z fields
00070   typedef pcl::traits::has_xyz<boost::mpl::_1> IsCompatible;
00071 
00072   // Storage
00073   Eigen::Vector3f xyz;
00074 
00075   inline AccumulatorXYZ()
00076     : xyz(Eigen::Vector3f::Zero())
00077   {
00078   }
00079 
00080   template <typename PointT>
00081   void add(const PointT& t)
00082   {
00083     xyz += t.getVector3fMap();
00084   }
00085 
00086   template <typename PointT>
00087   void get(PointT& t, size_t n) const
00088   {
00089     t.getVector3fMap() = xyz / n;
00090   }
00091 
00092   EIGEN_MAKE_ALIGNED_OPERATOR_NEW
00093 };
00094 
00095 struct AccumulatorNormal
00096 {
00097   // Requires that point type has normal_x, normal_y, and normal_z fields
00098   typedef pcl::traits::has_normal<boost::mpl::_1> IsCompatible;
00099 
00100   // Storage
00101   Eigen::Vector4f normal;
00102 
00103   inline AccumulatorNormal()
00104     : normal(Eigen::Vector4f::Zero())
00105   {
00106   }
00107 
00108   // Requires that the normal of the given point is normalized, otherwise it
00109   // does not make sense to sum it up with the accumulated value.
00110   template <typename PointT>
00111   void add(const PointT& t)
00112   {
00113     normal += t.getNormalVector4fMap();
00114   }
00115 
00116   template <typename PointT>
00117   void get(PointT& t, size_t) const
00118   {
00119 #if EIGEN_VERSION_AT_LEAST(3, 3, 0)
00120     t.getNormalVector4fMap() = normal.normalized();
00121 #else
00122     if (normal.squaredNorm() > 0)
00123       t.getNormalVector4fMap() = normal.normalized();
00124     else
00125       t.getNormalVector4fMap() = Eigen::Vector4f::Zero();
00126 #endif
00127   }
00128 
00129   EIGEN_MAKE_ALIGNED_OPERATOR_NEW
00130 };
00131 
00132 struct AccumulatorCurvature
00133 {
00134   // Requires that point type has curvature field
00135   typedef pcl::traits::has_curvature<boost::mpl::_1> IsCompatible;
00136 
00137   // Storage
00138   float curvature;
00139 
00140   inline AccumulatorCurvature()
00141     : curvature(0)
00142   {
00143   }
00144 
00145   template <typename PointT>
00146   void add(const PointT& t)
00147   {
00148     curvature += t.curvature;
00149   }
00150 
00151   template <typename PointT>
00152   void get(PointT& t, size_t n) const
00153   {
00154     t.curvature = curvature / n;
00155   }
00156 };
00157 
00158 struct AccumulatorRGBA
00159 {
00160   // Requires that point type has rgb or rgba field
00161   typedef pcl::traits::has_color<boost::mpl::_1> IsCompatible;
00162 
00163   // Storage
00164   float r, g, b, a;
00165 
00166   inline AccumulatorRGBA()
00167     : r(0)
00168     , g(0)
00169     , b(0)
00170     , a(0)
00171   {
00172   }
00173 
00174   template <typename PointT>
00175   void add(const PointT& t)
00176   {
00177     r += static_cast<float>(t.r);
00178     g += static_cast<float>(t.g);
00179     b += static_cast<float>(t.b);
00180     a += static_cast<float>(t.a);
00181   }
00182 
00183   template <typename PointT>
00184   void get(PointT& t, size_t n) const
00185   {
00186     t.rgba = static_cast<uint32_t>(a / n) << 24 |
00187              static_cast<uint32_t>(r / n) << 16 |
00188              static_cast<uint32_t>(g / n) << 8 |
00189              static_cast<uint32_t>(b / n);
00190   }
00191 };
00192 
00193 struct AccumulatorIntensity
00194 {
00195   // Requires that point type has intensity field
00196   typedef pcl::traits::has_intensity<boost::mpl::_1> IsCompatible;
00197 
00198   // Storage
00199   float intensity;
00200 
00201   inline AccumulatorIntensity()
00202     : intensity(0)
00203   {
00204   }
00205 
00206   template <typename PointT>
00207   void add(const PointT& t)
00208   {
00209     intensity += t.intensity;
00210   }
00211 
00212   template <typename PointT>
00213   void get(PointT& t, size_t n) const
00214   {
00215     t.intensity = intensity / n;
00216   }
00217 };
00218 
00219 struct AccumulatorLabel
00220 {
00221   // Requires that point type has label field
00222   typedef pcl::traits::has_label<boost::mpl::_1> IsCompatible;
00223 
00224   // Storage
00225   // A better performance may be achieved with a heap structure
00226   std::map<uint32_t, size_t> labels;
00227 
00228   inline AccumulatorLabel()
00229   {
00230   }
00231 
00232   template <typename PointT>
00233   void add(const PointT& t)
00234   {
00235     auto itr = labels.find(t.label);
00236     if (itr == labels.end())
00237       labels.insert(std::make_pair(t.label, 1));
00238     else
00239       ++itr->second;
00240   }
00241 
00242   template <typename PointT>
00243   void get(PointT& t, size_t) const
00244   {
00245     size_t max = 0;
00246     for (const auto& label : labels)
00247       if (label.second > max)
00248       {
00249         max = label.second;
00250         t.label = label.first;
00251       }
00252   }
00253 };
00254 
00255 /* Meta-function that checks if an accumulator is compatible with given
00256      * point type(s). */
00257 template <typename Point1T, typename Point2T = Point1T>
00258 struct IsAccumulatorCompatible
00259 {
00260   template <typename AccumulatorT>
00261   struct apply : boost::mpl::and_<
00262                      boost::mpl::apply<typename AccumulatorT::IsCompatible, Point1T>,
00263                      boost::mpl::apply<typename AccumulatorT::IsCompatible, Point2T>>
00264   {
00265   };
00266 };
00267 
00268 /* Meta-function that creates a Fusion vector of accumulator types that are
00269      * compatible with a given point type. */
00270 template <typename PointT>
00271 struct Accumulators
00272 {
00273   typedef
00274       typename boost::fusion::result_of::as_vector<
00275           typename boost::mpl::filter_view<
00276               boost::mpl::vector<
00277                   AccumulatorXYZ, AccumulatorNormal, AccumulatorCurvature,
00278                   AccumulatorRGBA, AccumulatorIntensity, AccumulatorLabel>,
00279               IsAccumulatorCompatible<PointT>>>::type
00280           type;
00281 };
00282 
00283 /* Fusion function object to invoke point addition on every accumulator in
00284      * a fusion sequence. */
00285 template <typename PointT>
00286 struct AddPoint
00287 {
00288   const PointT& p;
00289 
00290   explicit AddPoint(const PointT& point)
00291     : p(point)
00292   {
00293   }
00294 
00295   template <typename AccumulatorT>
00296   void operator()(AccumulatorT& accumulator) const
00297   {
00298     accumulator.add(p);
00299   }
00300 };
00301 
00302 /* Fusion function object to invoke get point on every accumulator in a
00303      * fusion sequence. */
00304 template <typename PointT>
00305 struct GetPoint
00306 {
00307   PointT& p;
00308   size_t n;
00309 
00310   GetPoint(PointT& point, size_t num)
00311     : p(point)
00312     , n(num)
00313   {
00314   }
00315 
00316   template <typename AccumulatorT>
00317   void operator()(AccumulatorT& accumulator) const
00318   {
00319     accumulator.get(p, n);
00320   }
00321 };
00322 }  // namespace detail
00323 }  // namespace pcl
00324 
00325 #endif  // BACKPORT_PCL_CENTROID
00326 #endif  // PCL18_BACKPORTS_ACCUMULATORS_H


mcl_3dl
Author(s): Atsushi Watanabe
autogenerated on Thu Jun 20 2019 20:04:43