From 947ee5b44138a36287f7ebeea8e74d7542d0a8dc Mon Sep 17 00:00:00 2001 From: Panyloi Date: Mon, 11 Nov 2024 17:15:30 +0100 Subject: [PATCH 1/7] Stable version v1 --- example.py | 3 ++- src/editor/artists.py | 9 +++++++++ src/editor/state.py | 6 ++++-- src/editor/views/hulls_view.py | 29 ++++++++++++++++------------- 4 files changed, 31 insertions(+), 16 deletions(-) diff --git a/example.py b/example.py index 42ec55b..8b31b64 100644 --- a/example.py +++ b/example.py @@ -1,7 +1,8 @@ from src.main import draw_maps, draw_maps_editor import time -draw_maps_editor("data/kk_swap_2d.csv", None) +# draw_maps_editor("data/kk_swap_2d.csv", None) +draw_maps_editor("data/size-7/mds_katz_cen_2d.csv", None) # s = time.time() # draw_maps("data/size-7/mds_katz_cen_2d.csv", "pngs/size-7/mds_katz_cen_2d_daq", config_id='iterative') diff --git a/src/editor/artists.py b/src/editor/artists.py index 878807f..a9ac359 100644 --- a/src/editor/artists.py +++ b/src/editor/artists.py @@ -597,6 +597,15 @@ def show_labels_and_hulls(self, ax: Axes): State.show_labels_and_hulls = show_labels_and_hulls + +def show_labels(self, ax: Axes): + for arrow in ArrowArtist.get_all_arrows(ax): + arrow.show() + for label in LabelArtist.get_all_labels(ax): + label.show() + +State.show_labels = show_labels + # ------------------------------ DRAW DEFINITION ----------------------------- # def draw(self, ax: Axes) -> None: diff --git a/src/editor/state.py b/src/editor/state.py index 188353d..15985ec 100644 --- a/src/editor/state.py +++ b/src/editor/state.py @@ -114,6 +114,9 @@ def hide_labels_and_hulls(self, ax: Axes): def show_labels_and_hulls(self, ax: Axes): raise NotImplementedError + def show_labels(self, ax: Axes): + raise NotImplementedError + # ---------------------------------- GETTERS --------------------------------- # @KeyErrorWrap("") @@ -282,12 +285,11 @@ def hull_set_point(self, point_id: int, x: float, y: float) -> None: def hull_remove_point(self, point_id) -> None: df = self.data['clusters_data']['points'] - df = df.drop(point_id) + self.data['clusters_data']['points'] = df.drop(point_id) df_1 = self.data['clusters_data']['colors'] if 'mine' in df_1.keys(): del df_1['mine'] - # df_1 = df_1.drop('mine') def set_hull_polygon_cords(self, hull_name: str, new_cords) -> None: self.data['hulls_data']['hulls'][hull_name]['cords'] = new_cords diff --git a/src/editor/views/hulls_view.py b/src/editor/views/hulls_view.py index 0584212..1196e13 100644 --- a/src/editor/views/hulls_view.py +++ b/src/editor/views/hulls_view.py @@ -40,6 +40,7 @@ def draw(self, *args, **kwargs) -> None: self.state.hide_labels_and_hulls(self.vm.ax) HullArtist.show_hulls(self.vm.ax) + # self.state.show_labels(self.vm.ax) self.confirm_button.hide() self.cancel_button.hide() @@ -49,6 +50,7 @@ def draw(self, *args, **kwargs) -> None: # remove current hulls for child in self.vm.ax.get_children(): + print(child) if type(child) is LineCollection: child.remove() @@ -59,7 +61,7 @@ def draw(self, *args, **kwargs) -> None: # events self.cem.add(SharedEvent('pick_event', self.pick_event)) self.cem.add(SharedEvent('button_press_event', self.press_event)) - + self.vem.refresh() plt.draw() def switch_mode(self, mode_name): @@ -117,7 +119,7 @@ def press_event(self, event: MouseEvent) -> None: ) elif self.mode == "remove": print("removing mode") - if self.remove_line_state <= 2: + if self.remove_line_state < 2: self.remove_line_state += 1 self.points_to_add.append((event.xdata, event.ydata)) @@ -125,10 +127,7 @@ def press_event(self, event: MouseEvent) -> None: self.pointer_points.append( PointArtist.point(self.vm.ax, -len(self.points_to_add), facecolor="red", edgecolor="red") ) - elif self.remove_line_state == 3: - self.remove_line_state = 0 - self.is_adding_points = False - self.remove_line_from_hull() + elif self.mode == "main": return @@ -138,11 +137,13 @@ def confirm(self): if self.mode == "add": self.calculate_new_hull() plt.draw() - elif self.mode == "remove": - self.remove_line_state = 1 + elif self.mode == "remove" and self.remove_line_state == 2: + self.remove_line_state = 0 + self.remove_line_from_hull() + plt.draw() self.switch_mode("main") # todo kinda stupid fix, gotta add a separate return button - def search_for_hull_name_in_hole(self, point: tuple[float, float], _hull_name: str = "") -> (str, tuple[float, float]): + def search_for_hull_name_in_hole(self, point: tuple[float, float], _hull_name: str = "") -> tuple[str, tuple[float, float]]: hulls_name = self.state.get_all_hulls_name() best_dist = float("inf") best_hull_name = "" @@ -166,7 +167,7 @@ def search_for_hull_name_in_hole(self, point: tuple[float, float], _hull_name: s return best_hull_name, best_cord - def find_closest_edge_point_in_hull(self, hull_name: str, point: tuple[float, float]) -> (tuple[float, float], int): + def find_closest_edge_point_in_hull(self, hull_name: str, point: tuple[float, float]) -> tuple[tuple[float, float], int]: hull_cords = self.state.get_hull_interpolated_cords(hull_name) best_cords = (0, 0) best_dist = float("inf") @@ -219,7 +220,7 @@ def calculate_new_hull(self): for i, pointer_point in enumerate(self.pointer_points): pointer_point.remove() - self.state.hull_remove_point(i * (-1)) + self.state.hull_remove_point((i + 1) * (-1)) self.pointer_points = [] @@ -286,7 +287,7 @@ def calculate_new_hull(self): for i, pointer_point in enumerate(self.pointer_points): pointer_point.remove() - self.state.hull_remove_point(i * (-1)) + self.state.hull_remove_point((i + 1) * (-1)) self.pointer_points = [] @@ -328,7 +329,7 @@ def remove_line_from_hull(self): for i, pointer_point in enumerate(self.pointer_points): pointer_point.remove() - self.state.hull_remove_point(i * (-1)) + self.state.hull_remove_point((i + 1) * (-1)) self.pointer_points = [] @@ -338,6 +339,7 @@ def remove_line_from_hull(self): if hull_name_1 == hull_name_2: self.exec_remove_line_from_hull(hull_name_1, cord_1, cord_2) + self.vem.refresh() plt.draw() def exec_remove_line_from_hull(self, hull_name, point1, point2): @@ -384,6 +386,7 @@ def exec_remove_line_from_hull(self, hull_name, point1, point2): self.state.set_hull_lines_cords(hull_name, final_lines) HullArtist.hull(self.vm.ax, hull_name) + self.vem.refresh() plt.draw() def hide(self) -> None: From eade18d911e2a46086c555415dfd2d82c593676e Mon Sep 17 00:00:00 2001 From: Panyloi Date: Mon, 11 Nov 2024 23:14:14 +0100 Subject: [PATCH 2/7] Stable version v2 --- src/editor/state.py | 35 ++++++++ src/editor/views/hulls_view.py | 146 ++++++++++++++++++++++++++++---- src/generator/hull_generator.py | 52 +++++++++--- src/main.py | 9 +- 4 files changed, 211 insertions(+), 31 deletions(-) diff --git a/src/editor/state.py b/src/editor/state.py index 15985ec..89f6b23 100644 --- a/src/editor/state.py +++ b/src/editor/state.py @@ -203,12 +203,41 @@ def update_hulls(self): 'cluster_points': new_hull[hull_name]['cluster_points'], "interpolate_points": new_hull[hull_name]["interpolate_points"], "hole_in_hulls": [], + "gathering_radius": new_hull[hull_name]["gathering_radius"], "artist": None } self.data['hulls_data']['undraw'] = set() self.data['hulls_data']['change'] = {} + def calc_and_add_one_hull(self, + hull_name, + points, + circle_radious = None, + points_in_circle = None, + segment_length = None, + radius_domain_expansion = None, + closest_points_radius = None): + + new_hull = calc_one_hull(hull_name=hull_name, + points=points, + state=self.data, + circle_radious=circle_radious, + points_in_circle=points_in_circle, + segment_length=segment_length, + radius_domain_expansion=radius_domain_expansion, + closest_points_radius=closest_points_radius) + + self.data['hulls_data']['hulls'][hull_name] = { + 'cords': new_hull[hull_name]['polygon_points'], + 'line_cords': new_hull[hull_name]['polygon_lines'], + 'cluster_points': new_hull[hull_name]['cluster_points'], + "interpolate_points": new_hull[hull_name]["interpolate_points"], + "hole_in_hulls": [], + "gathering_radius": new_hull[hull_name]["gathering_radius"], + "artist": None + } + def redefine_hull(self, hull_name: str): hull_resources = self.data['hulls_data']['hulls'][hull_name] @@ -249,6 +278,9 @@ def get_hole_in_hulls(self, hull_name: str) -> None: def get_hulls_render_name(self) -> None: return self.data['hulls_data']['render_name'] + 1 + + def get_hulls_closest_radius_param(self, hull_name): + return self.data['hulls_data']['hulls'][hull_name]['gathering_radius'] # ------------------------------- HULLS SETTERS ------------------------------ # @@ -309,6 +341,9 @@ def set_hole_in_hulls(self, hull_name: str, points: tuple[tuple[float, float], t def remove_hole_in_hulls(self, hull_name: str, points: tuple[tuple[float, float], tuple[float, float]]) -> None: self.data['hulls_data']['hulls'][hull_name]['hole_in_hulls'].remove(points) + def set_hulls_closest_radius_param(self, hull_name, new_gathering_radius): + self.data['hulls_data']['hulls'][hull_name]['gathering_radius'] = new_gathering_radius + # ------------------------------- CLUSTER GETTERS ------------------------------ # diff --git a/src/editor/views/hulls_view.py b/src/editor/views/hulls_view.py index 1196e13..055b921 100644 --- a/src/editor/views/hulls_view.py +++ b/src/editor/views/hulls_view.py @@ -14,6 +14,8 @@ def __init__(self, view_manager: ViewManager) -> None: self.pointer_points = [] self.remove_line_state = 0 self.removed_lines = [] + self.picked_item = None + self.picked_event = None self.vem.add(ChangeViewButton(self, self.home_ax, "Home", ViewsEnum.HOME)) self.vem.add(ChangeViewButton(self, self.labels_ax, "Labels", ViewsEnum.LABELS)) @@ -28,6 +30,12 @@ def __init__(self, view_manager: ViewManager) -> None: self.reset_button: NormalButton = self.vem.add(NormalButton(self, [0.85, 0.05, 0.1, 0.075], "Reset", self.reset)) self.cancel_button: NormalButton = self.vem.add(NormalButton(self, [0.85, 0.05, 0.1, 0.075], "Cancel", self.cancel)) + self.hull_name_box = self.vem.add(ShiftingTextBox(self, [0.5, 0.05, 0.25, 0.075], + self.hull_name_update, self.hull_name_submit)) + self.hull_parameter_set_box = self.vem.add(LimitedTextBox(self, [0.8, 0.05, 0.10, 0.075], + self.spread_size_update, self.spread_size_submit)) + + view_button.highlight() self.reset_button.button_ax.set_facecolor("lightcoral") self.reset_button.button_ref.color = "lightcoral" @@ -43,6 +51,7 @@ def draw(self, *args, **kwargs) -> None: # self.state.show_labels(self.vm.ax) self.confirm_button.hide() self.cancel_button.hide() + self.reset_button.hide() df = self.state.get_all_points() self.vm.ax.set_xlim(df['x'].min() - 10, df['x'].max() + 10) @@ -61,7 +70,6 @@ def draw(self, *args, **kwargs) -> None: # events self.cem.add(SharedEvent('pick_event', self.pick_event)) self.cem.add(SharedEvent('button_press_event', self.press_event)) - self.vem.refresh() plt.draw() def switch_mode(self, mode_name): @@ -70,7 +78,9 @@ def switch_mode(self, mode_name): if self.mode == 'main': self.add_line_button.show() self.remove_line_button.show() - self.reset_button.show() + self.hull_name_box.show() + self.hull_parameter_set_box.show() + self.reset_button.hide() self.confirm_button.hide() self.cancel_button.hide() else: @@ -79,11 +89,29 @@ def switch_mode(self, mode_name): self.reset_button.hide() self.confirm_button.show() self.cancel_button.show() + self.hull_parameter_set_box.hide() + self.hull_name_box.hide() plt.draw() def cancel(self): """ Switch to main mode and cancel every action since last switch """ # todo code action like resetting picked points etc + + if self.mode == 'add': + + for i, pointer_point in enumerate(self.pointer_points): + pointer_point.remove() + self.state.hull_remove_point((i + 1) * (-1)) + + if self.mode == 'remove': + + self.remove_line_state = 0 + for i, pointer_point in enumerate(self.pointer_points): + pointer_point.remove() + self.state.hull_remove_point((i + 1) * (-1)) + + self.pointer_points = [] + self.points_to_add = [] self.switch_mode('main') def reset(self): @@ -100,9 +128,13 @@ def pick_event(self, event: PickEvent) -> None: Hull artist doesn't inherit after Artist, so it can't ever be picked up Instead LineCollection can be used """ + self.picked_event = None + self.picked_item = None + if event is None: + return - # if isinstance(event.artist, LineCollection): - # pass + if isinstance(event.artist, LineCollection): + self.picked_event = event def press_event(self, event: MouseEvent) -> None: logging.info(f"""{self.__class__} POS: {event.xdata}, {event.ydata}""") @@ -111,14 +143,12 @@ def press_event(self, event: MouseEvent) -> None: return if self.mode == "add": - print("creating mode") self.points_to_add.append((event.xdata, event.ydata)) self.state.hull_set_point(-len(self.points_to_add), event.xdata, event.ydata) self.pointer_points.append( PointArtist.point(self.vm.ax, -len(self.points_to_add), facecolor="red", edgecolor="red") ) elif self.mode == "remove": - print("removing mode") if self.remove_line_state < 2: self.remove_line_state += 1 @@ -129,7 +159,22 @@ def press_event(self, event: MouseEvent) -> None: ) elif self.mode == "main": - return + if self.picked_event is None: + return + + if not hasattr(self.picked_event, "artist"): + return + if isinstance(self.picked_event.artist, LineCollection): + + hull_name, hull_cord = self.search_for_hull_name((event.xdata, event.ydata)) + if hull_name == "": + return + + if not self.picked_event.artist.get_visible(): + return + + self.picked_item = hull_name + self.vem.refresh() plt.draw() @@ -184,20 +229,30 @@ def find_closest_edge_point_in_hull(self, hull_name: str, point: tuple[float, fl def calculate_new_hull(self): # self.state -> doable - print("POINTS TO ADD") - self.points_to_add = self.points_to_add[:-1] - print(self.points_to_add) + # self.points_to_add = self.points_to_add[:-1] hull_name, hole_cord = self.search_for_hull_name_in_hole(self.points_to_add[0]) - print(hull_name) - print(hole_cord) final_cords = [] final_lines = [] if hull_name == "": - interpolated_points = interpolate_points(self.points_to_add) + + try: + interpolated_points = interpolate_points(self.points_to_add + [self.points_to_add[0]]) + except TypeError as e: + print(f"|ERROR| {e}") + + for i, pointer_point in enumerate(self.pointer_points): + pointer_point.remove() + self.state.hull_remove_point((i + 1) * (-1)) + + self.pointer_points = [] + + self.points_to_add = [] + return + polygon_lines = [ ( interpolated_points[j % len(interpolated_points)], @@ -228,6 +283,7 @@ def calculate_new_hull(self): plt.draw() return + hull_interpolated_points = self.state.get_hull_interpolated_cords(hull_name) lines = self.state.get_hull_lines_cords(hull_name) @@ -239,7 +295,20 @@ def calculate_new_hull(self): print(point_1, point_2) print(f"IDX: {idx_1} {idx_2}") - interpolated_points = interpolate_points(self.points_to_add[1:-1]) + try: + interpolated_points = interpolate_points(self.points_to_add[1:-1]) + except TypeError as e: + print(f"|ERROR| {e}") + + for i, pointer_point in enumerate(self.pointer_points): + pointer_point.remove() + self.state.hull_remove_point((i + 1) * (-1)) + + self.pointer_points = [] + + self.points_to_add = [] + return + for index, (line_point_1, line_point_2) in enumerate(lines): if ((point_1[0] == line_point_1[0] and point_1[1] == line_point_1[1]) @@ -314,7 +383,7 @@ def search_for_hull_name(self, point, _hull_name=""): continue for cord in cords: dist = np.sqrt((cord[0] - point[0]) ** 2 + (cord[1] - point[1]) ** 2) - if dist < 10 and dist < best_dist: + if dist < 5 and dist < best_dist: best_dist = dist best_hull_name = hull_name best_cord = cord @@ -339,7 +408,6 @@ def remove_line_from_hull(self): if hull_name_1 == hull_name_2: self.exec_remove_line_from_hull(hull_name_1, cord_1, cord_2) - self.vem.refresh() plt.draw() def exec_remove_line_from_hull(self, hull_name, point1, point2): @@ -386,7 +454,6 @@ def exec_remove_line_from_hull(self, hull_name, point1, point2): self.state.set_hull_lines_cords(hull_name, final_lines) HullArtist.hull(self.vm.ax, hull_name) - self.vem.refresh() plt.draw() def hide(self) -> None: @@ -397,3 +464,48 @@ def hide(self) -> None: @staticmethod def distance(point_1: tuple[float, float], point_2: tuple[float, float]) -> float: return np.sqrt((point_1[0] - point_2[0]) ** 2 + (point_1[1] - point_2[1]) ** 2) + + def hull_name_update(self) -> str: + if self.picked_item is None: + return '' + return self.picked_item + + + def hull_name_submit(self, nname) -> None: + if self.picked_item is None: + return + if nname != "": + print(f"nname: {nname}") + plt.draw() + + def spread_size_update(self) -> int: + if self.picked_item is None: + return '' + return self.state.get_hulls_closest_radius_param(self.picked_item) + + def spread_size_submit(self, radius: str) -> None: + try: + if self.picked_item is None: + return + radius = float(radius) + if radius < 0: + return + + if self.state.data['hulls_data']['hulls'][self.picked_item]["gathering_radius"] == radius: + return + + if isinstance(self.picked_event, LineCollection): + self.picked_event.remove() + else: + self.picked_event.artist.remove() + + + self.state.calc_and_add_one_hull(self.picked_item, self.state.get_cluster(self.picked_item), closest_points_radius=radius) + + self.picked_event = HullArtist.hull(self.vm.ax, self.picked_item).line_collection + + self.vem.refresh() + plt.draw() + except ValueError: + return + \ No newline at end of file diff --git a/src/generator/hull_generator.py b/src/generator/hull_generator.py index f9b5397..76515bb 100644 --- a/src/generator/hull_generator.py +++ b/src/generator/hull_generator.py @@ -344,8 +344,6 @@ def greedy_selecting_1(points): current_point = last_point # if second_point in first_points_set: - if i_array >= num_of_sets + 2: - break if current_point in current_points_array: straight_points += 1 @@ -372,6 +370,8 @@ def greedy_selecting_1(points): second_next_points_array = points[i_array % len(points)] outline_points.append(current_point) + if i_array >= num_of_sets + 3: + break return outline_points @@ -535,11 +535,16 @@ def join_closest_points_copy(points, circle_points_to_main, main_points_to_circl idxes = concave_hull_indexes( points_transform, - concavity=float("inf"), + concavity=1000, length_threshold=0, ) _idx_points = [points_transform[idxes[j]] for j in range(len(idxes))] + # _idx_points = make_dense_points( + # _idx_points, + # segment_length=segment_length, + # ) + new_points.append(_idx_points) return new_points @@ -597,6 +602,7 @@ def calc_hull( ) to_jarvis = join_closest_points(to_jarvis, circuit_points, main_points_of_circuit, 1, 0.1) + hulls[i]["gathering_radius"] = 1 selected_points = greedy_selecting_1(to_jarvis) @@ -631,16 +637,25 @@ def calc_hull( def set_hull_parameters( - state, circle_radious: float, points_in_circle: int, segment_length: int + state, circle_radious: float, points_in_circle: int, segment_length: float, domain_expansion: float, closest_points_radius: float ): state["hulls_data"]["parameters"] = { "circle_radious": circle_radious, "points_in_circle": points_in_circle, "segment_length": segment_length, + "domain_expansion": domain_expansion, + "closest_points_radius": closest_points_radius, } -def calc_one_hull(hull_name, points, state): +def calc_one_hull(hull_name, + points, + state, + circle_radious = None, + points_in_circle = None, + segment_length = None, + radius_domain_expansion = None, + closest_points_radius = None): hulls = {} hulls[hull_name] = {} @@ -650,7 +665,7 @@ def calc_one_hull(hull_name, points, state): # x = np.repeat(x, 2) # y = np.repeat(y, 2) hulls[hull_name]["cluster_points"] = {"x": x, "y": y} - print(f"HULL NAME {hull_name}") + # print(f"HULL NAME {hull_name}") points_transform = np.hstack( ( x.reshape(-1, 1), @@ -669,26 +684,37 @@ def calc_one_hull(hull_name, points, state): _idx_points = [points_transform[idxes[i]] for i in range(len(idxes))] _idx_points = sort_elements_in_concave_hull(_idx_points) + _num_of_points = state["hulls_data"]["parameters"]["points_in_circle"] if points_in_circle is None else points_in_circle + _circle_radious = state["hulls_data"]["parameters"]["circle_radious"] if circle_radious is None else circle_radious + points_transform, to_jarvis, circuit_points, main_points_of_circuit = convert_points( _idx_points, - state["hulls_data"]["parameters"]["circle_radious"], - num_of_points=state["hulls_data"]["parameters"]["points_in_circle"], + _circle_radious, + num_of_points=_num_of_points, ) - to_jarvis = join_closest_points_copy(to_jarvis, circuit_points, main_points_of_circuit, 1, 0.1) + + _closest_points_radius = state["hulls_data"]["parameters"]["closest_points_radius"] if closest_points_radius is None else closest_points_radius + to_jarvis = join_closest_points_copy(to_jarvis, circuit_points, main_points_of_circuit, _closest_points_radius, 0.1) - to_jarvis = handle_corners_points(to_jarvis, circuit_points,state["hulls_data"]["parameters"]["points_in_circle"]) + hulls[hull_name]["gathering_radius"] = _closest_points_radius + + # to_jarvis = handle_corners_points(to_jarvis, circuit_points,state["hulls_data"]["parameters"]["points_in_circle"]) selected_points = greedy_selecting_1(to_jarvis) + _radius_domain_expansion = state["hulls_data"]["parameters"]["domain_expansion"] if radius_domain_expansion is None else radius_domain_expansion + _idx_points = domain_expansion( - selected_points, circuit_points, drawing_point_radius=1.5, multiplier=1 + selected_points, circuit_points, drawing_point_radius=_radius_domain_expansion, multiplier=1 ) + _segment_length = state["hulls_data"]["parameters"]["segment_length"] if segment_length is None else segment_length + _idx_points = make_dense_points( _idx_points, - segment_length=state["hulls_data"]["parameters"]["segment_length"], + segment_length=_segment_length, ) hulls[hull_name]["polygon_points"] = _idx_points @@ -729,6 +755,7 @@ def parse_solution_to_editor_hull(hulls: dict, state: dict) -> dict: "cluster_points": hulls[i]["cluster_points"], "interpolate_points": hulls[i]["interpolate_points"], "hole_in_hulls": [], + "gathering_radius": hulls[i]["gathering_radius"], "artist": None } state["hulls_data"]["change"] = {} @@ -745,5 +772,4 @@ def parse_solution_to_editor_hull(hulls: dict, state: dict) -> dict: # 'val': hulls[i]["name"] # } - return state diff --git a/src/main.py b/src/main.py index 2960cac..acfca30 100644 --- a/src/main.py +++ b/src/main.py @@ -147,7 +147,14 @@ def draw_maps(raw_data: Union[str, Experiment], # hulls generator state_dict['hulls_data']['render_name'] = 1 state_dict["hulls_data"]["view_state"] = True - set_hull_parameters(state_dict, 0.1, 20, 20) + + set_hull_parameters(state=state_dict, + circle_radious=0.1, + points_in_circle=20, + segment_length=10, + domain_expansion=1.5, + closest_points_radius=2) + hulls = calc_hull(normalized_data, 0.1, 20, 20) state_dict = parse_solution_to_editor_hull(hulls, state_dict) state_dict["hulls_data"]['line_size'] = 1.0 From 822895fdf44501dbcb6e01f6656d87f8986c11f7 Mon Sep 17 00:00:00 2001 From: Panyloi Date: Mon, 2 Dec 2024 12:16:37 +0100 Subject: [PATCH 3/7] Save removed hulls by toggle list --- src/editor/views/home_view.py | 14 +++++++++++++- src/editor/views/hulls_view.py | 9 ++++++--- src/editor/views/labels_view.py | 13 ++++++++++++- 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/src/editor/views/home_view.py b/src/editor/views/home_view.py index af0186d..d321d5f 100644 --- a/src/editor/views/home_view.py +++ b/src/editor/views/home_view.py @@ -14,13 +14,25 @@ def __init__(self, view_manager: ViewManager) -> None: def draw(self, *args, **kwargs) -> None: super().draw() - self.state.show_labels_and_hulls(self.vm.ax) + self.state.show_labels(self.vm.ax) + self.draw_hull() self.vm.list_manager.hide_button() self.vm.ax.set_xlim(-190, 190) self.vm.ax.set_ylim(-150, 150) plt.draw() + def draw_hull(self): + self.hulls_off = False + HullArtist.remove_hulls(self.vm.ax) + + selected_hulls = self.vm.list_manager.get_only_active() + for hull_name in self.state.data['hulls_data']['hulls'].keys(): + artist: HullArtist = HullArtist.hull(self.vm.ax, hull_name) + if artist.id not in selected_hulls: + artist.hide() + + def hide(self) -> None: self.vm.list_manager.show_button() super().hide() diff --git a/src/editor/views/hulls_view.py b/src/editor/views/hulls_view.py index 1a9b461..2d14ac9 100644 --- a/src/editor/views/hulls_view.py +++ b/src/editor/views/hulls_view.py @@ -47,8 +47,6 @@ def draw(self, *args, **kwargs) -> None: super().draw() self.state.hide_labels_and_hulls(self.vm.ax) - HullArtist.show_hulls(self.vm.ax) - # self.state.show_labels(self.vm.ax) self.confirm_button.hide() self.cancel_button.hide() self.reset_button.hide() @@ -57,9 +55,10 @@ def draw(self, *args, **kwargs) -> None: self.vm.ax.set_xlim(df['x'].min() - 10, df['x'].max() + 10) self.vm.ax.set_ylim(df['y'].min() - 10, df['y'].max() + 10) + not_visible_hull_names = set(self.state.data['hulls_data']['hulls'].keys()).difference(set(self.vm.list_manager.get_only_active())) + print(f"|not_visible_hull_names|: {not_visible_hull_names}") # remove current hulls for child in self.vm.ax.get_children(): - print(child) if type(child) is LineCollection: child.remove() @@ -67,6 +66,10 @@ def draw(self, *args, **kwargs) -> None: for hull_name in self.state.data['hulls_data']['hulls'].keys(): HullArtist.hull(self.vm.ax, hull_name) + for not_visible_hull_name in not_visible_hull_names: + artist = HullArtist.get_artist_by_id(not_visible_hull_name) + artist.hide() + # events self.cem.add(SharedEvent('pick_event', self.pick_event)) self.cem.add(SharedEvent('button_press_event', self.press_event)) diff --git a/src/editor/views/labels_view.py b/src/editor/views/labels_view.py index 9d1a231..db1a0fb 100644 --- a/src/editor/views/labels_view.py +++ b/src/editor/views/labels_view.py @@ -39,7 +39,9 @@ def __init__(self, view_manager: ViewManager) -> None: def draw(self, *args, **kwargs) -> None: super().draw() - self.state.show_labels_and_hulls(self.vm.ax) + # self.state.show_labels_and_hulls(self.vm.ax) + self.state.show_labels(self.vm.ax) + self.draw_hull() self.vm.ax.set_xlim(-190, 190) self.vm.ax.set_ylim(-150, 150) @@ -59,6 +61,15 @@ def draw(self, *args, **kwargs) -> None: self.vem.refresh() plt.draw() + def draw_hull(self): + HullArtist.remove_hulls(self.vm.ax) + + selected_hulls = self.vm.list_manager.get_only_active() + for hull_name in self.state.data['hulls_data']['hulls'].keys(): + artist: HullArtist = HullArtist.hull(self.vm.ax, hull_name) + if artist.id not in selected_hulls: + artist.hide() + def hide(self) -> None: super().hide() From cd7468e2772c9bd0ae110ce606da05b23c16d7bf Mon Sep 17 00:00:00 2001 From: Panyloi Date: Mon, 2 Dec 2024 16:12:19 +0100 Subject: [PATCH 4/7] Hulls not doing puf puf --- src/editor/state.py | 20 ++++++++++++++++---- src/editor/view_manager.py | 2 ++ src/editor/views/clusters_view.py | 4 ++-- src/editor/views/hulls_view.py | 8 ++++++-- src/generator/hull_generator.py | 1 + 5 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/editor/state.py b/src/editor/state.py index 89f6b23..f27c436 100644 --- a/src/editor/state.py +++ b/src/editor/state.py @@ -261,9 +261,17 @@ def add_hull(self, hull_name: str, cords = [], line_cords = [], cluster_points = "cluster_points": cluster_points, "interpolate_points": interpolate_points, "hole_in_hulls": [], + "gathering_radius": 1, "artist": None } + def add_self_made_hull(self, hull_name): + self.data['hulls_data']['self_drawn'].append(hull_name) + + def remove_self_made_hull(self, hull_name): + if hull_name in self.data['hulls_data']['self_drawn']: + self.data['hulls_data']['self_drawn'].remove(hull_name) + def get_hull_view_state(self) -> bool: return self.data["hulls_data"]["view_state"] @@ -277,11 +285,15 @@ def get_hole_in_hulls(self, hull_name: str) -> None: return self.data['hulls_data']['hulls'][hull_name]['hole_in_hulls'] def get_hulls_render_name(self) -> None: - return self.data['hulls_data']['render_name'] + 1 + self.data['hulls_data']['render_name'] += 1 + return self.data['hulls_data']['render_name'] def get_hulls_closest_radius_param(self, hull_name): return self.data['hulls_data']['hulls'][hull_name]['gathering_radius'] + def get_hulls_created_by_hand(self): + return self.data['hulls_data']['self_drawn'] + # ------------------------------- HULLS SETTERS ------------------------------ # def set_hull_to_undraw(self, hull_name): @@ -319,9 +331,9 @@ def hull_remove_point(self, point_id) -> None: df = self.data['clusters_data']['points'] self.data['clusters_data']['points'] = df.drop(point_id) - df_1 = self.data['clusters_data']['colors'] - if 'mine' in df_1.keys(): - del df_1['mine'] + + if 'mine' in self.data['clusters_data']['colors'].keys(): + del self.data['clusters_data']['colors']['mine'] def set_hull_polygon_cords(self, hull_name: str, new_cords) -> None: self.data['hulls_data']['hulls'][hull_name]['cords'] = new_cords diff --git a/src/editor/view_manager.py b/src/editor/view_manager.py index 973c707..ebc4946 100644 --- a/src/editor/view_manager.py +++ b/src/editor/view_manager.py @@ -941,6 +941,8 @@ def uncheck_all(self) -> None: def get_actives(self) -> dict[str: bool]: labels = [label.get_text() for label in self.ref.labels] + print("|LABELS|") + print(labels) return dict(zip(labels, self.ref.get_status())) def update(self, labels: list[str]) -> None: diff --git a/src/editor/views/clusters_view.py b/src/editor/views/clusters_view.py index fd71d9a..1c06e59 100644 --- a/src/editor/views/clusters_view.py +++ b/src/editor/views/clusters_view.py @@ -76,7 +76,7 @@ def reset_clusters(self): #todo really ugly, gotta clean up later HullArtist.remove_hulls(self.vm.ax) self.state.update_hulls() - self.vm.list_manager.check_list.update(sorted(list(self.state.get_all_clusters().keys()))) + self.vm.list_manager.check_list.update(sorted(list(self.state.get_all_clusters().keys()) + self.state.get_hulls_created_by_hand())) selected_hulls = self.vm.list_manager.get_only_active() for hull_name in self.state.get_normalised_clusters(): artist = HullArtist.hull(self.vm.ax, hull_name) @@ -142,7 +142,7 @@ def draw_hull(self): self.state.update_hulls() self.vm.list_manager.show_button() - self.vm.list_manager.check_list.update(sorted(list(self.state.get_all_clusters().keys()))) + self.vm.list_manager.check_list.update(sorted(list(self.state.get_all_clusters().keys()) + self.state.get_hulls_created_by_hand())) selected_hulls = self.vm.list_manager.get_only_active() for hull_name in self.state.data['hulls_data']['hulls'].keys(): artist: HullArtist = HullArtist.hull(self.vm.ax, hull_name) diff --git a/src/editor/views/hulls_view.py b/src/editor/views/hulls_view.py index 2d14ac9..187e9da 100644 --- a/src/editor/views/hulls_view.py +++ b/src/editor/views/hulls_view.py @@ -269,17 +269,21 @@ def calculate_new_hull(self): next_name = self.state.get_hulls_render_name() _name = f'hull_{next_name}' - print(_name) - self.state.set_hulls_render_name = next_name self.state.add_hull(hull_name=_name, line_cords=final_lines, interpolate_points=final_cords) HullArtist.hull(self.vm.ax, _name) + + # selected_hulls = self.vm.list_manager.get_only_active() for i, pointer_point in enumerate(self.pointer_points): pointer_point.remove() self.state.hull_remove_point((i + 1) * (-1)) + self.state.add_self_made_hull(_name) + # self.vm.list_manager.check_list.update(['_name']) + self.vm.list_manager.check_list.update(sorted(list(self.state.get_all_clusters().keys()) + self.state.get_hulls_created_by_hand())) + self.pointer_points = [] self.points_to_add = [] diff --git a/src/generator/hull_generator.py b/src/generator/hull_generator.py index 76515bb..d1c7ee3 100644 --- a/src/generator/hull_generator.py +++ b/src/generator/hull_generator.py @@ -742,6 +742,7 @@ def calc_one_hull(hull_name, def parse_solution_to_editor_hull(hulls: dict, state: dict) -> dict: state["hulls_data"]["hulls"] = {} + state["hulls_data"]['self_drawn'] = [] for i in hulls.keys(): # state['hulls_data'][i] = { # 'name': hulls[i]['name'], From c9a4c74ce7340d3b120b7d0b4b8788836973c9ae Mon Sep 17 00:00:00 2001 From: Panyloi Date: Mon, 2 Dec 2024 16:15:04 +0100 Subject: [PATCH 5/7] Cosmetics --- src/editor/view_manager.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/editor/view_manager.py b/src/editor/view_manager.py index ebc4946..973c707 100644 --- a/src/editor/view_manager.py +++ b/src/editor/view_manager.py @@ -941,8 +941,6 @@ def uncheck_all(self) -> None: def get_actives(self) -> dict[str: bool]: labels = [label.get_text() for label in self.ref.labels] - print("|LABELS|") - print(labels) return dict(zip(labels, self.ref.get_status())) def update(self, labels: list[str]) -> None: From 1c1ffc7e66a81a01514368d96fda33e34b0f1b91 Mon Sep 17 00:00:00 2001 From: Panyloi Date: Mon, 2 Dec 2024 16:16:00 +0100 Subject: [PATCH 6/7] Cosmetics v2 --- src/editor/view_manager.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/editor/view_manager.py b/src/editor/view_manager.py index 973c707..94f3eb2 100644 --- a/src/editor/view_manager.py +++ b/src/editor/view_manager.py @@ -951,7 +951,7 @@ def update(self, labels: list[str]) -> None: self.ref = CheckButtons(self.ax, labels, new_actives) self.ref.on_clicked(self.callback) self.hide() # clear "exes" that still show - self.show() + # self.show() def remove(self) -> None: super().remove() From 1d730efa890a64fd79b9243d24fdef96d44dcae9 Mon Sep 17 00:00:00 2001 From: Panyloi Date: Fri, 20 Dec 2024 21:44:53 +0100 Subject: [PATCH 7/7] Removing hulls repair and cosmetic changes --- src/editor/views/hulls_view.py | 32 +++++++++++++++++++++++++------- src/main.py | 2 +- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/editor/views/hulls_view.py b/src/editor/views/hulls_view.py index 187e9da..dc681a6 100644 --- a/src/editor/views/hulls_view.py +++ b/src/editor/views/hulls_view.py @@ -330,7 +330,6 @@ def calculate_new_hull(self): if idx_1 < idx_2: first_part_hull_points = hull_interpolated_points[:idx_1 + 1] second_part_hull_points = hull_interpolated_points[idx_2:] - line_first_part_hull_points = lines[:line_start_index + 1] line_second_part_hull_points = lines[line_end_index:] @@ -342,14 +341,33 @@ def calculate_new_hull(self): for j in range(len(interpolated_points) - 1) ] - print(polygon_lines) - final_cords = first_part_hull_points + interpolated_points + second_part_hull_points final_lines = (line_first_part_hull_points - + [((line_first_part_hull_points[-1][1]), (polygon_lines[0][0]))] - + polygon_lines - + [((polygon_lines[-1][1]), (line_second_part_hull_points[0][0]))] - + line_second_part_hull_points) + + [((line_first_part_hull_points[-1][1]), (polygon_lines[0][0]))] + + polygon_lines + + [((polygon_lines[-1][1]), (line_second_part_hull_points[0][0]))] + + line_second_part_hull_points) + + else: + part_hull_points = hull_interpolated_points[idx_2:idx_1 + 1] + line_part_hull_points = lines[line_end_index:line_start_index + 1] + + polygon_lines = [ + ( + interpolated_points[j % len(interpolated_points)], + interpolated_points[(j + 1) % len(interpolated_points)], + ) + for j in range(len(interpolated_points) - 1) + ] + + final_cords = part_hull_points + interpolated_points + final_lines = (line_part_hull_points + + [((line_part_hull_points[-1][1]), (polygon_lines[0][0]))] + + polygon_lines + + [((polygon_lines[-1][1]), (line_part_hull_points[0][0]))] + ) + + hull_artist = HullArtist.get_line_by_id(hull_name) hull_artist.remove() diff --git a/src/main.py b/src/main.py index 96e3d19..ba134af 100644 --- a/src/main.py +++ b/src/main.py @@ -155,7 +155,7 @@ def draw_maps(raw_data: Union[str, Experiment], points_in_circle=20, segment_length=10, domain_expansion=1.5, - closest_points_radius=2) + closest_points_radius=1) if Configuration['global']['generate_hulls']: hulls = calc_hull(normalized_data, 0.1, 20, 20)