18

我正在尝试使用该shapely.geometry.Polygon模块来查找多边形的面积,但它会在xy平面上执行所有计算。这对我的一些多边形来说很好,但其他多边形也有z尺寸,所以它并没有完全符合我的要求。

是否有一个包可以从xyz坐标中给我一个平面多边形的面积,或者一个包或算法将多边形旋转到xy平面以便我可以使用shapely.geometry.Polygon().area

多边形表示为表格中的元组列表[(x1,y1,z1),(x2,y2,z3),...(xn,yn,zn)]

4

7 回答 7

23

这是计算 3D 平面多边形面积的公式的推导

这是实现它的 Python 代码:

#determinant of matrix a
def det(a):
    return a[0][0]*a[1][1]*a[2][2] + a[0][1]*a[1][2]*a[2][0] + a[0][2]*a[1][0]*a[2][1] - a[0][2]*a[1][1]*a[2][0] - a[0][1]*a[1][0]*a[2][2] - a[0][0]*a[1][2]*a[2][1]

#unit normal vector of plane defined by points a, b, and c
def unit_normal(a, b, c):
    x = det([[1,a[1],a[2]],
             [1,b[1],b[2]],
             [1,c[1],c[2]]])
    y = det([[a[0],1,a[2]],
             [b[0],1,b[2]],
             [c[0],1,c[2]]])
    z = det([[a[0],a[1],1],
             [b[0],b[1],1],
             [c[0],c[1],1]])
    magnitude = (x**2 + y**2 + z**2)**.5
    return (x/magnitude, y/magnitude, z/magnitude)

#dot product of vectors a and b
def dot(a, b):
    return a[0]*b[0] + a[1]*b[1] + a[2]*b[2]

#cross product of vectors a and b
def cross(a, b):
    x = a[1] * b[2] - a[2] * b[1]
    y = a[2] * b[0] - a[0] * b[2]
    z = a[0] * b[1] - a[1] * b[0]
    return (x, y, z)

#area of polygon poly
def area(poly):
    if len(poly) < 3: # not a plane - no area
        return 0

    total = [0, 0, 0]
    for i in range(len(poly)):
        vi1 = poly[i]
        if i is len(poly)-1:
            vi2 = poly[0]
        else:
            vi2 = poly[i+1]
        prod = cross(vi1, vi2)
        total[0] += prod[0]
        total[1] += prod[1]
        total[2] += prod[2]
    result = dot(total, unit_normal(poly[0], poly[1], poly[2]))
    return abs(result/2)

为了测试它,这是一个倾斜的 10x5 正方形:

>>> poly = [[0, 0, 0], [10, 0, 0], [10, 3, 4], [0, 3, 4]]
>>> poly_translated = [[0+5, 0+5, 0+5], [10+5, 0+5, 0+5], [10+5, 3+5, 4+5], [0+5, 3+5, 4+5]]
>>> area(poly)
50.0
>>> area(poly_translated)
50.0
>>> area([[0,0,0],[1,1,1]])
0

最初的问题是我过于简单化了。它需要计算垂直于平面的单位向量。面积是其点积和所有叉积总和的一半,而不是叉积所有量值总和的一半。

这可以稍微清理一下(如果有矩阵和向量类,或者行列式/叉积/点积的标准实现,它会更好),但它在概念上应该是合理的。

于 2012-09-28T15:53:07.680 回答
8

这是我使用的最终代码。它没有使用shapely,而是实现了Stoke's theorem直接计算面积。它建立在@Tom Smilack 的答案之上,该答案展示了如何在没有 numpy 的情况下做到这一点。

import numpy as np

#unit normal vector of plane defined by points a, b, and c
def unit_normal(a, b, c):
    x = np.linalg.det([[1,a[1],a[2]],
         [1,b[1],b[2]],
         [1,c[1],c[2]]])
    y = np.linalg.det([[a[0],1,a[2]],
         [b[0],1,b[2]],
         [c[0],1,c[2]]])
    z = np.linalg.det([[a[0],a[1],1],
         [b[0],b[1],1],
         [c[0],c[1],1]])
    magnitude = (x**2 + y**2 + z**2)**.5
    return (x/magnitude, y/magnitude, z/magnitude)

#area of polygon poly
def poly_area(poly):
    if len(poly) < 3: # not a plane - no area
        return 0
    total = [0, 0, 0]
    N = len(poly)
    for i in range(N):
        vi1 = poly[i]
        vi2 = poly[(i+1) % N]
        prod = np.cross(vi1, vi2)
        total[0] += prod[0]
        total[1] += prod[1]
        total[2] += prod[2]
    result = np.dot(total, unit_normal(poly[0], poly[1], poly[2]))
    return abs(result/2)
于 2012-09-29T15:02:32.627 回答
1

#pythonn 3D 多边形区域代码(优化版)

