From afd8efac5c6be2aefac665b8686208a67d19cd22 Mon Sep 17 00:00:00 2001 From: Karishma Kumar Date: Sat, 25 Jan 2025 01:25:13 +0100 Subject: [PATCH] m to pixels --- src/odemis/gui/comp/overlay/cryo_feature.py | 111 ++++++-------------- src/odemis/gui/cont/correlation.py | 32 +++--- src/odemis/gui/util/conversion.py | 20 ++++ 3 files changed, 72 insertions(+), 91 deletions(-) diff --git a/src/odemis/gui/comp/overlay/cryo_feature.py b/src/odemis/gui/comp/overlay/cryo_feature.py index d0a82ff8e2..6a12922ffe 100644 --- a/src/odemis/gui/comp/overlay/cryo_feature.py +++ b/src/odemis/gui/comp/overlay/cryo_feature.py @@ -35,6 +35,7 @@ from odemis.gui.comp.overlay.base import DragMixin, WorldOverlay from odemis.gui.comp.overlay.stage_point_select import StagePointSelectOverlay from odemis.gui.model import TOOL_FEATURE, TOOL_NONE, TOOL_FIDUCIAL, TOOL_REGION_OF_INTEREST +from odemis.gui.util.conversion import pixel_pos_to_canvas_pos, canvas_pos_to_pixel_pos MODE_EDIT_FEATURES = 1 MODE_SHOW_FEATURES = 2 @@ -425,10 +426,9 @@ def _update_selected_target_position(self, v_pos): Update the selected target with the newly moved position :param v_pos: (int, int) the coordinates in the view """ - p_pos = self.cnvs.view_to_phys(v_pos, self.cnvs.get_half_buffer_size()) - # todo - self._selected_target.coordinates.value = tuple((p_pos[0], p_pos[1], 0)) #Todo - # Reset the selected tool to signal end of target moving operation + p_pos = canvas_pos_to_pixel_pos(self.cnvs.view_to_phys(v_pos, self.cnvs.get_half_buffer_size()), 1e-07) + self._selected_feature.pos.value = [p_pos[0], p_pos[1], self._selected_feature.pos.value[2]] + # Reset the selected tool to signal end of feature moving operation # self._selected_tool_va.value = TOOL_NONE self.cnvs.update_drawing() @@ -439,62 +439,13 @@ def _detect_point_inside_target(self, v_pos): :return: (CryoFeature or None) Found target, None if not found """ - def in_radius(c_x, c_y, r, x, y): - return math.hypot(c_x - x, c_y - y) <= r - - offset = self.cnvs.get_half_buffer_size() # to convert physical target positions to pixels - for target in self.tab_data.main.targets.value: - coordinates = target.coordinates.value - fvsp = self.cnvs.phys_to_view(coordinates, offset) - if in_radius(fvsp[0], fvsp[1], FEATURE_DIAMETER, v_pos[0], v_pos[1]): - return target + pass def draw(self, ctx, shift=(0, 0), scale=1.0): """ Draw all the targets, on their location, indicating their status and whether it's selected or hovered on. """ - return - if not self.show: - return - - - # Check if the current view is "FLM Overview" - # if hasattr(self.tab_data, "focussedView"): - # current_view = self.tab_data.focussedView.value.name.value - # if current_view != "SEM Overview": - # return - - # Show each target icon and label if applicable - for target in self.tab_data.main.targets.value: - coordinates = target.coordinates.value - half_size_offset = self.cnvs.get_half_buffer_size() - - # convert physical position to buffer 'world' coordinates - bpos = self.cnvs.phys_to_buffer_pos((coordinates[0], coordinates[1]), self.cnvs.p_buffer_center, self.cnvs.scale, - offset=half_size_offset) - - def set_icon(feature_icon): - ctx.set_source_surface(feature_icon, bpos[0] - FEATURE_ICON_CENTER, bpos[1] - FEATURE_ICON_CENTER) - - # Show proper feature icon based on selected target + status - try: - if target is self.tab_data.main.currentTarget.value: - set_icon(self._feature_icons_selected[target.type.value]) - # set_icon(self._feature_icons_selected[target.status.value]) - else: - set_icon(self._feature_icons[target.type.value]) - # set_icon(self._feature_icons[target.status.value]) - except KeyError: - raise - # logging.error("Feature status for feature {} is not one of the predefined statuses.".format(feature.name.value)) - - if target is self._hover_target: - # show target name on hover - self._label.text = target.name.value # str(target.index.value) - self._label.pos = (bpos[0], bpos[1]) - self._label.draw(ctx) - - ctx.paint() + pass class CryoCorrelationFmPointsOverlay(CryoCorrelationPointsOverlay): @@ -511,7 +462,7 @@ def in_radius(c_x, c_y, r, x, y): offset = self.cnvs.get_half_buffer_size() # to convert physical target positions to pixels for target in self.tab_data.main.targets.value: if "FM" in target.name.value or "POI" in target.name.value: - coordinates = target.coordinates.value + coordinates = pixel_pos_to_canvas_pos(target.coordinates.value, scale = 1e-07) fvsp = self.cnvs.phys_to_view(coordinates, offset) if in_radius(fvsp[0], fvsp[1], FEATURE_DIAMETER, v_pos[0], v_pos[1]): return target @@ -533,11 +484,13 @@ def on_left_down(self, evt): self.cnvs.set_dynamic_cursor(gui.DRAG_CURSOR) else: # create new target based on the physical position then disable the target tool - p_pos = self.cnvs.view_to_phys(v_pos, self.cnvs.get_half_buffer_size()) + p_pos = canvas_pos_to_pixel_pos(self.cnvs.view_to_phys(v_pos, self.cnvs.get_half_buffer_size()), + 1e-07) + # p_pos = self.cnvs.view_to_phys(v_pos, self.cnvs.get_half_buffer_size()) # if self.tab_data.main.selected_target_type is None: # self.tab_data.main.selected_target_type.value = "Fiducial" self.tab_data.add_new_target(p_pos[0], p_pos[1], - type=self.tab_data.main.selected_target_type.value) # TODO + type=self.tab_data.main.selected_target_type.value) # self._selected_tool_va.value = TOOL_NONE else: @@ -582,10 +535,10 @@ def on_motion(self, evt): if self.left_dragging: self.cnvs.set_dynamic_cursor(gui.DRAG_CURSOR) DragMixin._on_motion(self, evt) - p_pos = self.cnvs.view_to_phys(v_pos, self.cnvs.get_half_buffer_size()) + p_pos = canvas_pos_to_pixel_pos(self.cnvs.view_to_phys(v_pos, self.cnvs.get_half_buffer_size()), 1e-07) + # p_pos = self.cnvs.view_to_phys(v_pos, self.cnvs.get_half_buffer_size()) # self._selected_target = self.tab_data.main.currentTarget.value - self._selected_target.coordinates.value = tuple( - (p_pos[0], p_pos[1], 0)) # TODO self._selected_target.coordinates.value[2])) + self._selected_target.coordinates.value = [p_pos[0], p_pos[1], self._selected_target.coordinates.value[2]] self.cnvs.update_drawing() return target = self._detect_point_inside_target(v_pos) @@ -619,7 +572,8 @@ def draw(self, ctx, shift=(0, 0), scale=1.0): # Show each target icon and label if applicable for target in self.tab_data.main.targets.value: if "FM" in target.name.value or "POI" in target.name.value: - coordinates = target.coordinates.value + coordinates = pixel_pos_to_canvas_pos(target.coordinates.value, scale=1e-07) + # coordinates = target.coordinates.value half_size_offset = self.cnvs.get_half_buffer_size() # convert physical position to buffer 'world' coordinates @@ -668,7 +622,8 @@ def in_radius(c_x, c_y, r, x, y): offset = self.cnvs.get_half_buffer_size() # to convert physical target positions to pixels for target in self.tab_data.main.targets.value: if "FIB" in target.name.value: - coordinates = target.coordinates.value + coordinates = pixel_pos_to_canvas_pos(target.coordinates.value, scale=1e-07) + # coordinates = target.coordinates.value fvsp = self.cnvs.phys_to_view(coordinates, offset) if in_radius(fvsp[0], fvsp[1], FEATURE_DIAMETER, v_pos[0], v_pos[1]): return target @@ -680,13 +635,13 @@ def on_motion(self, evt): if self.left_dragging: self.cnvs.set_dynamic_cursor(gui.DRAG_CURSOR) DragMixin._on_motion(self, evt) - p_pos = self.cnvs.view_to_phys(v_pos, self.cnvs.get_half_buffer_size()) + p_pos = canvas_pos_to_pixel_pos(self.cnvs.view_to_phys(v_pos, self.cnvs.get_half_buffer_size()), 1e-07) + # p_pos = self.cnvs.view_to_phys(v_pos, self.cnvs.get_half_buffer_size()) if self._mode == MODE_EDIT_REFRACTIVE_INDEX: - self.tab_data.fib_surface_point.value.coordinates.value = tuple( - (p_pos[0], p_pos[1], 0)) + self.tab_data.fib_surface_point.value.coordinates.value = [p_pos[0], p_pos[1], int(0)] # self._selected_target = self.tab_data.main.currentTarget.value else: - self._selected_target.coordinates.value = tuple((p_pos[0], p_pos[1], 0)) # TODO self._selected_target.coordinates.value[2])) + self._selected_target.coordinates.value = [p_pos[0], p_pos[1], self._selected_target.coordinates.value[2]] self.cnvs.update_drawing() return target = self._detect_point_inside_target(v_pos) @@ -711,13 +666,13 @@ def on_left_down(self, evt): if self.active.value: v_pos = evt.Position target = self._detect_point_inside_target(v_pos) - p_pos = self.cnvs.view_to_phys(v_pos, self.cnvs.get_half_buffer_size()) + p_pos = canvas_pos_to_pixel_pos(self.cnvs.view_to_phys(v_pos, self.cnvs.get_half_buffer_size()), 1e-07) + # p_pos = self.cnvs.view_to_phys(v_pos, self.cnvs.get_half_buffer_size()) if self._mode == MODE_EDIT_REFRACTIVE_INDEX: # Todo what does mode do if self.tab_data.fib_surface_point.value: # add/modify fib_surface_fiducial - self.tab_data.fib_surface_point.value.coordinates.value = tuple( - (p_pos[0], p_pos[1], 0)) # TODO self._selected_target.coordinates.value[2])) + self.tab_data.fib_surface_point.value.coordinates.value = [p_pos[0], p_pos[1], int(0)] self.cnvs.set_dynamic_cursor(gui.DRAG_CURSOR) else: self.tab_data.add_new_target(p_pos[0], p_pos[1], type=self.tab_data.main.selected_target_type.value) @@ -761,9 +716,10 @@ def on_left_up(self, evt): if self.left_dragging: #TODO separate concerns if and else are not looking for same conditiond if self._mode == MODE_EDIT_REFRACTIVE_INDEX: - p_pos = self.cnvs.view_to_phys(evt.Position, self.cnvs.get_half_buffer_size()) - self.tab_data.fib_surface_point.value.coordinates.value = tuple( - (p_pos[0], p_pos[1], 0)) # TODO self._selected_target.coordinates.value[2])) + p_pos = canvas_pos_to_pixel_pos(self.cnvs.view_to_phys(evt.Position, self.cnvs.get_half_buffer_size()), + 1e-07) + # p_pos = self.cnvs.view_to_phys(evt.Position, self.cnvs.get_half_buffer_size()) + self.tab_data.fib_surface_point.value.coordinates.value = [p_pos[0], p_pos[1], int(0)] self.cnvs.update_drawing() else: if self._selected_target: @@ -791,7 +747,8 @@ def draw(self, ctx, shift=(0, 0), scale=1.0): # Show each target icon and label if applicable for target in self.tab_data.main.targets.value: if "FIB" in target.name.value:# or "Projected" in target.name.value: - coordinates = target.coordinates.value + coordinates = pixel_pos_to_canvas_pos(target.coordinates.value, scale=1e-07) + # coordinates = target.coordinates.value half_size_offset = self.cnvs.get_half_buffer_size() # convert physical position to buffer 'world' coordinates @@ -826,7 +783,8 @@ def set_icon(feature_icon): ctx.paint() if self.tab_data.fib_surface_point.value: - coordinates = self.tab_data.fib_surface_point.value.coordinates.value + coordinates = pixel_pos_to_canvas_pos(self.tab_data.fib_surface_point.value.coordinates.value, scale=1e-07) + # coordinates = self.tab_data.fib_surface_point.value.coordinates.value half_size_offset = self.cnvs.get_half_buffer_size() bpos = self.cnvs.phys_to_buffer_pos((coordinates[0], coordinates[1]), self.cnvs.p_buffer_center, self.cnvs.scale, @@ -839,7 +797,8 @@ def set_icon(feature_icon): ctx.paint() for target in self.tab_data.projected_points: - coordinates = target.coordinates.value + coordinates = pixel_pos_to_canvas_pos(target.coordinates.value, scale=1e-07) + # coordinates = target.coordinates.value half_size_offset = self.cnvs.get_half_buffer_size() # convert physical position to buffer 'world' coordinates diff --git a/src/odemis/gui/cont/correlation.py b/src/odemis/gui/cont/correlation.py index 54ede02b7f..09236c8e6a 100644 --- a/src/odemis/gui/cont/correlation.py +++ b/src/odemis/gui/cont/correlation.py @@ -487,7 +487,10 @@ class GridColumns(Enum): DEBUG = False class CorrelationPointsController(object): - + # TODO reset output + # save and show output + # POI > 1 + # xyz changes def __init__(self, tab_data, panel, tab, viewports): """ :param tab_data: (MicroscopyGUIData) the representation of the microscope GUI @@ -546,9 +549,9 @@ def __init__(self, tab_data, panel, tab, viewports): # Set column labels for correlation points # Set the data type and if the column can be edited self.grid.SetColLabelValue(GridColumns.Type.value, GridColumns.Type.name) - attr = wx.grid.GridCellAttr() - attr.SetReadOnly(True) - self.grid.SetColAttr(0, attr) + # attr = wx.grid.GridCellAttr() + # attr.SetReadOnly(True) + # self.grid.SetColAttr(0, attr) # self.grid.SetColLabelValue(1, "X") # self.grid.SetColLabelValue(2, "Y") # self.grid.SetColLabelValue(3, "Z") @@ -934,11 +937,12 @@ def on_cell_changing(self, event): vp.canvas.update_drawing() # Todo KEEP 1 POI IN CRYO_FEATURE # \TODO pos load error + # pencin in fib view except ValueError: wx.MessageBox("Index must be a int!", "Invalid Input", wx.OK | wx.ICON_ERROR) event.Veto() # Prevent the change return - elif col_name in [GridColumns.X.name, GridColumns.Y.name, GridColumns.Z.name]: + elif col_name in [GridColumns.X.name, GridColumns.Y.name]: #, GridColumns.Z.name]: try: p = float(new_value) # if Z and FIB target, do not allow the change, before calling this function TODO @@ -1015,7 +1019,7 @@ def _on_current_coordinates_changes(self, coordinates): # self.grid.SelectRow(row) self.grid.SetCellValue(row, GridColumns.X.value, str(target.coordinates.value[0])) self.grid.SetCellValue(row, GridColumns.Y.value, str(target.coordinates.value[1])) - if target.coordinates.value[2]: + if "FIB" not in target.name.value: self.grid.SetCellValue(row, GridColumns.Z.value, str(target.coordinates.value[2])) self.update_feature_correlation_target() @@ -1075,16 +1079,14 @@ def _on_target_changes(self, targets): self.grid.SetCellValue(current_row_count, GridColumns.X.value, str(target.coordinates.value[0])) self.grid.SetCellValue(current_row_count, GridColumns.Y.value, str(target.coordinates.value[1])) self.grid.SetCellValue(current_row_count, GridColumns.Index.value, str(target.index.value)) + self.grid.SetCellValue(current_row_count, GridColumns.Type.value, target.name.value) + + + if "FIB" in target.name.value: + self.grid.SetCellValue(current_row_count, GridColumns.Z.value, "") + else: + self.grid.SetCellValue(current_row_count, GridColumns.Z.value, str(target.coordinates.value[2])) - if target.type.value == "Fiducial": - if target.coordinates.value[2]: - self.grid.SetCellValue(current_row_count, GridColumns.Type.value, target.name.value) - self.grid.SetCellValue(current_row_count, GridColumns.Z.value, str(target.coordinates.value[2])) - else: - self.grid.SetCellValue(current_row_count, GridColumns.Type.value, target.name.value) - self.grid.SetCellValue(current_row_count, GridColumns.Z.value, "") - elif target.type.value == "RegionOfInterest": - self.grid.SetCellValue(current_row_count, GridColumns.Type.value, target.name.value) # self.grid.Layout() self.reorder_table() diff --git a/src/odemis/gui/util/conversion.py b/src/odemis/gui/util/conversion.py index fb4b8ce37f..2441867771 100644 --- a/src/odemis/gui/util/conversion.py +++ b/src/odemis/gui/util/conversion.py @@ -25,6 +25,26 @@ from odemis.util.conversion import (frgb_to_rgb, hex_to_frgb, hex_to_rgb, rgb_to_frgb) +def canvas_pos_to_pixel_pos(canvas_pos, scale, offset=(0, 0)): + """ Convert canvas position to pixel position + :param canvas_pos: (float, float) + :param scale: float + :param offset: (float, float) + :return: (float, float) + """ + return ((canvas_pos[0] - offset[0]) / scale, + (canvas_pos[1] - offset[1]) / scale) + +def pixel_pos_to_canvas_pos(pixel_pos, scale, offset = (0,0)): + """ Convert pixel position to canvas position + :param pixel_pos: (float, float) + :param scale: float + :param offset: (float, float) + :return: (float, float) + """ + return (pixel_pos[0] * scale + offset[0], + pixel_pos[1] * scale + offset[1]) + def wxcol_to_rgb(wxcol): """ Convert a wx.Colour to an RGB int tuple