我编写了以下函数来存储 vtkPoint
的 (x, y, z)在 double
类型的数组中和大小 3*N
, 其中N
是顶点(或点)的数量。
double* myClass::getMyPoints(void)
{
double* vertices = new double[this->m_numberOfVertices * 3];
for (vtkIdType ivert = 0; ivert < this->m_numberOfVertices; ivert++)
for (auto i = 0; i < 3; ++i)
this->m_points->GetPoint(ivert, &vertices[3 * ivert]);
return vertices;
}
哪里m_points
是 myClass
的成员类型为 vtkSmartPointer<vtkPoints>
.
这个函数做我想做的并且工作得很好。我想知道是否有一种优雅的方式来获取顺序指针。我试过 GetVoidPointer()
,这看起来像一个优雅的单行代码,以避免 for
循环但在函数返回后无法正确获取坐标 vertices
.
(double*)(m_points->GetData()->GetVoidPointer(0));
有人可以帮我吗?
最佳答案
vtkPoints
在内部将其数据存储为 float
数组而不是 double
数组。因此,您可能需要修改函数以使用 float*
而不是 double*
。如果我们想为 vtkPoints
使用 double
数组,那么我们应该调用 SetDataTypeToDouble()
在 vtkPoints
对象上。
#include <stdio.h>
#include <stdlib.h>
#include <vtkPoints.h>
#include <vtkSmartPointer.h>
int main(){
// Create data
auto N = 5;
vtkNew<vtkPoints> pts;
pts->SetDataTypeToDouble();
for(auto i=0; i < N; ++i)
pts->InsertNextPoint(rand()%100,rand()%100,rand()%100);
// Read using for loop
std::cout<< "Using for loop ... " << std::endl;
for( auto j=0; j < N; ++j ){
double p[3];
pts->GetPoint( j, p );
std::cout<< p[0] << "," << p[1] << "," << p[2] << std::endl;
}
// Read using GetVoidPointer()
std::cout<< "Using GetVoidPointer() ... " << std::endl;
auto data_ptr = (double*) pts->GetData()->GetVoidPointer(0);
for( auto k = 0; k < N; ++k )
std::cout<< *(data_ptr + 3*k) << ","
<< *(data_ptr + 3*k + 1) << ","
<< *(data_ptr + 3*k + 2) << std::endl;
return 0;
}
结果如下:
Test that there are N = 5 tuples.
Using for loop ...
83,86,77
15,93,35
86,92,49
21,62,27
90,59,63
Using GetVoidPointer() ...
83,86,77
15,93,35
86,92,49
21,62,27
90,59,63
关于c++ - 获取 vtkPoint 数据坐标的顺序指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51905940/