[vtkusers] How to get data ptr from the vtkpolydata?
Lin M
majcjc at gmail.com
Wed Sep 17 08:26:52 EDT 2014
My code is splitted in many parts... I hope this time it contains most
related parts. It should be compilable if you put it into a main() function
const char *fileName_str = "xxxxx.obj";
vtkSmartPointer< vtkOBJReader > m_SkullMesh = vtkSmartPointer< vtkOBJReader
>::New();
m_SkullMesh->SetFileName(fileName_str);
m_SkullMesh->Update();
double *skullTemplate = new double[3*meshNum];
int meshNum = m_SkullMesh->GetOutput()->GetNumberOfPoints();
for (int i = 0; i < meshNum; ++i) {
double* temp = m_SkullMesh->GetOutput()->GetPoint(i);
skullTemplate[3*i] = temp[0];
skullTemplate[3*i+1] = temp[1];
skullTemplate[3*i+2] = temp[2];
}
I have called update before and I can indeed get access to each point using
m_SkullMesh->GetOutput()->GetPoint(i). The code I post above is workable.
After the for loop, all the points in vtkpolydata is stored in
skullTemplate and have correct results. What I asked is that I think copy
each point data into a double array is not a very efficient way and it
should be possible to get the pointer to the internal array of all points.
So I tried to use another way as below
const char *fileName_str = ba.data();
m_SkullMesh = vtkSmartPointer< vtkOBJReader >::New();
m_SkullMesh->SetFileName(fileName_str);
m_SkullMesh->Update();
double *skullTemplate =
vtkDoubleArray::SafeDownCast(m_SkullMesh->GetOutput()->GetPoints()->GetData())->GetPointer(0);
This will have a run-time error.
Best,
Lin
On Wed, Sep 17, 2014 at 7:56 PM, David Doria <daviddoria at gmail.com> wrote:
> On Wed, Sep 17, 2014 at 7:52 AM, Lin M <majcjc at gmail.com> wrote:
>
>> Thank you for your advice, David.
>>
>> I made a mistake, m_SkullMesh is a vtkOBJReader and the
>> m_SkullMesh->GetOutput() should be a vtkPolyData.
>>
>> I hope to get the point array of it. The code I wrote before could work
>> properly
>>
>> int meshNum = m_SkullMesh->GetOutput()->GetNumberOfPoints();
>> double *skullTemplate = new double[3*meshNum];
>> for(int i = 0; i < meshNum; ++i) {
>> double* temp = m_SkullMesh->GetOutput()->GetPoint(i);
>> skullTemplate[3*i] = temp[0];
>> skullTemplate[3*i+1] = temp[1];
>> skullTemplate[3*i+2] = temp[2];
>> }
>>
>> But I think it should have a better way to avoid the copy and get the
>> data pointer directly. Can you give me some suggestion? Thank you!
>>
>> Best,
>> Lin
>>
>
> Again, please post compilable code. My first guess would be that you did
> not update the reader before using the data you are expecting from
> GetOutput(). Try adding a m_SkullMesh->Update() before the block of code
> you've posted.
>
> David
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20140917/796cfe42/attachment.html>
More information about the vtkusers
mailing list