[英]Area of polygon calculation inconsistent
我正在使用我在StackOverflow上的其他地方找到的 voronoi_finite_polygons_2d(vor, radius=None)
函數。
我想修改它以顯示每個voronoi單元的質心 。 調試為什么有些質心顯得非常錯誤(參見綠色恐怖指出雜草中的質心方式)。 我發現的第一個錯誤:一些計算沒有以正確的順時針或全逆時針順序處理頂點。
不確定為什么有些點不能正確排序,但在我調查之前,我發現了另一個異常現象。
如果我順時針或逆時針走,我應該得到相同的區域(符號相反)。 在簡單的例子中,我這樣做。 但是在我制作的隨機多邊形中,我得到/稍微/不同的結果。
import numpy as np
import matplotlib.pyplot as plt
from scipy.spatial import Voronoi
import random
import math
def measure_polygon(vertices):
xs = vertices[:,0]
ys = vertices[:,1]
xs = np.append(xs,xs[0])
ys = np.append(ys,ys[0])
#https://en.wikipedia.org/wiki/Centroid#Centroid_of_polygon
area = sum(xs[i]*(ys[i+1]-ys[i-1]) for i in range(0, len(xs)-1))/2.0
centroid_x = sum((xs[i]+xs[i+1])*(xs[i]*ys[i+1] - xs[i+1]*ys[i]) for i in range(0, len(xs)-1))/(6.0*area)
centroid_y = sum((ys[i]+ys[i+1])*(xs[i]*ys[i+1] - xs[i+1]*ys[i]) for i in range(0, len(xs)-1))/(6.0*area)
return (area, (centroid_x, centroid_y))
第一個示例按預期工作 - 無論處理順序(cw或ccw)如何,相同的區域和質心。
d = [[0.0 , 0.0], [1.0,3.0],[ 5.0,3.0],[ 4.0 , 0.0] ]
print len(d)
defects = []
defects.append([d[0], d[1], d[2], d[3]])
defects.append([d[3], d[2], d[1], d[0]])
for v in defects:
print measure_polygon(np.array(v))
簡單的平行四邊形輸出
4
(-12.0, (2.5, 1.5))
(12.0, (2.5, 1.5))
但現在看看這個4邊多邊形(幾乎是一個三角形)
#original list of vertices
d = [[-148.35290745 , -1.95467472], [-124.93580616 , -2.09420039],[ -0.58281373, 1.32530292],[ 8.77020932 , 22.79390931] ]
print len(d)
defects = []
#cw
defects.append([d[0], d[2], d[3], d[1]])
#ccw
defects.append([d[1], d[3], d[2], d[0]])
for v in defects:
print measure_polygon(np.array(v))
給我奇怪的輸出:
4
(1280.4882517358433, (-36.609159411740798, 7.5961622623413145))
(-1278.8546083623708, (-36.655924939495335, 7.6058658049196115))
區域不同。 如果區域不同,那么質心將會不同。 面積的差異(1280對1278)是如此之大,我懷疑它是一個浮點圓角的東西。 但除此之外,我已經沒有假設為什么這不起作用。
===============================
我發現錯誤....我的列表 - 理解/索引黑客啟用y-1和y + 1符號被打破(以一種險惡的方式,半工作)。 正確的例程如下:
def measure_polygon(vertices):
xs = vertices[:,0]
ys = vertices[:,1]
#the first and last elements are for +1 -1 to work at end of range
xs = vertices[-1:,0]
xs = np.append(xs,vertices[:,0])
xs = np.append(xs,vertices[:1,0])
ys = vertices[-1:,1]
ys = np.append(ys,vertices[:,1])
ys = np.append(ys,vertices[:1,1])
#for i in range(1, len(xs)-1):
# print ("digesting x, y+1, y-1 points: {0}/{1}/{2}".format(xs[i], ys[i+1], ys[i-1]))
#https://en.wikipedia.org/wiki/Centroid#Centroid_of_polygon
area = sum(xs[i]*(ys[i+1]-ys[i-1]) for i in range(1, len(xs)-1))/2.0
centroid_x = sum((xs[i]+xs[i+1])*(xs[i]*ys[i+1] - xs[i+1]*ys[i]) for i in range(1, len(xs)-1))/(6.0*area)
centroid_y = sum((ys[i]+ys[i+1])*(xs[i]*ys[i+1] - xs[i+1]*ys[i]) for i in range(1, len(xs)-1))/(6.0*area)
return (area, (centroid_x, centroid_y))
所以現在NaN的例子是正確的:
#NaN Example
d = [[3.0 , 4], [5.0,11],[ 12.0,8],[ 9.0 , 5],[5,6] ]
print "number of vertices: {0}".format(len(d))
defects = []
defects.append([d[0], d[1], d[2], d[3], d[4] ])
defects.append([ d[4], d[3], d[2], d[1], d[0]])
for v in defects:
print measure_polygon(np.array(v))
結果:
number of vertices: 5
(-30.0, (7.166666666666667, 7.6111111111111107))
(30.0, (7.166666666666667, 7.6111111111111107))
多邊形必須自動關閉,因此第一點和最后一點是相等的。 這是非常標准的。 您可以使用Shoelace公式( https://en.m.wikipedia.org/wiki/Shoelace_formula )和常規坐標,但是如果我得到一個數據集缺少復制的最后一個點,我只需添加它..這使得計算更輕松。 因此,考慮一個沒有由以下坐標定義的孔的多邊形(來自參考)。 請注意,第一個和最后一個點是相同的...如果它們不是,您將獲得多部分多邊形的對齊錯誤(例如,帶有孔的多邊形)
x = np.array([3,5,12,9,5,3]) # wikipedia
y= np.array([4,11,8,5,6,4])
a = np.array(list(zip(x,y)))
area1 = 0.5*np.abs(np.dot(x, np.roll(y, 1))-np.dot(y, np.roll(x, 1)))
area2 =0.5*np.abs(np.dot(x[1:], y[:-1])-np.dot(y[1:], x[:-1]))
print("\nroll area {}\nslice area{}".format(area1, area2))
生產
roll area 30.0
slice area30.0
現在,您的多邊形被處理相同,通過添加第一個點作為最后一個點來為多邊形提供閉包
x = np.array([-148.35290745, -124.93580616, -0.58281373, 8.77029032, -148.35290745])
y = np.array([-1.95467472, -2.09420039, 1.32530292, 22.79390931, -1.95467472])
roll area 1619.5826480482792
slice area 1619.5826480482792
區域結果與您的不同,但我使用einsum的第三種方法確認了它。 腳本的一部分在下面
def ein_area(a, b=None):
"""Area calculation, using einsum.
:Requires:
:--------
: a - either a 2D+ array of coordinates or an array of x values
: b - if a < 2D, then the y values need to be supplied
: Outer rings are ordered clockwise, inner holes are counter-clockwise
:Notes:
: x => array([ 0.000, 0.000, 10.000, 10.000, 0.000]) .... OR ....
: t = x.reshape((1,) + x.shape)
: array([[ 0.000, 0.000, 10.000, 10.000, 0.000]]) .... OR ....
: u = np.atleast_2d(x)
: array([[ 0.000, 0.000, 10.000, 10.000, 0.000]]) .... OR ....
: v = x[None, :]
: array([[ 0.000, 0.000, 10.000, 10.000, 0.000]])
"""
a = np.array(a)
if b is None:
xs = a[..., 0]
ys = a[..., 1]
else:
xs, ys = a, b
x0 = np.atleast_2d(xs[..., 1:])
y0 = np.atleast_2d(ys[..., :-1])
x1 = np.atleast_2d(xs[..., :-1])
y1 = np.atleast_2d(ys[..., 1:])
e0 = np.einsum('...ij,...ij->...i', x0, y0)
e1 = np.einsum('...ij,...ij->...i', x1, y1)
area = abs(np.sum((e0 - e1)*0.5))
return area
但是你可以看到這主要是基於切片/滾動方法。 我會檢查你是否可以通過包括多邊形列表中通常缺少的最后一個點來確認結果。
原因是它錯過了最后一點。 它可以與第一個幾乎相同,多邊形必須是電路。
數據庫通常會省略它,因為它與第一個數據庫一樣是標准的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.