Skip to content

Instantly share code, notes, and snippets.

@chrissunny94
Forked from PerceptMD/PCL_smart_pointer.md
Created September 21, 2023 10:45
Show Gist options
  • Select an option

  • Save chrissunny94/6f6ddb7826a661487789b45c61f743c1 to your computer and use it in GitHub Desktop.

Select an option

Save chrissunny94/6f6ddb7826a661487789b45c61f743c1 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

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