matplotlib - Delaunay triangularization of Polyhedron (Python) -


i'm trying delaunay triangulation of polyhedron in python can calculate centroid. see there delaunay function in scipy.spatial , works in n-dimensions. trouble documentation shows 2d use , gives me no indication of higher dimensions. being able decompose object array solve issue me, don't know how that.

the problem i'm running not know how verify working correctly outputting object. can find nothing on google how graph polyhedron or how use object scipy spitting back.

if do

import numpy np scipy.spatial import delaunay  points = np.array([[0,0,0],[1,0,0],[1,1,0],[1,0,1],[1,1,1],[0,1,0],[0,1,1],[0,0,1]]) delaunay(points) 

i able coordinates of these tetrahedrons can calculate centroids of polyhedrons. nice if able graph tesselated polyhedron too. saw in matlab can fuction called trimesn, , found 1 matplotlib seems different , documentation not great.

from matplotlib.collections import trimesh trimesh.__doc__   u'\n      class efficient drawing of triangular mesh using\n    gouraud shading.\n\n    triangular mesh :class:`~matplotlib.tri.triangulation`\n    object.\n    ' 

what tess = delaunay(pts) returns object of delanauy class. can check tetrahedrons tess.simplices. has different attributes , methods. in 2d, example, can plot triangulation, convex hull , voronoi tesselation.

regarding visualization of final collection of tetrahedrons didn't find straightforward way of doing it, managed working script. check code below.

from __future__ import division import numpy np scipy.spatial import delaunay import matplotlib.pyplot plt mpl_toolkits.mplot3d import axes3d mpl_toolkits.mplot3d.art3d import poly3dcollection, line3dcollection itertools import combinations   def plot_tetra(tetra, pts, color="green", alpha=0.1, lc="k", lw=1):     combs = combinations(tetra, 3)     comb in combs:         x = pts[comb, 0]         y = pts[comb, 1]         z = pts[comb, 2]         verts = [zip(x, y, z)]         triangle = poly3dcollection(verts, facecolors=color, alpha=0.1)         lines = line3dcollection(verts, colors=lc, linewidths=lw)         ax.add_collection3d(triangle)         ax.add_collection3d(lines)  pts = np.array([             [0,0,0],             [1,0,0],             [1,1,0],             [1,0,1],             [1,1,1],             [0,1,0],             [0,1,1],             [0,0,1]]) tess = delaunay(pts)  fig = plt.figure() ax = fig.add_subplot(111, projection='3d') k, tetra in enumerate(tess.simplices):     color = plt.cm.accent(k/(tess.nsimplex - 1))     plot_tetra(tetra, pts, color=color, alpha=0.1, lw=0.5, lc="k") ax.scatter(pts[:, 0], pts[:, 1], pts[:, 2], c='k') plt.savefig("delaunay.png", dpi=600) plt.show() 

the resulting image is

enter image description here


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -