获取 vtkPoint 数据坐标的顺序指针

Getting sequential pointers for the coordinates of a vtkPoint data

我编写了以下函数来将 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_pointsmyClass 的成员并且类型为 vtkSmartPointer<vtkPoints>.

这个函数可以满足我的要求并且工作得很好。我想知道是否有一种优雅的方式来获取顺序指针。我尝试了 GetVoidPointer(),它看起来像一个优雅的单行代码,以避免 for 循环但是它在函数 returns vertices 之后没有正确获取坐标。

(double*)(m_points->GetData()->GetVoidPointer(0));

有人可以帮我解决这个问题吗?

vtkPoints 在内部将其数据存储为 float 数组而不是 double 数组。因此,您可能需要修改函数以使用 float* 而不是 double*。如果我们想为 vtkPoints 使用 double 数组,那么我们应该在 vtkPoints 对象上调用 SetDataTypeToDouble()

#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