Skip to content

Commit

Permalink
Show coil center/target even when coil is hidden. Handle exceptions w…
Browse files Browse the repository at this point in the history
…hen loading state file. Update environment.yml. Minor refactors ie. naming changes.
  • Loading branch information
Tolonen Luka committed Sep 11, 2024
1 parent 9fb0631 commit 81f4251
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 22 deletions.
2 changes: 1 addition & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ dependencies:
- pyclaron
- polhemusFT
- polhemus
- pypolaris==0.0.7
- pypolaris==0.0.8
- scikit-image==0.24.0
- scikit-learn==1.5.0
- Trekker==0.9
Expand Down
3 changes: 1 addition & 2 deletions invesalius/data/viewer_volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,12 +309,11 @@ def __init__(self, parent):
# An object to manage visualizing coils in the 3D viewer.
self.coil_visualizer = CoilVisualizer(
renderer=self.ren,
interactor=self.interactor,
actor_factory=self.actor_factory,
vector_field_visualizer=self.vector_field_visualizer,
)

self.probe_visualizer = ProbeVisualizer(self.ren, self.interactor)
self.probe_visualizer = ProbeVisualizer(self.ren)

self.seed_offset = const.SEED_OFFSET
self.radius_list = vtkIdList()
Expand Down
17 changes: 8 additions & 9 deletions invesalius/data/visualization/coil_visualizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ class CoilVisualizer:
# Color for the marker for target when the coil at the target.
COIL_AT_TARGET_COLOR = vtk.vtkNamedColors().GetColor3d("Green")

def __init__(self, renderer, interactor, actor_factory, vector_field_visualizer):
def __init__(self, renderer, actor_factory, vector_field_visualizer):
self.renderer = renderer
self.interactor = interactor
self.tracker = Tracker()

# Keeps track of whether tracker fiducials have been set.
Expand Down Expand Up @@ -96,7 +95,7 @@ def UpdateVectorField(self):

# If not navigating, render the scene.
if not self.is_navigating:
self.interactor.Render()
Publisher.sendMessage("Render volume viewer")

def SetCoilAtTarget(self, state):
self.coil_at_target = state
Expand All @@ -119,14 +118,14 @@ def ShowCoil(self, state, coil_name=None):
if coil_name is None: # Show/hide all coils
for coil in self.coils.values():
coil["actor"].SetVisibility(state)
coil["center_actor"].SetVisibility(state)
coil["center_actor"].SetVisibility(True) # Always show the center donut actor
if self.target_coil_actor is not None: # LUKATODO: target mode...
self.target_coil_actor.SetVisibility(state)
self.target_coil_actor.SetVisibility(True) # Keep target visible for now
elif (coil := self.coils.get(coil_name, None)) is not None:
# Just toggle the visibility when dealing with specific coils
new_state = not coil["actor"].GetVisibility()
coil["actor"].SetVisibility(new_state)
coil["center_actor"].SetVisibility(new_state)
coil["center_actor"].SetVisibility(True) # Always show the center donut actor

# If all coils are hidden/shown, update the color of Show-coil button
coils_visible = [coil["actor"].GetVisibility() for coil in self.coils.values()]
Expand All @@ -138,7 +137,7 @@ def ShowCoil(self, state, coil_name=None):
# self.vector_field_assembly.SetVisibility(state) # LUKATODO: Keep this hidden for now

if not self.is_navigating:
self.interactor.Render()
Publisher.sendMessage("Render volume viewer")

def AddTargetCoil(self, m_target):
self.RemoveTargetCoil()
Expand Down Expand Up @@ -191,7 +190,7 @@ def AddTargetCoil(self, m_target):
self.renderer.AddActor(self.target_coil_actor)

if not self.is_navigating:
self.interactor.Render()
Publisher.sendMessage("Render volume viewer")

def RemoveTargetCoil(self):
if self.target_coil_actor is None:
Expand Down Expand Up @@ -274,7 +273,7 @@ def RemoveCoil(self, coil_name=None):

# self.vector_field_assembly.SetVisibility(0)
if not self.is_navigating:
self.interactor.Render()
Publisher.sendMessage("Render volume viewer")

