Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

small fixes following #21 #26

Merged
merged 3 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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