Commit c5fe4f20a54045b4110d1fe5ec334ceb4137d5b1

Authored by okahilak
Committed by GitHub
1 parent 9ed8efe8
Exists in master

CLP: Move Tracker class to its own file (tracker.py) (#354)

invesalius/gui/task_navigator.py
@@ -66,6 +66,7 @@ import invesalius.project as prj @@ -66,6 +66,7 @@ import invesalius.project as prj
66 from invesalius import utils 66 from invesalius import utils
67 from invesalius.gui import utils as gui_utils 67 from invesalius.gui import utils as gui_utils
68 from invesalius.navigation.navigation import Navigation 68 from invesalius.navigation.navigation import Navigation
  69 +from invesalius.navigation.tracker import Tracker
69 70
70 HAS_PEDAL_CONNECTION = True 71 HAS_PEDAL_CONNECTION = True
71 try: 72 try:
@@ -315,122 +316,6 @@ class InnerFoldPanel(wx.Panel): @@ -315,122 +316,6 @@ class InnerFoldPanel(wx.Panel):
315 Publisher.sendMessage('Update volume camera state', camera_state=self.checkcamera.GetValue()) 316 Publisher.sendMessage('Update volume camera state', camera_state=self.checkcamera.GetValue())
316 317
317 318
318 -class Tracker():  
319 - def __init__(self):  
320 - self.trk_init = None  
321 - self.tracker_id = const.DEFAULT_TRACKER  
322 -  
323 - self.tracker_fiducials = np.full([3, 3], np.nan)  
324 - self.tracker_fiducials_raw = np.zeros((6, 6))  
325 -  
326 - self.tracker_connected = False  
327 -  
328 - def SetTracker(self, new_tracker):  
329 - if new_tracker:  
330 - self.DisconnectTracker()  
331 -  
332 - self.trk_init = dt.TrackerConnection(new_tracker, None, 'connect')  
333 - if not self.trk_init[0]:  
334 - dlg.ShowNavigationTrackerWarning(self.tracker_id, self.trk_init[1])  
335 -  
336 - self.tracker_id = 0  
337 - self.tracker_connected = False  
338 - else:  
339 - self.tracker_id = new_tracker  
340 - self.tracker_connected = True  
341 -  
342 - def DisconnectTracker(self):  
343 - if self.tracker_connected:  
344 - self.ResetTrackerFiducials()  
345 - Publisher.sendMessage('Update status text in GUI',  
346 - label=_("Disconnecting tracker ..."))  
347 - Publisher.sendMessage('Remove sensors ID')  
348 - Publisher.sendMessage('Remove object data')  
349 - self.trk_init = dt.TrackerConnection(self.tracker_id, self.trk_init[0], 'disconnect')  
350 - if not self.trk_init[0]:  
351 - self.tracker_connected = False  
352 - self.tracker_id = 0  
353 -  
354 - Publisher.sendMessage('Update status text in GUI',  
355 - label=_("Tracker disconnected"))  
356 - print("Tracker disconnected!")  
357 - else:  
358 - Publisher.sendMessage('Update status text in GUI',  
359 - label=_("Tracker still connected"))  
360 - print("Tracker still connected!")  
361 -  
362 - def IsTrackerInitialized(self):  
363 - return self.trk_init and self.tracker_id and self.tracker_connected  
364 -  
365 - def AreTrackerFiducialsSet(self):  
366 - return not np.isnan(self.tracker_fiducials).any()  
367 -  
368 - def GetTrackerCoordinates(self, ref_mode_id, n_samples=1):  
369 - coord_raw_samples = {}  
370 - coord_samples = {}  
371 -  
372 - for i in range(n_samples):  
373 - coord_raw = dco.GetCoordinates(self.trk_init, self.tracker_id, ref_mode_id)  
374 -  
375 - if ref_mode_id == const.DYNAMIC_REF:  
376 - coord = dco.dynamic_reference_m(coord_raw[0, :], coord_raw[1, :])  
377 - else:  
378 - coord = coord_raw[0, :]  
379 - coord[2] = -coord[2]  
380 -  
381 - coord_raw_samples[i] = coord_raw  
382 - coord_samples[i] = coord  
383 -  
384 - coord_raw_avg = np.median(list(coord_raw_samples.values()), axis=0)  
385 - coord_avg = np.median(list(coord_samples.values()), axis=0)  
386 -  
387 - return coord_avg, coord_raw_avg  
388 -  
389 - def SetTrackerFiducial(self, ref_mode_id, fiducial_index):  
390 - coord, coord_raw = self.GetTrackerCoordinates(  
391 - ref_mode_id=ref_mode_id,  
392 - n_samples=const.CALIBRATION_TRACKER_SAMPLES,  
393 - )  
394 -  
395 - # Update tracker fiducial with tracker coordinates  
396 - self.tracker_fiducials[fiducial_index, :] = coord[0:3]  
397 -  
398 - assert 0 <= fiducial_index <= 2, "Fiducial index out of range (0-2): {}".format(fiducial_index)  
399 -  
400 - self.tracker_fiducials_raw[2 * fiducial_index, :] = coord_raw[0, :]  
401 - self.tracker_fiducials_raw[2 * fiducial_index + 1, :] = coord_raw[1, :]  
402 -  
403 - print("Set tracker fiducial {} to coordinates {}.".format(fiducial_index, coord[0:3]))  
404 -  
405 - def ResetTrackerFiducials(self):  
406 - for m in range(3):  
407 - self.tracker_fiducials[m, :] = [np.nan, np.nan, np.nan]  
408 -  
409 - def GetTrackerFiducials(self):  
410 - return self.tracker_fiducials, self.tracker_fiducials_raw  
411 -  
412 - def GetTrackerInfo(self):  
413 - return self.trk_init, self.tracker_id  
414 -  
415 - def UpdateUI(self, selection_ctrl, numctrls_fiducial, txtctrl_fre):  
416 - if self.tracker_connected:  
417 - selection_ctrl.SetSelection(self.tracker_id)  
418 - else:  
419 - selection_ctrl.SetSelection(0)  
420 -  
421 - # Update tracker location in the UI.  
422 - for m in range(3):  
423 - coord = self.tracker_fiducials[m, :]  
424 - for n in range(0, 3):  
425 - value = 0.0 if np.isnan(coord[n]) else float(coord[n])  
426 - numctrls_fiducial[m][n].SetValue(value)  
427 -  
428 - txtctrl_fre.SetValue('')  
429 - txtctrl_fre.SetBackgroundColour('WHITE')  
430 -  
431 - def get_trackers(self):  
432 - return const.TRACKERS  
433 -  
434 class ICP(): 319 class ICP():
435 def __init__(self): 320 def __init__(self):
436 self.use_icp = False 321 self.use_icp = False
invesalius/navigation/tracker.py 0 → 100644
@@ -0,0 +1,143 @@ @@ -0,0 +1,143 @@
  1 +#--------------------------------------------------------------------------
  2 +# Software: InVesalius - Software de Reconstrucao 3D de Imagens Medicas
  3 +# Copyright: (C) 2001 Centro de Pesquisas Renato Archer
  4 +# Homepage: http://www.softwarepublico.gov.br
  5 +# Contact: invesalius@cti.gov.br
  6 +# License: GNU - GPL 2 (LICENSE.txt/LICENCA.txt)
  7 +#--------------------------------------------------------------------------
  8 +# Este programa e software livre; voce pode redistribui-lo e/ou
  9 +# modifica-lo sob os termos da Licenca Publica Geral GNU, conforme
  10 +# publicada pela Free Software Foundation; de acordo com a versao 2
  11 +# da Licenca.
  12 +#
  13 +# Este programa eh distribuido na expectativa de ser util, mas SEM
  14 +# QUALQUER GARANTIA; sem mesmo a garantia implicita de
  15 +# COMERCIALIZACAO ou de ADEQUACAO A QUALQUER PROPOSITO EM
  16 +# PARTICULAR. Consulte a Licenca Publica Geral GNU para obter mais
  17 +# detalhes.
  18 +#--------------------------------------------------------------------------
  19 +
  20 +import numpy as np
  21 +
  22 +import invesalius.constants as const
  23 +import invesalius.data.coordinates as dco
  24 +import invesalius.data.trackers as dt
  25 +import invesalius.gui.dialogs as dlg
  26 +from invesalius.pubsub import pub as Publisher
  27 +
  28 +
  29 +class Tracker():
  30 + def __init__(self):
  31 + self.trk_init = None
  32 + self.tracker_id = const.DEFAULT_TRACKER
  33 +
  34 + self.tracker_fiducials = np.full([3, 3], np.nan)
  35 + self.tracker_fiducials_raw = np.zeros((6, 6))
  36 +
  37 + self.tracker_connected = False
  38 +
  39 + def SetTracker(self, new_tracker):
  40 + if new_tracker:
  41 + self.DisconnectTracker()
  42 +
  43 + self.trk_init = dt.TrackerConnection(new_tracker, None, 'connect')
  44 + if not self.trk_init[0]:
  45 + dlg.ShowNavigationTrackerWarning(self.tracker_id, self.trk_init[1])
  46 +
  47 + self.tracker_id = 0
  48 + self.tracker_connected = False
  49 + else:
  50 + self.tracker_id = new_tracker
  51 + self.tracker_connected = True
  52 +
  53 + def DisconnectTracker(self):
  54 + if self.tracker_connected:
  55 + self.ResetTrackerFiducials()
  56 + Publisher.sendMessage('Update status text in GUI',
  57 + label=_("Disconnecting tracker ..."))
  58 + Publisher.sendMessage('Remove sensors ID')
  59 + Publisher.sendMessage('Remove object data')
  60 + self.trk_init = dt.TrackerConnection(self.tracker_id, self.trk_init[0], 'disconnect')
  61 + if not self.trk_init[0]:
  62 + self.tracker_connected = False
  63 + self.tracker_id = 0
  64 +
  65 + Publisher.sendMessage('Update status text in GUI',
  66 + label=_("Tracker disconnected"))
  67 + print("Tracker disconnected!")
  68 + else:
  69 + Publisher.sendMessage('Update status text in GUI',
  70 + label=_("Tracker still connected"))
  71 + print("Tracker still connected!")
  72 +
  73 + def IsTrackerInitialized(self):
  74 + return self.trk_init and self.tracker_id and self.tracker_connected
  75 +
  76 + def AreTrackerFiducialsSet(self):
  77 + return not np.isnan(self.tracker_fiducials).any()
  78 +
  79 + def GetTrackerCoordinates(self, ref_mode_id, n_samples=1):
  80 + coord_raw_samples = {}
  81 + coord_samples = {}
  82 +
  83 + for i in range(n_samples):
  84 + coord_raw = dco.GetCoordinates(self.trk_init, self.tracker_id, ref_mode_id)
  85 +
  86 + if ref_mode_id == const.DYNAMIC_REF:
  87 + coord = dco.dynamic_reference_m(coord_raw[0, :], coord_raw[1, :])
  88 + else:
  89 + coord = coord_raw[0, :]
  90 + coord[2] = -coord[2]
  91 +
  92 + coord_raw_samples[i] = coord_raw
  93 + coord_samples[i] = coord
  94 +
  95 + coord_raw_avg = np.median(list(coord_raw_samples.values()), axis=0)
  96 + coord_avg = np.median(list(coord_samples.values()), axis=0)
  97 +
  98 + return coord_avg, coord_raw_avg
  99 +
  100 + def SetTrackerFiducial(self, ref_mode_id, fiducial_index):
  101 + coord, coord_raw = self.GetTrackerCoordinates(
  102 + ref_mode_id=ref_mode_id,
  103 + n_samples=const.CALIBRATION_TRACKER_SAMPLES,
  104 + )
  105 +
  106 + # Update tracker fiducial with tracker coordinates
  107 + self.tracker_fiducials[fiducial_index, :] = coord[0:3]
  108 +
  109 + assert 0 <= fiducial_index <= 2, "Fiducial index out of range (0-2): {}".format(fiducial_index)
  110 +
  111 + self.tracker_fiducials_raw[2 * fiducial_index, :] = coord_raw[0, :]
  112 + self.tracker_fiducials_raw[2 * fiducial_index + 1, :] = coord_raw[1, :]
  113 +
  114 + print("Set tracker fiducial {} to coordinates {}.".format(fiducial_index, coord[0:3]))
  115 +
  116 + def ResetTrackerFiducials(self):
  117 + for m in range(3):
  118 + self.tracker_fiducials[m, :] = [np.nan, np.nan, np.nan]
  119 +
  120 + def GetTrackerFiducials(self):
  121 + return self.tracker_fiducials, self.tracker_fiducials_raw
  122 +
  123 + def GetTrackerInfo(self):
  124 + return self.trk_init, self.tracker_id
  125 +
  126 + def UpdateUI(self, selection_ctrl, numctrls_fiducial, txtctrl_fre):
  127 + if self.tracker_connected:
  128 + selection_ctrl.SetSelection(self.tracker_id)
  129 + else:
  130 + selection_ctrl.SetSelection(0)
  131 +
  132 + # Update tracker location in the UI.
  133 + for m in range(3):
  134 + coord = self.tracker_fiducials[m, :]
  135 + for n in range(0, 3):
  136 + value = 0.0 if np.isnan(coord[n]) else float(coord[n])
  137 + numctrls_fiducial[m][n].SetValue(value)
  138 +
  139 + txtctrl_fre.SetValue('')
  140 + txtctrl_fre.SetBackgroundColour('WHITE')
  141 +
  142 + def get_trackers(self):
  143 + return const.TRACKERS