Point Cloud Library (PCL)  1.13.1-dev
trajkovic_3d.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2013-, 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 Willow Garage, Inc. 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/keypoints/keypoint.h>
41 #include <pcl/common/intensity.h>
42 
43 namespace pcl
44 {
45  /** \brief TrajkovicKeypoint3D implements Trajkovic and Hedley corner detector on
46  * point cloud using geometric information.
47  * It uses first order statistics to find variation of normals.
48  * This work is part of Nizar Sallem PhD thesis.
49  *
50  * \author Nizar Sallem
51  * \ingroup keypoints
52  */
53  template <typename PointInT, typename PointOutT, typename NormalT = pcl::Normal>
54  class TrajkovicKeypoint3D : public Keypoint<PointInT, PointOutT>
55  {
56  public:
57  using Ptr = shared_ptr<TrajkovicKeypoint3D<PointInT, PointOutT, NormalT> >;
58  using ConstPtr = shared_ptr<const TrajkovicKeypoint3D<PointInT, PointOutT, NormalT> >;
61  using PointCloudInConstPtr = typename PointCloudIn::ConstPtr;
63  using NormalsPtr = typename Normals::Ptr;
65 
71 
73 
74  /** \brief Constructor
75  * \param[in] method the method to be used to determine the corner responses
76  * \param[in] window_size
77  * \param[in] first_threshold the threshold used in the simple cornerness test.
78  * \param[in] second_threshold the threshold used to reject weak corners.
79  */
81  int window_size = 3,
82  float first_threshold = 0.00046,
83  float second_threshold = 0.03589)
84  : method_ (method)
85  , window_size_ (window_size)
86  , first_threshold_ (first_threshold)
87  , second_threshold_ (second_threshold)
88  , threads_ (1)
89  {
90  name_ = "TrajkovicKeypoint3D";
91  }
92 
93  /** \brief set the method of the response to be calculated.
94  * \param[in] method either 4 corners or 8 corners
95  */
96  inline void
97  setMethod (ComputationMethod method) { method_ = method; }
98 
99  /// \brief \return the computation method
100  inline ComputationMethod
101  getMethod () const { return (method_); }
102 
103  /// \brief Set window size
104  inline void
105  setWindowSize (int window_size) { window_size_= window_size; }
106 
107  /// \brief \return window size i.e. window width or height
108  inline int
109  getWindowSize () const { return (window_size_); }
110 
111  /** \brief set the first_threshold to reject corners in the simple cornerness
112  * computation stage.
113  * \param[in] threshold
114  */
115  inline void
116  setFirstThreshold (float threshold) { first_threshold_= threshold; }
117 
118  /// \brief \return first threshold
119  inline float
120  getFirstThreshold () const { return (first_threshold_); }
121 
122  /** \brief set the second threshold to reject corners in the final cornerness
123  * computation stage.
124  * \param[in] threshold
125  */
126  inline void
127  setSecondThreshold (float threshold) { second_threshold_= threshold; }
128 
129  /// \brief \return second threshold
130  inline float
131  getSecondThreshold () const { return (second_threshold_); }
132 
133  /** \brief Set normals if precalculated normals are available.
134  * \param normals
135  */
136  inline void
137  setNormals (const NormalsConstPtr &normals) { normals_ = normals; }
138 
139  /// \brief \return points normals as calculated or given
140  inline void
141  getNormals () const { return (normals_); }
142 
143  /** \brief Initialize the scheduler and set the number of threads to use.
144  * \param nr_threads the number of hardware threads to use, 0 for automatic.
145  */
146  inline void
147  setNumberOfThreads (unsigned int nr_threads = 0) { threads_ = nr_threads; }
148 
149  /// \brief \return the number of threads
150  inline unsigned int
151  getNumberOfThreads () const { return (threads_); }
152 
153  protected:
154  bool
155  initCompute () override;
156 
157  void
158  detectKeypoints (PointCloudOut &output) override;
159 
160  private:
161  /** Return a const reference to the normal at (i,j) if it is finite else return
162  * a reference to a null normal.
163  * If the returned normal is valid \a counter is incremented.
164  */
165  inline const NormalT&
166  getNormalOrNull (int i, int j, int& counter) const
167  {
168  static const NormalT null;
169  if (!isFinite ((*normals_) (i,j))) return (null);
170  ++counter;
171  return ((*normals_) (i,j));
172  }
173  /// \return difference of two normals vectors
174  inline float
175  normalsDiff (const NormalT& a, const NormalT& b) const
176  {
177  double nx = a.normal_x; double ny = a.normal_y; double nz = a.normal_z;
178  double mx = b.normal_x; double my = b.normal_y; double mz = b.normal_z;
179  return (static_cast<float> (1.0 - (nx*mx + ny*my + nz*mz)));
180  }
181  /// \return squared difference of two normals vectors
182  inline float
183  squaredNormalsDiff (const NormalT& a, const NormalT& b) const
184  {
185  float diff = normalsDiff (a,b);
186  return (diff * diff);
187  }
188  /** Comparator for responses intensity
189  * \return true if \a response_ at index \aa is greater than response at index \ab
190  */
191  inline bool
192  greaterCornernessAtIndices (int a, int b) const
193  {
194  return (response_->points [a] > response_->points [b]);
195  }
196  /// computation method
197  ComputationMethod method_;
198  /// window size
199  int window_size_;
200  /// half window size
201  int half_window_size_;
202  /// first threshold for quick rejection
203  float first_threshold_;
204  /// second threshold for corner evaluation
205  float second_threshold_;
206  /// number of threads to be used
207  unsigned int threads_;
208  /// point cloud normals
209  NormalsConstPtr normals_;
210  /// point cloud response
211  pcl::PointCloud<float>::Ptr response_;
212  };
213 }
214 
215 #include <pcl/keypoints/impl/trajkovic_3d.hpp>
Keypoint represents the base class for key points.
Definition: keypoint.h:49
std::string name_
The key point detection method's name.
Definition: keypoint.h:169
shared_ptr< PointCloud< NormalT > > Ptr
Definition: point_cloud.h:413
std::vector< PointT, Eigen::aligned_allocator< PointT > > points
The point data.
Definition: point_cloud.h:395
shared_ptr< const PointCloud< NormalT > > ConstPtr
Definition: point_cloud.h:414
TrajkovicKeypoint3D implements Trajkovic and Hedley corner detector on point cloud using geometric in...
Definition: trajkovic_3d.h:55
typename Keypoint< PointInT, PointOutT >::PointCloudOut PointCloudOut
Definition: trajkovic_3d.h:60
shared_ptr< TrajkovicKeypoint3D< PointInT, PointOutT, NormalT > > Ptr
Definition: trajkovic_3d.h:57
typename Keypoint< PointInT, PointOutT >::PointCloudIn PointCloudIn
Definition: trajkovic_3d.h:59
void setFirstThreshold(float threshold)
set the first_threshold to reject corners in the simple cornerness computation stage.
Definition: trajkovic_3d.h:116
bool initCompute() override
typename Normals::ConstPtr NormalsConstPtr
Definition: trajkovic_3d.h:64
unsigned int getNumberOfThreads() const
Definition: trajkovic_3d.h:151
ComputationMethod getMethod() const
Definition: trajkovic_3d.h:101
void setMethod(ComputationMethod method)
set the method of the response to be calculated.
Definition: trajkovic_3d.h:97
void detectKeypoints(PointCloudOut &output) override
typename PointCloudIn::ConstPtr PointCloudInConstPtr
Definition: trajkovic_3d.h:61
void setWindowSize(int window_size)
Set window size.
Definition: trajkovic_3d.h:105
float getSecondThreshold() const
Definition: trajkovic_3d.h:131
void setNumberOfThreads(unsigned int nr_threads=0)
Initialize the scheduler and set the number of threads to use.
Definition: trajkovic_3d.h:147
float getFirstThreshold() const
Definition: trajkovic_3d.h:120
shared_ptr< const TrajkovicKeypoint3D< PointInT, PointOutT, NormalT > > ConstPtr
Definition: trajkovic_3d.h:58
void setNormals(const NormalsConstPtr &normals)
Set normals if precalculated normals are available.
Definition: trajkovic_3d.h:137
TrajkovicKeypoint3D(ComputationMethod method=FOUR_CORNERS, int window_size=3, float first_threshold=0.00046, float second_threshold=0.03589)
Constructor.
Definition: trajkovic_3d.h:80
void setSecondThreshold(float threshold)
set the second threshold to reject corners in the final cornerness computation stage.
Definition: trajkovic_3d.h:127
typename Normals::Ptr NormalsPtr
Definition: trajkovic_3d.h:63
bool isFinite(const PointT &pt)
Tests if the 3D components of a point are all finite param[in] pt point to be tested return true if f...
Definition: point_tests.h:55
A point structure representing normal coordinates and the surface curvature estimate.