Python中三维多边形的交点

时间:2010-03-31 00:49:19

标签: python geometry intersection shapefile

是否有任何开源工具或库(理想情况下在python中)可用于执行从ESRI shapefile读取的3D几何体的大量交叉点?大多数测试将是简单的线段与多边形。

我已经研究过OGR 1.7.1 / GEOS 3.2.0,虽然它正确地加载了数据,但是得到的交叉点并不正确,并且大多数其他可用的工具似乎都是基于这项工作。

虽然CGAL本来是一种替代方案,但它的许可证并不合适。 Boost通用几何库看起来很棒,但api非常庞大,并且似乎不支持开箱即用的wkt或wkb读卡器。

2 个答案:

答案 0 :(得分:4)

回答有点晚,但是我的python光线跟踪程序pvtrace正是这样做的。它会像这样工作:

1)使用一个点列表定义多边形并制作一个多边形对象

points = [[0,0,0],[0,0.1,0],[0.1,0.1,-0.03],[0.1,0,-0.03]]
polygon = Polygon(points)

2)获取Ray对象的交叉点

ray = Ray(position=(0,0,0), direction=(0,0,1))
print polygon.intersection(ray)

答案 1 :(得分:3)

您可以尝试使用最新的lib Geometry3D,

pip install Geometry3D

下面显示一个示例

>>> 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()

Example Image

您也可以查看文档Geomrtry3D