19 #include "mitkIGTException.h"
24 #include <itksys/SystemTools.hxx>
25 #include <itkMutexLockHolder.h>
33 m_AllTools(), m_ToolsMutex(NULL), m_MultiThreader(NULL), m_ThreadID(-1), m_RefreshRate(100), m_NumberOfControlPoints(20), m_GaussianNoiseEnabled(false),
34 m_MeanDistributionParam(0.0), m_DeviationDistributionParam(1.0)
44 if (this->GetState() == Tracking)
48 if (this->GetState() == Ready)
50 this->CloseConnection();
53 if (m_MultiThreader.IsNotNull() && (m_ThreadID != -1))
55 m_MultiThreader->TerminateThread(m_ThreadID);
56 m_MultiThreader = NULL;
68 t->SetToolName(toolName);
70 this->InitializeSpline(t);
72 m_AllTools.push_back(t);
78 if (this->GetState() != Ready)
80 this->SetState(Tracking);
81 this->m_StopTrackingMutex->Lock();
82 this->m_StopTracking =
false;
83 this->m_StopTrackingMutex->Unlock();
87 if (m_MultiThreader.IsNotNull() && (m_ThreadID != -1))
88 m_MultiThreader->TerminateThread(m_ThreadID);
89 if (m_MultiThreader.IsNull())
92 m_ThreadID = m_MultiThreader->SpawnThread(this->ThreadStartTracking,
this);
98 if (this->GetState() == Tracking)
100 m_StopTrackingMutex->Lock();
101 m_StopTracking =
true;
102 m_StopTrackingMutex->Unlock();
104 m_TrackingFinishedMutex->Lock();
105 this->SetState(Ready);
106 m_TrackingFinishedMutex->Unlock();
117 return static_cast<unsigned int>(this->m_AllTools.size());
123 if (toolNumber < m_AllTools.size())
124 return this->m_AllTools.at(toolNumber);
130 if (m_NumberOfControlPoints < 1)
136 this->SetState(Ready);
147 SplineType::ControlPointListType controlPoints;
148 controlPoints.reserve(m_NumberOfControlPoints + 1);
150 controlPoints.push_back(this->GetRandomPoint());
152 for (
unsigned int i = 1; i < m_NumberOfControlPoints - 1; ++i)
154 SplineType::ControlPointType pos;
155 pos = this->GetRandomPoint();
156 length += controlPoints.at(i - 1).EuclideanDistanceTo(pos);
157 controlPoints.push_back(pos);
159 controlPoints.push_back(controlPoints.at(0));
160 length += controlPoints.at(controlPoints.size() - 2).EuclideanDistanceTo(controlPoints.at(controlPoints.size() - 1));
163 SplineType::KnotListType knotList;
164 knotList.push_back(0.0);
166 knotList.push_back(i);
176 bool returnValue =
true;
177 if (this->GetState() ==
Setup)
180 this->SetState(
Setup);
190 throw std::invalid_argument(
"invalid index");
195 if (roundsPerSecond < 0.0001)
196 throw std::invalid_argument(
"Minimum tool speed is 0.0001 rounds per second");
202 throw std::invalid_argument(
"invalid index");
208 if (idx < m_AllTools.size())
209 return m_AllTools.at(idx);
219 if (this->GetState() != Tracking)
222 bool localStopTracking;
224 this->m_StopTrackingMutex->Lock();
225 localStopTracking = this->m_StopTracking;
226 this->m_StopTrackingMutex->Unlock();
229 while ((this->GetState() == Tracking) && (localStopTracking ==
false))
232 for (
unsigned int i = 0; i < this->GetToolCount(); ++i)
237 pos = currentTool->GetSpline()->EvaluateSpline(t);
242 if (this->m_GaussianNoiseEnabled)
244 std::random_device rd;
245 std::mt19937 generator(rd());
246 std::normal_distribution<double> dist(this->m_MeanDistributionParam, this->m_DeviationDistributionParam);
247 double noise = dist(generator);
251 currentTool->SetPosition(mp);
264 currentTool->SetOrientation(quat);
267 currentTool->SetTrackingError(2 * (rand() / (RAND_MAX + 1.0)));
268 currentTool->SetDataValid(
true);
269 currentTool->Modified();
271 itksys::SystemTools::Delay(m_RefreshRate);
273 this->m_StopTrackingMutex->Lock();
274 localStopTracking = m_StopTracking;
275 this->m_StopTrackingMutex->Unlock();
282 struct itk::MultiThreader::ThreadInfoStruct * pInfo = (
struct itk::MultiThreader::ThreadInfoStruct*)pInfoStruct;
285 return ITK_THREAD_RETURN_VALUE;
287 if (pInfo->UserData == NULL)
289 return ITK_THREAD_RETURN_VALUE;
293 if (trackingDevice != NULL)
297 return ITK_THREAD_RETURN_VALUE;
303 pos[0] = m_Bounds[0] + (m_Bounds[1] - m_Bounds[0]) * (rand() / (RAND_MAX + 1.0));
304 pos[1] = m_Bounds[2] + (m_Bounds[3] - m_Bounds[2]) * (rand() / (RAND_MAX + 1.0));
305 pos[2] = m_Bounds[4] + (m_Bounds[5] - m_Bounds[4]) * (rand() / (RAND_MAX + 1.0));
312 this->m_GaussianNoiseEnabled =
true;
317 this->m_GaussianNoiseEnabled =
false;
322 this->m_MeanDistributionParam = meanDistribution;
323 this->m_DeviationDistributionParam = deviationDistribution;
328 return m_DeviationDistributionParam;
333 return m_MeanDistributionParam;
void EnableGaussianNoise()
enable addition of Gaussian Noise to tracking coordinates
itk::FastMutexLock::Pointer m_ToolsMutex
mutex for coordinated access of tool container
itk::SmartPointer< Self > Pointer
double GetDeviationDistribution()
returns the deviation distribution for the Gaussian Noise
static ITK_THREAD_RETURN_TYPE ThreadStartTracking(void *data)
static start method for tracking thread
TrackingTool * AddTool(const char *toolName)
Adds a tool to the tracking device.
TrackingTool * GetTool(unsigned int toolNumber) const override
An object of this class represents an exception of the MITK-IGT module.
DataCollection - Class to facilitate loading/accessing structured data.
void SetParamsForGaussianNoise(double meanDistribution, double deviationDistribution)
sets the mean distribution and the standard deviation for the Gaussian Noise
itk::MutexLockHolder< itk::FastMutexLock > MutexLockHolder
double GetMeanDistribution()
returns the mean distribution for the Gaussian Noise
void DisableGaussianNoise()
disable addition of Gaussian Noise to Trackin coordinates
mitk::VirtualTrackingTool * GetInternalTool(unsigned int idx)
void TrackTools()
This method tracks tools as long as the variable m_Mode is set to "Tracking". Tracking tools means ge...
virtual bool CloseConnection() override
Closes the connection and clears all resources.
Interface for all Tracking Devices.
static IGTTimeStamp * GetInstance()
returns a pointer to the current instance of mitkTimeStamp
vnl_quaternion< ScalarType > Quaternion
void Start(itk::Object::Pointer device)
starts the time-acquisition
virtual bool OpenConnection() override
Opens the connection to the device. This have to be done before the tracking is started.
mitk::VirtualTrackingTool::SplineType::ControlPointType ControlPointType
virtual bool StartTracking() override
Starts the tracking.
void InitializeSpline(mitk::VirtualTrackingTool *t)
initializes the spline path of the tool t with random control points inside the current tracking volu...
TrackingDeviceData m_Data
current device Data
void itk2vtk(const Tin &in, Tout &out)
#define mitkThrowException(classname)
mitk::ScalarType m_Bounds[6]
bounding box of the tracking volume stored as {xMin, xMax, yMin, yMax, zMin, zMax} ...
virtual unsigned int GetToolCount() const override
void Stop(itk::Object::Pointer device)
stops the time-acqusition
void SetToolSpeed(unsigned int idx, mitk::ScalarType roundsPerSecond)
sets the speed of the tool idx in rounds per second
ControlPointType GetRandomPoint()
returns a random position inside the tracking volume (defined by m_Bounds)
Class representing a tracking device which generates random positions / orientations. No hardware is needed for tracking device.
virtual bool StopTracking() override
Stops the tracking.
mitk::ScalarType GetSplineChordLength(unsigned int idx)
return the approximate length of the spline for tool with index idx in millimeter ...
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.