shashashamti2008
shashashamti2008

Reputation: 2337

Getting sequential pointers for the coordinates of a vtkPoint data

I wrote the following function to store the (x, y, z) of a vtkPoint in an array of type double and size of 3*N, where N is the number of vertices (or points).

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;
}

where m_points is a member of myClass and is of type vtkSmartPointer<vtkPoints>.

This function does what I want and works just fine. I was wondering if there is an elegant way of getting the sequential pointers. I tried GetVoidPointer(), which looks like an elegant one-line code, to avoid the for loop but it does not get the coordinates correctly after the function returns vertices.

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

Could someone help me with this?

Upvotes: 0

Views: 380

Answers (1)

user6764549
user6764549

Reputation:

vtkPoints internally stores it's data as a float array instead of a double array. So you may need to modify your function to work with float* instead of double*. If we want to use double array for vtkPoints then we should call SetDataTypeToDouble() on the vtkPoints object.

#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;
}

This gives result as follows:

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

Upvotes: 1

Related Questions