numba无法编译光线投射函数

2024-09-30 01:23:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我依靠numba JIT编译的函数来执行光线投射操作。该函数在99%的情况下都能可靠地执行,但我发现了一个失败的情况。你知道吗

可再现的示例为:

from numba import vectorize, bool_, float64
import numpy as np

# Define points to clip
x_vec = [468559.33, 468559.99]
y_vec = [5016610.22, 5016609.12]

bad_poly = np.array([[468560.495, 5016609.98611111],
                [468560.495, 5016607.625],
                [468558.245, 5016609.875],
                [468557.97700443, 5016610.41099114],
                [468558.13454149, 5016610.51065745],
                [468560.495, 5016609.98611111]])

good_poly = np.array([[468560.495, 5016609.98611111],
                     [468560.495, 5016607.625],
                     [468558.245, 5016609.875],
                     [468558.13454149, 5016610.51065745],
                     [468560.495, 5016609.98611111]])

poly = bad_poly

@vectorize([bool_(float64, float64)])
def ray(x, y):
    n = len(poly)
    inside = False
    p2x = 0.0
    p2y = 0.0
    xints = 0.0
    p1x, p1y = poly[0]
    for i in range(n + 1):
        p2x, p2y = poly[i % n]
        if y > min(p1y, p2y):
            if y <= max(p1y, p2y):
                if x <= max(p1x, p2x):
                    if p1y != p2y:
                        xints = (y - p1y) * (p2x - p1x) / (p2y - p1y) + p1x
                    if p1x == p2x or x <= xints:
                        inside = not inside
        p1x, p1y = p2x, p2y
    return inside

如果poly=bad_poly,则函数不编译,并产生以下错误

python: /tmp/build/80754af9/llvmdev_1546540969021/work/include/llvm/ADT/APInt.h:1124: bool llvm::APInt::operator==(const llvm::APInt&) const: Assertion `BitWidth == RHS.BitWidth && "Comparison requires equal bit widths"' failed.

如果poly=good_poly,那么函数将编译。似乎删除一个或一些坐标可以让函数编译。你知道吗


Tags: 函数ifnpllvmboolbadinsidepoly

热门问题