在点云库中,指向云的指针是这样创建的:
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) , ¬hing<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/