def polygon_area(poly):
    #shape (N, 3)
    if isinstance(poly, list):
        poly = np.array(poly)
    #all edges
    edges = poly[1:] - poly[0:1]
    # row wise cross product
    cross_product = np.cross(edges[:-1],edges[1:], axis=1)
    #area of all triangles
    area = np.linalg.norm(cross_product, axis=1)/2
    return sum(area)



if __name__ == "__main__":
    poly = [[0+5, 0+5, 0+5], [10+5, 0+5, 0+5], [10+5, 3+5, 4+5], [0+5, 3+5, 4+5]]
    print(polygon_area(poly))
于 2021-06-24T11:38:01.007 回答
0

可以使用 Numpy 作为单线计算 2D 多边形的面积...

poly_Area(vertices) = np.sum( [0.5, -0.5] * vertices * np.roll( np.roll(vertices, 1, axis=0), 1, axis=1) )
于 2013-07-28T09:55:57.857 回答
0

仅供参考,这是 Mathematica 中的相同算法,带有婴儿单元测试

ClearAll[vertexPairs, testPoly, area3D, planeUnitNormal, pairwise];
pairwise[list_, fn_] := MapThread[fn, {Drop[list, -1], Drop[list, 1]}];
vertexPairs[Polygon[{points___}]] := Append[{points}, First[{points}]];
testPoly = Polygon[{{20, -30, 0}, {40, -30, 0}, {40, -30, 20}, {20, -30, 20}}];
planeUnitNormal[Polygon[{points___}]] :=
  With[{ps = Take[{points}, 3]},
   With[{p0 = First[ps]},
    With[{qs = (# - p0) & /@ Rest[ps]},
     Normalize[Cross @@ qs]]]];
area3D[p : Polygon[{polys___}]] :=
  With[{n = planeUnitNormal[p], vs = vertexPairs[p]},
   With[{areas = (Dot[n, #]) & /@ pairwise[vs, Cross]},
    Plus @@ areas/2]];
area3D[testPoly]
于 2014-04-06T22:23:44.930 回答
0

与@Tom Smilack 的答案相同,但在 javascript 中

//determinant of matrix a
function det(a) {
  return a[0][0] * a[1][1] * a[2][2] + a[0][1] * a[1][2] * a[2][0] + a[0][2] * a[1][0] * a[2][1] - a[0][2] * a[1][1] * a[2][0] - a[0][1] * a[1][0] * a[2][2] - a[0][0] * a[1][2] * a[2][1];
}
//unit normal vector of plane defined by points a, b, and c
function unit_normal(a, b, c) {
  let x = math.det([
    [1, a[1], a[2]],
    [1, b[1], b[2]],
    [1, c[1], c[2]]
  ]);
  let y = math.det([
    [a[0], 1, a[2]],
    [b[0], 1, b[2]],
    [c[0], 1, c[2]]
  ]);
  let z = math.det([
    [a[0], a[1], 1],
    [b[0], b[1], 1],
    [c[0], c[1], 1]
  ]);
  let magnitude = Math.pow(Math.pow(x, 2) + Math.pow(y, 2) + Math.pow(z, 2), 0.5);
  return [x / magnitude, y / magnitude, z / magnitude];
}
// dot product of vectors a and b
function dot(a, b) {
  return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
}
// cross product of vectors a and b
function cross(a, b) {
  let x = (a[1] * b[2]) - (a[2] * b[1]);
  let y = (a[2] * b[0]) - (a[0] * b[2]);
  let z = (a[0] * b[1]) - (a[1] * b[0]);
  return [x, y, z];
}

// area of polygon poly
function area(poly) {
  if (poly.length < 3) {
    console.log("not a plane - no area");
    return 0;
  } else {
    let total = [0, 0, 0]
    for (let i = 0; i < poly.length; i++) {
      var vi1 = poly[i];
      if (i === poly.length - 1) {
        var vi2 = poly[0];
      } else {
        var vi2 = poly[i + 1];
      }
      let prod = cross(vi1, vi2);
      total[0] = total[0] + prod[0];
      total[1] = total[1] + prod[1];
      total[2] = total[2] + prod[2];
    }
    let result = dot(total, unit_normal(poly[0], poly[1], poly[2]));

    return Math.abs(result/2);
  }

}

于 2019-01-09T05:22:37.630 回答
0

感谢您提供详细的答案,但我有点惊讶没有简单的答案来获得该区域。

因此,我只是发布了一种使用 pyny3d 使用多边形或曲面的 3d 坐标计算面积的简化方法。

#Install pyny3d as:
pip install pyny3d

#Calculate area
import numpy as np
import pyny3d.geoms as pyny

coords_3d = np.array([[0,  0, 0],
                           [7,  0, 0],
                           [7, 10, 2],
                           [0, 10, 2]])
polygon = pyny.Polygon(coords_3d)
print(f'Area is : {polygon.get_area()}')
于 2021-10-26T20:30:35.083 回答