13 #include "PlusConfigure.h" 18 #include <MicronTrackerInterface.h> 19 #include <MicronTrackerLogger.h> 22 #include <vtkImageData.h> 23 #include <vtkImageImport.h> 24 #include <vtkMatrix4x4.h> 26 #include <vtkObjectFactory.h> 43 : IsMicronTrackingInitialized(false)
44 , MicronTracker(new MicronTrackerInterface())
45 #ifdef USE_MicronTracker_TIMESTAMPS
46 , TrackerTimeToSystemTimeSec(0)
47 , TrackerTimeToSystemTimeComputed(false)
49 , IniFile(
"MicronTracker.ini")
50 , FrameLeft(vtkSmartPointer<vtkImageData>::New())
51 , FrameRight(vtkSmartPointer<vtkImageData>::New())
62 this->
FrameLeft = vtkSmartPointer<vtkImageData>::New();
63 this->
FrameRight = vtkSmartPointer<vtkImageData>::New();
93 LOG_ERROR(
"vtkPlusMicronTracker::Probe should not be called while the device is already initialized");
98 LOG_DEBUG(
"Use MicronTracker ini file: " << iniFilePath);
99 if (!vtksys::SystemTools::FileExists(iniFilePath.c_str(),
true))
101 LOG_WARNING(
"Unable to find MicronTracker IniFile file at: " << iniFilePath);
104 LOG_DEBUG(
"Loading the marker templates from " << templateFullPath);
105 if (!vtksys::SystemTools::FileExists(templateFullPath.c_str(),
false))
107 LOG_WARNING(
"Unable to find MicronTracker TemplateDirectory at: " << templateFullPath);
112 LOG_ERROR(
"Error in initializing Micron Tracker");
119 LOG_ERROR(
"Error in initializing Micron Tracker: setup cameras failed. Check the camera connections.");
124 if (numOfCameras == 0)
126 LOG_ERROR(
"Error in initializing Micron Tracker: no cameras attached. Check the camera connections.");
130 LOG_DEBUG(
"Number of attached cameras: " << numOfCameras);
143 LOG_ERROR(
"InternalStartRecording failed: MicronTracker has not been initialized");
154 LOG_ERROR(
"InternalUpdate failed: MicronTracker has not been initialized");
159 const double unfilteredTimestamp = vtkIGSIOAccurateTimer::GetSystemTime();
164 LOG_WARNING(
"Failed to grab a new frame (" << this->
MicronTracker->GetLastErrorString() <<
"). Maybe the requested frame rate is too high.");
168 #ifdef USE_MicronTracker_TIMESTAMPS 169 if (!this->TrackerTimeToSystemTimeComputed)
171 const double timeSystemSec = unfilteredTimestamp;
172 const double timeTrackerSec = this->
MicronTracker->mtGetLatestFrameTime();
173 this->TrackerTimeToSystemTimeSec = timeSystemSec - timeTrackerSec;
174 this->TrackerTimeToSystemTimeComputed =
true;
176 const double timeTrackerSec = this->
MicronTracker->mtGetLatestFrameTime();
177 const double timeSystemSec = timeTrackerSec + this->TrackerTimeToSystemTimeSec;
182 LOG_ERROR(
"Error in processing a frame! (" << this->
MicronTracker->GetLastErrorString() <<
")");
192 int numOfIdentifiedMarkers = this->
MicronTracker->mtGetIdentifiedMarkersCount();
193 LOG_TRACE(
"Number of identified markers: " << numOfIdentifiedMarkers);
196 std::set<std::string> identifiedToolSourceIds;
197 vtkNew<vtkMatrix4x4> toolToTracker;
198 for (
int identifedMarkerIndex = 0; identifedMarkerIndex < numOfIdentifiedMarkers; identifedMarkerIndex++)
200 char* identifiedTemplateName = this->
MicronTracker->mtGetIdentifiedTemplateName(identifedMarkerIndex);
204 LOG_DEBUG(
"Marker " << identifiedTemplateName <<
" has no associated tool");
209 #ifdef USE_MicronTracker_TIMESTAMPS 215 identifiedToolSourceIds.insert(tool->
GetSourceId());
219 vtkNew<vtkMatrix4x4> transformMatrix;
222 if (identifiedToolSourceIds.find(it->second->GetSourceId()) != identifiedToolSourceIds.end())
225 LOG_TRACE(
"Tool " << it->second->GetSourceId() <<
": found");
228 LOG_TRACE(
"Tool " << it->second->GetSourceId() <<
": not found");
229 #ifdef USE_MicronTracker_TIMESTAMPS 244 LOG_ERROR(
"Failed to retrieve MicronTracker video source");
249 if (aSource->
AddItem((
i == 0) ? this->FrameLeft : this->FrameRight, US_IMG_ORIENT_MN, US_IMG_BRIGHTNESS, this->FrameNumber, unfilteredTimestamp) !=
PLUS_SUCCESS)
251 LOG_ERROR(
"Failed to add item " <<
i <<
" to MicronTracker video source");
264 std::vector<std::string> vTemplatesName;
265 std::vector<std::string> vTemplatesError;
268 LOG_DEBUG(
"Loading the marker templates from " << templateFullPath);
269 if (!vtksys::SystemTools::FileExists(templateFullPath.c_str(),
false))
271 LOG_WARNING(
"Unable to find MicronTracker TemplateDirectory at: " << templateFullPath);
273 int callResult = this->
MicronTracker->mtRefreshTemplates(vTemplatesName, vTemplatesError, templateFullPath);
274 for (
unsigned int i = 0;
i < vTemplatesName.size();
i++)
276 LOG_DEBUG(
"Loaded " << vTemplatesName[
i]);
280 LOG_ERROR(
"Failed to load marker templates from " << templateFullPath);
281 for (
unsigned int i = 0;
i < vTemplatesError.size();
i++)
283 LOG_ERROR(
"Error loading template: " << vTemplatesError[
i]);
293 std::vector<double> vRotMat;
295 std::vector<double> vPos;
298 transformMatrix->Identity();
300 for (
int col = 0; col < 3; col++)
302 for (
int row = 0; row < 3; row++)
304 transformMatrix->SetElement(row, col, vRotMat[rotIndex++]);
308 transformMatrix->SetElement(0, 3, vPos[0]);
309 transformMatrix->SetElement(1, 3, vPos[1]);
310 transformMatrix->SetElement(2, 3, vPos[2]);
316 igsioLockGuard<vtkIGSIORecursiveCriticalSection> updateMutexGuardedLock(this->
UpdateMutex);
318 unsigned char** leftImageArray = 0;
319 unsigned char** rightImageArray = 0;
320 if (this->
MicronTracker->mtGetLeftRightImageArray(leftImageArray, rightImageArray) != 0)
322 LOG_ERROR(
"Error getting images from MicronTracker");
326 if (leftImage != NULL)
328 vtkSmartPointer<vtkImageImport> imageImport = vtkSmartPointer<vtkImageImport>::New();
329 imageImport->SetDataScalarTypeToUnsignedChar();
330 imageImport->SetImportVoidPointer((
unsigned char*)leftImageArray);
331 imageImport->SetDataScalarTypeToUnsignedChar();
332 imageImport->SetDataExtent(0, this->
FrameSize[0] - 1, 0, this->
FrameSize[1] - 1, 0, 0);
333 imageImport->SetWholeExtent(0, this->
FrameSize[0] - 1, 0, this->
FrameSize[1] - 1, 0, 0);
334 imageImport->Update();
335 leftImage->DeepCopy(imageImport->GetOutput());
338 if (rightImage != NULL)
340 vtkSmartPointer<vtkImageImport> imageImport = vtkSmartPointer<vtkImageImport>::New();
341 imageImport->SetDataScalarTypeToUnsignedChar();
342 imageImport->SetImportVoidPointer((
unsigned char*)rightImageArray);
343 imageImport->SetDataScalarTypeToUnsignedChar();
344 imageImport->SetDataExtent(0, this->
FrameSize[0] - 1, 0, this->
FrameSize[1] - 1, 0, 0);
345 imageImport->SetWholeExtent(0, this->
FrameSize[0] - 1, 0, this->
FrameSize[1] - 1, 0, 0);
346 imageImport->Update();
347 rightImage->DeepCopy(imageImport->GetOutput());
359 XML_READ_STRING_ATTRIBUTE_OPTIONAL(
IniFile, deviceConfig);
370 XML_WRITE_STRING_ATTRIBUTE_IF_NOT_EMPTY(
IniFile, trackerConfig);
380 LOG_DEBUG(
"Already connected to MicronTracker");
385 LOG_DEBUG(
"Use MicronTracker ini file: " << iniFilePath);
386 if (!vtksys::SystemTools::FileExists(iniFilePath.c_str(),
true))
388 LOG_WARNING(
"Unable to find MicronTracker IniFile file at: " << iniFilePath);
391 LOG_DEBUG(
"Loading the marker templates from " << templateFullPath);
392 if (!vtksys::SystemTools::FileExists(templateFullPath.c_str(),
false))
394 LOG_ERROR(
"Unable to find MicronTracker TemplateDirectory at: " << templateFullPath);
399 LOG_ERROR(
"Error in initializing Micron Tracker");
406 LOG_ERROR(
"Error in initializing Micron Tracker: setup cameras failed. Check the camera connections and INI and Markers file locations.");
412 if (numOfCameras == 0)
414 LOG_ERROR(
"Error in initializing Micron Tracker: no cameras attached. Check the camera connections and INI and Markers file locations.");
418 LOG_DEBUG(
"Number of attached cameras: " << numOfCameras);
419 for (
int i = 0;
i < numOfCameras;
i++)
421 LOG_DEBUG(
"Camera " <<
i <<
": " 424 <<
"(serial number: " << this->
MicronTracker->mtGetSerialNum(
i) <<
")");
429 LOG_ERROR(
"Error in initializing Micron Tracker: Failed to load marker templates. Check if the marker directory is set correctly.");
434 #ifdef USE_MicronTracker_TIMESTAMPS 435 this->TrackerTimeToSystemTimeSec = 0;
436 this->TrackerTimeToSystemTimeComputed =
false;
441 if (imageWidth < 0 || imageHeight < 0)
443 LOG_ERROR(
"Invalid resolution returned from Micron device.");
446 this->
FrameSize[0] = static_cast<unsigned int>(imageWidth);
447 this->
FrameSize[1] = static_cast<unsigned int>(imageHeight);
470 LOG_ERROR(
"Micron stereo camera capture requires exactly 2 video sources. Check configuration.");
482 case MicronTrackerLogger::WARNING_LEVEL:
485 case MicronTrackerLogger::DEBUG_LEVEL:
488 LOG_DEBUG(
"MicronTracker: " <<
message);
491 case MicronTrackerLogger::ERROR_LEVEL:
DataSourceContainer::const_iterator DataSourceContainerConstIterator
void GetTransformMatrix(int markerIndex, vtkMatrix4x4 *transformMatrix)
PlusStatus RefreshMarkerTemplates()
const char int const char const char Phidget_LogLevel level
vtkSmartPointer< vtkImageData > FrameRight
virtual int GetNumberOfVideoSources() const
vtkStandardNewMacro(vtkPlusMicronTracker)
#define XML_FIND_DEVICE_ELEMENT_REQUIRED_FOR_WRITING(deviceConfig, rootConfigElement)
virtual PlusStatus ToolTimeStampedUpdateWithoutFiltering(const std::string &aToolSourceId, vtkMatrix4x4 *matrix, ToolStatus status, double unfilteredtimestamp, double filteredtimestamp, const igsioFieldMapType *customFields=NULL)
std::string GetSourceId() const
PlusStatus GetVideoSourceByIndex(const unsigned int index, vtkPlusDataSource *&aVideoSource)
PlusStatus SetInputFrameSize(unsigned int x, unsigned int y, unsigned int z)
virtual PlusStatus ToolTimeStampedUpdate(const std::string &aToolSourceId, vtkMatrix4x4 *matrix, ToolStatus status, unsigned long frameNumber, double unfilteredtimestamp, const igsioFieldMapType *customFields=NULL)
virtual PlusStatus AddItem(vtkImageData *frame, US_IMAGE_ORIENTATION usImageOrientation, US_IMAGE_TYPE imageType, long frameNumber, double unfilteredTimestamp=UNDEFINED_TIMESTAMP, double filteredTimestamp=UNDEFINED_TIMESTAMP, const igsioFieldMapType *customFields=NULL)
bool RequirePortNameInDeviceSetConfiguration
PlusStatus InternalStartRecording()
virtual PlusStatus InternalDisconnect()
PlusStatus GetImage(vtkImageData *leftImage, vtkImageData *rightImage)
virtual PlusStatus SetInputImageOrientation(US_IMAGE_ORIENTATION imageOrientation)
static vtkPlusConfig * GetInstance()
Interface class to Claron MicronTracker optical trackers.
MicronTrackerInterface * MicronTracker
PlusStatus InternalUpdate()
unsigned long FrameNumber
PlusStatus GetToolByPortName(const char *aPortName, vtkPlusDataSource *&aSource)
virtual PlusStatus NotifyConfigured()
vtkIGSIORecursiveCriticalSection * UpdateMutex
bool IsMicronTrackingInitialized
static void LogMessageCallback(int level, const char *message, void *userdata)
#define XML_FIND_DEVICE_ELEMENT_REQUIRED_FOR_READING(deviceConfig, rootConfigElement)
bool StartThreadForInternalUpdates
DataSourceContainerConstIterator GetToolIteratorBegin() const
virtual PlusStatus InternalConnect()
virtual PlusStatus ReadConfiguration(vtkXMLDataElement *config)
std::string TemplateDirectory
std::string GetDeviceSetConfigurationPath(const std::string &subPath)
virtual PlusStatus WriteConfiguration(vtkXMLDataElement *rootConfigElement)
DataSourceContainerConstIterator GetToolIteratorEnd() const
virtual std::string GetSdkVersion()
vtkSmartPointer< vtkImageData > FrameLeft
Interface to a 3D positioning tool, video source, or generalized data stream.