Skip to content

Commit

Permalink
Transform classic loops to range-based for loops in module recognition (
Browse files Browse the repository at this point in the history
#2855)

Changes are based on the result of run-clang-tidy -header-filter='.*' -checks='-*,modernize-loop-convert' -fix
Use always const reference in for-ranged loop instead of copying primitive data types
  • Loading branch information
SunBlack authored and SergioRAgostinho committed Mar 13, 2019
1 parent 986a373 commit 4b36645
Show file tree
Hide file tree
Showing 18 changed files with 170 additions and 184 deletions.
11 changes: 5 additions & 6 deletions recognition/include/pcl/recognition/crh_alignment.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,10 @@ namespace pcl

//if the number of peaks is too big, we should try to reduce using siluette matching

for (size_t i = 0; i < peaks.size(); i++)
for (const float &peak : peaks)
{
Eigen::Affine3f rollToRot;
computeRollTransform (centroid_input_, centroid_target_, peaks[i], rollToRot);
computeRollTransform (centroid_input_, centroid_target_, peak, rollToRot);

Eigen::Matrix4f rollHomMatrix = Eigen::Matrix4f ();
rollHomMatrix.setIdentity (4, 4);
Expand Down Expand Up @@ -245,11 +245,10 @@ namespace pcl
{
bool insert = true;

for (size_t j = 0; j < peaks_indices.size (); j++)
for (const int &peaks_index : peaks_indices)
{ //check inserted peaks, first pick always inserted
if (std::abs (peaks_indices[j] - scored_peaks[i].second) <= peak_distance || std::abs (
peaks_indices[j] - (scored_peaks[i].second
- nr_bins_after_padding)) <= peak_distance)
if ((std::abs (peaks_index - scored_peaks[i].second) <= peak_distance) ||
(std::abs (peaks_index - (scored_peaks[i].second - nr_bins_after_padding)) <= peak_distance))
{
insert = false;
break;
Expand Down
22 changes: 11 additions & 11 deletions recognition/include/pcl/recognition/hv/hv_go.h
Original file line number Diff line number Diff line change
Expand Up @@ -295,21 +295,21 @@ namespace pcl
}
}

for (size_t i = 0; i < explained.size (); i++)
for (const int &i : explained)
{
if (val < 0)
{
//the hypothesis is being removed, check that there are no points that become unexplained and have clutter unexplained hypotheses
if ((explained_by_RM[explained[i]] == 0) && (unexplained_by_RM[explained[i]] > 0))
if ((explained_by_RM[i] == 0) && (unexplained_by_RM[i] > 0))
{
add_to_unexplained += unexplained_by_RM[explained[i]]; //the points become unexplained
add_to_unexplained += unexplained_by_RM[i]; //the points become unexplained
}
} else
{
//std::cout << "being added..." << add_to_unexplained << " " << unexplained_by_RM[explained[i]] << std::endl;
if ((explained_by_RM[explained[i]] == 1) && (unexplained_by_RM[explained[i]] > 0))
if ((explained_by_RM[i] == 1) && (unexplained_by_RM[i] > 0))
{ //the only hypothesis explaining that point
add_to_unexplained -= unexplained_by_RM[explained[i]]; //the points are not unexplained any longer because this hypothesis explains them
add_to_unexplained -= unexplained_by_RM[i]; //the points are not unexplained any longer because this hypothesis explains them
}
}
}
Expand Down Expand Up @@ -353,17 +353,17 @@ namespace pcl

void updateCMDuplicity(std::vector<int> & vec, std::vector<int> & occupancy_vec, float sign) {
int add_to_duplicity_ = 0;
for (size_t i = 0; i < vec.size (); i++)
for (const int &i : vec)
{
bool prev_dup = occupancy_vec[vec[i]] > 1;
occupancy_vec[vec[i]] += static_cast<int> (sign);
if ((occupancy_vec[vec[i]] > 1) && prev_dup)
bool prev_dup = occupancy_vec[i] > 1;
occupancy_vec[i] += static_cast<int> (sign);
if ((occupancy_vec[i] > 1) && prev_dup)
{ //its still a duplicate, we are adding
add_to_duplicity_ += static_cast<int> (sign); //so, just add or remove one
} else if ((occupancy_vec[vec[i]] == 1) && prev_dup)
} else if ((occupancy_vec[i] == 1) && prev_dup)
{ //if was duplicate before, now its not, remove 2, we are removing the hypothesis
add_to_duplicity_ -= 2;
} else if ((occupancy_vec[vec[i]] > 1) && !prev_dup)
} else if ((occupancy_vec[i] > 1) && !prev_dup)
{ //it was not a duplicate but it is now, add 2, we are adding a conflicting hypothesis for the point
add_to_duplicity_ += 2;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ pcl::GeometricConsistencyGrouping<PointModelT, PointSceneT>::clusterCorresponden
{
//Let's check if j fits into the current consensus set
bool is_a_good_candidate = true;
for (size_t k = 0; k < consensus_set.size (); ++k)
for (const int &k : consensus_set)
{
int scene_index_k = model_scene_corrs_->at (consensus_set[k]).index_match;
int model_index_k = model_scene_corrs_->at (consensus_set[k]).index_query;
int scene_index_k = model_scene_corrs_->at (k).index_match;
int model_index_k = model_scene_corrs_->at (k).index_query;
int scene_index_j = model_scene_corrs_->at (j).index_match;
int model_index_j = model_scene_corrs_->at (j).index_query;

Expand Down Expand Up @@ -133,10 +133,10 @@ pcl::GeometricConsistencyGrouping<PointModelT, PointSceneT>::clusterCorresponden
if (static_cast<int> (consensus_set.size ()) > gc_threshold_)
{
Correspondences temp_corrs, filtered_corrs;
for (size_t j = 0; j < consensus_set.size (); j++)
for (const int &j : consensus_set)
{
temp_corrs.push_back (model_scene_corrs_->at (consensus_set[j]));
taken_corresps[ consensus_set[j] ] = true;
temp_corrs.push_back (model_scene_corrs_->at (j));
taken_corresps[ j ] = true;
}
//ransac filtering
corr_rejector.getRemainingCorrespondences (temp_corrs, filtered_corrs);
Expand Down
4 changes: 2 additions & 2 deletions recognition/include/pcl/recognition/impl/cg/hough_3d.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,9 @@ pcl::Hough3DGrouping<PointModelT, PointSceneT, PointModelRfT, PointSceneRfT>::cl
for (size_t j = 0; j < max_values.size (); ++j)
{
Correspondences temp_corrs, filtered_corrs;
for (size_t i = 0; i < max_ids[j].size (); ++i)
for (const int &i : max_ids[j])
{
temp_corrs.push_back (model_scene_corrs_->at (max_ids[j][i]));
temp_corrs.push_back (model_scene_corrs_->at (i));
}
// RANSAC filtering
corr_rejector.getRemainingCorrespondences (temp_corrs, filtered_corrs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ template<typename ModelT, typename SceneT>

recognition_models_.push_back (recog_model);

for (size_t i = 0; i < explained_indices.size (); i++)
for (const int &explained_index : explained_indices)
{
points_explained_by_rm_[explained_indices[i]].push_back (recog_model);
points_explained_by_rm_[explained_index].push_back (recog_model);
}
}

Expand Down
34 changes: 17 additions & 17 deletions recognition/include/pcl/recognition/impl/hv/hv_go.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ mets::gol_type pcl::GlobalHypothesesVerification<ModelT, SceneT>::evaluateSoluti
setPreviousBadInfo (bad_info);

int n_active_hyp = 0;
for(size_t i=0; i < active.size(); i++) {
if(active[i])
for(const bool &i : active) {
if(i)
n_active_hyp++;
}

Expand Down Expand Up @@ -267,11 +267,11 @@ void pcl::GlobalHypothesesVerification<ModelT, SceneT>::initialize()

float intens_incr = 100.f / static_cast<float> (clusters.size ());
float intens = intens_incr;
for (size_t i = 0; i < clusters.size (); i++)
for (const auto &cluster : clusters)
{
for (size_t j = 0; j < clusters[i].indices.size (); j++)
for (const auto &vertex : cluster.indices)
{
clusters_cloud_->points[clusters[i].indices[j]].intensity = intens;
clusters_cloud_->points[vertex].intensity = intens;
}

intens += intens_incr;
Expand Down Expand Up @@ -381,9 +381,9 @@ void pcl::GlobalHypothesesVerification<ModelT, SceneT>::SAOptimize(std::vector<i

recognition_models_.clear ();

for (size_t j = 0; j < cc_indices.size (); j++)
for (const int &cc_index : cc_indices)
{
recognition_models_.push_back (recognition_models_copy[cc_indices[j]]);
recognition_models_.push_back (recognition_models_copy[cc_index]);
}

for (size_t j = 0; j < recognition_models_.size (); j++)
Expand Down Expand Up @@ -692,21 +692,21 @@ void pcl::GlobalHypothesesVerification<ModelT, SceneT>::computeClutterCue(boost:

size_t p = 0;
size_t j = 0;
for (size_t i = 0; i < neighborhood_indices.size (); i++)
for (const auto &neighborhood_index : neighborhood_indices)
{
if ((j < exp_idces.size ()) && (neighborhood_indices[i].first == exp_idces[j]))
if ((j < exp_idces.size ()) && (neighborhood_index.first == exp_idces[j]))
{
//this index is explained by the hypothesis so ignore it, advance j
j++;
} else
{
//indices_in_nb[i] < exp_idces[j]
//recog_model->unexplained_in_neighborhood.push_back(neighborhood_indices[i]);
recog_model->unexplained_in_neighborhood[p] = neighborhood_indices[i].first;
recog_model->unexplained_in_neighborhood[p] = neighborhood_index.first;

if (clusters_cloud_->points[recog_model->explained_[neighborhood_indices[i].second]].intensity != 0.f
&& (clusters_cloud_->points[recog_model->explained_[neighborhood_indices[i].second]].intensity
== clusters_cloud_->points[neighborhood_indices[i].first].intensity))
if (clusters_cloud_->points[recog_model->explained_[neighborhood_index.second]].intensity != 0.f
&& (clusters_cloud_->points[recog_model->explained_[neighborhood_index.second]].intensity
== clusters_cloud_->points[neighborhood_index.first].intensity))
{

recog_model->unexplained_in_neighborhood_weights[p] = clutter_regularizer_;
Expand All @@ -716,13 +716,13 @@ void pcl::GlobalHypothesesVerification<ModelT, SceneT>::computeClutterCue(boost:
//neighborhood_indices[i].first gives the index to the scene point and second to the explained scene point by the model causing this...
//calculate weight of this clutter point based on the distance of the scene point and the model point causing it
float d = static_cast<float> (pow (
(scene_cloud_downsampled_->points[recog_model->explained_[neighborhood_indices[i].second]].getVector3fMap ()
- scene_cloud_downsampled_->points[neighborhood_indices[i].first].getVector3fMap ()).norm (), 2));
(scene_cloud_downsampled_->points[recog_model->explained_[neighborhood_index.second]].getVector3fMap ()
- scene_cloud_downsampled_->points[neighborhood_index.first].getVector3fMap ()).norm (), 2));
float d_weight = -(d / rn_sqr) + 1; //points that are close have a strong weight*/

//using normals to weight clutter points
Eigen::Vector3f scene_p_normal = scene_normals_->points[neighborhood_indices[i].first].getNormalVector3fMap ();
Eigen::Vector3f model_p_normal = scene_normals_->points[recog_model->explained_[neighborhood_indices[i].second]].getNormalVector3fMap ();
Eigen::Vector3f scene_p_normal = scene_normals_->points[neighborhood_index.first].getNormalVector3fMap ();
Eigen::Vector3f model_p_normal = scene_normals_->points[recog_model->explained_[neighborhood_index.second]].getNormalVector3fMap ();
float dotp = scene_p_normal.dot (model_p_normal); //[-1,1] from antiparallel trough perpendicular to parallel

if (dotp < 0)
Expand Down
6 changes: 3 additions & 3 deletions recognition/include/pcl/recognition/impl/hv/hv_papazov.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,10 @@ template<typename ModelT, typename SceneT>
recognition_models_.push_back (recog_model);

// update explained_by_RM_, add 1
for (size_t i = 0; i < explained_indices.size (); i++)
for (const int &explained_index : explained_indices)
{
explained_by_RM_[explained_indices[i]]++;
points_explained_by_rm_[explained_indices[i]].push_back (recog_model);
explained_by_RM_[explained_index]++;
points_explained_by_rm_[explained_index].push_back (recog_model);
}
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,11 @@ pcl::features::ISMVoteList<PointT>::getColoredCloud (typename pcl::PointCloud<Po
point.r = 0;
point.g = 0;
point.b = 255;
for (size_t i_vote = 0; i_vote < votes_->points.size (); i_vote++)
for (const auto &i_vote : votes_->points)
{
point.x = votes_->points[i_vote].x;
point.y = votes_->points[i_vote].y;
point.z = votes_->points[i_vote].z;
point.x = i_vote.x;
point.y = i_vote.y;
point.z = i_vote.z;
colored_cloud->points.push_back (point);
}
colored_cloud->height += static_cast<uint32_t> (votes_->points.size ());
Expand Down Expand Up @@ -404,8 +404,8 @@ pcl::features::ISMModel::saveModelToFile (std::string& file_name)
for (unsigned int i_cluster = 0; i_cluster < number_of_clusters_; i_cluster++)
{
output_file << static_cast<unsigned int> (clusters_[i_cluster].size ()) << " ";
for (unsigned int i_visual_word = 0; i_visual_word < static_cast<unsigned int> (clusters_[i_cluster].size ()); i_visual_word++)
output_file << clusters_[i_cluster][i_visual_word] << " ";
for (const unsigned int &visual_word : clusters_[i_cluster])
output_file << visual_word << " ";
}

output_file.close ();
Expand Down
20 changes: 10 additions & 10 deletions recognition/include/pcl/recognition/linemod.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,21 +100,21 @@ namespace pcl

const size_t mapsSize = width*height;

for (size_t map_index = 0; map_index < maps_.size (); ++map_index)
for (auto &map : maps_)
{
//maps_[map_index] = new unsigned char[mapsSize];
maps_[map_index] = reinterpret_cast<unsigned char*> (aligned_malloc (mapsSize));
memset (maps_[map_index], 0, mapsSize);
map = reinterpret_cast<unsigned char*> (aligned_malloc (mapsSize));
memset (map, 0, mapsSize);
}
}

/** \brief Releases the internal data. */
void
releaseAll ()
{
for (size_t map_index = 0; map_index < maps_.size (); ++map_index)
for (auto &map : maps_)
//if (maps_[map_index] != NULL) delete[] maps_[map_index];
if (maps_[map_index] != NULL) aligned_free (maps_[map_index]);
if (map != NULL) aligned_free (map);

maps_.clear ();
width_ = 0;
Expand Down Expand Up @@ -242,21 +242,21 @@ namespace pcl

const size_t mapsSize = mem_width_ * mem_height_;

for (size_t map_index = 0; map_index < maps_.size (); ++map_index)
for (auto &map : maps_)
{
//maps_[map_index] = new unsigned char[2*mapsSize];
maps_[map_index] = reinterpret_cast<unsigned char*> (aligned_malloc (2*mapsSize));
memset (maps_[map_index], 0, 2*mapsSize);
map = reinterpret_cast<unsigned char*> (aligned_malloc (2*mapsSize));
memset (map, 0, 2*mapsSize);
}
}

/** \brief Releases the internal memory. */
void
releaseAll ()
{
for (size_t map_index = 0; map_index < maps_.size (); ++map_index)
for (auto &map : maps_)
//if (maps_[map_index] != NULL) delete[] maps_[map_index];
if (maps_[map_index] != NULL) aligned_free (maps_[map_index]);
if (map != NULL) aligned_free (map);

maps_.clear ();
width_ = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,20 +229,20 @@ namespace pcl
int max_num_transforms = 0;

// For each full leaf
for ( std::vector<CellOctree::Node*>::const_iterator leaf = full_leaves.begin () ; leaf != full_leaves.end () ; ++leaf )
for (const auto &full_leaf : full_leaves)
{
// Is there an entry for 'model' in the current cell
const RotationSpaceCell::Entry *entry = (*leaf)->getData ().getEntry (model);
const RotationSpaceCell::Entry *entry = full_leaf->getData ().getEntry (model);
if ( !entry )
continue;

int num_transforms = entry->getNumberOfTransforms ();
const std::set<CellOctree::Node*>& neighs = (*leaf)->getNeighbors ();
const std::set<CellOctree::Node*>& neighs = full_leaf->getNeighbors ();

// For each neighbor
for ( std::set<CellOctree::Node*>::const_iterator neigh = neighs.begin () ; neigh != neighs.end () ; ++neigh )
for (const auto &neigh : neighs)
{
const RotationSpaceCell::Entry *neigh_entry = (*neigh)->getData ().getEntry (model);
const RotationSpaceCell::Entry *neigh_entry = neigh->getData ().getEntry (model);
if ( !neigh_entry )
continue;

Expand Down
8 changes: 4 additions & 4 deletions recognition/include/pcl/recognition/surface_normal_modality.h
Original file line number Diff line number Diff line change
Expand Up @@ -980,8 +980,8 @@ pcl::SurfaceNormalModality<PointInT>::extractFeatures (const MaskMap & mask,
//}

MaskMap mask_maps[8];
for (size_t map_index = 0; map_index < 8; ++map_index)
mask_maps[map_index].resize (width, height);
for (auto &mask_map : mask_maps)
mask_map.resize (width, height);

unsigned char map[255];
memset(map, 0, 255);
Expand Down Expand Up @@ -1251,8 +1251,8 @@ pcl::SurfaceNormalModality<PointInT>::extractAllFeatures (
//}

MaskMap mask_maps[8];
for (size_t map_index = 0; map_index < 8; ++map_index)
mask_maps[map_index].resize (width, height);
for (auto &mask_map : mask_maps)
mask_map.resize (width, height);

unsigned char map[255];
memset(map, 0, 255);
Expand Down
Loading

0 comments on commit 4b36645

Please sign in to comment.