Point Cloud Library (PCL)  1.14.0-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  {
89  name_ = "TrajkovicKeypoint3D";
90  }
91 
92  /** \brief set the method of the response to be calculated.
93  * \param[in] method either 4 corners or 8 corners
94  */
95  inline void
96  setMethod (ComputationMethod method) { method_ = method; }
97 
98  /// \brief \return the computation method
99  inline ComputationMethod
100  getMethod () const { return (method_); }
101 
102  /// \brief Set window size
103  inline void
104  setWindowSize (int window_size) { window_size_= window_size; }
105 
106  /// \brief \return window size i.e. window width or height
107  inline int
108  getWindowSize () const { return (window_size_); }
109 
110  /** \brief set the first_threshold to reject corners in the simple cornerness
111  * computation stage.
112  * \param[in] threshold
113  */
114  inline void
115  setFirstThreshold (float threshold) { first_threshold_= threshold; }
116 
117  /// \brief \return first threshold
118  inline float
119  getFirstThreshold () const { return (first_threshold_); }
120 
121  /** \brief set the second threshold to reject corners in the final cornerness
122  * computation stage.
123  * \param[in] threshold
124  */
125  inline void
126  setSecondThreshold (float threshold) { second_threshold_= threshold; }
127 
128  /// \brief \return second threshold
129  inline float
130  getSecondThreshold () const { return (second_threshold_); }
131 
132  /** \brief Set normals if precalculated normals are available.
133  * \param normals
134  */
135  inline void
136  setNormals (const NormalsConstPtr &normals) { normals_ = normals; }
137 
138  /// \brief \return points normals as calculated or given
139  inline void
140  getNormals () const { return (normals_); }
141 
142  /** \brief Initialize the scheduler and set the number of threads to use.
143  * \param nr_threads the number of hardware threads to use, 0 for automatic.
144  */
145  inline void
146  setNumberOfThreads (unsigned int nr_threads = 0) { threads_ = nr_threads; }
147 
148  /// \brief \return the number of threads
149  inline unsigned int
150  getNumberOfThreads () const { return (threads_); }
151 
152  protected:
153  bool
154  initCompute () override;
155 
156  void
157  detectKeypoints (PointCloudOut &output) override;
158 
159  private:
160  /** Return a const reference to the normal at (i,j) if it is finite else return
161  * a reference to a null normal.
162  * If the returned normal is valid \a counter is incremented.
163  */
164  inline const NormalT&
165  getNormalOrNull (int i, int j, int& counter) const
166  {
167  static const NormalT null;
168  if (!isFinite ((*normals_) (i,j))) return (null);
169  ++counter;
170  return ((*normals_) (i,j));
171  }
172  /// \return difference of two normals vectors
173  inline float
174  normalsDiff (const NormalT& a, const NormalT& b) const
175  {
176  double nx = a.normal_x; double ny = a.normal_y; double nz = a.normal_z;
177  double mx = b.normal_x; double my = b.normal_y; double mz = b.normal_z;
178  return (static_cast<float> (1.0 - (nx*mx + ny*my + nz*mz)));
179  }
180  /// \return squared difference of two normals vectors
181  inline float
182  squaredNormalsDiff (const NormalT& a, const NormalT& b) const
183  {
184  float diff = normalsDiff (a,b);
185  return (diff * diff);
186  }
187  /** Comparator for responses intensity
188  * \return true if \a response_ at index \aa is greater than response at index \ab
189  */
190  inline bool
191  greaterCornernessAtIndices (int a, int b) const
192  {
193  return (response_->points [a] > response_->points [b]);
194  }
195  /// computation method
196  ComputationMethod method_;
197  /// window size
198  int window_size_;
199  /// half window size
200  int half_window_size_;
201  /// first threshold for quick rejection
202  float first_threshold_;
203  /// second threshold for corner evaluation
204  float second_threshold_;
205  /// number of threads to be used
206  unsigned int threads_{1};
207  /// point cloud normals
208  NormalsConstPtr normals_;
209  /// point cloud response
210  pcl::PointCloud<float>::Ptr response_;
211  };
212 }
213 
214 #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:167
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:115
bool initCompute() override
typename Normals::ConstPtr NormalsConstPtr
Definition: trajkovic_3d.h:64
unsigned int getNumberOfThreads() const
Definition: trajkovic_3d.h:150
ComputationMethod getMethod() const
Definition: trajkovic_3d.h:100
void setMethod(ComputationMethod method)
set the method of the response to be calculated.
Definition: trajkovic_3d.h:96
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:104
float getSecondThreshold() const
Definition: trajkovic_3d.h:130
void setNumberOfThreads(unsigned int nr_threads=0)
Initialize the scheduler and set the number of threads to use.
Definition: trajkovic_3d.h:146
float getFirstThreshold() const
Definition: trajkovic_3d.h:119
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:136
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:126
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.