def ResetCoilVisualizer(self, n_coils):
self.RemoveCoil() # Remove all coils
Expand Down
5 changes: 2 additions & 3 deletions invesalius/data/visualization/probe_visualizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ class ProbeVisualizer:
A class for visualizing probe in the volume viewer.
"""

def __init__(self, renderer, interactor):
def __init__(self, renderer):
self.renderer = renderer
self.interactor = interactor

self.probe_actor = None
self.probe_path = os.path.join(inv_paths.OBJ_DIR, "stylus.stl")
Expand All @@ -38,7 +37,7 @@ def ShowProbe(self, state):
if self.probe_actor:
self.probe_actor.SetVisibility(self.show_probe)
if not self.is_navigating:
self.interactor.Render()
Publisher.sendMessage("Render volume viewer")

def AddProbeActor(self, probe_path):
vtk_colors = vtk.vtkNamedColors()
Expand Down
2 changes: 1 addition & 1 deletion invesalius/gui/preferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,7 @@ def DeleteCoil(event, name):
(lambda event, name=name: self.OnSaveCoilToOBR(event, coil_name=name)),
save_coil,
)
self.PopupMenu(menu, event.GetPosition())
self.PopupMenu(menu)
menu.Destroy()

def OnCreateNewCoil(self, event=None):
Expand Down
7 changes: 5 additions & 2 deletions invesalius/gui/task_navigator.py
Original file line number Diff line number Diff line change
Expand Up @@ -1080,7 +1080,7 @@ def onRecord(self, evt):
)

if marker_visibilities[0] and marker_visibilities[1]: # if probe and head are visible
if self.navigation.SetStylusOrientation(coord_raw) and not self.done:
if self.navigation.OnRecordStylusOrientation(coord_raw) and not self.done:
# if successfully created r_stylus in navigation for the first time: make the illustration green to show success
self.done = True
self.help.Destroy() # show a colored (green) bitmap as opposed to grayscale
Expand Down Expand Up @@ -2060,7 +2060,10 @@ def __init__(self, parent, nav_hub):
)

# In the future, it would be better if the panel could initialize itself based on markers in MarkersControl
self.markers.LoadState()
try:
self.markers.LoadState()
except:
self.session.DeleteStateFile() # Delete state file if it is erroneous

# Add all lines into main sizer
group_sizer = wx.BoxSizer(wx.VERTICAL)
Expand Down
7 changes: 6 additions & 1 deletion invesalius/navigation/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,14 @@ def UpdateFiducialMarkers(self):
def OnStateProject(self, state):
if state:
if self.load_from_state:
self.LoadState()
self.load_from_state = False
try:
self.LoadState()
except:
ses.Session.DeleteStateFile()
self.LoadProject() # Load project if failed to load from state
else:
self.LoadProject()

self.SaveState()
self.UpdateFiducialMarkers()
5 changes: 4 additions & 1 deletion invesalius/navigation/iterativeclosestpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ def __init__(self):
self.m_icp = None
self.icp_fre = None

self.LoadState()
try:
self.LoadState()
except:
ses.Session().DeleteStateFile()

def SaveState(self):
m_icp = self.m_icp.tolist() if self.m_icp is not None else None
Expand Down
2 changes: 1 addition & 1 deletion invesalius/navigation/navigation.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ def EstimateTrackerToInVTransformationMatrix(self, tracker, image):
self.all_fiducials[3:, :].T, self.all_fiducials[:3, :].T, shear=False, scale=False
)

def SetStylusOrientation(self, coord_raw):
def OnRecordStylusOrientation(self, coord_raw):
if self.m_change is not None:
m_probe = dcr.compute_marker_transformation(coord_raw, 0)

Expand Down
5 changes: 4 additions & 1 deletion invesalius/navigation/tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ def __init__(self):

self.TrackerCoordinates = dco.TrackerCoordinates()

self.LoadState()
try:
self.LoadState()
except:
ses.Session().DeleteStateFile()

def SaveState(self):
tracker_id = self.tracker_id
Expand Down

0 comments on commit 81f4251

Please sign in to comment.