Rev. | c30a7f5c33c935d863797a46d6bef292f1531eb7 |
---|---|
Größe | 1,661 Bytes |
Zeit | 2016-04-14 18:58:14 |
Autor | Lorenzo Isella |
Log Message | A self-contained example about plotting some point in 3D, calculating their convex hull and visualizing them. |
#! /usr/bin/env python
import numpy as np
import pylab as pl
import scipy as sp
from scipy.spatial import ConvexHull
from scipy.spatial.distance import euclidean
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d as a3
# see http://bit.ly/1qJlWkP
aspect = 0
while aspect == 0:
# Generate random points & convex hull
points = np.random.rand(20,3)
hull = ConvexHull(points)
# Check aspect ratios of surface facets
aspectRatio = []
for simplex in hull.simplices:
a = euclidean(points[simplex[0],:], points[simplex[1],:])
b = euclidean(points[simplex[1],:], points[simplex[2],:])
c = euclidean(points[simplex[2],:], points[simplex[0],:])
circRad = (a*b*c)/(np.sqrt((a+b+c)*(b+c-a)*(c+a-b)*(a+b-c)))
inRad = 0.5*np.sqrt(((b+c-a)*(c+a-b)*(a+b-c))/(a+b+c))
aspectRatio.append(inRad/circRad)
# Threshold for minium allowable aspect raio of surface facets
if np.amin(aspectRatio) > 0.3:
aspect = 1
ax = a3.Axes3D(pl.figure())
facetCol = sp.rand(3) #[0.0, 1.0, 0.0]
# Plot hull's vertices
#for vert in hull.vertices:
# ax.scatter(points[vert,0], points[vert,1], zs=points[vert,2])
# Plot surface traingulation
for simplex in hull.simplices:
vtx = [points[simplex[0],:], points[simplex[1],:], points[simplex[2],:]]
tri = a3.art3d.Poly3DCollection([vtx], linewidths = 2, alpha = 0.8)
tri.set_color(facetCol)
tri.set_edgecolor('k')
ax.add_collection3d(tri)
# mp = Axes3D.scatter(points[:,0], points[:,1], point[:,2])
ax.scatter(points[:,0], points[:,1], points[:,2], s=100)
plt.axis('off')
plt.show()
print "So far so good"