Skip to content

Commit

Permalink
Tranform classic loops to range-based for loops in module common
Browse files Browse the repository at this point in the history
Changes are based on the result of run-clang-tidy -header-filter='.*' -checks='-*,modernize-loop-convert' -fix
  • Loading branch information
Heiko Thiel committed Feb 9, 2019
1 parent 1cb184d commit 14c3c75
Show file tree
Hide file tree
Showing 11 changed files with 131 additions and 135 deletions.
8 changes: 4 additions & 4 deletions common/include/pcl/common/impl/accumulators.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,11 @@ namespace pcl
get (PointT& t, size_t) const
{
size_t max = 0;
for (auto itr = labels.cbegin (); itr != labels.cend (); ++itr)
if (itr->second > max)
for (const auto label : labels)
if (label.second > max)
{
max = itr->second;
t.label = itr->first;
max = label.second;
t.label = label.first;
}
}

Expand Down
110 changes: 55 additions & 55 deletions common/include/pcl/common/impl/centroid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,11 @@ pcl::compute3DCentroid (const pcl::PointCloud<PointT> &cloud,
// If the data is dense, we don't need to check for NaN
if (cloud.is_dense)
{
for (size_t i = 0; i < indices.size (); ++i)
for (const int index : indices)
{
centroid[0] += cloud[indices[i]].x;
centroid[1] += cloud[indices[i]].y;
centroid[2] += cloud[indices[i]].z;
centroid[0] += cloud[index].x;
centroid[1] += cloud[index].y;
centroid[2] += cloud[index].z;
}
centroid /= static_cast<Scalar> (indices.size ());
centroid[3] = 1;
Expand All @@ -149,15 +149,15 @@ pcl::compute3DCentroid (const pcl::PointCloud<PointT> &cloud,
else
{
unsigned cp = 0;
for (size_t i = 0; i < indices.size (); ++i)
for (const int index : indices)
{
// Check if the point is invalid
if (!isFinite (cloud [indices[i]]))
if (!isFinite (cloud [index]))
continue;

centroid[0] += cloud[indices[i]].x;
centroid[1] += cloud[indices[i]].y;
centroid[2] += cloud[indices[i]].z;
centroid[0] += cloud[index].x;
centroid[1] += cloud[index].y;
centroid[2] += cloud[index].z;
++cp;
}
centroid /= static_cast<Scalar> (cp);
Expand Down Expand Up @@ -300,16 +300,16 @@ pcl::computeCovarianceMatrix (const pcl::PointCloud<PointT> &cloud,
{
point_count = 0;
// For each point in the cloud
for (size_t i = 0; i < indices.size (); ++i)
for (const int index : indices)
{
// Check if the point is invalid
if (!isFinite (cloud[indices[i]]))
if (!isFinite (cloud[index]))
continue;

Eigen::Matrix<Scalar, 4, 1> pt;
pt[0] = cloud[indices[i]].x - centroid[0];
pt[1] = cloud[indices[i]].y - centroid[1];
pt[2] = cloud[indices[i]].z - centroid[2];
pt[0] = cloud[index].x - centroid[0];
pt[1] = cloud[index].y - centroid[1];
pt[2] = cloud[index].z - centroid[2];

covariance_matrix (1, 1) += pt.y () * pt.y ();
covariance_matrix (1, 2) += pt.y () * pt.z ();
Expand Down Expand Up @@ -434,32 +434,32 @@ pcl::computeCovarianceMatrix (const pcl::PointCloud<PointT> &cloud,
if (cloud.is_dense)
{
point_count = static_cast<unsigned int> (indices.size ());
for (std::vector<int>::const_iterator iIt = indices.begin (); iIt != indices.end (); ++iIt)
for (const int index : indices)
{
//const PointT& point = cloud[*iIt];
accu [0] += cloud[*iIt].x * cloud[*iIt].x;
accu [1] += cloud[*iIt].x * cloud[*iIt].y;
accu [2] += cloud[*iIt].x * cloud[*iIt].z;
accu [3] += cloud[*iIt].y * cloud[*iIt].y;
accu [4] += cloud[*iIt].y * cloud[*iIt].z;
accu [5] += cloud[*iIt].z * cloud[*iIt].z;
accu [0] += cloud[index].x * cloud[index].x;
accu [1] += cloud[index].x * cloud[index].y;
accu [2] += cloud[index].x * cloud[index].z;
accu [3] += cloud[index].y * cloud[index].y;
accu [4] += cloud[index].y * cloud[index].z;
accu [5] += cloud[index].z * cloud[index].z;
}
}
else
{
point_count = 0;
for (std::vector<int>::const_iterator iIt = indices.begin (); iIt != indices.end (); ++iIt)
for (const int index : indices)
{
if (!isFinite (cloud[*iIt]))
if (!isFinite (cloud[index]))
continue;

++point_count;
accu [0] += cloud[*iIt].x * cloud[*iIt].x;
accu [1] += cloud[*iIt].x * cloud[*iIt].y;
accu [2] += cloud[*iIt].x * cloud[*iIt].z;
accu [3] += cloud[*iIt].y * cloud[*iIt].y;
accu [4] += cloud[*iIt].y * cloud[*iIt].z;
accu [5] += cloud[*iIt].z * cloud[*iIt].z;
accu [0] += cloud[index].x * cloud[index].x;
accu [1] += cloud[index].x * cloud[index].y;
accu [2] += cloud[index].x * cloud[index].z;
accu [3] += cloud[index].y * cloud[index].y;
accu [4] += cloud[index].y * cloud[index].z;
accu [5] += cloud[index].z * cloud[index].z;
}
}
if (point_count != 0)
Expand Down Expand Up @@ -562,38 +562,38 @@ pcl::computeMeanAndCovarianceMatrix (const pcl::PointCloud<PointT> &cloud,
if (cloud.is_dense)
{
point_count = indices.size ();
for (std::vector<int>::const_iterator iIt = indices.begin (); iIt != indices.end (); ++iIt)
for (const int index : indices)
{
//const PointT& point = cloud[*iIt];
accu [0] += cloud[*iIt].x * cloud[*iIt].x;
accu [1] += cloud[*iIt].x * cloud[*iIt].y;
accu [2] += cloud[*iIt].x * cloud[*iIt].z;
accu [3] += cloud[*iIt].y * cloud[*iIt].y;
accu [4] += cloud[*iIt].y * cloud[*iIt].z;
accu [5] += cloud[*iIt].z * cloud[*iIt].z;
accu [6] += cloud[*iIt].x;
accu [7] += cloud[*iIt].y;
accu [8] += cloud[*iIt].z;
accu [0] += cloud[index].x * cloud[index].x;
accu [1] += cloud[index].x * cloud[index].y;
accu [2] += cloud[index].x * cloud[index].z;
accu [3] += cloud[index].y * cloud[index].y;
accu [4] += cloud[index].y * cloud[index].z;
accu [5] += cloud[index].z * cloud[index].z;
accu [6] += cloud[index].x;
accu [7] += cloud[index].y;
accu [8] += cloud[index].z;
}
}
else
{
point_count = 0;
for (std::vector<int>::const_iterator iIt = indices.begin (); iIt != indices.end (); ++iIt)
for (const int index : indices)
{
if (!isFinite (cloud[*iIt]))
if (!isFinite (cloud[index]))
continue;

++point_count;
accu [0] += cloud[*iIt].x * cloud[*iIt].x;
accu [1] += cloud[*iIt].x * cloud[*iIt].y;
accu [2] += cloud[*iIt].x * cloud[*iIt].z;
accu [3] += cloud[*iIt].y * cloud[*iIt].y; // 4
accu [4] += cloud[*iIt].y * cloud[*iIt].z; // 5
accu [5] += cloud[*iIt].z * cloud[*iIt].z; // 8
accu [6] += cloud[*iIt].x;
accu [7] += cloud[*iIt].y;
accu [8] += cloud[*iIt].z;
accu [0] += cloud[index].x * cloud[index].x;
accu [1] += cloud[index].x * cloud[index].y;
accu [2] += cloud[index].x * cloud[index].z;
accu [3] += cloud[index].y * cloud[index].y; // 4
accu [4] += cloud[index].y * cloud[index].z; // 5
accu [5] += cloud[index].z * cloud[index].z; // 8
accu [6] += cloud[index].x;
accu [7] += cloud[index].y;
accu [8] += cloud[index].z;
}
}

Expand Down Expand Up @@ -886,12 +886,12 @@ pcl::computeCentroid (const pcl::PointCloud<PointInT>& cloud,
pcl::CentroidPoint<PointInT> cp;

if (cloud.is_dense)
for (size_t i = 0; i < indices.size (); ++i)
cp.add (cloud[indices[i]]);
for (const int index : indices)
cp.add (cloud[index]);
else
for (size_t i = 0; i < indices.size (); ++i)
if (pcl::isFinite (cloud[indices[i]]))
cp.add (cloud[indices[i]]);
for (const int index : indices)
if (pcl::isFinite (cloud[index]))
cp.add (cloud[index]);

cp.get (centroid);
return (cp.getSize ());
Expand Down
34 changes: 17 additions & 17 deletions common/include/pcl/common/impl/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ pcl::getMeanStd (const std::vector<float> &values, double &mean, double &stddev)

double sum = 0, sq_sum = 0;

for (size_t i = 0; i < values.size (); ++i)
for (const float value : values)
{
sum += values[i];
sq_sum += values[i] * values[i];
sum += value;
sq_sum += value * value;
}
mean = sum / static_cast<double>(values.size ());
double variance = (sq_sum - sum * sum / static_cast<double>(values.size ())) / (static_cast<double>(values.size ()) - 1);
Expand Down Expand Up @@ -324,24 +324,24 @@ pcl::getMinMax3D (const pcl::PointCloud<PointT> &cloud, const pcl::PointIndices
// If the data is dense, we don't need to check for NaN
if (cloud.is_dense)
{
for (size_t i = 0; i < indices.indices.size (); ++i)
for (const int index : indices.indices)
{
pcl::Array4fMapConst pt = cloud.points[indices.indices[i]].getArray4fMap ();
pcl::Array4fMapConst pt = cloud.points[index].getArray4fMap ();
min_p = min_p.min (pt);
max_p = max_p.max (pt);
}
}
// NaN or Inf values could exist => check for them
else
{
for (size_t i = 0; i < indices.indices.size (); ++i)
for (const int index : indices.indices)
{
// Check if the point is invalid
if (!std::isfinite (cloud.points[indices.indices[i]].x) ||
!std::isfinite (cloud.points[indices.indices[i]].y) ||
!std::isfinite (cloud.points[indices.indices[i]].z))
if (!std::isfinite (cloud.points[index].x) ||
!std::isfinite (cloud.points[index].y) ||
!std::isfinite (cloud.points[index].z))
continue;
pcl::Array4fMapConst pt = cloud.points[indices.indices[i]].getArray4fMap ();
pcl::Array4fMapConst pt = cloud.points[index].getArray4fMap ();
min_p = min_p.min (pt);
max_p = max_p.max (pt);
}
Expand All @@ -361,24 +361,24 @@ pcl::getMinMax3D (const pcl::PointCloud<PointT> &cloud, const std::vector<int> &
// If the data is dense, we don't need to check for NaN
if (cloud.is_dense)
{
for (size_t i = 0; i < indices.size (); ++i)
for (const int index : indices)
{
pcl::Array4fMapConst pt = cloud.points[indices[i]].getArray4fMap ();
pcl::Array4fMapConst pt = cloud.points[index].getArray4fMap ();
min_pt = min_pt.array ().min (pt);
max_pt = max_pt.array ().max (pt);
}
}
// NaN or Inf values could exist => check for them
else
{
for (size_t i = 0; i < indices.size (); ++i)
for (const int index : indices)
{
// Check if the point is invalid
if (!std::isfinite (cloud.points[indices[i]].x) ||
!std::isfinite (cloud.points[indices[i]].y) ||
!std::isfinite (cloud.points[indices[i]].z))
if (!std::isfinite (cloud.points[index].x) ||
!std::isfinite (cloud.points[index].y) ||
!std::isfinite (cloud.points[index].z))
continue;
pcl::Array4fMapConst pt = cloud.points[indices[i]].getArray4fMap ();
pcl::Array4fMapConst pt = cloud.points[index].getArray4fMap ();
min_pt = min_pt.array ().min (pt);
max_pt = max_pt.array ().max (pt);
}
Expand Down
8 changes: 3 additions & 5 deletions common/include/pcl/common/impl/generate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,12 +278,10 @@ pcl::common::CloudGenerator<pcl::PointXY, GeneratorT>::fill (int width, int heig
cloud.resize (cloud.width * cloud.height);
cloud.is_dense = true;

for (pcl::PointCloud<pcl::PointXY>::iterator points_it = cloud.begin ();
points_it != cloud.end ();
++points_it)
for (auto &point : cloud)
{
points_it->x = x_generator_.run ();
points_it->y = y_generator_.run ();
point.x = x_generator_.run ();
point.y = y_generator_.run ();
}
return (0);
}
Expand Down
20 changes: 10 additions & 10 deletions common/include/pcl/common/impl/io.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,8 @@ pcl::copyPointCloud (const pcl::PointCloud<PointT> &cloud_in,
pcl::PointCloud<PointT> &cloud_out)
{
int nr_p = 0;
for (size_t i = 0; i < indices.size (); ++i)
nr_p += indices[i].indices.size ();
for (const auto &index : indices)
nr_p += index.indices.size ();

// Do we want to copy everything? Remember we assume UNIQUE indices
if (nr_p == cloud_in.points.size ())
Expand All @@ -290,13 +290,13 @@ pcl::copyPointCloud (const pcl::PointCloud<PointT> &cloud_in,

// Iterate over each cluster
int cp = 0;
for (size_t cc = 0; cc < indices.size (); ++cc)
for (const auto &index : indices)
{
// Iterate over each idx
for (size_t i = 0; i < indices[cc].indices.size (); ++i)
for (size_t i = 0; i < index.indices.size (); ++i)
{
// Iterate over each dimension
cloud_out.points[cp] = cloud_in.points[indices[cc].indices[i]];
cloud_out.points[cp] = cloud_in.points[index.indices[i]];
cp++;
}
}
Expand All @@ -309,8 +309,8 @@ pcl::copyPointCloud (const pcl::PointCloud<PointInT> &cloud_in,
pcl::PointCloud<PointOutT> &cloud_out)
{
int nr_p = 0;
for (size_t i = 0; i < indices.size (); ++i)
nr_p += indices[i].indices.size ();
for (const auto &index : indices)
nr_p += index.indices.size ();

// Do we want to copy everything? Remember we assume UNIQUE indices
if (nr_p == cloud_in.points.size ())
Expand All @@ -330,12 +330,12 @@ pcl::copyPointCloud (const pcl::PointCloud<PointInT> &cloud_in,

// Iterate over each cluster
int cp = 0;
for (size_t cc = 0; cc < indices.size (); ++cc)
for (const auto &index : indices)
{
// Iterate over each idx
for (size_t i = 0; i < indices[cc].indices.size (); ++i)
for (size_t i = 0; i < index.indices.size (); ++i)
{
copyPoint (cloud_in.points[indices[cc].indices[i]], cloud_out.points[cp]);
copyPoint (cloud_in.points[index.indices[i]], cloud_out.points[cp]);
++cp;
}
}
Expand Down
16 changes: 8 additions & 8 deletions common/include/pcl/impl/cloud_iterator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,13 +358,13 @@ pcl::CloudIterator<PointT>::CloudIterator (
indices.reserve (corrs.size ());
if (source)
{
for (typename Correspondences::const_iterator indexIt = corrs.begin (); indexIt != corrs.end (); ++indexIt)
indices.push_back (indexIt->index_query);
for (const auto &corr : corrs)
indices.push_back (corr.index_query);
}
else
{
for (typename Correspondences::const_iterator indexIt = corrs.begin (); indexIt != corrs.end (); ++indexIt)
indices.push_back (indexIt->index_match);
for (const auto &corr : corrs)
indices.push_back (corr.index_match);
}
iterator_ = new IteratorIdx<PointT> (cloud, indices);
}
Expand Down Expand Up @@ -472,13 +472,13 @@ pcl::ConstCloudIterator<PointT>::ConstCloudIterator (
indices.reserve (corrs.size ());
if (source)
{
for (typename Correspondences::const_iterator indexIt = corrs.begin (); indexIt != corrs.end (); ++indexIt)
indices.push_back (indexIt->index_query);
for (const auto &corr : corrs)
indices.push_back (corr.index_query);
}
else
{
for (typename Correspondences::const_iterator indexIt = corrs.begin (); indexIt != corrs.end (); ++indexIt)
indices.push_back (indexIt->index_match);
for (const auto &corr : corrs)
indices.push_back (corr.index_match);
}
iterator_ = new typename pcl::ConstCloudIterator<PointT>::ConstIteratorIdx (cloud, indices);
}
Expand Down
Loading

0 comments on commit 14c3c75

Please sign in to comment.