matplotlib.Path.contains_points : "radius" 参数定义不一致
matplotlib.Path.contains_points : "radius" parameter defined inconsistently
问题:
matplotlib.path函数contains_point中的radius参数定义不一致。此函数检查指定点是在封闭路径的内部还是外部。 radius参数用来使路径有点smaller/larger(取决于radius的符号)。这样,可以考虑 into/out 靠近路径的点。问题是,半径的符号取决于路径的方向(顺时针或逆时针)。
inconsistency(在我看来)就在那里,因为在检查一个点是在路径内部还是外部时,路径的方向被忽略了。在严格的数学意义上说:沿路径留下的所有东西都包括在内。
简而言之:
如果路径是逆时针方向,正半径会考虑更多的点。
如果路径顺时针方向,正半径会考虑较少的点。
示例:
在下面的示例中,检查了 3 种情况 - 分别用于顺时针和逆时针路径:
- 是一个包含正半径的点(靠近路径)
- 是一个包含负半径的点(靠近路径)
- 是否包含原点(在两条路径的中间)
代码:
import matplotlib.path as path
import numpy as np
verts=np.array([[-11.5, 16. ],[-11.5, -16. ],[ 11.5, -16. ],[ 11.5, 16. ],[-11.5, 16. ]])
ccwPath=path.Path(verts, closed=True)
cwPath=path.Path(verts[::-1,:], closed=True)
testPoint=[12,0]
print('contains: ','|\t', '[12,0], radius=3','|\t', '[12,0], radius=-3','|\t', '[0,0]|')
print('counterclockwise: ','|\t'
,'{0:>16s}'.format(str(ccwPath.contains_point(testPoint,radius=3) )),'|\t'
,'{0:>17s}'.format(str(ccwPath.contains_point(testPoint,radius=-3) )),'|\t'
,ccwPath.contains_point([0,0],radius=0) ,'|\t'
,'=> radius increases tolerance \t'
)
print('clockwise: ','|\t'
,'{0:>16s}'.format(str(cwPath.contains_point(testPoint,radius=3) )),'|\t'
,'{0:>17s}'.format(str(cwPath.contains_point(testPoint,radius=-3) )),'|\t'
,cwPath.contains_point([0,0],radius=0) ,'|\t'
,'=> radius decreases tolerance \t'
)
输出:
contains: | [12,0], radius=3 | [12,0], radius=-3 | [0,0]|
counterclockwise: | True | False | True | => radius increases tolerance
clockwise: | False | True | True | => radius decreases tolerance
凸路径的解法:
我想到的唯一想法是强制路径逆时针方向并根据此使用半径。
import matplotlib.path as path
import numpy as np
verts=np.array([[-11.5, 16. ],[-11.5, -16. ],[ 11.5, -16. ],[ 11.5, 16. ],[-11.5, 16. ]])
#comment following line out to make isCounterClockWise crash
#verts=np.array([[-11.5, 16. ],[-10,0],[-11.5, -16. ],[ 11.5, -16. ],[ 11.5, 16. ],[-11.5, 16. ]])
ccwPath=path.Path(verts, closed=True)
cwPath=path.Path(verts[::-1,:], closed=True)
testPoint=[12,0]
def isCounterClockWise(myPath):
#directions from on vertex to the other
dirs=myPath.vertices[1:]-myPath.vertices[0:-1]
#rot: array of rotations at ech edge
rot=np.cross(dirs[:-1],dirs[1:])
if len(rot[rot>0])==len(rot):
#counterclockwise
return True
elif len(rot[rot<0])==len(rot):
#clockwise
return False
else:
assert False, 'no yet implemented: This case applies if myPath is concave'
def forceCounterClockWise(myPath):
if not isCounterClockWise(myPath):
myPath.vertices=myPath.vertices[::-1]
forceCounterClockWise(cwPath)
print('contains: ','|\t', '[12,0], radius=3','|\t', '[12,0], radius=-3','|\t', '[0,0]|')
print('counterclockwise: ','|\t'
,'{0:>16s}'.format(str(ccwPath.contains_point(testPoint,radius=3) )),'|\t'
,'{0:>17s}'.format(str(ccwPath.contains_point(testPoint,radius=-3) )),'|\t'
,ccwPath.contains_point([0,0],radius=0) ,'|\t'
,'=> radius increases tolerance \t'
)
print('forced ccw: ','|\t'
,'{0:>16s}'.format(str(cwPath.contains_point(testPoint,radius=3) )),'|\t'
,'{0:>17s}'.format(str(cwPath.contains_point(testPoint,radius=-3) )),'|\t'
,cwPath.contains_point([0,0],radius=0) ,'|\t'
,'=> radius increases tolerance \t'
)
给出以下输出:
contains: | [12,0], radius=3 | [12,0], radius=-3 | [0,0]|
counterclockwise: | True | False | True | => radius increases tolerance
forced ccw: | True | False | True | => radius increases tolerance
代码注释中给出了此解决方案失败的示例(对于凹路径)。
我的问题:
- 有谁知道,为什么会出现这种不一致?
- 有没有更优雅的方法来规避这个问题?示例可能是:为 contains_point 使用其他库,以 smarter/proper 方式使用半径参数或使用预定义函数查找路径的方向。
我认为这里唯一错误的假设是"everything which is left along the path is included."。相反,contains_point
的字面意思是闭合路径是否包含一个点。
然后将radius
定义为
- 当路径逆时针旋转到
时展开路径
- 顺时针方向缩小路径
这在以下示例中显示,其中对于(逆时针)路径,绘制了 expanded/shunk 区域中包含的点。 (红色 = not contains_point
,蓝色 = contains_point
)
import matplotlib.pyplot as plt
import matplotlib.path as path
import matplotlib.patches as patches
import numpy as np
verts=np.array([[-1, 1 ],[-1, -1 ],[ 1, -1 ],[ 1, 0 ],[ 1, 1],[-1, 1 ]])
ccwPath=path.Path(verts, closed=True)
cwPath=path.Path(verts[::-1,:], closed=True)
paths = [ccwPath, cwPath]
pathstitle = ["ccwPath", "cwPath"]
radii = [1,-1]
testPoint=(np.random.rand(400,2)-.5)*4
c = lambda p,x,r: p.contains_point(x,radius=r)
fig, axes = plt.subplots(nrows=len(paths),ncols=len(radii))
for j in range(len(paths)):
for i in range(len(radii)):
ax = axes[i,j]
r = radii[i]
patch = patches.PathPatch(paths[j], fill=False, lw=2)
ax.add_patch(patch)
col = [c(paths[j], point[0], r) for point in zip(testPoint)]
ax.scatter(testPoint[:,0], testPoint[:,1], c=col, s=8, vmin=0,vmax=1, cmap="bwr_r")
ax.set_title("{}, r={}".format(pathstitle[j],radii[i]) )
plt.tight_layout()
plt.show()
一个似乎根本没有记录的特殊性是 radius
实际上将路径扩展或缩小了 radius/2.
。如上所示,半径为 1
,包括 -1.5
和 1.5
之间的点,而不是 -2
和 2
之间的点。
关于路径的方向,可能没有一个固定的方向。如果你有 3 个点,方向可以明确地确定为顺时针、逆时针(或共线)。一旦你有了更多的点,方向的概念就不好定义了。
一个选项可能是检查路径是否为 "mostly counterclockwise"。
def is_ccw(p):
v = p.vertices-p.vertices[0,:]
a = np.arctan2(v[1:,1],v[1:,0])
return (a[1:] >= a[:-1]).astype(int).mean() >= 0.5
这将允许在 "mostly clockwise" 路径的情况下调整 radius
,
r = r*is_ccw(p) - r*(1-is_ccw(p))
正半径总是扩大路径,负半径总是缩小路径。
完整示例:
import matplotlib.pyplot as plt
import matplotlib.path as path
import matplotlib.patches as patches
import numpy as np
verts=np.array([[-1, 1 ],[-1, -1 ],[ 1, -1 ],[ 1, 0 ],[ 1, 1],[-1, 1 ]])
ccwPath=path.Path(verts, closed=True)
cwPath=path.Path(verts[::-1,:], closed=True)
paths = [ccwPath, cwPath]
pathstitle = ["ccwPath", "cwPath"]
radii = [1,-1]
testPoint=(np.random.rand(400,2)-.5)*4
c = lambda p,x,r: p.contains_point(x,radius=r)
def is_ccw(p):
v = p.vertices-p.vertices[0,:]
a = np.arctan2(v[1:,1],v[1:,0])
return (a[1:] >= a[:-1]).astype(int).mean() >= 0.5
fig, axes = plt.subplots(nrows=len(radii),ncols=len(paths))
for j in range(len(paths)):
for i in range(len(radii)):
ax = axes[i,j]
r = radii[i]
isccw = is_ccw(paths[j])
r = r*isccw - r*(1-isccw)
patch = patches.PathPatch(paths[j], fill=False, lw=2)
ax.add_patch(patch)
col = [c(paths[j], point[0], r) for point in zip(testPoint)]
ax.scatter(testPoint[:,0], testPoint[:,1], c=col, s=8, vmin=0,vmax=1, cmap="bwr_r")
ax.set_title("{}, r={} (isccw={})".format(pathstitle[j],radii[i], isccw) )
plt.tight_layout()
plt.show()
问题:
matplotlib.path函数contains_point中的radius参数定义不一致。此函数检查指定点是在封闭路径的内部还是外部。 radius参数用来使路径有点smaller/larger(取决于radius的符号)。这样,可以考虑 into/out 靠近路径的点。问题是,半径的符号取决于路径的方向(顺时针或逆时针)。 inconsistency(在我看来)就在那里,因为在检查一个点是在路径内部还是外部时,路径的方向被忽略了。在严格的数学意义上说:沿路径留下的所有东西都包括在内。
简而言之:
如果路径是逆时针方向,正半径会考虑更多的点。 如果路径顺时针方向,正半径会考虑较少的点。
示例:
在下面的示例中,检查了 3 种情况 - 分别用于顺时针和逆时针路径:
- 是一个包含正半径的点(靠近路径)
- 是一个包含负半径的点(靠近路径)
- 是否包含原点(在两条路径的中间)
代码:
import matplotlib.path as path
import numpy as np
verts=np.array([[-11.5, 16. ],[-11.5, -16. ],[ 11.5, -16. ],[ 11.5, 16. ],[-11.5, 16. ]])
ccwPath=path.Path(verts, closed=True)
cwPath=path.Path(verts[::-1,:], closed=True)
testPoint=[12,0]
print('contains: ','|\t', '[12,0], radius=3','|\t', '[12,0], radius=-3','|\t', '[0,0]|')
print('counterclockwise: ','|\t'
,'{0:>16s}'.format(str(ccwPath.contains_point(testPoint,radius=3) )),'|\t'
,'{0:>17s}'.format(str(ccwPath.contains_point(testPoint,radius=-3) )),'|\t'
,ccwPath.contains_point([0,0],radius=0) ,'|\t'
,'=> radius increases tolerance \t'
)
print('clockwise: ','|\t'
,'{0:>16s}'.format(str(cwPath.contains_point(testPoint,radius=3) )),'|\t'
,'{0:>17s}'.format(str(cwPath.contains_point(testPoint,radius=-3) )),'|\t'
,cwPath.contains_point([0,0],radius=0) ,'|\t'
,'=> radius decreases tolerance \t'
)
输出:
contains: | [12,0], radius=3 | [12,0], radius=-3 | [0,0]|
counterclockwise: | True | False | True | => radius increases tolerance
clockwise: | False | True | True | => radius decreases tolerance
凸路径的解法:
我想到的唯一想法是强制路径逆时针方向并根据此使用半径。
import matplotlib.path as path
import numpy as np
verts=np.array([[-11.5, 16. ],[-11.5, -16. ],[ 11.5, -16. ],[ 11.5, 16. ],[-11.5, 16. ]])
#comment following line out to make isCounterClockWise crash
#verts=np.array([[-11.5, 16. ],[-10,0],[-11.5, -16. ],[ 11.5, -16. ],[ 11.5, 16. ],[-11.5, 16. ]])
ccwPath=path.Path(verts, closed=True)
cwPath=path.Path(verts[::-1,:], closed=True)
testPoint=[12,0]
def isCounterClockWise(myPath):
#directions from on vertex to the other
dirs=myPath.vertices[1:]-myPath.vertices[0:-1]
#rot: array of rotations at ech edge
rot=np.cross(dirs[:-1],dirs[1:])
if len(rot[rot>0])==len(rot):
#counterclockwise
return True
elif len(rot[rot<0])==len(rot):
#clockwise
return False
else:
assert False, 'no yet implemented: This case applies if myPath is concave'
def forceCounterClockWise(myPath):
if not isCounterClockWise(myPath):
myPath.vertices=myPath.vertices[::-1]
forceCounterClockWise(cwPath)
print('contains: ','|\t', '[12,0], radius=3','|\t', '[12,0], radius=-3','|\t', '[0,0]|')
print('counterclockwise: ','|\t'
,'{0:>16s}'.format(str(ccwPath.contains_point(testPoint,radius=3) )),'|\t'
,'{0:>17s}'.format(str(ccwPath.contains_point(testPoint,radius=-3) )),'|\t'
,ccwPath.contains_point([0,0],radius=0) ,'|\t'
,'=> radius increases tolerance \t'
)
print('forced ccw: ','|\t'
,'{0:>16s}'.format(str(cwPath.contains_point(testPoint,radius=3) )),'|\t'
,'{0:>17s}'.format(str(cwPath.contains_point(testPoint,radius=-3) )),'|\t'
,cwPath.contains_point([0,0],radius=0) ,'|\t'
,'=> radius increases tolerance \t'
)
给出以下输出:
contains: | [12,0], radius=3 | [12,0], radius=-3 | [0,0]|
counterclockwise: | True | False | True | => radius increases tolerance
forced ccw: | True | False | True | => radius increases tolerance
代码注释中给出了此解决方案失败的示例(对于凹路径)。
我的问题:
- 有谁知道,为什么会出现这种不一致?
- 有没有更优雅的方法来规避这个问题?示例可能是:为 contains_point 使用其他库,以 smarter/proper 方式使用半径参数或使用预定义函数查找路径的方向。
我认为这里唯一错误的假设是"everything which is left along the path is included."。相反,contains_point
的字面意思是闭合路径是否包含一个点。
然后将radius
定义为
- 当路径逆时针旋转到 时展开路径
- 顺时针方向缩小路径
这在以下示例中显示,其中对于(逆时针)路径,绘制了 expanded/shunk 区域中包含的点。 (红色 = not contains_point
,蓝色 = contains_point
)
import matplotlib.pyplot as plt
import matplotlib.path as path
import matplotlib.patches as patches
import numpy as np
verts=np.array([[-1, 1 ],[-1, -1 ],[ 1, -1 ],[ 1, 0 ],[ 1, 1],[-1, 1 ]])
ccwPath=path.Path(verts, closed=True)
cwPath=path.Path(verts[::-1,:], closed=True)
paths = [ccwPath, cwPath]
pathstitle = ["ccwPath", "cwPath"]
radii = [1,-1]
testPoint=(np.random.rand(400,2)-.5)*4
c = lambda p,x,r: p.contains_point(x,radius=r)
fig, axes = plt.subplots(nrows=len(paths),ncols=len(radii))
for j in range(len(paths)):
for i in range(len(radii)):
ax = axes[i,j]
r = radii[i]
patch = patches.PathPatch(paths[j], fill=False, lw=2)
ax.add_patch(patch)
col = [c(paths[j], point[0], r) for point in zip(testPoint)]
ax.scatter(testPoint[:,0], testPoint[:,1], c=col, s=8, vmin=0,vmax=1, cmap="bwr_r")
ax.set_title("{}, r={}".format(pathstitle[j],radii[i]) )
plt.tight_layout()
plt.show()
一个似乎根本没有记录的特殊性是 radius
实际上将路径扩展或缩小了 radius/2.
。如上所示,半径为 1
,包括 -1.5
和 1.5
之间的点,而不是 -2
和 2
之间的点。
关于路径的方向,可能没有一个固定的方向。如果你有 3 个点,方向可以明确地确定为顺时针、逆时针(或共线)。一旦你有了更多的点,方向的概念就不好定义了。
一个选项可能是检查路径是否为 "mostly counterclockwise"。
def is_ccw(p):
v = p.vertices-p.vertices[0,:]
a = np.arctan2(v[1:,1],v[1:,0])
return (a[1:] >= a[:-1]).astype(int).mean() >= 0.5
这将允许在 "mostly clockwise" 路径的情况下调整 radius
,
r = r*is_ccw(p) - r*(1-is_ccw(p))
正半径总是扩大路径,负半径总是缩小路径。
完整示例:
import matplotlib.pyplot as plt
import matplotlib.path as path
import matplotlib.patches as patches
import numpy as np
verts=np.array([[-1, 1 ],[-1, -1 ],[ 1, -1 ],[ 1, 0 ],[ 1, 1],[-1, 1 ]])
ccwPath=path.Path(verts, closed=True)
cwPath=path.Path(verts[::-1,:], closed=True)
paths = [ccwPath, cwPath]
pathstitle = ["ccwPath", "cwPath"]
radii = [1,-1]
testPoint=(np.random.rand(400,2)-.5)*4
c = lambda p,x,r: p.contains_point(x,radius=r)
def is_ccw(p):
v = p.vertices-p.vertices[0,:]
a = np.arctan2(v[1:,1],v[1:,0])
return (a[1:] >= a[:-1]).astype(int).mean() >= 0.5
fig, axes = plt.subplots(nrows=len(radii),ncols=len(paths))
for j in range(len(paths)):
for i in range(len(radii)):
ax = axes[i,j]
r = radii[i]
isccw = is_ccw(paths[j])
r = r*isccw - r*(1-isccw)
patch = patches.PathPatch(paths[j], fill=False, lw=2)
ax.add_patch(patch)
col = [c(paths[j], point[0], r) for point in zip(testPoint)]
ax.scatter(testPoint[:,0], testPoint[:,1], c=col, s=8, vmin=0,vmax=1, cmap="bwr_r")
ax.set_title("{}, r={} (isccw={})".format(pathstitle[j],radii[i], isccw) )
plt.tight_layout()
plt.show()