我正在制作一个涉及阴影投射的游戏。 为了计算线上的点我做了这个功能。
def raySegmentIntersept(ray_start, ray_dir, segment_start, segment_end):
segment_dir = segment_end - segment_start
"""
find point where parametric equasions intersept
A + t*r = B + u*s
find the coeficient t for ray
(A + t*r) x s = (B + t*s) x s
find the coeficient u for segment
(A + t*r) x r = (B + t*r) x r
where x is cross function and cross is v1[0] * v2[1] - v1[1] * v2[0]
"""
denominator = np.cross(ray_dir, segment_dir)
if denominator == 0:
return np.NaN
t = (np.cross(segment_start - ray_start, segment_dir) / denominator)
u = (np.cross(ray_start - segment_start, ray_dir) / -denominator)
"""
since u is a segment it must be 0 <= u <= 1
since t is a ray it must be 0 <= t
"""
if 0 <= u <= 1 and 0 <= t:
return ray_start + ray_dir * t
return np.NaN
所有四个参数(ray_start,ray_dir,segment_start,segment_end)都是numpy数组,(将它们视为向量,即shape =(2,)。
我想,因为这个函数使用了很多而且它只使用数学,是否可以加快它的速度?
输入看起来(打印时)像这样,每行都是不同的输入
[ 120. 120.] [-0.70710678 -0.70710678] [160 240] [240 240]
[ 120. 120.] [-0.70710678 -0.70710678] [240 320] [160 320]
[ 120. 120.] [-0.70710678 -0.70710678] [320 240] [320 320]
[ 120. 120.] [-0.70710678 -0.70710678] [320 320] [240 320]
[ 120. 120.] [-0.70710678 -0.70710678] [160 320] [160 400]
[ 120. 120.] [-0.70710678 -0.70710678] [ 80 320] [ 80 400]
我想开始研究numba,以及如何使用GPU进行计算。是否有可能使用任何numba装饰器?我需要以某种方式重构代码吗?
由于
修改
代码包括:
def raySegmentIntersept(ray_start, ray_dir, segment_start, segment_end):
...
class Vision:
def see(self, obstacles):
# prepare for calculation
# call self.getInterceptsWithWalls(endpointAngles, visibleLines)
def getInterceptsWithWalls(sellf endpointAngles, visibleLines):
# for angle in enpointAngles do
# for tile in visibleLines
# for line in tile
# intercept = raySegmentIntersept(
# ray_start,
# ray_dir,
# segment_start,
# segment_end)