Skip to content

Commit ecb2a96

Browse files
committed
Add const "guards" in correspondence estimation
* Add const keyword to function parameters which are not meant to be modified inside the function in correspondence estimation classes * Add const keyword to local variables which are not meant to be modified in correspondence estimation classes except for variables in clone() functions Signed-off-by: Ramir Sultanov <sumir0@proton.me>
1 parent f5fd215 commit ecb2a96

8 files changed

Lines changed: 49 additions & 45 deletions

registration/include/pcl/registration/correspondence_estimation.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ class CorrespondenceEstimationBase : public PCLBase<PointSource> {
142142
* automatic)
143143
*/
144144
void
145-
setNumberOfThreads(unsigned int nr_threads)
145+
setNumberOfThreads(const unsigned int nr_threads)
146146
{
147147
#ifdef _OPENMP
148148
num_threads_ = nr_threads != 0 ? nr_threads : omp_get_num_procs();
@@ -163,7 +163,7 @@ class CorrespondenceEstimationBase : public PCLBase<PointSource> {
163163

164164
/** \brief Abstract method for setting the source normals */
165165
virtual void
166-
setSourceNormals(pcl::PCLPointCloud2::ConstPtr /*cloud2*/)
166+
setSourceNormals(const pcl::PCLPointCloud2::ConstPtr /*cloud2*/)
167167
{
168168
PCL_WARN("[pcl::registration::%s::setSourceNormals] This class does not require "
169169
"input source normals\n",
@@ -179,7 +179,7 @@ class CorrespondenceEstimationBase : public PCLBase<PointSource> {
179179

180180
/** \brief Abstract method for setting the target normals */
181181
virtual void
182-
setTargetNormals(pcl::PCLPointCloud2::ConstPtr /*cloud2*/)
182+
setTargetNormals(const pcl::PCLPointCloud2::ConstPtr /*cloud2*/)
183183
{
184184
PCL_WARN("[pcl::registration::%s::setTargetNormals] This class does not require "
185185
"input target normals\n",
@@ -228,7 +228,7 @@ class CorrespondenceEstimationBase : public PCLBase<PointSource> {
228228
* confident that the tree will be set correctly.
229229
*/
230230
inline void
231-
setSearchMethodTarget(const KdTreePtr& tree, bool force_no_recompute = false)
231+
setSearchMethodTarget(const KdTreePtr& tree, const bool force_no_recompute = false)
232232
{
233233
tree_ = tree;
234234
force_no_recompute_ = force_no_recompute;
@@ -253,7 +253,7 @@ class CorrespondenceEstimationBase : public PCLBase<PointSource> {
253253
*/
254254
inline void
255255
setSearchMethodSource(const KdTreeReciprocalPtr& tree,
256-
bool force_no_recompute = false)
256+
const bool force_no_recompute = false)
257257
{
258258
tree_reciprocal_ = tree;
259259
force_no_recompute_reciprocal_ = force_no_recompute;
@@ -277,7 +277,7 @@ class CorrespondenceEstimationBase : public PCLBase<PointSource> {
277277
virtual void
278278
determineCorrespondences(
279279
pcl::Correspondences& correspondences,
280-
double max_distance = std::numeric_limits<double>::max()) = 0;
280+
const double max_distance = std::numeric_limits<double>::max()) = 0;
281281

282282
/** \brief Determine the reciprocal correspondences between input and target cloud.
283283
* A correspondence is considered reciprocal if both Src_i has Tgt_i as a
@@ -290,7 +290,7 @@ class CorrespondenceEstimationBase : public PCLBase<PointSource> {
290290
virtual void
291291
determineReciprocalCorrespondences(
292292
pcl::Correspondences& correspondences,
293-
double max_distance = std::numeric_limits<double>::max()) = 0;
293+
const double max_distance = std::numeric_limits<double>::max()) = 0;
294294

295295
/** \brief Provide a boost shared pointer to the PointRepresentation for target cloud
296296
* to be used when searching for nearest neighbors.
@@ -476,7 +476,7 @@ class CorrespondenceEstimation
476476
void
477477
determineCorrespondences(
478478
pcl::Correspondences& correspondences,
479-
double max_distance = std::numeric_limits<double>::max()) override;
479+
const double max_distance = std::numeric_limits<double>::max()) override;
480480

481481
/** \brief Determine the reciprocal correspondences between input and target cloud.
482482
* A correspondence is considered reciprocal if both Src_i has Tgt_i as a
@@ -489,7 +489,7 @@ class CorrespondenceEstimation
489489
void
490490
determineReciprocalCorrespondences(
491491
pcl::Correspondences& correspondences,
492-
double max_distance = std::numeric_limits<double>::max()) override;
492+
const double max_distance = std::numeric_limits<double>::max()) override;
493493

494494
/** \brief Clone and cast to CorrespondenceEstimationBase */
495495
typename CorrespondenceEstimationBase<PointSource, PointTarget, Scalar>::Ptr

registration/include/pcl/registration/correspondence_estimation_backprojection.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,9 @@ class CorrespondenceEstimationBackProjection
150150

151151
/** \brief Blob method for setting the source normals */
152152
void
153-
setSourceNormals(pcl::PCLPointCloud2::ConstPtr cloud2)
153+
setSourceNormals(const pcl::PCLPointCloud2::ConstPtr cloud2)
154154
{
155-
NormalsPtr cloud(new PointCloudNormals);
155+
const NormalsPtr cloud(new PointCloudNormals);
156156
fromPCLPointCloud2(*cloud2, *cloud);
157157
setSourceNormals(cloud);
158158
}
@@ -166,9 +166,9 @@ class CorrespondenceEstimationBackProjection
166166

167167
/** \brief Method for setting the target normals */
168168
void
169-
setTargetNormals(pcl::PCLPointCloud2::ConstPtr cloud2)
169+
setTargetNormals(const pcl::PCLPointCloud2::ConstPtr cloud2)
170170
{
171-
NormalsPtr cloud(new PointCloudNormals);
171+
const NormalsPtr cloud(new PointCloudNormals);
172172
fromPCLPointCloud2(*cloud2, *cloud);
173173
setTargetNormals(cloud);
174174
}
@@ -180,8 +180,9 @@ class CorrespondenceEstimationBackProjection
180180
* cloud
181181
*/
182182
void
183-
determineCorrespondences(pcl::Correspondences& correspondences,
184-
double max_distance = std::numeric_limits<double>::max());
183+
determineCorrespondences(
184+
pcl::Correspondences& correspondences,
185+
const double max_distance = std::numeric_limits<double>::max());
185186

186187
/** \brief Determine the reciprocal correspondences between input and target cloud.
187188
* A correspondence is considered reciprocal if both Src_i has Tgt_i as a
@@ -194,15 +195,15 @@ class CorrespondenceEstimationBackProjection
194195
virtual void
195196
determineReciprocalCorrespondences(
196197
pcl::Correspondences& correspondences,
197-
double max_distance = std::numeric_limits<double>::max());
198+
const double max_distance = std::numeric_limits<double>::max());
198199

199200
/** \brief Set the number of nearest neighbours to be considered in the target
200201
* point cloud. By default, we use k = 10 nearest neighbors.
201202
*
202203
* \param[in] k the number of nearest neighbours to be considered
203204
*/
204205
inline void
205-
setKSearch(unsigned int k)
206+
setKSearch(const unsigned int k)
206207
{
207208
k_ = k;
208209
}

registration/include/pcl/registration/correspondence_estimation_normal_shooting.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,9 @@ class CorrespondenceEstimationNormalShooting
166166

167167
/** \brief Blob method for setting the source normals */
168168
void
169-
setSourceNormals(pcl::PCLPointCloud2::ConstPtr cloud2) override
169+
setSourceNormals(const pcl::PCLPointCloud2::ConstPtr cloud2) override
170170
{
171-
NormalsPtr cloud(new PointCloudNormals);
171+
const NormalsPtr cloud(new PointCloudNormals);
172172
fromPCLPointCloud2(*cloud2, *cloud);
173173
setSourceNormals(cloud);
174174
}
@@ -182,7 +182,7 @@ class CorrespondenceEstimationNormalShooting
182182
void
183183
determineCorrespondences(
184184
pcl::Correspondences& correspondences,
185-
double max_distance = std::numeric_limits<double>::max()) override;
185+
const double max_distance = std::numeric_limits<double>::max()) override;
186186

187187
/** \brief Determine the reciprocal correspondences between input and target cloud.
188188
* A correspondence is considered reciprocal if both Src_i has Tgt_i as a
@@ -195,15 +195,15 @@ class CorrespondenceEstimationNormalShooting
195195
void
196196
determineReciprocalCorrespondences(
197197
pcl::Correspondences& correspondences,
198-
double max_distance = std::numeric_limits<double>::max()) override;
198+
const double max_distance = std::numeric_limits<double>::max()) override;
199199

200200
/** \brief Set the number of nearest neighbours to be considered in the target
201201
* point cloud. By default, we use k = 10 nearest neighbors.
202202
*
203203
* \param[in] k the number of nearest neighbours to be considered
204204
*/
205205
inline void
206-
setKSearch(unsigned int k)
206+
setKSearch(const unsigned int k)
207207
{
208208
k_ = k;
209209
}

registration/include/pcl/registration/correspondence_estimation_organized_projection.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ class CorrespondenceEstimationOrganizedProjection
190190
* will be rejected
191191
*/
192192
void
193-
determineCorrespondences(Correspondences& correspondences, double max_distance);
193+
determineCorrespondences(Correspondences& correspondences, const double max_distance);
194194

195195
/** \brief Computes the correspondences, applying a maximum Euclidean distance
196196
* threshold.
@@ -201,7 +201,7 @@ class CorrespondenceEstimationOrganizedProjection
201201
*/
202202
void
203203
determineReciprocalCorrespondences(Correspondences& correspondences,
204-
double max_distance);
204+
const double max_distance);
205205

206206
/** \brief Clone and cast to CorrespondenceEstimationBase */
207207
virtual typename CorrespondenceEstimationBase<PointSource, PointTarget, Scalar>::Ptr

registration/include/pcl/registration/impl/correspondence_estimation.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ pointCopyOrRef(typename pcl::PointCloud<PointSource>::ConstPtr& input, const Ind
145145
template <typename PointSource, typename PointTarget, typename Scalar>
146146
void
147147
CorrespondenceEstimation<PointSource, PointTarget, Scalar>::determineCorrespondences(
148-
pcl::Correspondences& correspondences, double max_distance)
148+
pcl::Correspondences& correspondences, const double max_distance)
149149
{
150150
if (!initCompute())
151151
return;
@@ -158,7 +158,7 @@ CorrespondenceEstimation<PointSource, PointTarget, Scalar>::determineCorresponde
158158
for (auto& corrs : per_thread_correspondences) {
159159
corrs.reserve(2 * indices_->size() / num_threads_);
160160
}
161-
double max_dist_sqr = max_distance * max_distance;
161+
const double max_dist_sqr = max_distance * max_distance;
162162

163163
#pragma omp parallel for default(none) \
164164
shared(max_dist_sqr, per_thread_correspondences) firstprivate(index, distance) \
@@ -221,7 +221,7 @@ template <typename PointSource, typename PointTarget, typename Scalar>
221221
void
222222
CorrespondenceEstimation<PointSource, PointTarget, Scalar>::
223223
determineReciprocalCorrespondences(pcl::Correspondences& correspondences,
224-
double max_distance)
224+
const double max_distance)
225225
{
226226
if (!initCompute())
227227
return;
@@ -230,7 +230,6 @@ CorrespondenceEstimation<PointSource, PointTarget, Scalar>::
230230
// Set the internal point representation of choice
231231
if (!initComputeReciprocal())
232232
return;
233-
double max_dist_sqr = max_distance * max_distance;
234233

235234
correspondences.resize(indices_->size());
236235
pcl::Indices index(1);
@@ -241,6 +240,7 @@ CorrespondenceEstimation<PointSource, PointTarget, Scalar>::
241240
for (auto& corrs : per_thread_correspondences) {
242241
corrs.reserve(2 * indices_->size() / num_threads_);
243242
}
243+
const double max_dist_sqr = max_distance * max_distance;
244244

245245
#pragma omp parallel for default(none) \
246246
shared(max_dist_sqr, per_thread_correspondences) \

registration/include/pcl/registration/impl/correspondence_estimation_backprojection.hpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ CorrespondenceEstimationBackProjection<PointSource, PointTarget, NormalT, Scalar
6666
template <typename PointSource, typename PointTarget, typename NormalT, typename Scalar>
6767
void
6868
CorrespondenceEstimationBackProjection<PointSource, PointTarget, NormalT, Scalar>::
69-
determineCorrespondences(pcl::Correspondences& correspondences, double max_distance)
69+
determineCorrespondences(pcl::Correspondences& correspondences,
70+
const double max_distance)
7071
{
7172
if (!initCompute())
7273
return;
@@ -98,7 +99,7 @@ CorrespondenceEstimationBackProjection<PointSource, PointTarget, NormalT, Scalar
9899
(*target_normals_)[nn_indices[j]].normal_y +
99100
(*source_normals_)[idx_i].normal_z *
100101
(*target_normals_)[nn_indices[j]].normal_z;
101-
float dist = nn_dists[j] * (2.0f - cos_angle * cos_angle);
102+
const float dist = nn_dists[j] * (2.0f - cos_angle * cos_angle);
102103

103104
if (dist < min_dist) {
104105
min_dist = dist;
@@ -121,7 +122,7 @@ template <typename PointSource, typename PointTarget, typename NormalT, typename
121122
void
122123
CorrespondenceEstimationBackProjection<PointSource, PointTarget, NormalT, Scalar>::
123124
determineReciprocalCorrespondences(pcl::Correspondences& correspondences,
124-
double max_distance)
125+
const double max_distance)
125126
{
126127
if (!initCompute())
127128
return;
@@ -166,7 +167,7 @@ CorrespondenceEstimationBackProjection<PointSource, PointTarget, NormalT, Scalar
166167
(*target_normals_)[nn_indices[j]].normal_y +
167168
(*source_normals_)[idx_i].normal_z *
168169
(*target_normals_)[nn_indices[j]].normal_z;
169-
float dist = nn_dists[j] * (2.0f - cos_angle * cos_angle);
170+
const float dist = nn_dists[j] * (2.0f - cos_angle * cos_angle);
170171

171172
if (dist < min_dist) {
172173
min_dist = dist;

registration/include/pcl/registration/impl/correspondence_estimation_normal_shooting.hpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ CorrespondenceEstimationNormalShooting<PointSource, PointTarget, NormalT, Scalar
6666
template <typename PointSource, typename PointTarget, typename NormalT, typename Scalar>
6767
void
6868
CorrespondenceEstimationNormalShooting<PointSource, PointTarget, NormalT, Scalar>::
69-
determineCorrespondences(pcl::Correspondences& correspondences, double max_distance)
69+
determineCorrespondences(pcl::Correspondences& correspondences,
70+
const double max_distance)
7071
{
7172
if (!initCompute())
7273
return;
@@ -111,7 +112,7 @@ CorrespondenceEstimationNormalShooting<PointSource, PointTarget, NormalT, Scalar
111112
Eigen::Vector3d C = N.cross(V);
112113

113114
// Check if we have a better correspondence
114-
double dist = C.dot(C);
115+
const double dist = C.dot(C);
115116
if (dist < min_dist) {
116117
min_dist = dist;
117118
min_index = static_cast<int>(j);
@@ -133,7 +134,7 @@ template <typename PointSource, typename PointTarget, typename NormalT, typename
133134
void
134135
CorrespondenceEstimationNormalShooting<PointSource, PointTarget, NormalT, Scalar>::
135136
determineReciprocalCorrespondences(pcl::Correspondences& correspondences,
136-
double max_distance)
137+
const double max_distance)
137138
{
138139
if (!initCompute())
139140
return;
@@ -186,7 +187,7 @@ CorrespondenceEstimationNormalShooting<PointSource, PointTarget, NormalT, Scalar
186187
Eigen::Vector3d C = N.cross(V);
187188

188189
// Check if we have a better correspondence
189-
double dist = C.dot(C);
190+
const double dist = C.dot(C);
190191
if (dist < min_dist) {
191192
min_dist = dist;
192193
min_index = static_cast<int>(j);

registration/include/pcl/registration/impl/correspondence_estimation_organized_projection.hpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ CorrespondenceEstimationOrganizedProjection<PointSource, PointTarget, Scalar>::
7575
template <typename PointSource, typename PointTarget, typename Scalar>
7676
void
7777
CorrespondenceEstimationOrganizedProjection<PointSource, PointTarget, Scalar>::
78-
determineCorrespondences(pcl::Correspondences& correspondences, double max_distance)
78+
determineCorrespondences(pcl::Correspondences& correspondences,
79+
const double max_distance)
7980
{
8081
if (!initCompute())
8182
return;
@@ -85,17 +86,17 @@ CorrespondenceEstimationOrganizedProjection<PointSource, PointTarget, Scalar>::
8586

8687
for (const auto& src_idx : (*indices_)) {
8788
if (isFinite((*input_)[src_idx])) {
88-
Eigen::Vector4f p_src(src_to_tgt_transformation_ *
89-
(*input_)[src_idx].getVector4fMap());
90-
Eigen::Vector3f p_src3(p_src[0], p_src[1], p_src[2]);
91-
Eigen::Vector3f uv(projection_matrix_ * p_src3);
89+
const Eigen::Vector4f p_src(src_to_tgt_transformation_ *
90+
(*input_)[src_idx].getVector4fMap());
91+
const Eigen::Vector3f p_src3(p_src[0], p_src[1], p_src[2]);
92+
const Eigen::Vector3f uv(projection_matrix_ * p_src3);
9293

9394
/// Check if the point was behind the camera
9495
if (uv[2] <= 0)
9596
continue;
9697

97-
int u = static_cast<int>(uv[0] / uv[2]);
98-
int v = static_cast<int>(uv[1] / uv[2]);
98+
const int u = static_cast<int>(uv[0] / uv[2]);
99+
const int v = static_cast<int>(uv[1] / uv[2]);
99100

100101
if (u >= 0 && u < static_cast<int>(target_->width) && v >= 0 &&
101102
v < static_cast<int>(target_->height)) {
@@ -106,7 +107,7 @@ CorrespondenceEstimationOrganizedProjection<PointSource, PointTarget, Scalar>::
106107
if (std::abs(uv[2] - pt_tgt.z) > depth_threshold_)
107108
continue;
108109

109-
double dist = (p_src3 - pt_tgt.getVector3fMap()).norm();
110+
const double dist = (p_src3 - pt_tgt.getVector3fMap()).norm();
110111
if (dist < max_distance)
111112
correspondences[c_index++] = pcl::Correspondence(
112113
src_idx, v * target_->width + u, static_cast<float>(dist));
@@ -121,7 +122,7 @@ template <typename PointSource, typename PointTarget, typename Scalar>
121122
void
122123
CorrespondenceEstimationOrganizedProjection<PointSource, PointTarget, Scalar>::
123124
determineReciprocalCorrespondences(pcl::Correspondences& correspondences,
124-
double max_distance)
125+
const double max_distance)
125126
{
126127
// Call the normal determineCorrespondences (...), as doing it both ways will not
127128
// improve the results

0 commit comments

Comments
 (0)