Skip to content

Instantly share code, notes, and snippets.

@PerceptMD
Created September 22, 2020 10:17
Show Gist options
  • Select an option

  • Save PerceptMD/26d73d673a64c381d62c7c5b638994db to your computer and use it in GitHub Desktop.

Select an option

Save PerceptMD/26d73d673a64c381d62c7c5b638994db to your computer and use it in GitHub Desktop.
PCL Smart Pointer

Links

http://www.pcl-users.org/PointCloud-Ptr-td3559529.html

https://stackoverflow.com/questions/10644429/create-a-pclpointcloudptr-from-a-pclpointcloud

Test1 - Adding / manipulation Points in const Point Cloud

const pcl::PointCloud<pcl::PointXYZ> cloud(10,10);
const pcl::PointXYZ p(1,1,1);
cloud[0,0] = p; // set Point 0,0,0 of organized cloud
cloud.push_back (p); // add Point to the end, increases cloud size

last 2 lines changing cloud dont compile as long as cloud is const:

passing ‘const pcl::PointXYZ’ as ‘this’ argument discards qualifiers [-fpermissive] test.cpp /pcl_icp line 28 C/C++ Problem

Test2 - Naive way to set reference of Ptr to Cloud address

const pcl::PointCloud<pcl::PointXYZ> cloud(10,10);
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_ptr;
cloud_ptr = &cloud;

no match for ‘operator=’ (operand types are ‘pcl::PointCloudpcl::PointXYZ::Ptr {aka std::shared_ptr<pcl::PointCloudpcl::PointXYZ >}’ and ‘const pcl::PointCloudpcl::PointXYZ*’) test.cpp /pcl_icp line 31 C/C++ Problem

Test3 - Creating Point Cloud in scope and use Pointer outside that scope

pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_ptr;

{
	pcl::PointCloud<pcl::PointXYZ> cloud(10,10);
	const pcl::PointXYZ p(1,1,1);
	cloud[0,0] = p;
	cloud_ptr.reset(new pcl::PointCloud<pcl::PointXYZ> (cloud));
}


std::cerr << "Point cloud data: " << cloud_ptr->size () << " points" << std::endl;
for (const auto& point: *cloud_ptr)
std::cerr << "    " << point.x << " "
					<< point.y << " "
					<< point.z << std::endl;

Works like a charm and prints Point Cloud from outside that scope! But cloud does not exist outside small scope! Only cloud_ptr makes it accessible.

Test4 -

https://stackoverflow.com/questions/32235862/trouble-with-const-pointers

bool pcl::visualization::PCLVisualizer::addPointCloud(
    const pcl::PointCloud<pcl::PointXYZ >::ConstPtr & cloud,
    const std::string &     id = "cloud",
    int     viewport = 0     
)

Its first parameter is a const reference to a pcl::PointCloudpcl::PointXYZ::ConstPtr, where ConstPtr is a typedef for boost::shared_ptr<const PointCloudpcl::PointXYZ>.

pcl::PointCloud<pcl::PointXYZ> cloud(10,10);
const pcl::PointXYZ p(1,1,1);
cloud[0,0] = p;
pcl::PointCloud<pcl::PointXYZ>::ConstPtr cloud_constptr (new pcl::PointCloud<pcl::PointXYZ>(cloud));
const pcl::PointXYZ p2(2,2,2);
cloud_constptr->points[0,0] = p2;

passing ‘const value_type {aka const pcl::PointXYZ}’ as ‘this’ argument discards qualifiers [-fpermissive] test.cpp /pcl_icp line 32 C/C++ Problem

pcl::PointCloud<pcl::PointXYZ> cloud(10,10);
const pcl::PointXYZ p(1,1,1);
cloud[0,0] = p;
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_ptr (new pcl::PointCloud<pcl::PointXYZ>(cloud));
const pcl::PointXYZ p2(2,2,2);
cloud_ptr->points[0,0] = p2;

Works! Because non const Ptr!

Set ConstPtr to different Cloud:

pcl::PointCloud<pcl::PointXYZ>::ConstPtr cloud_constptr (new pcl::PointCloud<pcl::PointXYZ>(cloud1));
cloud_constptr.reset(new pcl::PointCloud<pcl::PointXYZ>(cloud2));

Compile Error because const ... ConstPtr: no matching function for call to ‘std::shared_ptr<const pcl::PointCloudpcl::PointXYZ >::reset(pcl::PointCloudpcl::PointXYZ*) const’ test.cpp /pcl_icp line 33 C/C++ Problem

@mbe19
Copy link

mbe19 commented Sep 25, 2020

#Test 1:
It is very understandable I guess, the Cloud is const changing it should not be allowed. (C++ Standards)

#Test 2:
Shared pointers (pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_ptr;) and raw pointers (&cloud) are managed differently, hence they cannot be interchanged.

#Test 3:
Amazing!

#Test 4:
For me, as I interpret const in either ::ConstPtr or const PointCloud, is that you will not be able to modify the const part in any way and that also includes passing the consted object to a function which does not clearly declare arguments as const, but you can in some cases (maybe every time) to pass a normal variable pointer to a const argument...
One more thing, should the call be like pointer->method() or ((*pointer).method()) because we calling using a pointer I guess the call with an object can accept this object.method() but not pointers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment