PlusLib  2.9.0
Software library for tracked ultrasound image acquisition, calibration, and processing.
vtkDataCollectorTest2.cxx
Go to the documentation of this file.
1 /*=Plus=header=begin======================================================
2 Program: Plus
3 Copyright (c) Laboratory for Percutaneous Surgery. All rights reserved.
4 See License.txt for details.
5 =========================================================Plus=header=end*/
6 
12 // Local includes
13 #include "PlusConfigure.h"
14 #include "vtkPlusBuffer.h"
15 #include "vtkPlusChannel.h"
16 #include "vtkPlusDataCollector.h"
17 #include "vtkPlusDataSource.h"
18 #include "vtkPlusDevice.h"
19 #include "vtkPlusSavedDataSource.h"
20 
21 // VTK includes
22 #include <vtkSmartPointer.h>
23 #include <vtkTimerLog.h>
24 #include <vtkXMLUtilities.h>
25 #include <vtksys/CommandLineArguments.hxx>
26 #include <vtksys/SystemTools.hxx>
27 
28 int main(int argc, char** argv)
29 {
30  int numberOfFailures(0);
31  std::string inputConfigFileName;
32  double inputAcqTimeLength(20);
33  std::string outputTrackerBufferSequenceFileName("TrackerBufferMetafile.nrrd");
34  std::string outputVideoBufferSequenceFileName("VideoBufferMetafile.nrrd");
35  std::string inputVideoBufferMetafile;
36  std::string inputTrackerBufferMetafile;
37  bool outputCompressed(true);
38 
39  int verboseLevel = vtkPlusLogger::LOG_LEVEL_UNDEFINED;
40 
41  vtksys::CommandLineArguments args;
42  args.Initialize(argc, argv);
43 
44  args.AddArgument("--config-file", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &inputConfigFileName, "Name of the input configuration file.");
45  args.AddArgument("--acq-time-length", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &inputAcqTimeLength, "Length of acquisition time in seconds (Default: 20s)");
46  args.AddArgument("--video-buffer-seq-file", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &inputVideoBufferMetafile, "Video buffer sequence metafile.");
47  args.AddArgument("--tracker-buffer-seq-file", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &inputTrackerBufferMetafile, "Tracker buffer sequence metafile.");
48  args.AddArgument("--output-tracker-buffer-seq-file", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &outputTrackerBufferSequenceFileName, "Filename of the output tracker buffer sequence metafile (Default: TrackerBufferMetafile)");
49  args.AddArgument("--output-video-buffer-seq-file", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &outputVideoBufferSequenceFileName, "Filename of the output video buffer sequence metafile (Default: VideoBufferMetafile)");
50  args.AddArgument("--output-compressed", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &outputCompressed, "Compressed output (0=non-compressed, 1=compressed, default:compressed)");
51  args.AddArgument("--verbose", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &verboseLevel, "Verbose level (1=error only, 2=warning, 3=info, 4=debug, 5=trace)");
52 
53  if (!args.Parse())
54  {
55  std::cerr << "Problem parsing arguments" << std::endl;
56  std::cout << "Help: " << args.GetHelp() << std::endl;
57  exit(EXIT_FAILURE);
58  }
59 
60  vtkPlusLogger::Instance()->SetLogLevel(verboseLevel);
61 
62  if (inputConfigFileName.empty())
63  {
64  std::cerr << "input-config-file-name is required" << std::endl;
65  exit(EXIT_FAILURE);
66  }
67 
69 
70  vtkSmartPointer<vtkXMLDataElement> configRootElement = vtkSmartPointer<vtkXMLDataElement>::New();
71  if (PlusXmlUtils::ReadDeviceSetConfigurationFromFile(configRootElement, inputConfigFileName.c_str()) == PLUS_FAIL)
72  {
73  LOG_ERROR("Unable to read configuration from file " << inputConfigFileName.c_str());
74  return EXIT_FAILURE;
75  }
76 
78 
79  vtkSmartPointer<vtkPlusDataCollector> dataCollector = vtkSmartPointer<vtkPlusDataCollector>::New();
80 
81  if (dataCollector->ReadConfiguration(configRootElement) != PLUS_SUCCESS)
82  {
83  LOG_ERROR("Unable to configure data collector in configuration file specified by: " << inputConfigFileName);
84  exit(EXIT_FAILURE);
85  }
86  vtkPlusDevice* videoDevice = NULL;
87  vtkPlusDevice* trackerDevice = NULL;
88 
89  if (!inputVideoBufferMetafile.empty())
90  {
91  if (dataCollector->GetDevice(videoDevice, "VideoDevice") != PLUS_SUCCESS)
92  {
93  LOG_ERROR("Unable to locate the device with Id=\"VideoDevice\". Check config file.");
94  exit(EXIT_FAILURE);
95  }
96  vtkPlusSavedDataSource* videoSource = dynamic_cast<vtkPlusSavedDataSource*>(videoDevice);
97  if (videoSource == NULL)
98  {
99  LOG_ERROR("Unable to cast device to vtkPlusSavedDataSource.");
100  exit(EXIT_FAILURE);
101  }
102  videoSource->SetSequenceFile(inputVideoBufferMetafile.c_str());
103  }
104 
105  if (!inputTrackerBufferMetafile.empty())
106  {
107  if (dataCollector->GetDevice(trackerDevice, "TrackerDevice") != PLUS_SUCCESS)
108  {
109  LOG_ERROR("Unable to locate the device with Id=\"TrackerDevice\". Check config file.");
110  exit(EXIT_FAILURE);
111  }
112  vtkPlusSavedDataSource* tracker = dynamic_cast<vtkPlusSavedDataSource*>(trackerDevice);
113  if (tracker == NULL)
114  {
115  LOG_ERROR("Unable to cast tracker to vtkPlusSavedDataSource");
116  exit(EXIT_FAILURE);
117  }
118  tracker->SetSequenceFile(inputTrackerBufferMetafile.c_str());
119  }
120 
121  if (dataCollector->Connect() != PLUS_SUCCESS)
122  {
123  LOG_ERROR("Failed to connect to data collector!");
124  exit(EXIT_FAILURE);
125  }
126 
127  if (dataCollector->Start() != PLUS_SUCCESS)
128  {
129  LOG_ERROR("Failed to start data collection");
130  exit(EXIT_FAILURE);
131  }
132 
133  const double acqStartTime = vtkTimerLog::GetUniversalTime();
134 
135  while (acqStartTime + inputAcqTimeLength > vtkTimerLog::GetUniversalTime())
136  {
137  LOG_INFO(acqStartTime + inputAcqTimeLength - vtkTimerLog::GetUniversalTime() << " seconds left...");
138  vtksys::SystemTools::Delay(1000);
139  }
140 
141  vtkSmartPointer<vtkPlusBuffer> videobuffer = vtkSmartPointer<vtkPlusBuffer>::New();
142  vtkPlusChannel* aChannel(NULL);
143  vtkPlusDataSource* aSource(NULL);
144  if (videoDevice == NULL || videoDevice->GetOutputChannelByName(aChannel, "VideoStream") != PLUS_SUCCESS || aChannel == NULL || aChannel->GetVideoSource(aSource) != PLUS_SUCCESS)
145  {
146  LOG_ERROR("Unable to retrieve the video source.");
147  exit(EXIT_FAILURE);
148  }
149 
150  aSource->DeepCopyBufferTo(*videobuffer);
151 
152  vtkSmartPointer<vtkPlusDevice> tracker = vtkSmartPointer<vtkPlusDevice>::New();
153  if (trackerDevice != NULL)
154  {
155  LOG_INFO("Copy tracker");
156  tracker->DeepCopy(*trackerDevice);
157  }
158 
159  if (videoDevice != NULL)
160  {
161  std::string fullPath = vtkPlusConfig::GetInstance()->GetOutputPath(outputVideoBufferSequenceFileName);
162  LOG_INFO("Write video buffer to " << fullPath);
163  videobuffer->WriteToSequenceFile(fullPath.c_str(), outputCompressed);
164  }
165 
166  if (trackerDevice != NULL)
167  {
168  std::string fullPath = vtkPlusConfig::GetInstance()->GetOutputPath(outputTrackerBufferSequenceFileName);
169  LOG_INFO("Write tracker buffer to " << fullPath);
170  tracker->WriteToolsToSequenceFile(fullPath.c_str(), outputCompressed);
171  }
172 
173  if (videoDevice != NULL)
174  {
175  std::string fullPath = vtkPlusConfig::GetInstance()->GetOutputPath(outputVideoBufferSequenceFileName);
176  if (vtksys::SystemTools::FileExists(fullPath.c_str(), true))
177  {
178  LOG_INFO("Remove generated video metafile!");
179  if (!vtksys::SystemTools::RemoveFile(fullPath.c_str()))
180  {
181  LOG_ERROR("Unable to remove generated video buffer: " << fullPath);
182  numberOfFailures++;
183  }
184  }
185  else
186  {
187  LOG_ERROR("Unable to find video buffer at: " << fullPath);
188  numberOfFailures++;
189  }
190  }
191 
192  if (trackerDevice != NULL)
193  {
194  std::string fullPath = vtkPlusConfig::GetInstance()->GetOutputPath(outputTrackerBufferSequenceFileName);
195  if (vtksys::SystemTools::FileExists(fullPath.c_str(), true))
196  {
197  LOG_INFO("Remove generated tracker metafile!");
198  if (!vtksys::SystemTools::RemoveFile(fullPath.c_str()))
199  {
200  LOG_ERROR("Unable to remove generated tracker buffer: " << fullPath);
201  numberOfFailures++;
202  }
203  }
204  else
205  {
206  LOG_ERROR("Unable to find tracker buffer at: " << fullPath);
207  numberOfFailures++;
208  }
209  }
210 
211  if (dataCollector->Stop() != PLUS_SUCCESS)
212  {
213  LOG_ERROR("Failed to stop data collection!");
214  numberOfFailures++;
215  }
216 
217  if (numberOfFailures > 0)
218  {
219  LOG_ERROR("Number of failures: " << numberOfFailures);
220  return EXIT_FAILURE;
221  }
222 
223  std::cout << "Test completed successfully!" << std::endl;
224  return EXIT_SUCCESS;
225 }
std::string GetOutputPath(const std::string &subPath)
Abstract interface for tracker and video devices.
Definition: vtkPlusDevice.h:60
#define PLUS_FAIL
Definition: PlusCommon.h:43
static vtkPlusConfig * GetInstance()
PlusStatus GetOutputChannelByName(vtkPlusChannel *&aChannel, const char *aChannelId)
#define PLUS_SUCCESS
Definition: PlusCommon.h:44
int main(int argc, char **argv)
virtual PlusStatus DeepCopyBufferTo(vtkPlusBuffer &bufferToFill)
virtual void SetSequenceFile(const char *)
Class for providing VTK video input interface from sequence fileAttributes:
static vtkIGSIOLogger * Instance()
Contains an optional timestamped circular buffer containing the video images and a number of timestam...
PlusStatus GetVideoSource(vtkPlusDataSource *&aVideoSource) const
void SetDeviceSetConfigurationData(vtkXMLDataElement *deviceSetConfigurationData)
static PlusStatus ReadDeviceSetConfigurationFromFile(vtkXMLDataElement *config, const char *filename)
Definition: PlusXmlUtils.h:23
Interface to a 3D positioning tool, video source, or generalized data stream.