1

マップで基本的なレイトレーシングを実行して、光線が壁に当たっているかどうかを判断するためのコードがいくつかあります。

[編集]: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%を占めます) 。誰かがこれを最適化する方法について何かアイデアがありますか?

4

1 に答える 1

2

あなたは本当にインデックスで遊ぶ必要はあまりありません。高度なインデックス作成とは、最初に座標に結合する必要なしに、2つの同じサイズの座標配列でインデックスを作成できることを意味します。

coord_mask = (x_coords < 799) & (y_coords < 799)
for i in xrange(len(coord_mask)):
    distance = 0
    row_mask = coord_mask[i]
    row_x = x_coords[i, row_mask]
    row_y = y_coords[i, row_mask]
    mapvals = self.map[row_x, row_y] # advanced indexing
    ray_values, = (mapvals <= 0).nonzero()
    ...
于 2012-10-02T15:56:13.610 に答える