34 m_continousTracking =
false;
47 pdiMDat.Append(PDI_MODATA_FRAMECOUNT);
48 pdiMDat.Append(PDI_MODATA_POS);
49 pdiMDat.Append(PDI_MODATA_ORI);
50 pdiMDat.Append(PDI_MODATA_DISTLEV);
56 if (!(cBE.IsClear())) {
m_pdiDev->ClearBITErrs(); }
63 m_continousTracking =
true;
69 m_continousTracking =
false;
74 bool mitk::PolhemusInterface::OpenConnection()
93 ePiCommType eType =
m_pdiDev->DiscoverCnx();
122 bool returnValue = OpenConnection();
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();
171 m_Hemispheres.assign(m_numberOfTools, temp);
172 m_HemisphereTracking.assign(m_numberOfTools,
false);
180 bool returnValue =
true;
182 if (m_continousTracking)
187 returnValue =
m_pdiDev->Disconnect();
195 std::vector<mitk::PolhemusInterface::trackingData> frame =
GetSingleFrame();
202 std::vector<mitk::PolhemusInterface::trackingData> _trackingData =
GetFrame();
203 return _trackingData.size();
208 if (m_continousTracking)
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)) {
245 return std::vector<mitk::PolhemusInterface::trackingData>();
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);
296 if (m_Hemispheres.empty())
298 MITK_ERROR <<
"No Hemispheres. This should never happen when connected. Check your code!";
303 if (_HemisphereTrackingEnabled)
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.";
348 for (
int index : GetToolIterator(_tool))
350 if (m_HemisphereTracking.at(index))
353 this->
SetHemisphere(m_ToolPorts[index], -1.*m_Hemispheres.at(index));
358 this->
SetHemisphere(m_ToolPorts[index], -1.*m_Hemispheres.at(index));
372 for (
int index : GetToolIterator(_tool))
374 if (m_HemisphereTracking.at(index))
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);
425 bool _returnValue =
true;
426 for (
bool currentValue : m_HemisphereTracking)
427 _returnValue = _returnValue && currentValue;
444 return std::find(m_ToolPorts.begin(), m_ToolPorts.end(), _tool) - m_ToolPorts.begin();
447 std::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);
std::vector< trackingData > GetSingleFrame()
PolhemusInterface()
standard constructor
bool StopTracking()
Clears all resources. After this method have been called the system isn't ready to track any longer...
mitk::Vector3D GetHemisphere(int _tool)
~PolhemusInterface()
standard destructor
unsigned int GetNumberOfTools()
std::vector< mitk::PolhemusInterface::trackingData > ParsePolhemusRawData(PBYTE pBuf, DWORD dwSize)
std::vector< trackingData > GetFrame()
Convenient method to get a frame from the tracking device.
bool GetHemisphereTrackingEnabled(int _tool)
void FillVector3D(Tout &out, mitk::ScalarType x, mitk::ScalarType y, mitk::ScalarType z)
void AdjustHemisphere(int _tool)
std::vector< trackingData > AutoDetectTools()
bool StartTracking()
Opens the connection to the device and makes it ready to track tools.
void ToggleHemisphere(int _tool=-1)
void SetHemisphereTrackingEnabled(bool _HemisphereTrackingEnabled, int _tool=-1)
void SetHemisphere(int _tool, mitk::Vector3D _hemisphere)
std::vector< int > GetToolPorts()
std::vector< trackingData > GetLastFrame()
int GetToolIndex(int _tool)