Skip to content

Commit

Permalink
Selection of points, map displayed and algorithm update.
Browse files Browse the repository at this point in the history
  • Loading branch information
Francis Bui committed May 15, 2021
1 parent bef3337 commit 87721b4
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 149 deletions.
Binary file modified __pycache__/algo.cpython-38.pyc
Binary file not shown.
Binary file modified __pycache__/tmap.cpython-38.pyc
Binary file not shown.
148 changes: 76 additions & 72 deletions algo.py
Original file line number Diff line number Diff line change
@@ -1,84 +1,88 @@
import math
import heapq

directions = [(0, 1), (1, 0), (-1, 0), (0, -1), (1, 1), (1, -1), (-1, 1), (-1, -1)]
directions = [(0, 1), (1, 0), (-1, 0), (0, -1),
(1, 1), (1, -1), (-1, 1), (-1, -1)]
max_g = 100000000000

class Point:
def __init__(self, x, y, topo_map=None):
self.x = x
self.y = y
self.topo_map = topo_map
self.height = topo_map.height_map[y][x] if (topo_map and self.inBounds()) else -1
def __init__(self, x, y, topo_map=None):
self.x = x
self.y = y
self.topo_map = topo_map
self.height = topo_map.height_map[y][x] if (
topo_map and self.inBounds()) else -1

self.g = 0
self.h = 0
self.f = 0
self.g = 0
self.h = 0
self.f = 0

def __lt__(self, other):
return self.f < other.f
def __lt__(self, other):
return self.f < other.f

def inBounds(self):
height_map = self.topo_map.height_map
def inBounds(self):
height_map = self.topo_map.height_map
map_height = len(height_map)
map_width = len(height_map[0])
return (self.x >= 0) and (self.x < map_width) and (self.y >= 0) and (self.y < map_height)

def dist(self, other):
return math.sqrt(
((self.x - other.x) * self.topo_map.dist_between_pixels) ** 2 +
((self.y - other.y) * self.topo_map.dist_between_pixels) ** 2 +
(self.height - other.height) ** 2)

def sameCoord(self, other):
return self.x == other.x and self.y == other.y

def heuristic(self, other):
return self.dist(other)


def find_path(topo_map, start, dest):
height_map = topo_map.height_map
map_height = len(height_map)
map_width = len(height_map[0])
return (self.x >= 0) and (self.x < map_width) and (self.y >= 0) and (self.y < map_height)

def dist(self, other):
return math.sqrt(
((self.x - other.x) * self.topo_map.dist_between_pixels) ** 2 +
((self.y - other.y) * self.topo_map.dist_between_pixels) ** 2 +
(self.height - other.height) ** 2)

def sameCoord(self, other):
return self.x == other.x and self.y == other.y

def heuristic(self, other):
return self.dist(other)

def find_path(topo_map, start, dest):
print("in lops")
height_map = topo_map.height_map
map_height = len(height_map)
map_width = len(height_map[0])

min_g = [x[:] for x in [[max_g] * map_width] * map_height]
min_g[start.y][start.x] = 0
parent = [x[:] for x in [[None] * map_width] * map_height]

to_visit = [Point(start.x, start.y, topo_map)]
heapq.heapify(to_visit)

while to_visit:
curr_point = heapq.heappop(to_visit)

if(curr_point.sameCoord(dest)):
break

if(min_g[curr_point.y][curr_point.x] < curr_point.g):
continue

for direction in directions:
next_point = Point(curr_point.x + direction[0], curr_point.y + direction[1], topo_map)

if(not next_point.inBounds()):
continue

next_point.g = min_g[curr_point.y][curr_point.x] + curr_point.dist(next_point)
next_point.h = next_point.heuristic(dest)
next_point.f = next_point.g + next_point.h

# Shorter path was found!
# It is possible that this point has already been traversed and we are revisiting a path
if(next_point.g < min_g[next_point.y][next_point.x]):
min_g[next_point.y][next_point.x] = next_point.g
parent[next_point.y][next_point.x] = curr_point
heapq.heappush(to_visit, next_point)

curr_point = dest
path = [x[:] for x in [[False] * map_width] * map_height]
while(curr_point != None):
path[curr_point.y][curr_point.x] = True
curr_point = parent[curr_point.y][curr_point.x]

return path
min_g = [x[:] for x in [[max_g] * map_width] * map_height]
min_g[start.y][start.x] = 0
parent = [x[:] for x in [[None] * map_width] * map_height]

to_visit = [Point(start.x, start.y, topo_map)]
heapq.heapify(to_visit)

while to_visit:
curr_point = heapq.heappop(to_visit)

if(curr_point.sameCoord(dest)):
break

if(min_g[curr_point.y][curr_point.x] < curr_point.g):
continue

for direction in directions:
next_point = Point(
curr_point.x + direction[0], curr_point.y + direction[1], topo_map)

if(not next_point.inBounds()):
continue

next_point.g = min_g[curr_point.y][curr_point.x] + \
curr_point.dist(next_point)
next_point.h = next_point.heuristic(dest)
next_point.f = next_point.g + next_point.h

# Shorter path was found!
# It is possible that this point has already been traversed and we are revisiting a path
if(next_point.g < min_g[next_point.y][next_point.x]):
min_g[next_point.y][next_point.x] = next_point.g
parent[next_point.y][next_point.x] = curr_point
heapq.heappush(to_visit, next_point)

curr_point = dest
path = [x[:] for x in [[False] * map_width] * map_height]
while(curr_point != None):
path[curr_point.y][curr_point.x] = True
curr_point = parent[curr_point.y][curr_point.x]

