绘制 3D 多边形
我未能成功浏览网页寻找以下简单问题的解决方案:
如何使用顶点值绘制 3D 多边形(例如填充矩形或三角形)? 我尝试了很多想法,但都失败了,请参阅:
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.collections import PolyCollection
import matplotlib.pyplot as plt
fig = plt.figure()
ax = Axes3D(fig)
x = [0,1,1,0]
y = [0,0,1,1]
z = [0,1,0,1]
verts = [zip(x, y,z)]
ax.add_collection3d(PolyCollection(verts),zs=z)
plt.show()
我提前感谢任何想法/评论。
根据接受的答案进行更新:
import mpl_toolkits.mplot3d as a3
import matplotlib.colors as colors
import pylab as pl
import numpy as np
ax = a3.Axes3D(pl.figure())
for i in range(10000):
vtx = np.random.rand(3,3)
tri = a3.art3d.Poly3DCollection([vtx])
tri.set_color(colors.rgb2hex(np.random.rand(3)))
tri.set_edgecolor('k')
ax.add_collection3d(tri)
pl.show()
结果如下:
I was unsuccessful browsing web for a solution for the following simple question:
How to draw 3D polygon (say a filled rectangle or triangle) using vertices values?
I have tried many ideas but all failed, see:
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.collections import PolyCollection
import matplotlib.pyplot as plt
fig = plt.figure()
ax = Axes3D(fig)
x = [0,1,1,0]
y = [0,0,1,1]
z = [0,1,0,1]
verts = [zip(x, y,z)]
ax.add_collection3d(PolyCollection(verts),zs=z)
plt.show()
I appreciate in advance any idea/comment.
Updates based on the accepted answer:
import mpl_toolkits.mplot3d as a3
import matplotlib.colors as colors
import pylab as pl
import numpy as np
ax = a3.Axes3D(pl.figure())
for i in range(10000):
vtx = np.random.rand(3,3)
tri = a3.art3d.Poly3DCollection([vtx])
tri.set_color(colors.rgb2hex(np.random.rand(3)))
tri.set_edgecolor('k')
ax.add_collection3d(tri)
pl.show()
Here is the result:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想你已经差不多明白了。这是你想要的吗?
您可能还对 art3d.pathpatch_2d_to_3d 感兴趣。
I think you've almost got it. Is this what you want?
You might also be interested in art3d.pathpatch_2d_to_3d.
不幸的是,答案和更新代码都会导致可视化不正确,但原因不同。下面提供了解释。
解决方案
首先考虑一个“正确”答案。 Matplotlib 将正确绘制填充多边形。多边形是平面。问题中的四个示例顶点不是平面的。但这些顶点可以用来定义两个三角形。 art3d.Poly3DCollection 是 (N, 3) 个类似数组的多边形序列的列表。使用四个顶点 v 的列表和两个三角形面 f 的顶点索引列表,一个简单的解决方案是:
生成具有不同颜色的两个多边形的图形,如下所示:
这甚至适用于具有混合边数的多边形列表。例如,正方形和三角形面的集合:
生成此图的代码是:
请注意,
colors
列表中的颜色与中的面列表的顺序相同。f
列表。此外,如果所有多边形都属于同一类型,例如所有三角形或所有四边形,则可以方便地使用 Numpy 数组来表示顶点 v 和面 f。那么verts
可以简单地定义为:而不是使用Python列表理解。此外,如果使用着色,则必须使用与右手或左手顶点顺序一致的每个面的顶点索引列表来定义所有多边形。
问题 1. 非平面
通过以下方式定义顶点列表:
导致定义一个四侧面。对于 azim=-60 的 Matplotlib 默认轴视图,可视化“显示”正确。然而,不同观点的视觉解释是不一致的。下面通过比较三个视图来显示这一点。这不是 Matplotlib 问题,而是使用不在同一平面上的四个顶点错误定义 4 边多边形的问题。
使用两个三角形面解决方案,此 3D 集合的可视化在任何观看方向上都是一致的:
可以从四个顶点构造扭曲曲面,但这需要将曲面细分为多个多边形。使用Matplotlib第三方包S3Dlib,由512个三角形构建了以下直纹曲面。
在本例中,表面的顶部和底部分别为蓝色和红色。然后应用阴影以增强表面的可视化。不再简单而是正确。
Matplotlib 3D 可视化可以通过沿视图方向正确堆叠多边形来处理多个多边形的集合。但是,Matplotlib 无法直接处理多边形相交时的可视化。可视化将不正确并且取决于视图。
例如,考虑三个三角形,定义为:
这是三个相交的三角形,但从不同方向查看时,可视化效果不一致。
通过将三个三角形中的每一个细分为更小的三角形,三个三角形之间的交点可以独立于视图而一致地可视化:
上面的相交三角形图是使用 S3Dlib 通过 Matplotlib 创建的。
问题更新产生随机三角形多边形的集合。然而,正如上面的简单示例所示,如此大量的多边形将会相交。因此,如果不考虑交叉点,实际集合将无法正确显示。
通过仔细检查来自不同方向的相似三角形集合,可以检测到可视化异常。如下图所示。
一个可能的解决方案是将大量三角形细分为更小的多边形以考虑相交。然而,这种方法会导致大量的计算成本。问题的解决不再“简单”。
总结
通过从不同的观察方向查看 3D 集合,这两个问题都可以被识别。这样,在开发过程中,解决方法可能会被识别为有缺陷的。
附加说明:种子 Numpy 随机生成器在采样之前,例如使用:
这样做允许在代码开发和验证期间使用同一组随机数。
Unfortunately, both the answer and update code result in visualizations that are incorrect, but for different reasons. Explanations are provided below.
A Solution
First consider a 'correct' answer. Matplotlib will correctly plot filled polygons. Polygons are planar. The four example vertices in the question are not planar. But these vertices can be used to define two triangles. The
verts
parameter in the art3d.Poly3DCollection is a list of (N, 3) array-like sequence of polygons. Using a list of the four vertices, v, and a list of vertex indices for two triangular faces, f, a simple solution is:which produces a figure of two polygons with different colors as:
This even works for a list of polygons with a mixed number of sides. For example, a collection of square and triangular faces:
The code to produce this figure is:
Notice that the colors in the
colors
list are in the same order as the list of faces in thef
list. Also, if all the polygons are of the same type, for example all triangles or all quadrilaterals, it is convenient to use Numpy arrays for the vertices, v, and faces, f. Then theverts
can be simply defined as:instead of using Python list comprehension. In addition, if shading is used, all polygons must be defined with the list of vertex indices for each face consistent with a right-handed or left-handed vertex order.
Problem 1. Non-planar faces
Defining the verts list by:
results in defining one four-sided face. The visualization 'appears' correct for the Matplotlib default axes view with azim=-60. However, the visual interpretation is inconsistent for different views. This is shown below by comparing three views. This is not a Matplotlib problem but a problem of incorrectly defining a 4-sided polygon using four vertices which are not in the same plane.
Using the two triangular face solution, the visualization of this 3D collection is consistent for any viewing direction:
A warped surface can be constructed from the four vertices, but this requires subdividing the surface into multiple polygons. Using the Matplotlib third-party package S3Dlib, the following ruled surface was constructed from 512 triangles.
In this case, the top and bottom of the surface was colored blue and red, respectively. Then shading was applied to enhance the visualization of the surface. No longer simple but correct.
Matplotlib 3D visualizations can handle a polycollection of multiple polygons by correctly stacking the polygons along the view direction. However, Matplotlib cannot directly handle the visualization when polygons intersect. Visualizations will be incorrect and dependent on the view.
For example, consider three triangles defined as:
These are three intersecting triangles, but when viewed from different orientations, the visualizations are inconsistent.
By subdividing each of the three triangles into smaller triangles, the intersections among the three triangles can be consistently visualized independent of the view:
The above figure of intersecting triangles was created with Matplotlib using S3Dlib.
The question update produces a collection of random triangular polygons. However, as demonstrated in the above simple example, such a large number of polygons will be intersecting. As a result, the actual collection will not be correctly displayed without accounting for the intersections.
By closely examining a similar collection of triangles from different orientations, the visualization anomalies can be detected. This is shown in the following figure.
A possible solution could be to subdivide the numerous triangles into smaller polygons to account for the intersections. However this method would result in a significant computational cost. The problem solution is no longer 'simple'.
Summary
Both problems could have been identified by viewing the 3D collection from various viewing directions. That way, during development, the solution method may have been identified as flawed.
As additional note: seed the Numpy random generator prior to sampling, for example using :
Doing this allows using the same set of random numbers during code development and verification.