Created
February 10, 2019 00:11
-
-
Save sofroniewn/6b3cecf77422edff63764c5d50059172 to your computer and use it in GitHub Desktop.
align and distribute code for napari
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def align_shapes(self, index=True, axis=0, location=1, to_canvas=False): | |
| """Aligns selected shapes either in horizontal or vertical axis to | |
| the left, center, right, or top, center, bottom. If to_canvas is True | |
| then it aligns to the canvas. | |
| Parameters | |
| ---------- | |
| index : bool, list, int | |
| index of objects to be selected. Where True corresponds to all | |
| objects, a list of integers to a list of objects, and a single | |
| integer to that particular object. | |
| axis : int | |
| integer specifying axis to align along. 0 corresponds to horizontal | |
| 1 corresponds to vertical | |
| location : int | |
| location that objects are to be aligned too. One of 0, 1, 2 for | |
| either left, center, right, or top, center, bottom for Horizontal | |
| and vertical axes respectively. | |
| to_canvas : bool | |
| Bool specifying if to align to canvas or not. | |
| """ | |
| if to_canvas: | |
| max_shape = self.viewer.dimensions.max_shape | |
| box = self.data._expand_box(np.array([[0, 0], max_shape[:2]])) | |
| else: | |
| self.data.select_box(index) | |
| box = self.data.selected_box | |
| coords = [[7, 8, 3], [1, 8, 5]] | |
| coord = coords[axis][location] | |
| align_point = box[coord][axis] | |
| if index is True: | |
| index = list(range(len(self.data.id))) | |
| if type(index) is list: | |
| for i in index: | |
| self.data.select_box(i) | |
| box = self.data.selected_box | |
| shift = [0, 0] | |
| shift[axis] = align_point - box[coord][axis] | |
| self.data.shift_shapes(shift, index=i) | |
| else: | |
| self.data.select_box(index) | |
| box = self.data.selected_box | |
| shift = [0, 0] | |
| shift[axis] = align_point - box[coord][axis] | |
| self.data.shift_shapes(shift, index=index) | |
| self.refresh() | |
| def distribute_shapes(self, index=True, axis=0, location=1, to_canvas=False): | |
| """Distributes selected shapes either in horizontal or vertical axis to | |
| the left, center, right, or top, center, bottom. If to_canvas is True | |
| then it dispributes along the canvas. | |
| Parameters | |
| ---------- | |
| index : bool, list, int | |
| index of objects to be selected. Where True corresponds to all | |
| objects, a list of integers to a list of objects, and a single | |
| integer to that particular object. | |
| axis : int | |
| integer specifying axis to align along. 0 corresponds to horizontal | |
| 1 corresponds to vertical | |
| location : int | |
| location that objects are to be aligned too. One of 0, 1, 2 for | |
| either left, center, right, or top, center, bottom for Horizontal | |
| and vertical axes respectively. | |
| to_canvas : bool | |
| Bool specifying if to align to canvas or not. | |
| """ | |
| if to_canvas: | |
| max_shape = self.viewer.dimensions.max_shape | |
| box = self.data._expand_box(np.array([[0, 0], max_shape[:2]])) | |
| else: | |
| self.data.select_box(index) | |
| box = self.data.selected_box | |
| coords = [[7, 8, 3], [1, 8, 5]] | |
| coord = coords[axis][location] | |
| align_points = [box[coords[axis][0]][axis], box[coords[axis][2]][axis]] | |
| if index is True: | |
| index = list(range(len(self.data.id))) | |
| if type(index) is list: | |
| num_objects = len(index) | |
| align_points = np.linspace(align_points[0], align_points[1], num_objects) | |
| offsets = [] | |
| for obj_ind in index: | |
| self.data.select_box(obj_ind) | |
| box = self.data.selected_box | |
| offsets.append(box[coord][axis]) | |
| offsets = np.array(offsets) | |
| order = np.argsort(offsets) | |
| for i in range(len(index)): | |
| obj_ind = index[order[i]] | |
| shift = [0, 0] | |
| shift[axis] = align_points[i] - offsets[order[i]] | |
| self.data.shift_shapes(shift, index=index[order[i]]) | |
| else: | |
| align_points = (align_points[0] + align_points[1])/2 | |
| self.data.select_box(index) | |
| box = self.data.selected_box | |
| shift = [0, 0] | |
| shift[axis] = align_points - box[coord][axis] | |
| self.data.shift_shapes(shift, index=index) | |
| self.refresh() |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Some example usage: