Problem:
函数中的radius参数包含_pointmatplotlib.path定义不一致。此函数检查指定点是否位于闭合路径的内部或外部。半径参数用于使路径变小/变大(取决于半径的符号)。这样,可以将靠近路径的点纳入考虑/不考虑。问题是,半径的符号取决于路径的方向(顺时针或逆时针)。
这不一致(在我看来)是存在的,因为在检查点是在路径内部还是外部时,路径的方向被忽略。从严格的数学意义上来说:路径上剩下的所有东西都包括在内。
简而言之:
如果路径逆时针方向,则正半径会考虑更多点。
如果路径是顺时针方向,则正半径会考虑较少的点。
Example:
在以下示例中,检查了 3 种情况 - 每种情况都针对顺时针和逆时针路径:
- 是一个包含正半径的点(靠近路径)
- 是一个包含负半径的点(靠近路径)
- 是否包含原点(位于两条路径的中间)
Code:
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'
)
Output:
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 的其他库、以更智能/正确的方式使用半径参数或使用预定义函数查找路径的方向。