Skip to content

Commit

Permalink
✨ Add 4-pass SDF approximation, compute absolute gradient derivative …
Browse files Browse the repository at this point in the history
…for each slice
  • Loading branch information
SquarerFive committed Feb 27, 2021
1 parent 9796db6 commit f6d3c52
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 18 deletions.
10 changes: 7 additions & 3 deletions AIHelper/navigation/utilities/jumpflooding.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,9 @@ def initial(texture, jfd_texture, seed_value = 0.01):
jfd_texture[x][y][0] = x
jfd_texture[x][y][1] = y

@njit(parallel=True)
@njit(parallel=True, debug=False)
def jfd(texture, jfd_texture, df_texture, seed_value, step_length):
# print(texture.shape, jfd_texture.shape, df_texture.shape)
for x in prange(0, texture.shape[0]):
if x % step_length != 0:
continue
Expand Down Expand Up @@ -137,7 +138,7 @@ def jfd(texture, jfd_texture, df_texture, seed_value, step_length):
df_texture[x][y] = distance(center_pos, nearest_pos) * (-1 if \
current_sample >= seed_value else 1)

@njit()
# @njit()
def run_jfd(texture, jfd_texture, df_texture, seed_value, step_length):
step_size= step_length
while step_size > 0:
Expand All @@ -159,7 +160,10 @@ def run_jfd(texture, jfd_texture, df_texture, seed_value, step_length):
run_jfd(tex, jf_tx, df_tx, 0.5, 4)
te = time.time()
print(te - ts)

ts = time.time()
run_jfd(tex, jf_tx, df_tx, 0.5, 4)
te = time.time()
print(te - ts)
# b = BasicJumpflooding(tex, 0.5)
# ts = time.time()
# b.jfd()
Expand Down
8 changes: 5 additions & 3 deletions AIHelper/navigation/utilities/level.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,12 @@ def create_dffinder(self):
self.dffinder = dffinding.DFFinder(self.costs, elevation, self.model.distance_field_threshold)

def generate_distance_fields(self):
if type(self.df) == type(None):
if type(self.df) == type(None) :
self.df = np.zeros(self.elevation.shape, dtype=np.float32)

scorecard.bruteforce_generate_distancefields(self.elevation, self.df, tuple((*self.transform.min_point,)), tuple((*self.transform.max_point,)))
if self.df.shape != self.elevation.shape:
self.df = np.zeros(self.elevation.shape, dtype=np.float32)
# scorecard.bruteforce_generate_distancefields(self.elevation, self.df, tuple((*self.transform.min_point,)), tuple((*self.transform.max_point,)))
scorecard.approximate_distance_fields(self.elevation, self.df, 0.5)
self.model.has_distance_field = True
self.model.save()

Expand Down
1 change: 1 addition & 0 deletions AIHelper/navigation/utilities/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def encode_level(in_level : level.Level) -> None:
with open(f'{l.relative_path}/df.npy', 'wb') as f:
df : np.ndarray = in_level.df
np.save(f, df.astype(np.float32))
print(np.max(in_level.df[0]))
print("Saved Distance Field Map")
l.transform = in_level.transform.as_dict()
l.save()
Expand Down
24 changes: 19 additions & 5 deletions AIHelper/navigation/utilities/scorecard.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,25 @@ def bruteforce_generate_distancefields(elevation_arr : np.ndarray, distance_fiel
distance_field_array[level][x][y] = 0.0

def approximate_distance_fields(elevation_array : np.ndarray, distance_field_array : np.ndarray, seed_value : np.float32):
for level in elevation_array.shape[0]:
jumpflooder = jumpflooding.BasicJumpflooding(elevation_array[level], seed_value)
jumpflooding.initial(elevation_array[level], jumpflooder.jf_tx, seed_value)
jumpflooder.jfd(True)
distance_field_array[level] = jumpflooder.df_tx
for level in range(elevation_array.shape[0]):
texture = np.abs(np.gradient(elevation_array[level], axis=0))
jf_tx = np.zeros((*elevation_array[level].shape, 3), dtype=np.float32)
jumpflooding.initial(texture, jf_tx, seed_value)
jumpflooding.run_jfd(
texture, jf_tx, distance_field_array[level], seed_value, 4
)
jumpflooding.run_jfd(
texture, jf_tx, distance_field_array[level], seed_value, 8
)
jumpflooding.run_jfd(
texture, jf_tx, distance_field_array[level], seed_value, 16
)
jumpflooding.run_jfd(
texture, jf_tx, distance_field_array[level], seed_value, 32
)
print(np.max(distance_field_array[level]))
print("Done level", level)
# distance_field_array[level] =

@njit(parallel=True)
def score_fast(
Expand Down
17 changes: 10 additions & 7 deletions AIHelper/navmesh_evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,18 @@ def get_path_to(start, end):
path = pyastar.astar_path(arr, (344, 601), get_valid_point_in_radius(arr, 353, 631), allow_diagonal=True)


with open("./models/Project/BF3 Bots 0.0.4/Level/MP_Subway/elevation.npy", "rb") as f:
elevation = numpy.load(f)[0]
with open("./models/Project/BF3 Bots 0.0.4/Level/XP1_004/elevation.npy", "rb") as f:
elevation : numpy.ndarray = numpy.load(f)[0]
elevation_g = numpy.abs(numpy.gradient(elevation, axis=0))
# print(elevation_g)


with open("./models/Project/BF3 Bots 0.0.4/Level/MP_Subway/data.npy", "rb") as f:
with open('./models/Project/BF3 Bots 0.0.4/Level/XP1_004/df.npy', 'rb') as fx:
elevation_arr = numpy.load(fx)[0]
elevation_arr = numpy.power(elevation_arr, 0.2)
elevation_arr = numpy.max(elevation_arr[1]) - elevation_arr
elevation_arr = numpy.power(elevation_arr, 4.0)
# elevation_arr = numpy.power(elevation_arr, 0.2)
# elevation_arr = numpy.max(elevation_arr[1]) - elevation_arr
# elevation_arr = numpy.power(elevation_arr, 4.0)

with open('./models/Project/BF3 Bots 0.0.4/Level/XP1_004/costs.npy', 'rb') as fx:
costs_arr = numpy.load(fx)[0]
Expand All @@ -125,12 +128,12 @@ def get_path_to(start, end):
#path = pyastar.astar_path(arr, (353, 659), get_valid_point_in_radius(arr, 341, 591), allow_diagonal=True)
#path = pyastar.astar_path(arr, (684, 397), get_valid_point_in_radius(arr, 658, 444), allow_diagonal=False)
# path = pyastar.astar_path(arr, (745, 528), get_valid_point_in_radius(arr, 848, 596), allow_diagonal=True)
path = pyastar.astar_path(arr, (1100, 924 ), get_valid_point_in_radius(arr, 1044, 938 ), allow_diagonal=True)
# path = pyastar.astar_path(arr, (1100, 924 ), get_valid_point_in_radius(arr, 1044, 938 ), allow_diagonal=True)

te = time.time()
print(te - ts)

print(path)
# print(path)

# print(arr[432,585])
# grid = Grid(matrix=arr)
Expand Down

0 comments on commit f6d3c52

Please sign in to comment.