Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add emplace[_back] to pcl::PointCloud #3207

Merged
merged 3 commits into from
Jul 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions common/include/pcl/point_cloud.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include <pcl/PCLHeader.h>
#include <pcl/exceptions.h>
#include <pcl/point_traits.h>
#include <utility>

namespace pcl
{
Expand Down Expand Up @@ -483,6 +484,20 @@ namespace pcl
height = 1;
}

/** \brief Emplace a new point in the cloud, at the end of the container.
* \note This breaks the organized structure of the cloud by setting the height to 1!
* \param[in] args the parameters to forward to the point to construct
* \return reference to the emplaced point
*/
template <class... Args> inline reference
emplace_back (Args&& ...args)
{
points.emplace_back (std::forward<Args> (args)...);
width = static_cast<uint32_t> (points.size ());
height = 1;
return points.back();
}

/** \brief Insert a new point in the cloud, given an iterator.
* \note This breaks the organized structure of the cloud by setting the height to 1!
* \param[in] position where to insert the point
Expand Down Expand Up @@ -526,6 +541,21 @@ namespace pcl
height = 1;
}

/** \brief Emplace a new point in the cloud, given an iterator.
* \note This breaks the organized structure of the cloud by setting the height to 1!
* \param[in] position iterator before which the point will be emplaced
* \param[in] args the parameters to forward to the point to construct
* \return returns the new position iterator
*/
template <class... Args> inline iterator
emplace (iterator position, Args&& ...args)
{
iterator it = points.emplace (position, std::forward<Args> (args)...);
width = static_cast<uint32_t> (points.size ());
height = 1;
return (it);
}

/** \brief Erase a point in the cloud.
* \note This breaks the organized structure of the cloud by setting the height to 1!
* \param[in] position what data point to erase
Expand Down
21 changes: 15 additions & 6 deletions test/common/test_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ TEST (PCL, PointXYZRGBNormal)
PointXYZRGBNormal p;

uint8_t r = 127, g = 64, b = 254;
uint32_t rgb = (static_cast<uint32_t> (r) << 16 |
static_cast<uint32_t> (g) << 8 |
uint32_t rgb = (static_cast<uint32_t> (r) << 16 |
static_cast<uint32_t> (g) << 8 |
static_cast<uint32_t> (b));
p.rgba = rgb;

Expand Down Expand Up @@ -217,7 +217,7 @@ TEST (PCL, PointCloud)
EXPECT_EQ (mat_xyz (0, 0), 0);
EXPECT_EQ (mat_xyz (2, cloud.width - 1), 3 * cloud.width - 1); // = 29
}

#ifdef NDEBUG
if (Eigen::MatrixXf::Flags & Eigen::RowMajorBit)
{
Expand Down Expand Up @@ -274,6 +274,15 @@ TEST (PCL, PointCloud)
cloud.erase (cloud.begin (), cloud.end ());
EXPECT_EQ (cloud.isOrganized (), false);
EXPECT_EQ (cloud.width, 0);

cloud.emplace (cloud.end (), 1, 1, 1);
EXPECT_EQ (cloud.isOrganized (), false);
EXPECT_EQ (cloud.width, 1);

auto& new_point = cloud.emplace_back (1, 1, 1);
EXPECT_EQ (cloud.isOrganized (), false);
EXPECT_EQ (cloud.width, 2);
EXPECT_EQ (&new_point, &cloud.back ());
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -391,7 +400,7 @@ TEST (PCL, CopyIfFieldExists)
p.normal_x = 1.0; p.normal_y = 0.0; p.normal_z = 0.0;

using FieldList = pcl::traits::fieldList<PointXYZRGBNormal>::type;
bool is_x = false, is_y = false, is_z = false, is_rgb = false,
bool is_x = false, is_y = false, is_z = false, is_rgb = false,
is_normal_x = false, is_normal_y = false, is_normal_z = false;

float x_val, y_val, z_val, normal_x_val, normal_y_val, normal_z_val, rgb_val;
Expand Down Expand Up @@ -422,7 +431,7 @@ TEST (PCL, CopyIfFieldExists)
pcl::for_each_type<FieldList> (CopyIfFieldExists<PointXYZRGBNormal, float> (p, "normal_z", is_normal_z, normal_z_val));
EXPECT_EQ (is_normal_z, true);
EXPECT_EQ (normal_z_val, 0.0);

pcl::for_each_type<FieldList> (CopyIfFieldExists<PointXYZRGBNormal, float> (p, "x", x_val));
EXPECT_EQ (x_val, 1.0);

Expand Down Expand Up @@ -479,7 +488,7 @@ TEST (PCL, IsSamePointType)
EXPECT_FALSE (status);
status = isSamePointType<PointXYZRGB, PointXYZRGB> ();
EXPECT_TRUE (status);

// Even though it's the "same" type, rgb != rgba
status = isSamePointType<PointXYZRGB, PointXYZRGBA> ();
EXPECT_FALSE (status);
Expand Down