c++ - 如何将 void* 类型转换为点云指针

标签 c++ point-cloud-library

在点云库中,指向云的指针是这样创建的:

pcl::PointCloud<pcl::PointXYZ>::Ptr cloudPTR(new pcl::PointCloud<pcl::PointXYZ>)

我需要将 void* 类型转换为云指针。这个的语法是什么?

更具体地说,我正在尝试使用 shmat() 函数并将点云写入共享内存,因此需要转换为云指针。

提前致谢

最佳答案

在发件人方面:

void * pSharedMemory = ... ; // from wherever
pcl::PointCloud<pcl::PointXYZ>::Ptr ptr = ... ; // from wherever
memcpy ( pSharedMemory , static_cast<void const*>(ptr.get()) , sizeof(pcl::PointCloud<pcl::PointXYZ>) ) ;

在接收端:

template <typename T> nothing ( T* ) { }

void * pSharedMemory = ... ; // from wherever
pcl::PointCloud<pcl::PointXYZ>::Ptr ptr ( static_cast<pcl::PointCloud<pcl::PointXYZ>*>(pSharedMemory) , &nothing<pcl::PointCloud<pcl::PointXYZ> > ) ; // sets the destructor to do nothing

此方法使用 shared_ptr 引用内存但不对其进行任何管理。这更像是一个兼容层。

另一种方式,在接收方:

void * pSharedMemory = ... ; // from wherever
pcl::PointCloud<pcl::PointXYZ> * pThing = static_cast<pcl::PointCloud<pcl::PointXYZ> > ( pSharedMemory ) ;
pcl::PointCloud<pcl::PointXYZ>::Ptr ptr = std::make_shared<pcl::PointCloud<pcl::PointXYZ> > ( *pThing ) ; // copies it

安全注意事项:

因为这取决于 pcl::PointCloud<pcl::PointXYZ>作为 POD 类型。您可以通过将其放在文件中的某个地方来确保此假设有效:

static_assert ( std::is_pod<pcl::PointCloud<pcl::PointXYZ> >::value ) ;

或者,如果您不使用 C++11:

BOOST_MPL_ASSERT (( boost::is_pod<pcl::PointCloud<pcl::PointXYZ> > )) ;

这将在编译时确保对象是 POD 类型;如果不是,则不能将其与共享内存一起使用。

关于c++ - 如何将 void* 类型转换为点云指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35762241/

相关文章:

c++ - 什么时候建议在 C++ 中使用 `size_t` 进行数组声明或迭代?

c++ - 使用 boost::interprocess offset_ptr

c++ - const TypedeffedIntPointer 不等于 const int *

point-cloud-library - 比较两个点云相似度的度量

c++ - 在 PCL 中从无组织的点云生成图像

c++ - PCL : X and Y values are changed when pcd file is loaded by PCL library

cmake - find_package(PCL 1.2 REQUIRED) 使用 CMake 返回错误路径

c++ - STL 映射 C++ 中的最高值?

c++ - 为现有缓冲区提供 std::istream 接口(interface)而不复制它

c++ - 当期望 T 作为模板化参数时,make_tuple 将类型 T 作为 T&& 传递