From b576814a2a36c249343acd1c517d2776a67fdbe2 Mon Sep 17 00:00:00 2001 From: ANameSpace <125720576+ANameSpace@users.noreply.github.com> Date: Fri, 19 Jan 2024 23:05:47 +0300 Subject: [PATCH] 0.0.6 [*] Getting started on the route construction system. [+] Optimization Data system. --- ui/UI.py | 12 +- utils/Data.py | 258 ++++++++++++++++++++++++----------------- utils/Navigation.py | 45 +++++-- utils/obj/LadderObj.py | 16 +++ utils/obj/RoomObj.py | 16 +++ 5 files changed, 225 insertions(+), 122 deletions(-) create mode 100644 utils/obj/LadderObj.py create mode 100644 utils/obj/RoomObj.py diff --git a/ui/UI.py b/ui/UI.py index 7a22eac..f7ebd75 100644 --- a/ui/UI.py +++ b/ui/UI.py @@ -16,7 +16,6 @@ def __init__(self, str_name="None"): # temp self.origin = None self.task_num = 0 - self.str_name_len = 15 * len(str(str_name)) + 8 self.floor_btns = [] # zoom @@ -111,7 +110,8 @@ def __init__(self, str_name="None"): self.name_label.setFont(QFont("Arial", 16, weight=QFont.Bold)) self.name_label.setStyleSheet("color: white; border-radius: 10px; background-color: rgb(49, 51, 56);") self.name_label.setAlignment(Qt.AlignTop | Qt.AlignRight) - self.name_label.setGeometry(self.width() - (self.str_name_len + 7), 7, self.str_name_len, 25) + self.str_name_len = self.name_label.fontMetrics().boundingRect(self.name_label.text()).width() + 15 + self.name_label.setGeometry(self.width() - self.str_name_len, 7, self.str_name_len, 25) # info frame self.info_frame = QFrame(self) @@ -268,13 +268,13 @@ def paintEvent(self, event): painter.drawRect(scaled_x, scaled_y, scaled_width, scaled_height) for ladder in Data().get_ladders(self.current_floor): - ladder_id, x, y, width, height = ladder + x, y, width, height = ladder.getLocation() scaled_x = x * self.zoom_factor + self.offset.x() scaled_y = y * self.zoom_factor + self.offset.y() scaled_width = width * self.zoom_factor scaled_height = height * self.zoom_factor if self.has_result: - if self.route_ladders.count(str(ladder_id)) == 1: + if self.route_ladders.count(str("123")) == 1: painter.setBrush(QBrush(QColor(238, 255, 46))) else: painter.setBrush(QBrush(QColor(183, 194, 64))) @@ -283,13 +283,13 @@ def paintEvent(self, event): painter.drawRect(scaled_x, scaled_y, scaled_width, scaled_height) for room in Data().get_rooms(self.current_floor): - name, x, y, width, height, x222 = room + x, y, width, height = room.getLocation() scaled_x = x * self.zoom_factor + self.offset.x() scaled_y = y * self.zoom_factor + self.offset.y() scaled_width = width * self.zoom_factor scaled_height = height * self.zoom_factor if self.has_result: - if self.input_field.text() == name: + if self.input_field.text() == "1": painter.setBrush(QBrush(QColor(59, 196, 57))) else: painter.setBrush(QBrush(QColor(37, 73, 115))) diff --git a/utils/Data.py b/utils/Data.py index 52800eb..c053d9a 100644 --- a/utils/Data.py +++ b/utils/Data.py @@ -1,128 +1,92 @@ import json -from PySide6.QtGui import QColor +from functools import * import os import sys +from utils.obj.LadderObj import LadderObj +from utils.obj.RoomObj import RoomObj from utils.tools.Log import Log -walls = [ - (0, 0, 0, 40), - (0, 40, 40, 40) -] - -ladders = [ - ("l1", 0, 25, 10, 10), - ("l2", 0, 80, 50, 60) -] - -erooms = [ - (0, 0, 1, 2), - (-10, 1, 8, 5) -] - -rooms = [ - ("1.1", 3, 1, 10, 12, QColor(0, 10, 10)), - ("1.2", 1, 15, 8, 5, QColor(0, 100, 100)), - ("1.3", 1, 20, 8, 5, QColor(0, 100, 100)), - ("1.4", 1, 25, 8, 5, QColor(0, 100, 100)), - ("1.5", 1, 32, 5, 8, QColor(0, 100, 100)), - ("1.6", 6, 32, 5, 8, QColor(0, 100, 100)), - - ("1.7", 13, 1, 10, 12, QColor(0, 25, 10)), -] - default_data = """ { "name": "Example", "default": { - "id": "1", + "id": "MAIN", "floor": "1", "x": "0", "y": "0", - "size": "10" + "size": "5" }, "floors": { "1": { "walls": { "1": { - "x1": "1", - "y1": "0", - "x2": "0", - "y2": "10" + "x1": "-5", + "y1": "-5", + "x2": "5", + "y2": "5" }, "2": { "x1": "1", - "y1": "0", - "x2": "0", - "y2": "10" - }, - "3": { - "x1": "1", - "y1": "0", - "x2": "0", - "y2": "10" + "y1": "3", + "x2": "4", + "y2": "5" } }, "empty_rooms": { "1": { - "x1": "1", - "y1": "0", - "width": "0", - "height": "10" + "x": "15", + "y": "15", + "width": "5", + "height": "5" }, "2": { - "x1": "1", - "y1": "0", - "width": "0", - "height": "10" + "x": "20", + "y": "15", + "width": "5", + "height": "5" } }, "ladders": { "1": { - "x1": "1", - "y1": "0", - "width": "0", - "height": "10" + "x": "20", + "y": "30", + "width": "5", + "height": "5" }, "2": { - "x1": "1", - "y1": "0", - "width": "0", - "height": "10" + "x": "20", + "y": "35", + "width": "5", + "height": "5" } }, "rooms": { "1.1": { - "x1": "1", - "y1": "0", - "width": "0", - "height": "10" + "x": "10", + "y": "0", + "width": "5", + "height": "4" }, "2.1": { - "x1": "1", - "y1": "0", - "width": "0", - "height": "10" + "x": "10", + "y": "5", + "width": "4", + "height": "4" } } }, "2": { "walls": { "1": { - "x1": "1", + "x1": "0", "y1": "0", - "x2": "0", - "y2": "10" + "x2": "10", + "y2": "0" }, "2": { - "x1": "1", - "y1": "0", - "x2": "0", - "y2": "10" - }, - "3": { - "x1": "1", + "x1": "0", "y1": "0", "x2": "0", "y2": "10" @@ -130,43 +94,43 @@ }, "empty_rooms": { "1": { - "x1": "1", - "y1": "0", - "width": "0", + "x": "2", + "y": "2", + "width": "5", "height": "10" }, "2": { - "x1": "1", - "y1": "0", - "width": "0", + "x": "9", + "y": "9", + "width": "5", "height": "10" } }, "ladders": { "1": { - "x1": "1", - "y1": "0", - "width": "0", + "x": "-10", + "y": "0", + "width": "10", "height": "10" }, "2": { - "x1": "1", - "y1": "0", - "width": "0", + "x": "-20", + "y": "0", + "width": "10", "height": "10" } }, "rooms": { "3.1": { - "x1": "1", - "y1": "0", - "width": "0", + "x": "20", + "y": "20", + "width": "10", "height": "10" }, "3.2": { - "x1": "1", - "y1": "0", - "width": "0", + "x": "20", + "y": "30", + "width": "10", "height": "10" } } @@ -191,6 +155,8 @@ def __init__(self): self.data_directory = os.path.join(self.current_directory, "UniversityNavigationSystem") self.data_file = os.path.join(self.data_directory, "map.json") self._generate_file() + # Temp + self.all_names = None Data._init_already = True def _generate_file(self): @@ -220,6 +186,7 @@ def _generate_file(self): self.you_pos = ("you_pos", 10, 0, 0) self.floors_count = 1 + @lru_cache def is_valid_name(self, name: str): """ Checking the existence of a room by its name @@ -251,6 +218,7 @@ def get_you_pos(self): """ return self.you_pos + @lru_cache def get_floors(self): """ Get a list of names of all floors @@ -264,8 +232,14 @@ def get_floors(self): return tuple("1") def get_floors_count(self): + """ + Get the number of all floors + :return: number of all floors + :rtype: int + """ return self.floors_count + @lru_cache def get_default_floor(self): """ Get the default floor name. @@ -275,9 +249,10 @@ def get_default_floor(self): try: return str(self.data["default"]["floor"]) except: - Log().send(Log.LogType.ERROR, "The section \"default.floors\" could not be loaded!") + Log().send(Log.LogType.ERROR, "The section \"default.floor\" could not be loaded!") return 1 + @lru_cache def get_rooms_names(self): """ Get a list of names of all rooms @@ -288,20 +263,95 @@ def get_rooms_names(self): names = [] for f in list(self.data["floors"].keys()): names += [n for n in self.data["floors"][f]["rooms"]] - self.all_names = tuple(names) + self.all_names = names return tuple(names) except: Log().send(Log.LogType.ERROR, "The section \"floors.rooms\" could not be loaded!") return tuple() - def get_walls(self, floor: str): - return walls + @lru_cache + def get_walls(self, floor_name: str): + """ + Get a list of walls + :param str floor_name: floor name + :return: (x1: int, y1: int, x2: int, y2: int) + :rtype: list + """ + floor_name = str(floor_name) + try: + walls = [] + for f in list(self.data["floors"][floor_name]["walls"].keys()): + walls.append(tuple([int(self.data["floors"][floor_name]["walls"][f]["x1"]), + int(self.data["floors"][floor_name]["walls"][f]["y1"]), + int(self.data["floors"][floor_name]["walls"][f]["x2"]), + int(self.data["floors"][floor_name]["walls"][f]["y2"])])) + return walls + except: + Log().send(Log.LogType.ERROR, "The section \"floors." + floor_name + ".walls\" could not be loaded!") + return [] - def get_empty_rooms(self, floor: str): - return erooms + @lru_cache + def get_empty_rooms(self, floor_name: str): + """ + Get a list of empty rooms + :param str floor_name: floor name + :return: (x: int, y: int, width: int, height: int) + :rtype: list + """ + floor_name = str(floor_name) + try: + rooms = [] + for f in list(self.data["floors"][floor_name]["empty_rooms"].keys()): + rooms.append(tuple([int(self.data["floors"][floor_name]["empty_rooms"][f]["x"]), + int(self.data["floors"][floor_name]["empty_rooms"][f]["y"]), + int(self.data["floors"][floor_name]["empty_rooms"][f]["width"]), + int(self.data["floors"][floor_name]["empty_rooms"][f]["height"])])) + return rooms + except: + Log().send(Log.LogType.ERROR, "The section \"floors." + floor_name + ".empty_rooms\" could not be loaded!") + return [] - def get_ladders(self, floor: str): - return ladders + @lru_cache + def get_ladders(self, floor_name: str): + """ + Get a list of ladders + :param str floor_name: floor name + :return: (LadderObj, LadderObj, ...) + :rtype: list + """ + floor_name = str(floor_name) + try: + rooms = [] + for f in list(self.data["floors"][floor_name]["empty_rooms"].keys()): + rooms.append( + LadderObj(str(f), + int(self.data["floors"][floor_name]["empty_rooms"][f]["x"]), + int(self.data["floors"][floor_name]["empty_rooms"][f]["y"]), + int(self.data["floors"][floor_name]["empty_rooms"][f]["width"]), + int(self.data["floors"][floor_name]["empty_rooms"][f]["height"]))) + return rooms + except: + Log().send(Log.LogType.ERROR, "The section \"floors." + floor_name + ".empty_rooms\" could not be loaded!") + return [] - def get_rooms(self, floor: str): - return rooms + @lru_cache + def get_rooms(self, floor_name: str): + """ + Get a list of rooms + :param str floor_name: floor name + :return: (RoomObj, RoomObj, ...) + :rtype: list + """ + try: + rooms = [] + for f in list(self.data["floors"][floor_name]["rooms"].keys()): + rooms.append( + RoomObj(str(f), + int(self.data["floors"][floor_name]["rooms"][f]["x"]), + int(self.data["floors"][floor_name]["rooms"][f]["y"]), + int(self.data["floors"][floor_name]["rooms"][f]["width"]), + int(self.data["floors"][floor_name]["rooms"][f]["height"]))) + return rooms + except: + Log().send(Log.LogType.ERROR, "The section \"floors." + floor_name + ".rooms\" could not be loaded!") + return [] diff --git a/utils/Navigation.py b/utils/Navigation.py index d1572c5..9b76498 100644 --- a/utils/Navigation.py +++ b/utils/Navigation.py @@ -35,19 +35,40 @@ def __new__(cls, *args, **kwargs): def __init__(self): if not Navigation._init_already: - # load file - self.current_directory = os.getcwd() - self.logs_directory = os.path.join(self.current_directory, "logs") - if not os.path.exists(self.logs_directory): - os.makedirs(self.logs_directory) - self.log_file = os.path.join(self.logs_directory, self.generate_file_name()) - - # add from file - G.add_edge("A", "B", weight=2) - G.add_edge("B", "C", weight=1) - G.add_edge("A", "C", weight=5) + self.d = Data() + + self.graph = nx.Graph() + self.nodes = [] + self.edges = [] + + # Load floors + self.nodes.append(self.d.get_you_pos()[0]) + for floor in self.d.get_floors(): + # load rooms + for room in self.d.get_rooms(floor): + n = str(floor + ".room." + room.getName()) + + self.nodes.append(n) + self.graph.add_edge(n, room.getCorridor(), weight=1) + + # load ladders + for ladder in self.d.get_ladders(floor): + n = str(floor + ".ladder." + ladder.getName()) + self.nodes.append(n) + self.graph.add_edge(n, ladder.getCorridor(), weight=1) + + # load corridors + for corridor in self.d.get_corridors(floor): + n = str(floor + ".corridor." + corridor.getName()) + self.nodes.append(n) + #self.edges.append(tuple(n, ladder.)) + + self.graph.add_nodes_from(self.nodes) + self.graph.add_edges_from(self.edges) + + Navigation._init_already = True def get_edges(self, end_point): - return nx.shortest_path(G, Data().get_you_pos()[0], str(end_point)) + return nx.shortest_path(self.graph, self.d.get_you_pos()[0], str(end_point)) diff --git a/utils/obj/LadderObj.py b/utils/obj/LadderObj.py new file mode 100644 index 0000000..dc4ea09 --- /dev/null +++ b/utils/obj/LadderObj.py @@ -0,0 +1,16 @@ +class LadderObj: + def __init__(self, name: str, x: int, y: int, width: int, height: int): + self.name = name + self.x = x + self.y = y + self.width = width + self.height = height + + def getLocation(self): + return tuple([self.x, self.y, self.width, self.height]) + + def getName(self): + return self.name + + def getCorridor(self): + return "self.name" \ No newline at end of file diff --git a/utils/obj/RoomObj.py b/utils/obj/RoomObj.py new file mode 100644 index 0000000..6f241e6 --- /dev/null +++ b/utils/obj/RoomObj.py @@ -0,0 +1,16 @@ +class RoomObj: + def __init__(self, name: str,x: int, y: int, width: int, height: int): + self.name = name + self.x = x + self.y = y + self.width = width + self.height = height + + def getLocation(self): + return tuple([self.x, self.y, self.width, self.height]) + + def getName(self): + return self.name + + def getCorridor(self): + return "" \ No newline at end of file