[vtkusers] How to get access to polygons?

David Gobbi david.gobbi at gmail.com
Fri Jan 4 18:23:19 EST 2013


On Fri, Jan 4, 2013 at 3:53 PM, Wenlong Wang <scc.wwl at gmail.com> wrote:

> Hi, David
>
> Thank you very much for your reply. It is really helpful and fits my
> problem perfectly.
>
> At the same time, I find another way, that is
>
> *    polys->InitTraversal();
>     for(int i = 0; i < polys->GetNumberOfCells(); i++)
>     {
>         polys->GetNextCell(idList);
>         int a = idList->GetId(0);
>         int b = idList->GetId(1);
>         int c = idList->GetId(2);
>     }*


I avoid this method because GetNextCell() is not thread-safe.
And generally if I'm just accessing data (as opposed to modifying it) then
I try to do so in a thread-safe manner.


> However, I find this way doesn't work in the following code clip
> *
>     polys->InitTraversal();
>     for(int i = 0; i < polys->GetNumberOfCells(); i++)
>     {
>         polys->GetCell(i, idList);    // This method returns wrong ids
>         int a = idList->GetId(0);
>         int b = idList->GetId(1);
>         int c = idList->GetId(2);
>     }*
>
> May I know why? And how to deal with it?
>

It shouldn't work.  Take another look at the code that I sent you,
specifically at the way that I calculated the cellLocation.  At each
iteration the cellLocation must be incremented by "1 + numIds".

Consider your .vtk file and the way it stores triangles:
triangle 0 starts at location 0
triangle 1 starts at location 4
triangle 2 starts at location 8

The location always increases by the number of pointIds plus 1.  You can't
use "i" as the location.

 - David

 2013/1/4 David Gobbi <david.gobbi at gmail.com>
>
>> Hi Wenlong,
>>
>> My favorite way of getting polygons out of a vtkCellArray is with
>> a loop like this:
>>
>>   vtkIdType numCells = cellArray->GetNumberOfCells();
>>   vtkIdType cellLocation = 0; // the index into the cell array
>>
>>   for (vtkIdType i = 0; i < numCells; i++)
>>     {
>>     vtkIdType numIds; // to hold the size of the cell
>>     vtkIdType *pointIds; // to hold the ids in the cell
>>
>>     cellArray->GetCell(cellLocation, numIds, pointIds);
>>     cellLocation += 1 + numIds;
>>
>>     ...
>>   }
>>
>>  - David
>>
>>
>> On Fri, Jan 4, 2013 at 2:44 PM, Wenlong Wang <scc.wwl at gmail.com> wrote:
>> > Hi, all
>> >
>> > I am looking into a 3D shape model. It has 5,000+ points, and 9,000+
>> cells.
>> > Thus in the .vtk file, I have the polygon data in the format such as
>> follows
>> >
>> > POLYGONS 9637 38548
>> > 3 0 1 2
>> > 3 1 3 4
>> > 3 2 1 4
>> > 3 3 5 6
>> > 3 4 3 6
>> > 3 5 7 8
>> > .......
>> >
>> > The first number of each line indicates how many points are included in
>> the
>> > cell, and the next 3 numbers indicate the point ids. In my case, the
>> polygon
>> > is combined by triangles.
>> >
>> > I use vtkGenericDataObjectReader to open the .vtk file, and then use
>> > vtkPolyData::GetPolys to get the polygons of the shape model, whose
>> type is
>> > vtkCellArray pointer.
>> >
>> > Here is my code clip:
>> >     vtkSmartPointer<vtkGenericDataObjectReader> reader =
>> > vtkSmartPointer<vtkGenericDataObjectReader>::New();
>> >     reader->SetFileName("C:\\meanshape.vtk");
>> >     reader->OpenVTKFile();
>> >     reader->Update();
>> >
>> >     vtkSmartPointer<vtkPolyData> shape =
>> > vtkSmartPointer<vtkPolyData>::New();
>> >     shape = reader->GetPolyDataOutput();
>> >
>> >     vtkSmartPointer<vtkCellArray> polys =
>> > vtkSmartPointer<vtkCellArray>::New();
>> >     polys = shape->GetPolys();
>> >
>> > My problem is: After I have polygon data in a vtkCellArray instance,
>> how can
>> > I get access to the point ids in each triangle cell? For example, I
>> want to
>> > read "0 1 2" in the first line, "1 3 4" in the second line, etc.
>> >
>> > Can you help me out of this? Many thanks in advance for your kind help.
>> >
>> > All bests
>> > Wenlong
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20130104/1f35fe17/attachment.htm>


More information about the vtkusers mailing list