This is a followup of a question of mine at stackoverflow partially reproduced here for clarity.
I'm playing a bit with scikit-image
marching cubes algorithm. Here is a simplified version of the example given in the docs.
from matplotlib import pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
from skimage import measure
x = np.linspace(-1, 1, 11)
X, Y, Z = np.meshgrid(x, x, x, indexing = 'ij')
def f(x, y, z):
return x
verts, faces = measure.marching_cubes(f(X, Y, Z), 0.6)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
mesh = Poly3DCollection(verts[faces])
ax.add_collection3d(mesh)
ax.set_xlim(0, 10)
ax.set_ylim(0, 10)
ax.set_zlim(0, 10)
plt.show()
Here is the resulting surface:
Now I'd like to draw the surface with asymptote. So basically, I have to find a way to export the coordinates of the triangles. Easy: each triangle is an element of verts[faces]
represented by 3 lists, the elements being the 3 coordinates of each vertice. So I can parse verts[faces]
and write to a file path3 triangle = (x1, y1, z1) -- (x2, y2, z2) -- (x3, y3, z3) -- cycle
for each triangle. Then, for each triangle
, I draw the surface: draw(surface(triangle))
. But is there a better way to define the surface for asymptote?
Note that in real life, the surface is far more complex than a simple plane :)
surface toreturn
and the three lines beginningpatch triangle =
. (Only ignore the line about normals, that has been removed since I wrote that answer.)