vendredi 24 janvier 2020

Problem while iterating a vector using range based loop

I am doing basic implementation of RANSAC using PCL lib. Although, the question here is related to c++ concepts only.

I am iterating point cloud in two ways, one works perfectly, and the other iterates less than half of the points. I just want to understand the reason why one of the two is not working.

Working one:

    for (int index=0; index < cloud->points.size(); index++)
    {
        float distance = abs(A * cloud->points[index].x + B * cloud->points[index].y + C * cloud->points[index].z + D) / sqrt(A * A + B * B + C * C);

        // Check for the two points set above, if present ignore
        if (set_inliers.count(index) > 0)
            continue;

        // If distance is smaller than threshold count it as inlier
        if (distance <= distanceTol)
            set_inliers.insert(index);

        std::cout << "Point Number: " << index << std::endl;
    }

The loop which doesnt work:

int index = 0;

for (auto elem : cloud->points)
{
    float distance = abs(A * elem.x + B * elem.y + C * elem.z + D) / sqrt(A * A + B * B + C * C);

    // Check for the two points set above, if present ignore
    if (set_inliers.count(index) > 0)
        continue;

    // If distance is smaller than threshold count it as inlier
    if (distance <= distanceTol)
        set_inliers.insert(index);

    std::cout << "Point Number: " << index << std::endl;
    index++;
} 

cloud->points is a vector ( see below ). Thus, the range based loop introduced in c++11 should work and both loops mentioned above should be identical, isnt? I guess, I have missed something here..

Variable Details:

In the above code, the var cloud is declared as:

 pcl::PointCloud<pcl::PointXYZ>::Ptr cloud

Ptr is a vector of following:

 std::vector<pcl::PointXYZ, Eigen::aligned_allocator<pcl::PointXYZ>

cloud->points is defined as:

 std::vector<PointT, Eigen::aligned_allocator<PointT> > pcl::PointCloud< PointT >::points

For reference: PCL Point Cloud Reference

I have some understanding issue here and thus it will be great if someone can help out!

Thanks alot!

Aucun commentaire:

Enregistrer un commentaire