マップで基本的なレイトレーシングを実行して、光線が壁に当たっているかどうかを判断するためのコードがいくつかあります。
[編集]:y_coordsとx_coordsのサイズは通常18x1000(対応するポイント)です。self.mapは800x800です
def ray_trace(self, x, y, x_coords, y_coords):
ray_distances = []
resolution = self.parameters['resolution']
for i in range(x_coords.shape[0]):
distance = 0
# filter x and y coords to stay within map regions
ray_range = np.bitwise_and(x_coords[i]<799,y_coords[i]<799)
# determine ending index where the ray stops
len_ray = ray_range[ray_range==True].shape[0]
# zip up the x and y coords
ray_coords = np.c_[x_coords[i,0:len_ray], y_coords[i,0:len_ray]]
# look up all the coordinates in the map and find where the map is
# less than or equal to zero (this is a wall)
ray_values, = np.where(self.map[tuple(ray_coords.T)] <= 0)
# some special exceptions
if not ray_values.shape[0]:
if not len(ray_coords):
end_of_ray = np.array([x/resolution, y/resolution])
else:
end_of_ray = ray_coords[len(ray_values)]
else:
# get the end of the ray
end_of_ray = ray_coords[ray_values.item(0)]
# find the distance from the originating point
distance = math.sqrt((end_of_ray.item(0) - x/resolution)**2 +
(end_of_ray.item(1) - y/resolution)**2)
ray_distances.append(distance)
return ray_distances
np.c_とnp.whereの行に問題があります-これらの行とそれらの行をkernprof.pyでプロファイリングしましたが、非常に長い時間がかかります(特に、np.c_は時間の50%を占めます) 。誰かがこれを最適化する方法について何かアイデアがありますか?