Question

How can I extract triangles from delaunay filter in mayavi?

I want to extract the triangles just like matplotlib does

import numpy as np
import matplotlib.delaunay as triang
from enthought.mayavi import mlab

x = np.array([0, 1, 2, 0, 1, 2, 0, 1, 2])
y = np.array([0, 0, 0, 1, 1, 1, 2, 2, 2])
z = np.zeros(9)
#matplotlib 
centers, edges, triangles_index, neig = triang.delaunay(x,y)

#mayavi
vtk_source = mlab.pipeline.scalar_scatter(x, y, z, figure=False)
delaunay =  mlab.pipeline.delaunay2d(vtk_source)

I want to extract the triangles from mayavi delaunay filter to obtain the variables @triangle_index and @centers (just like matplotlib)

The only thing I've found is this http://docs.enthought.com/mayavi/mayavi/auto/example_delaunay_graph.html

but only get the edges, and are codificated different than matplotlib

Was it helpful?

Solution

To get the triangles index:

poly = delaunay.outputs[0]
tindex = poly.polys.data.to_array().reshape(-1, 4)[:, 1:]

poly is a PolyData object, poly.polys is a CellArray object that stores the index information. For detail about CellArray: http://www.vtk.org/doc/nightly/html/classvtkCellArray.html

To get the center of every circumcircle, you need to loop every triangle and calculate the center:

centers = []
for i in xrange(poly.number_of_cells):
    cell = poly.get_cell(i)
    points = cell.points.to_array()[:, :-1].tolist()
    center = [0, 0]
    points.append(center)
    cell.circumcircle(*points)
    centers.append(center)

centers = np.array(centers)

cell.circumcircle() is a static function, so you need to pass all the points of the triangle as arguments, the center data will be returned by modify the fourth argument.

Here is the full code:

import numpy as np
from enthought.mayavi import mlab

x = np.array([0, 1, 2, 0, 1, 2, 0, 1, 2])
y = np.array([0, 0, 0, 1, 1, 1, 2, 2, 2])
z = np.zeros(9)

vtk_source = mlab.pipeline.scalar_scatter(x, y, z, figure=False)
delaunay =  mlab.pipeline.delaunay2d(vtk_source)

poly = delaunay.outputs[0]
tindex = poly.polys.data.to_array().reshape(-1, 4)[:, 1:]

centers = []
for i in xrange(poly.number_of_cells):
    cell = poly.get_cell(i)
    points = cell.points.to_array()[:, :-1].tolist()
    center = [0, 0]
    points.append(center)
    cell.circumcircle(*points)
    centers.append(center)

centers = np.array(centers)

print centers
print tindex

The output is:

[[ 1.5  0.5]
 [ 1.5  0.5]
 [ 0.5  1.5]
 [ 0.5  0.5]
 [ 0.5  0.5]
 [ 0.5  1.5]
 [ 1.5  1.5]
 [ 1.5  1.5]]
[[5 4 2]
 [4 1 2]
 [7 6 4]
 [4 3 1]
 [3 0 1]
 [6 3 4]
 [8 7 4]
 [8 4 5]]

The result may not be the same as matplotlib.delaunay, because there are many possible solutions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top