return path
88 changes: 37 additions & 51 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import imageio
from algo import find_path, Point
from tmap import Map
import io
Expand All @@ -7,31 +8,28 @@
import numpy as np
import matplotlib.pyplot as plt

working = False
# doc is available at https://docs.google.com/document/d/16PPeIRIyT7jo98mTRqRZESiSXH6gSJzDIO5CjANJ8Bc/edit?usp=sharing

#https://stackoverflow.com/questions/25102461/python-rgb-matrix-of-an-image

import imageio

file_types = [("JPEG (*.jpg)", "*.jpg"),
("All files (*.*)", "*.*")]
("All files (*.*)", "*.*")]


def main():
layout = [
[sg.Image(key="-IMAGE-")],
[
sg.Text("x,y"),
sg.Input(size=(10, 1), key="coordOne"),
sg.Text("x,y"),
sg.Input(size=(10, 1), key="coordTwo"),
sg.Text("Scale/pixel"),
sg.Input(size=(10,1), key="scale"),
sg.Text("x,y"),
sg.Input(size=(10, 1), key="coordOne"),
sg.Text("x,y"),
sg.Input(size=(10, 1), key="coordTwo"),
sg.Text("Scale/pixel"),
sg.Input(size=(5, 1), key="scale"),
sg.Text("Height scale"),
sg.Input(size=(5, 1), key="height_scale"),

[
sg.Text("Image File"),
sg.Input(size=(25, 1), key="-FILE-"),
sg.FileBrowse(file_types=file_types),
sg.Button("Load Image"),
sg.Text("Image File"),
sg.Input(size=(25, 1), key="-FILE-"),
sg.FileBrowse(file_types=file_types),
sg.Button("Load Image"),
],
],
]
Expand All @@ -44,31 +42,34 @@ def main():
filename = values["-FILE-"]
coordOne = values["coordOne"]
coordTwo = values["coordTwo"]
pixel_scale = int(values["scale"])
height_scale = int(values["height_scale"])
c1 = [int(i) for i in coordOne.split(', ')]
c2 = [int(i) for i in coordTwo.split(', ')]
if os.path.exists(filename):
image = Image.open(values["-FILE-"])
image.thumbnail((400, 400))
bio = io.BytesIO()
image.save(bio, format="PNG")
window["-IMAGE-"].update(data=bio.getvalue())
print("Processing...")
img = imageio.imread(filename) # 512x1024x3 array
topo_map = Map(img, 100, 2)
secondOne = False
image = Image.open(values["-FILE-"])
image.thumbnail((400, 400))
bio = io.BytesIO()
image.save(bio, format="PNG")
window["-IMAGE-"].update(data=bio.getvalue())
img = imageio.imread(filename) # 512x1024x3 array
topo_map = Map(img, height_scale, pixel_scale)
secondOne = False

path = find_path(topo_map, Point(c1[0], c1[1]), Point(c2[0], c2[1]))
dist = len(path) # multiply
an_array = np.array(path)
plt.imshow(an_array)
plt.show()
# Construct the shortest path
print("Done!")
path = find_path(topo_map, Point(
c1[0], c1[1]), Point(c2[0], c2[1]))
dist = len(path) # multiply
an_array = np.array(path)
plt.imshow(an_array)
plt.show()
# Construct the shortest path
window.close()



if __name__ == "__main__":
main()


def main():
elements = [
[sg.Image(key="-IMAGE-")],
Expand All @@ -78,19 +79,4 @@ def main():
sg.FileBrowse(file_types=file_types),
],
]
window = sg.Window("Image Viewer", elements)




# for i in range(10):
# for j in range(10):
# print("T" if path[i][j] else "F", end=" ")
# print()

# for i in range(10):
# for j in range(10):
# print(topo_map.height_map[i][j], end=" ")
# print()


window = sg.Window("Image Viewer", elements)
51 changes: 25 additions & 26 deletions tmap.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
class Map:
def __init__(self, img, max_height_diff, dist_between_pixels):
self.img = img
self.height = img.shape[0]
self.width = img.shape[1]
self.max_height_diff = max_height_diff
self.dist_between_pixels = dist_between_pixels
self.height_map = self.generate_height_map()
def __init__(self, img, max_height_diff, dist_between_pixels):
self.img = img
self.height = img.shape[0]
self.width = img.shape[1]
self.max_height_diff = max_height_diff
self.dist_between_pixels = dist_between_pixels
self.height_map = self.generate_height_map()

def calculate_height(self, r, g, b):
normalized = ((r * 255 * 255) + (g * 255) + (b)) / (255**3 + 255**2 + 255)
return normalized * self.max_height_diff
# placeholder for height calculator
def calculate_height(self, r, g, b):
normalized = ((r * 255 * 255) + (g * 255) + (b)) / \
(255**3 + 255**2 + 255)
return normalized * self.max_height_diff
# placeholder for height calculator

def generate_height_map(self):
img_height = self.img.shape[0]
img_width = self.img.shape[1]

height_map = [x[:] for x in [[0] * img_width] * img_height]
for i in range(0, img_height):
for j in range(0, img_width):
# Calculate height based on rgb values
height_map[i][j] = self.calculate_height(
self.img[i, j, 0],
self.img[i, j, 1],
self.img[i, j, 2]
)
print("Done processing image")
return height_map
def generate_height_map(self):
img_height = self.img.shape[0]
img_width = self.img.shape[1]

height_map = [x[:] for x in [[0] * img_width] * img_height]
for i in range(0, img_height):
for j in range(0, img_width):
# Calculate height based on rgb values
height_map[i][j] = self.calculate_height(
self.img[i, j, 0],
self.img[i, j, 1],
self.img[i, j, 2]
)
return height_map

0 comments on commit 87721b4

Please sign in to comment.