Skip to content

Commit

Permalink
small fixes following #21 (#26)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
carlocastoldi and pre-commit-ci[bot] authored Dec 20, 2023
1 parent af71f8c commit f9e7f90
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 50 deletions.
6 changes: 3 additions & 3 deletions brainglobe_heatmap/heatmaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down Expand Up @@ -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),
Expand Down
56 changes: 11 additions & 45 deletions brainglobe_heatmap/plane.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -85,35 +53,33 @@ 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()
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
4 changes: 2 additions & 2 deletions brainglobe_heatmap/slicer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit f9e7f90

Please sign in to comment.