15#include "mitkIGTException.h"
20#include <itksys/SystemTools.hxx>
26m_AllTools(), m_RefreshRate(100), m_NumberOfControlPoints(20), m_GaussianNoiseEnabled(false),
27m_MeanDistributionParam(0.0), m_DeviationDistributionParam(1.0)
36 if (this->GetState() == Tracking)
40 if (this->GetState() == Ready)
42 this->CloseConnection();
45 if (m_Thread.joinable())
57 mitk::VirtualTrackingTool::Pointer t = mitk::VirtualTrackingTool::New();
58 t->SetToolName(toolName);
60 this->InitializeSpline(t);
61 std::lock_guard<std::mutex> lock(m_ToolsMutex);
62 m_AllTools.push_back(t);
69 if (this->GetState() != Ready)
71 this->SetState(Tracking);
72 this->m_StopTrackingMutex.lock();
73 this->m_StopTracking =
false;
74 this->m_StopTrackingMutex.unlock();
78 if (m_Thread.joinable())
87 if (this->GetState() == Tracking)
89 m_StopTrackingMutex.lock();
90 m_StopTracking =
true;
91 m_StopTrackingMutex.unlock();
93 m_TrackingFinishedMutex.lock();
94 this->SetState(Ready);
95 m_TrackingFinishedMutex.unlock();
105 std::lock_guard<std::mutex> lock(m_ToolsMutex);
106 return static_cast<unsigned int>(this->m_AllTools.size());
111 std::lock_guard<std::mutex> lock(m_ToolsMutex);
112 if (toolNumber < m_AllTools.size())
113 return this->m_AllTools.at(toolNumber);
119 if (m_NumberOfControlPoints < 1)
121 mitkThrowException(
mitk::IGTException) <<
"to few control points for spline interpolation";
123 srand(time(
nullptr));
125 this->SetState(Ready);
134 typedef mitk::VirtualTrackingTool::SplineType SplineType;
136 SplineType::ControlPointListType controlPoints;
137 controlPoints.reserve(m_NumberOfControlPoints + 1);
139 controlPoints.push_back(this->GetRandomPoint());
141 for (
unsigned int i = 1; i < m_NumberOfControlPoints - 1; ++i)
143 SplineType::ControlPointType pos;
144 pos = this->GetRandomPoint();
145 length += controlPoints.at(i - 1).EuclideanDistanceTo(pos);
146 controlPoints.push_back(pos);
148 controlPoints.push_back(controlPoints.at(0));
149 length += controlPoints.at(controlPoints.size() - 2).EuclideanDistanceTo(controlPoints.at(controlPoints.size() - 1));
152 SplineType::KnotListType knotList;
153 knotList.push_back(0.0);
154 for (
unsigned int i = 1; i < controlPoints.size() + t->GetSpline()->GetSplineOrder() + 1; ++i)
155 knotList.push_back(i);
156 knotList.push_back(controlPoints.size() + t->GetSpline()->GetSplineOrder() + 1);
158 t->GetSpline()->SetControlPoints(controlPoints);
159 t->GetSpline()->SetKnots(knotList);
160 t->SetSplineLength(length);
165 bool returnValue =
true;
166 if (this->GetState() == Setup)
169 this->SetState(Setup);
177 return t->GetSplineLength();
179 throw std::invalid_argument(
"invalid index");
184 if (roundsPerSecond < 0.0001)
185 throw std::invalid_argument(
"Minimum tool speed is 0.0001 rounds per second");
189 t->SetVelocity(roundsPerSecond);
191 throw std::invalid_argument(
"invalid index");
196 std::lock_guard<std::mutex> toolsMutexLockHolder(m_ToolsMutex);
197 if (idx < m_AllTools.size())
198 return m_AllTools.at(idx);
206 std::lock_guard<std::mutex> trackingFinishedLockHolder(m_TrackingFinishedMutex);
208 if (this->GetState() != Tracking)
211 bool localStopTracking;
213 this->m_StopTrackingMutex.lock();
214 localStopTracking = this->m_StopTracking;
215 this->m_StopTrackingMutex.unlock();
217 mitk::ScalarType t = 0.0;
218 while ((this->GetState() == Tracking) && (localStopTracking ==
false))
221 for (
unsigned int i = 0; i < this->GetToolCount(); ++i)
223 mitk::VirtualTrackingTool::Pointer currentTool = this->GetInternalTool(i);
224 mitk::VirtualTrackingTool::SplineType::PointType pos;
226 pos = currentTool->GetSpline()->EvaluateSpline(t);
228 mitk::itk2vtk(pos, mp);
231 if (this->m_GaussianNoiseEnabled)
233 std::random_device rd;
234 std::mt19937 generator(rd());
235 std::normal_distribution<double> dist(this->m_MeanDistributionParam, this->m_DeviationDistributionParam);
236 double noise = dist(generator);
237 mp = mp + mitk::Vector(noise);
240 currentTool->SetPosition(mp);
246 mitk::Quaternion quat;
253 currentTool->SetOrientation(quat);
256 currentTool->SetTrackingError(2 * (rand() / (RAND_MAX + 1.0)));
257 currentTool->SetDataValid(
true);
258 currentTool->Modified();
260 itksys::SystemTools::Delay(m_RefreshRate);
262 this->m_StopTrackingMutex.lock();
263 localStopTracking = m_StopTracking;
264 this->m_StopTrackingMutex.unlock();
276 pos[0] = m_Bounds[0] + (m_Bounds[1] - m_Bounds[0]) * (rand() / (RAND_MAX + 1.0));
277 pos[1] = m_Bounds[2] + (m_Bounds[3] - m_Bounds[2]) * (rand() / (RAND_MAX + 1.0));
278 pos[2] = m_Bounds[4] + (m_Bounds[5] - m_Bounds[4]) * (rand() / (RAND_MAX + 1.0));
285 this->m_GaussianNoiseEnabled =
true;
290 this->m_GaussianNoiseEnabled =
false;
295 this->m_MeanDistributionParam = meanDistribution;
296 this->m_DeviationDistributionParam = deviationDistribution;
301 return m_DeviationDistributionParam;
306 return m_MeanDistributionParam;
An object of this class represents an exception of the MITK-IGT module.
static IGTTimeStamp * GetInstance()
returns a pointer to the current instance of mitkTimeStamp
void Start(itk::Object::Pointer device)
starts the time-acquisition
void Stop(itk::Object::Pointer device)
stops the time-acqusition
Interface for all Tracking Devices.
TrackingDeviceData m_Data
current device Data
mitk::VirtualTrackingTool * GetInternalTool(unsigned int idx)
double GetDeviationDistribution()
returns the deviation distribution for the Gaussian Noise
void InitializeSpline(mitk::VirtualTrackingTool *t)
initializes the spline path of the tool t with random control points inside the current tracking volu...
bool StartTracking() override
Starts the tracking.
void SetToolSpeed(unsigned int idx, mitk::ScalarType roundsPerSecond)
sets the speed of the tool idx in rounds per second
void DisableGaussianNoise()
disable addition of Gaussian Noise to Trackin coordinates
~VirtualTrackingDevice() override
mitk::ScalarType m_Bounds[6]
bounding box of the tracking volume stored as {xMin, xMax, yMin, yMax, zMin, zMax}
TrackingTool * GetTool(unsigned int toolNumber) const override
void TrackTools()
This method tracks tools as long as the variable m_Mode is set to "Tracking". Tracking tools means ge...
mitk::VirtualTrackingTool::SplineType::ControlPointType ControlPointType
void ThreadStartTracking()
static start method for tracking thread
bool CloseConnection() override
Closes the connection and clears all resources.
ControlPointType GetRandomPoint()
returns a random position inside the tracking volume (defined by m_Bounds)
mitk::ScalarType GetSplineChordLength(unsigned int idx)
return the approximate length of the spline for tool with index idx in millimeter
double GetMeanDistribution()
returns the mean distribution for the Gaussian Noise
void EnableGaussianNoise()
enable addition of Gaussian Noise to tracking coordinates
unsigned int GetToolCount() const override
void SetParamsForGaussianNoise(double meanDistribution, double deviationDistribution)
sets the mean distribution and the standard deviation for the Gaussian Noise
bool StopTracking() override
Stops the tracking.
bool OpenConnection() override
Opens the connection to the device. This have to be done before the tracking is started.
TrackingTool * AddTool(const char *toolName)
Adds a tool to the tracking device.