31 m_pdiDev->ResetTracker();
32 m_pdiDev->ResetSAlignment(-1);
33 m_pdiDev->Trace(TRUE, 7);
34 m_continousTracking =
false;
40 m_pdiDev->SetPnoBuffer(
MotionBuf, 0x1FA400);
41 m_pdiDev->SetMetric(
true);
47 pdiMDat.Append(PDI_MODATA_FRAMECOUNT);
48 pdiMDat.Append(PDI_MODATA_POS);
49 pdiMDat.Append(PDI_MODATA_ORI);
50 pdiMDat.Append(PDI_MODATA_DISTLEV);
51 m_pdiDev->SetSDataList(-1, pdiMDat);
54 m_pdiDev->GetBITErrs(cBE);
56 if (!(cBE.IsClear())) { m_pdiDev->ClearBITErrs(); }
63 m_continousTracking =
true;
64 return m_pdiDev->StartContPno(0);
69 m_continousTracking =
false;
70 m_pdiDev->StopContPno();
74bool mitk::PolhemusInterface::OpenConnection()
78 if (!InitializeDevice())
83 else if (m_pdiDev->CnxReady())
91 m_pdiDev->SetSerialIF(&pdiSer);
93 ePiCommType eType = m_pdiDev->DiscoverCnx();
97 MITK_INFO <<
"USB Connection";
100 MITK_INFO <<
"Serial Connection";
103 MITK_INFO <<
"DiscoverCnx";
114 returnValue = m_pdiDev->CnxReady();
122 bool returnValue = OpenConnection();
129 m_numberOfTools = this->GetNumberOfTools();
132 std::vector<mitk::PolhemusInterface::trackingData> _trackingData = GetFrame();
136 if (m_ToolPorts.size() != _trackingData.size())
139 m_Hemispheres.clear();
140 m_HemisphereTracking.clear();
144 if (m_ToolPorts.size() == _trackingData.size())
146 for (
size_t i = 0; i < _trackingData.size(); ++i)
149 if (m_ToolPorts[i] != _trackingData.at(i).id)
152 m_Hemispheres.clear();
153 m_HemisphereTracking.clear();
160 if (m_ToolPorts.size() == 0)
162 for (
size_t i = 0; i < _trackingData.size(); ++i)
164 m_ToolPorts.push_back(_trackingData.at(i).id);
167 m_Hemispheres.clear();
168 m_HemisphereTracking.clear();
170 mitk::FillVector3D(temp, 1, 0, 0);
171 m_Hemispheres.assign(m_numberOfTools, temp);
172 m_HemisphereTracking.assign(m_numberOfTools,
false);
180 bool returnValue =
true;
182 if (m_continousTracking)
184 this->StopTracking();
187 returnValue = m_pdiDev->Disconnect();
188 MITK_INFO <<
"Disconnect";
195 std::vector<mitk::PolhemusInterface::trackingData> frame = GetSingleFrame();
196 m_pdiDev->Disconnect();
202 std::vector<mitk::PolhemusInterface::trackingData> _trackingData = GetFrame();
203 return _trackingData.size();
208 if (m_continousTracking)
209 return this->GetLastFrame();
211 return this->GetSingleFrame();
220 if (!m_pdiDev->LastPnoPtr(pBuf, dwSize)) { MITK_WARN <<
"There is an issue"; }
222 std::vector<mitk::PolhemusInterface::trackingData> returnValue = ParsePolhemusRawData(pBuf, dwSize);
224 if (returnValue.empty())
226 MITK_WARN <<
"Cannot parse data / no tools present";
234 if (m_continousTracking)
236 MITK_WARN <<
"Cannot get a single frame when continuous tracking is on!";
237 return std::vector<mitk::PolhemusInterface::trackingData>();
243 if (!m_pdiDev->ReadSinglePnoBuf(pBuf, dwSize)) {
244 MITK_WARN <<
"There is an issue";
245 return std::vector<mitk::PolhemusInterface::trackingData>();
248 return ParsePolhemusRawData(pBuf, dwSize);
253 std::vector<mitk::PolhemusInterface::trackingData> returnValue;
259 BYTE ucSensor = pBuf[i + 2];
260 SHORT shSize = pBuf[i + 6];
265 PDWORD pFC = (PDWORD)(&pBuf[i]);
266 PFLOAT pPno = (PFLOAT)(&pBuf[i + 4]);
267 PINT pDistLevel = (PINT)(&pBuf[i + 28]);
271 currentTrackingData.
id = ucSensor;
273 currentTrackingData.
pos[0] = pPno[0] * 10;
274 currentTrackingData.
pos[1] = pPno[1] * 10;
275 currentTrackingData.
pos[2] = pPno[2] * 10;
277 double azimuthAngle = pPno[3] / 180 * itk::Math::pi;
278 double elevationAngle = pPno[4] / 180 * itk::Math::pi;
279 double rollAngle = pPno[5] / 180 * itk::Math::pi;
280 vnl_quaternion<double> eulerQuat(rollAngle, elevationAngle, azimuthAngle);
281 currentTrackingData.
rot = eulerQuat;
284 returnValue.push_back(currentTrackingData);
293 if (!this->m_pdiDev->CnxReady())
296 if (m_Hemispheres.empty())
298 MITK_ERROR <<
"No Hemispheres. This should never happen when connected. Check your code!";
303 if (_HemisphereTrackingEnabled)
305 m_pdiDev->SetSHemiTrack(_tool);
308 m_HemisphereTracking.at(GetToolIndex(_tool)) =
true;
312 m_HemisphereTracking.assign(m_numberOfTools,
true);
320 std::vector<mitk::PolhemusInterface::trackingData> _position = GetFrame();
322 for (
int index : GetToolIterator(_tool))
325 double _scalarProduct = _position.at(index).pos.GetVectorFromOrigin() * m_Hemispheres.at(index);
329 if (_scalarProduct < 0)
331 m_Hemispheres.at(index) = -1. * m_Hemispheres.at(index);
333 else if (_scalarProduct == 0)
334 MITK_ERROR <<
"Something went wrong. Hemisphere or Position should not be zero.";
336 SetHemisphere(m_ToolPorts[index], m_Hemispheres.at(index));
344 if (!this->m_pdiDev->CnxReady())
348 for (
int index : GetToolIterator(_tool))
350 if (m_HemisphereTracking.at(index))
352 SetHemisphereTrackingEnabled(
false, m_ToolPorts[index]);
353 this->SetHemisphere(m_ToolPorts[index], -1.*m_Hemispheres.at(index));
354 SetHemisphereTrackingEnabled(
true, m_ToolPorts[index]);
358 this->SetHemisphere(m_ToolPorts[index], -1.*m_Hemispheres.at(index));
366 if (!this->m_pdiDev->CnxReady())
369 mitk::Vector3D _hemisphere;
370 mitk::FillVector3D(_hemisphere, 1, 0, 0);
372 for (
int index : GetToolIterator(_tool))
374 if (m_HemisphereTracking.at(index))
376 SetHemisphereTrackingEnabled(
false, m_ToolPorts[index]);
377 this->SetHemisphere(m_ToolPorts[index], _hemisphere);
378 SetHemisphereTrackingEnabled(
true, m_ToolPorts[index]);
382 this->SetHemisphere(m_ToolPorts[index], _hemisphere);
390 if (!this->m_pdiDev->CnxReady())
393 m_pdiDev->SetSHemisphere(_tool, { (float)_hemisphere[0], (
float)_hemisphere[1], (float)_hemisphere[2] });
395 for (
int index : GetToolIterator(_tool))
397 if (_hemisphere.GetNorm() != 0)
399 m_HemisphereTracking.at(index) =
false;
400 m_Hemispheres.at(index) = _hemisphere;
404 m_HemisphereTracking.at(index) =
true;
414 MITK_WARN <<
"Can't return hemisphere for all tools. Returning Hemisphere of first tool " << m_ToolPorts[0];
415 return m_Hemispheres.at(0);
417 return m_Hemispheres.at(GetToolIndex(_tool));
425 bool _returnValue =
true;
426 for (
bool currentValue : m_HemisphereTracking)
427 _returnValue = _returnValue && currentValue;
431 return m_HemisphereTracking.at(GetToolIndex(_tool));
444 return std::find(m_ToolPorts.begin(), m_ToolPorts.end(), _tool) - m_ToolPorts.begin();
447std::vector<int> mitk::PolhemusInterface::GetToolIterator(
int _tool)
449 std::vector<int> _iterator;
452 for (
int i = 0; i < static_cast<int>(m_numberOfTools); ++i)
453 _iterator.push_back(i);
457 _iterator.push_back(GetToolIndex(_tool));
464 MITK_INFO <<
"Polhemus status: " << this->m_pdiDev->CnxReady();
std::vector< trackingData > GetFrame()
Convenient method to get a frame from the tracking device.
std::vector< trackingData > GetLastFrame()
void SetHemisphereTrackingEnabled(bool _HemisphereTrackingEnabled, int _tool=-1)
void AdjustHemisphere(int _tool)
bool StopTracking()
Clears all resources. After this method have been called the system isn't ready to track any longer.
int GetToolIndex(int _tool)
mitk::Vector3D GetHemisphere(int _tool)
std::vector< int > GetToolPorts()
unsigned int GetNumberOfTools()
~PolhemusInterface()
standard destructor
std::vector< mitk::PolhemusInterface::trackingData > ParsePolhemusRawData(PBYTE pBuf, DWORD dwSize)
std::vector< trackingData > GetSingleFrame()
void ToggleHemisphere(int _tool=-1)
void SetHemisphere(int _tool, mitk::Vector3D _hemisphere)
std::vector< trackingData > AutoDetectTools()
bool StartTracking()
Opens the connection to the device and makes it ready to track tools.
PolhemusInterface()
standard constructor
bool GetHemisphereTrackingEnabled(int _tool)