MITK-IGT
IGT Extension of MITK
Loading...
Searching...
No Matches
mitkClaronTrackingDevice.cpp
Go to the documentation of this file.
1/*============================================================================
2
3The Medical Imaging Interaction Toolkit (MITK)
4
5Copyright (c) German Cancer Research Center (DKFZ)
6All rights reserved.
7
8Use of this source code is governed by a 3-clause BSD license that can be
9found in the LICENSE file.
10
11============================================================================*/
12
14#include "mitkClaronTool.h"
15#include "mitkIGTConfig.h"
16#include "mitkIGTTimeStamp.h"
17#include "mitkIGTHardwareException.h"
18#include <itksys/SystemTools.hxx>
19#include <iostream>
20#include <mitkIOUtil.h>
22
23
25{
26 //set the type of this tracking device
28
29 m_Device = mitk::ClaronInterface::New();
30 //############################# standard directories ##################################
31 if (m_Device->IsMicronTrackerInstalled())
32 {
33 m_ToolfilesDir = mitk::IOUtil::CreateTemporaryDirectory();
34#ifdef MITK_MICRON_TRACKER_CALIBRATION_DIR
35 m_CalibrationDir = std::string(MITK_MICRON_TRACKER_CALIBRATION_DIR);
36#endif
37 }
38 else
39 {
40 m_ToolfilesDir = "Error - No Microntracker installed";
41 m_CalibrationDir = "Error - No Microntracker installed";
42 }
43 //##################################################################################################
45}
46
48{
49 mitk::ClaronInterface::Pointer tempInterface = mitk::ClaronInterface::New();
50 return tempInterface->IsMicronTrackerInstalled();
51}
52
53
57
58
59mitk::TrackingTool* mitk::ClaronTrackingDevice::AddTool( const char* toolName, const char* fileName )
60{
61 mitk::ClaronTool::Pointer t = mitk::ClaronTool::New();
62 if (t->LoadFile(fileName) == false)
63 {
64 return nullptr;
65 }
66 t->SetToolName(toolName);
67 if (this->InternalAddTool(t) == false)
68 return nullptr;
69 return t.GetPointer();
70}
71
72
73bool mitk::ClaronTrackingDevice::InternalAddTool(ClaronTool::Pointer tool)
74{
75 m_AllTools.push_back(tool);
76 return true;
77}
78
79
80std::vector<mitk::ClaronTool::Pointer> mitk::ClaronTrackingDevice::DetectTools()
81{
82 std::vector<mitk::ClaronTool::Pointer> returnValue;
83 std::vector<claronToolHandle> allHandles = m_Device->GetAllActiveTools();
84 for (auto iter = allHandles.begin(); iter != allHandles.end(); ++iter)
85 {
86 ClaronTool::Pointer newTool = ClaronTool::New();
87 newTool->SetToolName(m_Device->GetName(*iter));
88 newTool->SetCalibrationName(m_Device->GetName(*iter));
89 newTool->SetToolHandle(*iter);
90 returnValue.push_back(newTool);
91 }
92 return returnValue;
93}
94
95
97{
98
99 //By Alfred: next line because no temp directory is set if MicronTracker is not installed
100 if (!m_Device->IsMicronTrackerInstalled())
101 return false;
102 //##################################################################################
103
104 //be sure that the temp-directory is empty at start: delete all files in the tool files directory
105 itksys::SystemTools::RemoveADirectory(m_ToolfilesDir.c_str());
106 itksys::SystemTools::MakeDirectory(m_ToolfilesDir.c_str());
107
108 //copy all toolfiles into the temp directory
109 for (unsigned int i=0; i<m_AllTools.size(); i++)
110 {
111 itksys::SystemTools::CopyAFile(m_AllTools[i]->GetFile().c_str(), m_ToolfilesDir.c_str());
112 }
113 this->SetState(Tracking); // go to mode Tracking
114 this->m_StopTrackingMutex.lock(); // update the local copy of m_StopTracking
115 this->m_StopTracking = false;
116 this->m_StopTrackingMutex.unlock();
117
118 //restart the Microntracker, so it will load the new tool files
119 m_Device->StopTracking();
120 m_Device->Initialize(m_CalibrationDir,m_ToolfilesDir);
121
122 if (m_Device->StartTracking())
123 {
125 m_Thread = std::thread(&ClaronTrackingDevice::ThreadStartTracking, this); // start a new thread that executes the TrackTools() method
126 return true;
127 }
128 else
129 {mitkThrowException(mitk::IGTHardwareException) << "Error while trying to start the device!";}
130}
131
132
134{
135 Superclass::StopTracking();
136 //delete all files in the tool files directory
137 itksys::SystemTools::RemoveADirectory(m_ToolfilesDir.c_str());
138 return true;
139}
140
141
143{
144 return (unsigned int)this->m_AllTools.size();
145}
146
147
149{
150 if ( toolNumber >= this->GetToolCount())
151 return nullptr;
152 else
153 return this->m_AllTools[toolNumber];
154}
155
156
158{
159 bool returnValue;
160 //Create the temp directory
161 itksys::SystemTools::MakeDirectory(m_ToolfilesDir.c_str());
162
163 m_Device->Initialize(m_CalibrationDir,m_ToolfilesDir);
164 returnValue = m_Device->StartTracking();
165
166 if (returnValue)
167 {
168 this->SetState(Ready);
169 }
170 else
171 {
172 //reset everything
173 if (m_Device.IsNull())
174 {
175 m_Device = mitk::ClaronInterface::New();
176 m_Device->Initialize(m_CalibrationDir, m_ToolfilesDir);
177 }
178 m_Device->StopTracking();
179 this->SetState(Setup);
180 mitkThrowException(mitk::IGTHardwareException) << "Error while trying to open connection to the MicronTracker.";
181 }
182 return returnValue;
183}
184
185
187{
188 bool returnValue = true;
189 if (this->GetState() == Setup)
190 return true;
191
192 returnValue = m_Device->StopTracking();
193
194 //delete the temporary directory
195 itksys::SystemTools::RemoveADirectory(m_ToolfilesDir.c_str());
196
197 this->SetState(Setup);
198 return returnValue;
199}
200
201
206
207
208std::vector<mitk::ClaronTool::Pointer> mitk::ClaronTrackingDevice::GetAllTools()
209{
210 return this->m_AllTools;
211}
212
213
215{
216 try
217 {
218 /* lock the TrackingFinishedMutex to signal that the execution rights are now transfered to the tracking thread */
219 std::lock_guard<std::mutex> trackingFinishedLockHolder(m_TrackingFinishedMutex); // keep lock until end of scope
220
221 bool localStopTracking; // Because m_StopTracking is used by two threads, access has to be guarded by a mutex. To minimize thread locking, a local copy is used here
222 this->m_StopTrackingMutex.lock(); // update the local copy of m_StopTracking
223 localStopTracking = this->m_StopTracking;
224 this->m_StopTrackingMutex.unlock();
225
226 while ((this->GetState() == Tracking) && (localStopTracking == false))
227 {
228 this->GetDevice()->GrabFrame();
229
230 std::vector<mitk::ClaronTool::Pointer> detectedTools = this->DetectTools();
231 std::vector<mitk::ClaronTool::Pointer> allTools = this->GetAllTools();
232 std::vector<mitk::ClaronTool::Pointer>::iterator itAllTools;
233 for(itAllTools = allTools.begin(); itAllTools != allTools.end(); itAllTools++)
234 {
235 mitk::ClaronTool::Pointer currentTool = *itAllTools;
236 //test if current tool was detected
237 std::vector<mitk::ClaronTool::Pointer>::iterator itDetectedTools;
238 bool foundTool = false;
239 for(itDetectedTools = detectedTools.begin(); itDetectedTools != detectedTools.end(); itDetectedTools++)
240 {
241 mitk::ClaronTool::Pointer aktuDet = *itDetectedTools;
242 std::string tempString(currentTool->GetCalibrationName());
243 if (tempString.compare(aktuDet->GetCalibrationName())==0)
244 {
245 currentTool->SetToolHandle(aktuDet->GetToolHandle());
246 foundTool = true;
247 }
248 }
249 if (!foundTool)
250 {
251 currentTool->SetToolHandle(0);
252 }
253
254 if (currentTool->GetToolHandle() != 0)
255 {
256 currentTool->SetDataValid(true);
257 //get tip position of tool:
258 std::vector<double> pos_vector = this->GetDevice()->GetTipPosition(currentTool->GetToolHandle());
259 //write tip position into tool:
260 mitk::Point3D pos;
261 pos[0] = pos_vector[0];
262 pos[1] = pos_vector[1];
263 pos[2] = pos_vector[2];
264 currentTool->SetPosition(pos);
265 //get tip quaternion of tool
266 std::vector<double> quat = this->GetDevice()->GetTipQuaternions(currentTool->GetToolHandle());
267 //write tip quaternion into tool
268 mitk::Quaternion orientation(quat[1], quat[2], quat[3], quat[0]);
269 currentTool->SetOrientation(orientation);
270
271 //TODO: read the timestamp data from the tracking device interface
272 currentTool->SetIGTTimeStamp(mitk::IGTTimeStamp::GetInstance()->GetElapsed());
273 }
274 else
275 {
276 mitk::Point3D origin;
277 origin.Fill(0);
278 currentTool->SetPosition(origin);
279 currentTool->SetOrientation(mitk::Quaternion(0,0,0,0));
280 currentTool->SetDataValid(false);
281 }
282 }
283 /* Update the local copy of m_StopTracking */
284 this->m_StopTrackingMutex.lock();
285 localStopTracking = m_StopTracking;
286 this->m_StopTrackingMutex.unlock();
287 }
288 }
289 catch(...)
290 {
291 this->StopTracking();
292 mitkThrowException(mitk::IGTHardwareException) << "Error while trying to track tools. Thread stopped.";
293 }
294}
295
296
297bool mitk::ClaronTrackingDevice::IsMicronTrackerInstalled()
298{
299 return this->m_Device->IsMicronTrackerInstalled();
300}
301
302
304{
305 this->TrackTools();
306}
An object of this class represents the interface to the MicronTracker. The methods of this class are ...
bool CloseConnection() override
Closes the connection and clears all resources.
ClaronInterface::Pointer m_Device
represents the interface to the tracking hardware
void TrackTools()
This method tracks tools as long as the variable m_Mode is set to "Tracking". Tracking tools means gr...
bool StopTracking() override
Stops the tracking.
bool StartTracking() override
Starts the tracking.
bool OpenConnection() override
Opens the connection to the device. This have to be done before the tracking is started.
unsigned int GetToolCount() const override
std::string m_ToolfilesDir
The directory where the tool calibration files can be found.
std::string m_CalibrationDir
The directory where the camera calibration files can be found.
TrackingTool * GetTool(unsigned int toolNumber) const override
mitk::TrackingTool * AddTool(const char *toolName, const char *fileName)
Create a new Claron tool with toolName and fileName and add it to the list of tools.
std::vector< ClaronTool::Pointer > GetAllTools()
std::vector< ClaronTool::Pointer > DetectTools()
Automatically detects tools in field of measurement of the tracking device. Tools can only be detecte...
bool InternalAddTool(ClaronTool::Pointer tool)
Adds a tool to the tracking device.
An object of this class represents an exception of the MITK-IGT module which are releated to the hard...
static IGTTimeStamp * GetInstance()
returns a pointer to the current instance of mitkTimeStamp
void Start(itk::Object::Pointer device)
starts the time-acquisition
Interface for all Tracking Devices.
TrackingDeviceData m_Data
current device Data
Interface for all Tracking Tools.
IGT Exceptions.