首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SciPy空间Delaunay/ConvexHull混淆

SciPy空间Delaunay/ConvexHull混淆
EN

Stack Overflow用户
提问于 2015-02-06 15:02:03
回答 1查看 2.3K关注 0票数 1

我试图生成随机凸多面体。我生成一组随机的3D坐标,然后找到它们的凸包(到目前为止还不错)。

然后我想我应该用Delaunay三角剖分来给出凸壳的三角剖分。这就是我的基本理解开始显现的地方!

这是密码

代码语言:javascript
复制
import numpy as np
from scipy.spatial import ConvexHull
from scipy.spatial import Delaunay
import matplotlib as mpl
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# Generate random points & convex hull
points = np.random.rand(20,3)
hull = ConvexHull(points)

fig = plt.figure()
ax = fig.gca(projection = '3d')

# Plot hull's vertices
for vert in hull.vertices:    
    ax.scatter(points[vert,0], points[vert,1], zs=points[vert,2])#, 'ro')

# Calculate Delaunay triangulation & plot
tri = Delaunay(points[hull.vertices])
for simplex in tri.simplices:
    vert1 = [points[simplex[0],0], points[simplex[0],1], points[simplex[0],2]]
    vert2 = [points[simplex[1],0], points[simplex[1],1], points[simplex[1],2]]
    vert3 = [points[simplex[2],0], points[simplex[2],1], points[simplex[2],2]]
    vert4 = [points[simplex[3],0], points[simplex[3],1], points[simplex[3],2]]
    ax.plot([vert1[0], vert2[0]], [vert1[1], vert2[1]], zs = [vert1[2], vert2[2]])
    ax.plot([vert2[0], vert3[0]], [vert2[1], vert3[1]], zs = [vert2[2], vert3[2]])
    ax.plot([vert3[0], vert4[0]], [vert3[1], vert4[1]], zs = [vert3[2], vert4[2]])
    ax.plot([vert4[0], vert1[0]], [vert4[1], vert1[1]], zs = [vert4[2], vert1[2]])

plt.show()

有几件事是关于我的,情节有时遗漏了船体上的点&这似乎是Delaunay四面体化,我想我不应该对此感到惊讶,而不是我想要的。

我想要一个仅仅是船体表面的三角剖分,所以我想一个包含表面小面的单纯形?这个是可能的吗?

谢谢

B

编辑:在pv的启示后,下面我已经修改了代码如下;

代码语言:javascript
复制
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

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)

plt.axis('off')
plt.show()

现在一切都如我所愿。我增加了一个纵横比阈值,以确保更好的三角剖分。

B

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-02-06 18:24:59

有些事情:

  • 您将points[hull.vertices]作为参数提供给Delaunay,因此tri.simplices中的整数是points[hull.vertices]的索引,而不是points的索引,因此最终会绘制错误的点。
  • 四面体有6个山脊,但你只画了4个。
  • 如果您只需要凸包表面的三角剖分,这可以作为hull.simplices提供。

也就是说,

代码语言:javascript
复制
for simplex in hull.simplices:
    xs, ys, zs = points[simplex].T
    xs = np.r_[xs, xs[0]] # close polygons
    ys = np.r_[ys, ys[0]]
    zs = np.r_[zs, zs[0]]
    ax.plot(xs, ys, zs)

或者只是:

代码语言:javascript
复制
ax.plot_trisurf(points[:,0], points[:,1], points[:,2],
                triangles=hull.simplices)
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28368600

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档