7 #include "PlusConfigure.h" 8 #include "vtkImageData.h" 9 #include "vtkMatrix4x4.h" 10 #include "vtkIGSIOSequenceIO.h" 11 #include "vtkObjectFactory.h" 16 #include "vtkIGSIOTrackedFrameList.h" 17 #include "vtksys/SystemTools.hxx" 23 : FrameBufferRowAlignment(1)
25 , RepeatEnabled(false)
26 , LoopStartTime_Local(0.0)
27 , LoopStopTime_Local(0.0)
28 , LocalVideoBuffer(NULL)
29 , UseAllFrameFields(false)
30 , UseOriginalTimestamps(false)
31 , LastAddedFrameUid(0)
32 , LastAddedLoopIndex(0)
33 , SimulatedStream(VIDEO_STREAM)
68 frameToBeAddedLoopIndex++;
69 frameToBeAddedUid -= numberOfFramesInTheLoop;
93 int currentLoopIndex = 0;
96 double currentFrameTime_Local = 0;
99 if (elapsedTime >= loopTime)
104 currentLoopIndex = 0;
105 currentFrameTime_Local = this->LoopStartTime_Local + elapsedTime;
109 currentLoopIndex = floor(elapsedTime / loopTime);
110 currentFrameTime_Local = this->LoopStartTime_Local + elapsedTime - loopTime * currentLoopIndex;
111 double latestTimestamp_Local = 0;
113 if (currentFrameTime_Local > latestTimestamp_Local)
117 currentFrameTime_Local = latestTimestamp_Local;
124 double closestFrameTime_Local = 0;
126 if (closestFrameTime_Local > currentFrameTime_Local)
129 currentFrameUid = closestFrameUid - 1;
133 currentFrameUid = closestFrameUid;
138 currentFrameUid += numberOfFramesInTheLoop;
143 currentFrameUid -= numberOfFramesInTheLoop;
151 for (
int addedFrames = 0; addedFrames < numberOfFramesToBeAdded; addedFrames++)
162 LOG_ERROR(
"vtkPlusSavedDataSource: Failed to retrieve item from the buffer, UID=" << frameToBeAddedUid);
169 double filteredTimestamp = dataBufferItemToBeAdded.
GetFilteredTimestamp(0.0) + frameToBeAddedLoopIndex * loopTime -
171 double unfilteredTimestamp = filteredTimestamp;
177 igsioFieldMapType fieldMap;
202 LOG_ERROR(
"vtkPlusSavedDataSource: Unable to get next item from local buffer from time for tool " << tool->GetId() <<
" - frame not available yet!");
206 LOG_ERROR(
"vtkPlusSavedDataSource: Unable to get next item from local buffer from time for tool " << tool->GetId() <<
" - frame not available anymore!");
210 LOG_ERROR(
"vtkPlusSavedDataSource: Unable to get next item from local buffer from time for tool " << tool->GetId() <<
"!");
216 vtkSmartPointer<vtkMatrix4x4> toolTransMatrix = vtkSmartPointer<vtkMatrix4x4>::New();
219 LOG_ERROR(
"Failed to get toolTransMatrix for tool " << tool->GetId());
224 ToolStatus toolStatus = bufferItem.
GetStatus();
245 frameToBeAddedLoopIndex++;
246 frameToBeAddedUid -= numberOfFramesInTheLoop;
269 LOG_ERROR(
"vtkPlusSavedDataSource: Failed to retrieve item from the buffer, UID=" << frameToBeAddedUid);
278 igsioFieldMapType fieldMap;
293 double nextFrameTimestamp = dataBufferItemToBeAdded.
GetTimestamp(0);
301 if (localTrackerBuffer)
309 LOG_ERROR(
"vtkPlusSavedDataSource: Unable to get next item from local buffer from time for tool " << tool->GetId() <<
" - frame not available yet!");
313 LOG_ERROR(
"vtkPlusSavedDataSource: Unable to get next item from local buffer from time for tool " << tool->GetId() <<
" - frame not available anymore!");
317 LOG_ERROR(
"vtkPlusSavedDataSource: Unable to get next item from local buffer from time for tool " << tool->GetId() <<
"!");
323 vtkSmartPointer<vtkMatrix4x4> toolTransMatrix = vtkSmartPointer<vtkMatrix4x4>::New();
326 LOG_ERROR(
"Failed to get toolTransMatrix for tool " << tool->GetId());
331 ToolStatus toolStatus = bufferItem.
GetStatus();
333 unsigned long frameNumber = tool->GetFrameNumber() + 1 ;
356 LOG_TRACE(
"vtkPlusSavedDataSource::Probe");
359 LOG_ERROR(
"vtkPlusSavedDataSource Probe failed: Unable to find sequence file!");
368 LOG_TRACE(
"vtkPlusSavedDataSource::InternalConnect");
372 LOG_ERROR(
"Unable to connect to saved data video source: Unable to read sequence metafile. No filename is specified.");
375 std::string foundAbsoluteImagePath;
377 if (!vtksys::SystemTools::FileExists(foundAbsoluteImagePath,
true))
379 LOG_ERROR(
"Unable to connect to saved data video source: Unable to read sequence metafile: " << this->
SequenceFile);
383 vtkSmartPointer<vtkIGSIOTrackedFrameList> savedDataBuffer = vtkSmartPointer<vtkIGSIOTrackedFrameList>::New();
386 vtkIGSIOSequenceIO::Read(foundAbsoluteImagePath, savedDataBuffer);
388 if (savedDataBuffer->GetNumberOfTrackedFrames() < 1)
390 LOG_ERROR(
"Failed to connect to saved dataset - there is no frame in the sequence metafile!");
414 LOG_ERROR(
"Local buffer is invalid");
418 double oldestTimestamp_Local = 0;
420 double latestTimestamp_Local = 0;
432 double framePeriodSec = 0;
434 if (frameRate != 0.0)
436 framePeriodSec = 1.0 / frameRate;
449 framePeriodSec = 1.0;
465 if (outputDataSource == NULL)
471 LOG_ERROR(
"Failed to set video buffer image type");
480 FrameSizeType frameSize;
481 if (savedDataBuffer->GetFrameSize(frameSize) !=
PLUS_SUCCESS)
483 LOG_ERROR(
"Unable to retrieve frame size.");
486 if (!savedDataBuffer->GetTrackedFrame(0)->GetImageData()->IsFrameEncoded())
490 unsigned int numberOfScalarComponents;
491 if (savedDataBuffer->GetTrackedFrame(0)->GetNumberOfScalarComponents(numberOfScalarComponents) !=
PLUS_SUCCESS)
493 LOG_ERROR(
"Unable to retrieve number of scalar components.");
497 if (!savedDataBuffer->GetTrackedFrame(0)->GetImageData()->IsFrameEncoded())
504 savedDataBuffer->Clear();
511 if (
source->SetInputImageOrientation(this->LocalVideoBuffer->GetImageOrientation()) !=
PLUS_SUCCESS)
513 LOG_ERROR(
source->GetId() <<
": Failed to set video image orientation");
518 if (
source->SetInputFrameSize(this->LocalVideoBuffer->GetFrameSize()) !=
PLUS_SUCCESS)
520 LOG_ERROR(
source->GetId() <<
": Failed to set video image orientation");
525 if (
source->SetNumberOfScalarComponents(this->LocalVideoBuffer->GetNumberOfScalarComponents()) !=
PLUS_SUCCESS)
527 LOG_ERROR(
source->GetId() <<
": Failed to set video image orientation");
534 if (
source->SetInputFrameSize(this->LocalVideoBuffer->GetFrameSize()) !=
PLUS_SUCCESS)
536 LOG_ERROR(
source->GetId() <<
": Failed to set video image orientation");
543 LOG_ERROR(
source->GetId() <<
": Failed to set video image orientation");
555 igsioTrackedFrame* frame = savedDataBuffer->GetTrackedFrame(0);
558 LOG_ERROR(
"The tracked frame buffer doesn't seem to contain any frames");
566 double transformMatrix[16] = {0};
570 if (tool->GetId().empty())
576 igsioTransformName toolTransformName(tool->GetId());
577 if (!frame->IsFrameTransformNameDefined(toolTransformName))
579 std::string strTransformName;
581 LOG_WARNING(
"Tool '" << tool->GetId() <<
"' has no matching transform in the file with name: " << strTransformName);
585 if (frame->GetFrameTransform(toolTransformName, transformMatrix) !=
PLUS_SUCCESS)
587 LOG_WARNING(
"Cannot convert the frame field ( for tool " << tool->GetId() <<
") to a transform");
591 tool->
SetBufferSize(savedDataBuffer->GetNumberOfTrackedFrames());
593 vtkSmartPointer<vtkPlusBuffer> buffer = vtkSmartPointer<vtkPlusBuffer>::New();
596 buffer->SetLocalTimeOffsetSec(0.0);
599 LOG_ERROR(
"Failed to retrieve tracking data from tracked frame list for tool " << tool->GetId());
603 buffer->Register(
this);
607 savedDataBuffer->Clear();
624 LOG_TRACE(
"vtkPlusSavedDataSource::ReadConfiguration");
629 if (deviceConfig->GetAttribute(
"SequenceMetafile"))
631 if (deviceConfig->GetAttribute(
"SequenceFile"))
633 LOG_WARNING(
"SavedDataSource SequenceMetafile and SequenceFile attributes are specified. Please remove the deprecated SequenceMetaFile attribute.");
634 XML_READ_CSTRING_ATTRIBUTE_REQUIRED(
SequenceFile, deviceConfig);
638 LOG_WARNING(
"Deprecated SequenceMetafile attribute is defined for SavedDataSource device. Please rename the SequenceMetafile attribute to SequenceFile.");
645 XML_READ_CSTRING_ATTRIBUTE_REQUIRED(
SequenceFile, deviceConfig);
650 LOG_ERROR(
"Sequence file attribute not read correctly. Check log.");
654 std::string foundAbsoluteImagePath;
658 std::string seqFileTrim = igsioCommon::Trim(sequenceFileStr);
659 std::string foundAbsoluteImagePath;
663 LOG_WARNING(
"Filename contains unexpected characters at beginning or end of string. Please correct. Filename: " << this->
SequenceFile);
667 LOG_ERROR(
"Unable to locate file: " << this->
SequenceFile <<
". Please verify location on disk.");
672 XML_READ_BOOL_ATTRIBUTE_OPTIONAL(
RepeatEnabled, deviceConfig);
675 const char* useData = deviceConfig->GetAttribute(
"UseData");
678 if (STRCASECMP(
"IMAGE", useData) == 0)
683 else if (STRCASECMP(
"IMAGE_AND_TRANSFORM", useData) == 0)
688 else if (STRCASECMP(
"TRANSFORM", useData) == 0)
695 LOG_WARNING(
"Unable to recognize UseData attribute: " << useData <<
" - changed to IMAGE by default!");
708 XML_WRITE_CSTRING_ATTRIBUTE_IF_NOT_NULL(
SequenceFile, imageAcquisitionConfig);
709 XML_WRITE_BOOL_ATTRIBUTE(
RepeatEnabled, imageAcquisitionConfig);
716 imageAcquisitionConfig->SetAttribute(
"UseData",
"IMAGE_AND_TRANSFORM");
720 imageAcquisitionConfig->SetAttribute(
"UseData",
"TRANSFORM");
725 imageAcquisitionConfig->SetAttribute(
"UseData",
"IMAGE");
736 LOG_ERROR(
"No output channels defined for vtkPlusSavedDataSource. Cannot proceed.");
746 LOG_ERROR(
"Buffer not created for vtkPlusSavedDataSource but it is required. Check configuration.");
774 LOG_ERROR(
"vtkPlusSavedDataSource::SetLoopTimeRange: Invalid local buffer");
793 LOG_ERROR(
"vtkPlusSavedDataSource::GetClosestFrameUidWithinTimeRange: Invalid local buffer");
798 if (time_Local < startTime_Local)
800 time_Local = startTime_Local;
802 else if (time_Local > stopTime_Local)
804 time_Local = stopTime_Local;
807 double oldestTimestamp_Local = 0;
809 double latestTimestamp_Local = 0;
813 if (time_Local < oldestTimestamp_Local)
815 time_Local = oldestTimestamp_Local;
817 else if (time_Local > latestTimestamp_Local)
819 time_Local = latestTimestamp_Local;
825 double closestFrameTime_Local = 0;
830 if (closestFrameTime_Local < startTime_Local)
832 return closestFrameUid + 1;
834 else if (closestFrameTime_Local > stopTime_Local)
836 return closestFrameUid - 1;
841 return closestFrameUid;
852 LOG_ERROR(
"Failed to get local tracker buffer - there is no active tool!");
869 if ((*it).second != NULL)
871 (*it).second->Delete();
896 LOG_WARNING(
"vtkPlusSavedDataSource LocalBuffer is invalid");
908 LOG_ERROR(
"No output channels defined");
919 LOG_ERROR(
"Unable to retrieve the video source in the SavedDataSource device.");
928 LOG_ERROR(
"Failed to get local tracker buffer - there is no active tool!");
938 LOG_WARNING(
"vtkPlusSavedDataSource OutputBuffer is invalid");
946 if (this->
Tools.size() > 0)
DataSourceContainer::const_iterator DataSourceContainerConstIterator
virtual PlusStatus SetBufferSize(int n)
virtual void PrintSelf(ostream &os, vtkIndent indent) VTK_OVERRIDE
ToolStatus GetStatus() const
virtual PlusStatus InternalConnectTracker(vtkIGSIOTrackedFrameList *savedDataBuffer)
PlusStatus SetImageType(US_IMAGE_TYPE imageType)
PlusStatus InternalUpdateCurrentTimestamp(BufferItemUidType frameToBeAddedUid, int frameToBeAddedLoopIndex)
std::string GetTransformName() const
virtual PlusStatus NotifyConfigured()
#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)
double LoopStartTime_Local
virtual PlusStatus InternalConnect()
static vtkPlusBuffer * New()
virtual PlusStatus ToolTimeStampedUpdate(const std::string &aToolSourceId, vtkMatrix4x4 *matrix, ToolStatus status, unsigned long frameNumber, double unfilteredtimestamp, const igsioFieldMapType *customFields=NULL)
std::vector< vtkPlusDataSource * > GetVideoSources() const
PlusStatus InternalUpdateOriginalTimestamp(BufferItemUidType frameToBeAddedUid, int frameToBeAddedLoopIndex)
PlusStatus SetImageOrientation(US_IMAGE_ORIENTATION imageOrientation)
void SetLoopTimeRange(double loopStartTime, double loopStopTime)
DataSourceContainer VideoSources
PlusStatus SetImageType(US_IMAGE_TYPE imageType)
virtual ItemStatus GetItemUidFromTime(double time, BufferItemUidType &uid)
BufferItemUidType LoopLastFrameUid
DataSourceContainer Tools
void DeleteLocalBuffers()
static vtkPlusConfig * GetInstance()
virtual PlusStatus SetBufferSize(int n)
igsioFieldMapType GetFrameFieldMap()
virtual BufferItemUidType GetLatestItemUidInBuffer()
double GetFilteredTimestamp(double localTimeOffsetSec)
virtual double GetStartTime()
virtual ItemStatus GetOldestTimeStamp(double &oldestTimestamp)
virtual PlusStatus WriteConfiguration(vtkXMLDataElement *config)
BufferItemUidType LoopFirstFrameUid
virtual PlusStatus Disconnect()
vtkPlusBuffer * LocalVideoBuffer
virtual ~vtkPlusSavedDataSource()
virtual void SetCorrectlyConfigured(bool)
PlusStatus GetFirstActiveTool(vtkPlusDataSource *&aTool) const
unsigned long FrameNumber
PlusStatus FindImagePath(const std::string &aImagePath, std::string &aFoundAbsolutePath)
virtual PlusStatus ReadConfiguration(vtkXMLDataElement *config)
virtual bool IsTracker() const
vtkPlusBuffer * GetLocalBuffer()
virtual void SetLocalTimeOffsetSec(double offsetSec)
vtkPlusBuffer * GetLocalTrackerBuffer()
std::map< std::string, vtkPlusBuffer * > LocalTrackerBuffers
virtual PlusStatus DeepCopyBufferTo(vtkPlusBuffer &bufferToFill)
void GetLoopTimeRange(double &loopStartTime, double &loopStopTime)
virtual double GetFrameRate(bool ideal=false, double *framePeriodStdevSecPtr=NULL)
virtual ItemStatus GetOldestStreamBufferItem(StreamBufferItem *bufferItem)
virtual ItemStatus GetStreamBufferItemFromTime(double time, StreamBufferItem *bufferItem, DataItemTemporalInterpolationType interpolation)
PlusStatus GetMatrix(vtkMatrix4x4 *outputMatrix)
PlusStatus SetFrameSize(unsigned int x, unsigned int y, unsigned int z, bool allocateFrames=true)
#define XML_FIND_DEVICE_ELEMENT_REQUIRED_FOR_READING(deviceConfig, rootConfigElement)
PlusStatus SetPixelType(igsioCommon::VTKScalarPixelType pixelType)
void PrintSelf(ostream &os, vtkIndent indent)
DataSourceContainer::iterator DataSourceContainerIterator
igsioVideoFrame & GetFrame()
virtual ItemStatus GetTimeStamp(BufferItemUidType uid, double ×tamp)
SimulatedStreamType SimulatedStream
virtual void SetSequenceFile(const char *)
bool StartThreadForInternalUpdates
Class for providing VTK video input interface from sequence fileAttributes:
DataSourceContainerConstIterator GetToolIteratorBegin() const
BufferItemUidType LastAddedFrameUid
vtkPlusDataSource * GetOutputDataSource()
virtual PlusStatus InternalDisconnect()
Contains an optional timestamped circular buffer containing the video images and a number of timestam...
ChannelContainer OutputChannels
virtual PlusStatus AddVideoItemToVideoSources(const std::vector< vtkPlusDataSource * > &videoSources, const igsioVideoFrame &frame, long frameNumber, double unfilteredTimestamp=UNDEFINED_TIMESTAMP, double filteredTimestamp=UNDEFINED_TIMESTAMP, const igsioFieldMapType *customFields=NULL)
virtual ItemStatus GetLatestTimeStamp(double &latestTimestamp)
PlusStatus CopyImagesFromTrackedFrameList(vtkIGSIOTrackedFrameList *sourceTrackedFrameList, TIMESTAMP_FILTERING_OPTION timestampFiltering, bool copyFrameFields)
virtual BufferItemUidType GetOldestItemUidInBuffer()
PlusStatus GetVideoSource(vtkPlusDataSource *&aVideoSource) const
vtkStandardNewMacro(vtkPlusSavedDataSource)
unsigned long long BufferItemUidType
PlusStatus InternalUpdate()
virtual PlusStatus InternalConnectVideo(vtkIGSIOTrackedFrameList *savedDataBuffer)
DataSourceContainerConstIterator GetToolIteratorEnd() const
PlusStatus SetNumberOfScalarComponents(unsigned int numberOfScalarComponents)
virtual char * GetSequenceFile()
bool UseOriginalTimestamps
double LoopStopTime_Local
bool HasValidTransformData() const
virtual int GetNumberOfItems()
double GetTimestamp(double localTimeOffsetSec)
BufferItemUidType GetClosestFrameUidWithinTimeRange(double time_Local, double startTime_Local, double stopTime_Local)
Interface to a 3D positioning tool, video source, or generalized data stream.