在2D / 3D

时间:2019-05-15 20:37:07

标签: python computational-geometry cgal

我正在尝试使用CGAL python绑定编写一个程序(在Python中),以查找1个对象(可以是线段或多边形/多面体)与另一个多边形/多面体之间的交集。当我目前在2D模式下工作时,我也希望将程序扩展到3D模式。

我已经为CGAL安装了python-bindings。我知道它仍在开发中,但似乎已经可以做很多事情。我设法使其输出线段和四面体之间的交点(使用AABB树算法),以及dD iso定向盒的相交序列。但是,AABB树算法似乎仅适用于3D对象。我想我可以将我的一般形状表示为伪3D对象,但是随后遇到了实际构造3D对象的问题。 python-bindings中给出的示例仅适用于四面体,而我无法弄清楚如何创建通用的多面体。所以我的问题变成了两个方面:

  1. 如何使用CGAL python绑定定义通用多面体?
  2. 如何使用定义的多面体来计算与其他常规形状(如线段和多边形)的相交和距离?

因此,对于四面体,该软件包可以这样做(来自https://github.com/CGAL/cgal-swig-bindings/blob/master/examples/python/AABB_polyhedron_facet_intersection_example.py):

from CGAL.CGAL_Kernel import Point_3
from CGAL.CGAL_Polyhedron_3 import Polyhedron_3

p=Point_3(1.0, 0.0, 0.0)
q=Point_3(0.0, 1.0, 0.0)
r=Point_3(0.0, 0.0, 1.0)
s=Point_3(0.0, 0.0, 0.0)
polyhedron = Polyhedron_3()
polyhedron.make_tetrahedron(p, q, r, s)

#constructs AABB tree
tree = AABB_tree_Polyhedron_3_Facet_handle(polyhedron.facets())

#constructs segment query
a = Point_3(-0.2, 0.2, -0.2)
b = Point_3(1.3, 0.2, 1.3)
segment_query=Segment_3(a,b)

这将创建一个四面体,可以支持对相交,平方距离等的查询。但是,这不适用于超过4个点。在我的情况下,多边形/多面体将由大约50个点定义。如何使用CGAL-python创建这样的对象?

1 个答案:

答案 0 :(得分:0)

如果您不关心程序的效率。 您可以尝试使用我最新的python软件包Geometry3D,

pip install Geometry3D

由于它是用纯Python编写的,因此速度有些慢,不适用于图形之类的作品。 但它在常见情况下效果很好,并且支持许多几何形状。

下面显示一个示例

>>> from Geometry3D import *
>>>
>>> a = Point(1,1,1)
>>> b = Point(-1,1,1)
>>> c = Point(-1,-1,1)
>>> d = Point(1,-1,1)
>>> e = Point(1,1,-1)
>>> f = Point(-1,1,-1)
>>> g = Point(-1,-1,-1)
>>> h = Point(1,-1,-1)
>>> cph0 = Parallelepiped(Point(-1,-1,-1),Vector(2,0,0),Vector(0,2,0),Vector(0,0,2))
>>> cpg12 = ConvexPolygon((e,c,h))
>>> cpg13 = ConvexPolygon((e,f,c))
>>> cpg14 = ConvexPolygon((c,f,g))
>>> cpg15 = ConvexPolygon((h,c,g))
>>> cpg16 = ConvexPolygon((h,g,f,e))
>>> cph1 = ConvexPolyhedron((cpg12,cpg13,cpg14,cpg15,cpg16))
>>> a1 = Point(1.5,1.5,1.5)
>>> b1 = Point(-0.5,1.5,1.5)
>>> c1 = Point(-0.5,-0.5,1.5)
>>> d1 = Point(1.5,-0.5,1.5)
>>> e1 = Point(1.5,1.5,-0.5)
>>> f1 = Point(-0.2,1.5,-0.5)
>>> g1 = Point(-0.2,-0.5,-0.5)
>>> h1 = Point(1.5,-0.5,-0.5)
>>>
>>> cpg6 = ConvexPolygon((a1,d1,h1,e1))
>>> cpg7 = ConvexPolygon((a1,e1,f1,b1))
>>> cpg8 = ConvexPolygon((c1,b1,f1,g1))
>>> cpg9 = ConvexPolygon((c1,g1,h1,d1))
>>> cpg10 = ConvexPolygon((a1,b1,c1,d1))
>>> cpg11 = ConvexPolygon((e1,h1,g1,f1))
>>> cph2 = ConvexPolyhedron((cpg6,cpg7,cpg8,cpg9,cpg10,cpg11))
>>> cph3 = intersection(cph0,cph2)
>>>
>>> cph4 = intersection(cph1,cph2)
>>> r = Renderer()
>>> r.add((cph0,'r',1),normal_length = 0)
>>> r.add((cph1,'r',1),normal_length = 0)
>>> r.add((cph2,'g',1),normal_length = 0)
>>> r.add((cph3,'b',3),normal_length = 0.5)
>>> r.add((cph4,'y',3),normal_length = 0.5)
>>> r.show()

Result

您也可以查看文档Geomrtry3D