Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mayavi How to do Delaunay Triangulation with xz data instead of xy data

I am trying to plot some experimental data and I am facing a problem with the triangulation as explained more lengthily here. I figure out that the solution might be to change the grid from a xy to xz one and use the y as the elevation.

Howevever I haven't information about such possibility. So is there a way to do so, maybe by using some masks or some filters to just invert the y and z columns for the triangulation?

Here is a basic code:

import numpy
from mayavi import mlab

X2 = numpy.array([0, 0, 1, 1])
Y2 = numpy.array([0.5, 0.45, 1, 0.5])
Z2 = numpy.array([0, 1, 0.5,0])

fig = mlab.figure(1, bgcolor=(1, 1, 1), fgcolor=(0.5, 0.5, 0.5))
# Define the points in 3D space
# including color code based on Z coordinate.
pts = mlab.points3d(X2, Y2, Z2, Y2, colormap='jet')
# Triangulate based on X, Y with Delaunay 2D algorithm.
# Save resulting triangulation.
mesh = mlab.pipeline.delaunay2d(pts)
# Remove the point representation from the plot
pts.remove()
# Draw a surface based on the triangulation
surf = mlab.pipeline.surface(mesh, colormap='jet')

# Simple plot.
mlab.outline(extent=(0,1,0,1,0,1))
mlab.axes(extent=(0,1,0,1,0,1))
mlab.show()
like image 498
TazgerO Avatar asked Nov 11 '22 15:11

TazgerO


1 Answers

You could use SciPy's Delaunay algorithm to be independent of the order of your data. triangular_mesh() allows you to specify a scalar for a colormap:

import numpy as np
from scipy.spatial import Delaunay
from mayavi import mlab

X2 = np.array([0, 0, 1, 1])
Y2 = np.array([0.5, 0.45, 1, 0.5])
Z2 = np.array([0, 1, 0.5,0])

# use scipy for delaunay:
p2d = np.vstack([X2,Y2]).T
d2d = Delaunay(p2d)

fig = mlab.figure(1, bgcolor=(1, 0.7, 1), fgcolor=(0.5, 0.5, 0.5))

# Generate triangular Mesh:
tmesh = mlab.triangular_mesh(X2, Y2, Z2, d2d.vertices,
                             scalars=Y2, colormap='jet')

# Simple plot.
mlab.outline(extent=(0,1,0,1,0,1))
mlab.axes(extent=(0,1,0,1,0,1))
mlab.show()
like image 157
Dietrich Avatar answered Nov 14 '22 22:11

Dietrich