From f9e7f90a03f136057b66aea40b0231033c8022b8 Mon Sep 17 00:00:00 2001 From: carlocastoldi <34198340+carlocastoldi@users.noreply.github.com> Date: Wed, 20 Dec 2023 14:46:25 +0100 Subject: [PATCH] small fixes following #21 (#26) * Use intersect_with_plane() from newest vedo instead of the backported function * fix some vedo function calls still using lowerCamelCase instead of snake_case * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- brainglobe_heatmap/heatmaps.py | 6 ++-- brainglobe_heatmap/plane.py | 56 +++++++--------------------------- brainglobe_heatmap/slicer.py | 4 +-- 3 files changed, 16 insertions(+), 50 deletions(-) diff --git a/brainglobe_heatmap/heatmaps.py b/brainglobe_heatmap/heatmaps.py index eac41df..8964002 100644 --- a/brainglobe_heatmap/heatmaps.py +++ b/brainglobe_heatmap/heatmaps.py @@ -19,10 +19,10 @@ # Set settings for transparent background # vedo for transparent bg -# settings.vsettings.screenshotTransparentBackground = True +# settings.vsettings.screenshot_transparent_background = True # This needs to be false for transparent bg -# settings.vsettings.useFXAA = False +# settings.vsettings.use_fxaa = False def check_values(values: dict, atlas: Atlas) -> Tuple[float, float]: @@ -157,7 +157,7 @@ def render(self, **kwargs) -> Scene: camera = self.orientation else: self.orientation = np.array(self.orientation) - com = self.slicer.plane0.centerOfMass() + com = self.slicer.plane0.center_of_mass() camera = { "pos": com - self.orientation * 2 * np.linalg.norm(com), "viewup": (0, -1, 0), diff --git a/brainglobe_heatmap/plane.py b/brainglobe_heatmap/plane.py index d898459..5d37dd4 100644 --- a/brainglobe_heatmap/plane.py +++ b/brainglobe_heatmap/plane.py @@ -4,42 +4,10 @@ import vedo as vd import vtkmodules.all as vtk from brainrender.actor import Actor -from vtkmodules.vtkFiltersCore import vtkPolyDataPlaneCutter -np.float = float # for compatibility with old vedo -vtk.vtkLogger.SetStderrVerbosity(vtk.vtkLogger.VERBOSITY_OFF) - - -# from vedo 2023.4.6 -def intersect_with_plane(mesh: vd.Mesh, origin=(0, 0, 0), normal=(1, 0, 0)): - """ - Intersect this Mesh with a plane to return a set of lines. - - Example: - ```python - from vedo import * - sph = Sphere() - mi = sph.clone().intersect_with_plane().join() - print(mi.lines()) - show(sph, mi, axes=1).close() - ``` - ![](https://vedo.embl.es/images/feats/intersect_plane.png) - """ - plane = vtk.vtkPlane() - plane.SetOrigin(origin) - plane.SetNormal(normal) - - cutter = vtkPolyDataPlaneCutter() - cutter.SetInputData(mesh.dataset) - cutter.SetPlane(plane) - cutter.InterpolateAttributesOn() - cutter.ComputeNormalsOff() - cutter.Update() - - msh = vd.Mesh(cutter.GetOutput(), "k", 1).lighting("off") - msh.properties.SetLineWidth(3) - msh.name = "PlaneIntersection" - return msh +vtk.vtkLogger.SetStderrVerbosity( + vtk.vtkLogger.VERBOSITY_OFF +) # remove logger's prints during intersect_with_plane() class Plane: @@ -57,7 +25,7 @@ def __init__( self.M = np.vstack([u, v]).T @staticmethod - def from_norm(origin: np.ndarray, norm: np.ndarray) -> vd.Plane: + def from_norm(origin: np.ndarray, norm: np.ndarray): u = np.zeros(3) m = np.where(norm != 0)[0][0] # orientation can't be all-zeros n = (m + 1) % 3 @@ -85,20 +53,18 @@ def to_mesh(self, actor: Actor): plane_mesh.width = length return plane_mesh - def centerOfMass(self): + def center_of_mass(self): return self.center - def P3toP2(self, ps): + def p3_to_p2(self, ps): # ps is a list of 3D points # returns a list of 2D point mapped on # the plane (u -> x axis, v -> y axis) return (ps - self.center) @ self.M - def intersectWith(self, mesh: vd.Mesh): - # mesh.intersect_with_plane( - # origin=self.center, normal=self.normal) in newer vedo - return intersect_with_plane( - mesh, origin=self.center, normal=self.normal + def intersect_with(self, mesh: vd.Mesh): + return mesh.intersect_with_plane( + origin=self.center, normal=self.normal ) # for Slicer.get_structures_slice_coords() @@ -106,14 +72,14 @@ def get_projections(self, actors: List[Actor]) -> Dict[str, np.ndarray]: projected = {} for actor in actors: mesh: vd.Mesh = actor._mesh - intersection = self.intersectWith(mesh) + intersection = self.intersect_with(mesh) if not intersection.vertices.shape[0]: continue pieces = intersection.split() # intersection.split() in newer vedo for piece_n, piece in enumerate(pieces): # sort coordinates points = piece.join(reset=True).vertices - projected[actor.name + f"_segment_{piece_n}"] = self.P3toP2( + projected[actor.name + f"_segment_{piece_n}"] = self.p3_to_p2( points ) return projected diff --git a/brainglobe_heatmap/slicer.py b/brainglobe_heatmap/slicer.py index b99e1b9..363b041 100644 --- a/brainglobe_heatmap/slicer.py +++ b/brainglobe_heatmap/slicer.py @@ -36,7 +36,7 @@ def __init__( 3D vector) + thickness (spacing between the two planes) """ if position is None: - position = root.centerOfMass() + position = root.center_of_mass() if isinstance(position, (float, int)): if isinstance(orientation, str): @@ -123,7 +123,7 @@ def show_plane_intersection( to the brainrender scene. """ for region in regions + [root]: - intersection = self.plane0.intersectWith(region._mesh) + intersection = self.plane0.intersect_with(region._mesh) if len(intersection.vertices): scene.add(intersection, transform=False)