Discussion:
[vtkusers] How do I get triangles from the vtkPolyData that vtkDelaunay2D produces?
Grant Edwards
2007-09-01 15:23:08 UTC
Permalink
I've been Googling and wandering through the class references
most of the afternoon, but I can't figure out how to get the
triangles out of the vtkPolyData that vtkDelaunay2D produces?

I can get the vertex corredinates like this:

delny = vtk.vtkDelaunay2D()
delny.SetInput(profile)
delny.SetTolerance(0.001)
delny.Update()

o = delny.GetOutput()

vertexes = [o.GetPoint(i) for i in xrange(o.GetNumberOfPoints())]

When I use vtkPolyDataWriter to write output to a file, I can
see that there are 84 triangles, and 'o' is a vtkPolyData
object that has 84 cells and 84 polygons, so they obviously
represent the triangles, but I can't figure out how to get
something useful out of the cells or polys. What I'm
ultimately looking for is a list/array of 3-tuples containing
indexes into the vertex list obtained via the alls to GetPoint().

[I've probably stumbled past the answer multiple times but
didn't recognize because I don't really grok C++.]
--
Grant Edwards grante Yow! ! The land of the
at rising SONY!!
visi.com
Godofredo
2007-09-03 11:49:13 UTC
Permalink
Hi. This is the code I use to extract the triangles of a Delaunay
triangulation:
//The input of this code is the triangulated polydata "oPolyDataDel"
// the output is an array with the indexes to the points "vTriangles"
int j=0;
int h;
int npts=3,*pts;
vtkCellArray* oCellArr= vtkCellArray::New();
oCellArr=oPolyDataDel->GetPolys();
for(int i=0;i<oPolyDataDel->GetNumberOfPolys();i++)
{
h=oCellArr->GetNextCell(npts,pts);

if(h==0){
break;
}
if(npts==3){
vTriangles[j]= pts[0];
vTriangles[j+1]= pts[1];
vTriangles[j+2]= pts[2];
j+=3;
}

}

HDH.
Post by Grant Edwards
I've been Googling and wandering through the class references
most of the afternoon, but I can't figure out how to get the
triangles out of the vtkPolyData that vtkDelaunay2D produces?
delny = vtk.vtkDelaunay2D()
delny.SetInput(profile)
delny.SetTolerance(0.001)
delny.Update()
o = delny.GetOutput()
vertexes = [o.GetPoint(i) for i in xrange(o.GetNumberOfPoints())]
When I use vtkPolyDataWriter to write output to a file, I can
see that there are 84 triangles, and 'o' is a vtkPolyData
object that has 84 cells and 84 polygons, so they obviously
represent the triangles, but I can't figure out how to get
something useful out of the cells or polys. What I'm
ultimately looking for is a list/array of 3-tuples containing
indexes into the vertex list obtained via the alls to GetPoint().
[I've probably stumbled past the answer multiple times but
didn't recognize because I don't really grok C++.]
--
Grant Edwards grante Yow! ! The land of the
at rising SONY!!
visi.com
_______________________________________________
This is the private VTK discussion list.
http://www.vtk.org/Wiki/VTK_FAQ
http://www.vtk.org/mailman/listinfo/vtkusers
--
View this message in context: http://www.nabble.com/How-do-I-get-triangles-from-the-vtkPolyData-that-vtkDelaunay2D-produces--tf4364875.html#a12460413
Sent from the VTK - Users mailing list archive at Nabble.com.
Grant Edwards
2007-09-03 13:36:25 UTC
Permalink
Post by Godofredo
Post by Grant Edwards
I've been Googling and wandering through the class references
most of the afternoon, but I can't figure out how to get the
triangles out of the vtkPolyData that vtkDelaunay2D produces?
Hi. This is the code I use to extract the triangles of a Delaunay
//The input of this code is the triangulated polydata "oPolyDataDel"
// the output is an array with the indexes to the points "vTriangles"
int j=0;
int h;
int npts=3,*pts;
vtkCellArray* oCellArr= vtkCellArray::New();
oCellArr=oPolyDataDel->GetPolys();
for(int i=0;i<oPolyDataDel->GetNumberOfPolys();i++)
{
h=oCellArr->GetNextCell(npts,pts);
if(h==0){
break;
}
if(npts==3){
vTriangles[j]= pts[0];
vTriangles[j+1]= pts[1];
vTriangles[j+2]= pts[2];
j+=3;
}
}
Thatnks for the example, but I can't figure out how to do the
equivalent in Python. Calling oPolyDataDel->GetPolys() returns
a vtkCellArray object (which agrees with your C++ example).
However, the returned vtkCellArray object doesn't have a
GetNextCell() method.

I also just tried indexing (e.g. cell = oCellArr[i]). Common
sense would indicate that since it's an array it should be
subscriptable, but I guess not.

Unfortunately there seems to be zero documentation for the
Python binding of the vtk library. :/

Here are the visible attributes/methods of a vtkCellArray
object:

AddObserver
Allocate
BreakOnError
DebugOff
DebugOn
DeepCopy
EstimateSize
FastDelete
GetActualMemorySize
GetAddressAsString
GetClassName
GetData
GetDebug
GetGlobalWarningDisplay
GetInsertLocation
GetMTime
GetMaxCellSize
GetNumberOfCells
GetNumberOfConnectivityEntries
GetReferenceCount
GetSize
GetTraversalLocation
GlobalWarningDisplayOff
GlobalWarningDisplayOn
HasObserver
InitTraversal
Initialize
InsertCellPoint
InsertNextCell
InvokeEvent
IsA
IsTypeOf
Modified
NewInstance
PrintRevisions
Register
RemoveObserver
RemoveObservers
Reset
ReverseCell
SafeDownCast
SetCells
SetDebug
SetGlobalWarningDisplay
SetNumberOfCells
SetReferenceCount
SetTraversalLocation
Squeeze
UnRegister
UpdateCellCount
--
Grant Edwards grante Yow! I request a weekend
at in Havana with Phil
visi.com Silvers!
Sylvain Jaume
2007-09-03 13:50:48 UTC
Permalink
Hi Edwards,

You can use:

vtkIdType npts, *pts;

o->GetPolys()->InitTraversal();

while(o->GetPolys()->GetNextCell(npts,pts))
{
cout << "triangle [" << pts[0] << " " << pts[1] << " " << pts[2] <<
"]" << endl;
}

Sincerely,
Sylvain

Date: Sat, 1 Sep 2007 15:23:08 +0000 (UTC)
From: Grant Edwards <***@visi.com>
Subject: [vtkusers] How do I get triangles from the vtkPolyData that
vtkDelaunay2D produces?
To: ***@public.kitware.com
Message-ID: <fbc04s$f17$***@sea.gmane.org>

I've been Googling and wandering through the class references
most of the afternoon, but I can't figure out how to get the
triangles out of the vtkPolyData that vtkDelaunay2D produces?

I can get the vertex corredinates like this:

delny = vtk.vtkDelaunay2D()
delny.SetInput(profile)
delny.SetTolerance(0.001)
delny.Update()

o = delny.GetOutput()

vertexes = [o.GetPoint(i) for i in xrange(o.GetNumberOfPoints())]

When I use vtkPolyDataWriter to write output to a file, I can
see that there are 84 triangles, and 'o' is a vtkPolyData
object that has 84 cells and 84 polygons, so they obviously
represent the triangles, but I can't figure out how to get
something useful out of the cells or polys. What I'm
ultimately looking for is a list/array of 3-tuples containing
indexes into the vertex list obtained via the alls to GetPoint().

[I've probably stumbled past the answer multiple times but
didn't recognize because I don't really grok C++.]

-- Grant Edwards grante Yow! ! The land of the at rising SONY!! visi.com
Grant Edwards
2007-09-04 15:38:11 UTC
Permalink
Post by Sylvain Jaume
Hi Edwards,
vtkIdType npts, *pts;
o->GetPolys()->InitTraversal();
while(o->GetPolys()->GetNextCell(npts,pts))
{
cout << "triangle [" << pts[0] << " " << pts[1] << " " << pts[2] << "]" << endl;
}
Thanks for the example.

The Python binding of the vtk library doesn't support the
GetNextCell() and GetCell() methods for the vtkCellArray object
returned by o->GetPolys(). Is there any other way to get the
triangle info out?
--
Grant Edwards grante Yow! FROZEN ENTREES may
at be flung by members of
visi.com opposing SWANSON SECTS ...
Grant Edwards
2007-09-04 16:25:20 UTC
Permalink
Post by Grant Edwards
Post by Sylvain Jaume
Hi Edwards,
vtkIdType npts, *pts;
o->GetPolys()->InitTraversal();
while(o->GetPolys()->GetNextCell(npts,pts))
{
cout << "triangle [" << pts[0] << " " << pts[1] << " " << pts[2] << "]" << endl;
}
Thanks for the example.
The Python binding of the vtk library doesn't support the
GetNextCell() and GetCell() methods for the vtkCellArray object
returned by o->GetPolys(). Is there any other way to get the
triangle info out?
I ended up having to call GetData and parse the polygon indexes
out of the raw data:

o = delny.GetOutput()
vertexes = [o.GetPoint(i) for i in xrange(o.GetNumberOfPoints())]

pdata = polys.GetPolys().GetData()

values = [int(pdata.GetTuple1(i)) for i in xrange(pdata.GetNumberOfTuples())]

triangles = []
while values:
n = values[0] # number of points in the polygon
triangles.append(values[1:n+1])
del values[0:n+1]

IMO, it's pretty lame to have to do it this way, but it seems
to work.
--
Grant Edwards grante Yow! I've read SEVEN
at MILLION books!!
visi.com
Loading...