PlusLib  2.9.0
Software library for tracked ultrasound image acquisition, calibration, and processing.
vtkWinProbeVideoSourceTest.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 
14 #include "PlusConfigure.h"
15 #include "vtkImageData.h"
16 #include "vtkSmartPointer.h"
18 #include "vtkXMLUtilities.h"
19 #include "vtksys/CommandLineArguments.hxx"
20 #include "vtkImageViewer.h"
21 #include "vtkActor2D.h"
22 #include "vtkRenderWindowInteractor.h"
23 #include "vtkPlusDataSource.h"
24 
25 class vtkMyCallback : public vtkCommand
26 {
27 public:
28  static vtkMyCallback* New() { return new vtkMyCallback; }
29 
30  virtual void Execute(vtkObject* caller, unsigned long, void*)
31  {
32  m_Viewer->Render();
33 
34  //update the timer so it will trigger again
35  m_Interactor->CreateTimer(VTKI_TIMER_UPDATE);
36  }
37 
38  vtkRenderWindowInteractor* m_Interactor;
39  vtkImageViewer* m_Viewer;
40 
41 private:
42 
43  vtkMyCallback()
44  {
45  m_Interactor = nullptr;
46  m_Viewer = nullptr;
47  }
48 };
49 
50 int main(int argc, char* argv[])
51 {
52  bool printHelp(false);
53  bool renderingOff(false);
54  std::string inputConfigFileName;
55  std::string outputFileName("WinProbeOutputSeq.nrrd");
56  int verboseLevel = vtkPlusLogger::LOG_LEVEL_DEBUG;
57 
58  vtksys::CommandLineArguments args;
59  args.Initialize(argc, argv);
60  args.AddArgument("--help", vtksys::CommandLineArguments::NO_ARGUMENT, &printHelp, "Print this help.");
61  args.AddArgument("--config-file", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &inputConfigFileName, "Config file containing the device configuration.");
62  args.AddArgument("--rendering-off", vtksys::CommandLineArguments::NO_ARGUMENT, &renderingOff, "Run test without rendering.");
63  args.AddArgument("--output-seq-file", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &outputFileName, "Filename of the output video buffer sequence metafile (Default: VideoBufferMetafile)");
64  args.AddArgument("--verbose", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &verboseLevel, "Verbose level 1=error only, 2=warning, 3=info, 4=debug, 5=trace)");
65 
66  if(!args.Parse())
67  {
68  std::cerr << "Problem parsing arguments" << std::endl;
69  std::cout << "\nvtkPlusWinProbeVideoSourceTest help:" << args.GetHelp() << std::endl;
70  exit(EXIT_FAILURE);
71  }
72 
73  vtkPlusLogger::Instance()->SetLogLevel(verboseLevel);
74  if(printHelp)
75  {
76  std::cout << "\n\nvtkPlusWinProbeVideoSourceTest help:" << args.GetHelp() << std::endl;
77  exit(EXIT_SUCCESS);
78  }
79 
80 
81  vtkSmartPointer< vtkPlusWinProbeVideoSource > WinProbeDevice = vtkSmartPointer< vtkPlusWinProbeVideoSource >::New();
82  WinProbeDevice->SetDeviceId("VideoDeviceWP");
83 
84  vtkSmartPointer<vtkXMLDataElement> configRootElement = vtkSmartPointer<vtkXMLDataElement>::New();
85  if(STRCASECMP(inputConfigFileName.c_str(), "") != 0)
86  {
87  LOG_DEBUG("Reading config file...");
88 
89  if(PlusXmlUtils::ReadDeviceSetConfigurationFromFile(configRootElement, inputConfigFileName.c_str()) == PLUS_FAIL)
90  {
91  LOG_ERROR("Unable to read configuration from file " << inputConfigFileName.c_str());
92  return EXIT_FAILURE;
93  }
94 
95  WinProbeDevice->ReadConfiguration(configRootElement);
96  }
97 
98  std::cout << "\n" << *WinProbeDevice; //invokes PrintSelf()
99 
100  if(WinProbeDevice->Connect() != PLUS_SUCCESS)
101  {
102  LOG_ERROR("Unable to connect to WinProbe Probe");
103  exit(EXIT_FAILURE);
104  }
105 
106  //test starting and stopping (pausing recording)
107  WinProbeDevice->StartRecording(); //applies the setting read from config file
108  std::cout << "\n" << *WinProbeDevice; //invokes PrintSelf()
109  WinProbeDevice->StopRecording();
110 
111  //test TGCs
112  double tgc = WinProbeDevice->GetTimeGainCompensation(7);
113  tgc = WinProbeDevice->GetTimeGainCompensation(3);
114  WinProbeDevice->SetTimeGainCompensation(3, 0.2);
115  tgc = WinProbeDevice->GetTimeGainCompensation(3);
116 
117  //test intensity compression
118  uint16_t uVal = WinProbeDevice->GetLogLinearKnee();
119  WinProbeDevice->SetLogLinearKnee(123);
120  uVal = WinProbeDevice->GetLogLinearKnee();
121 
122  WinProbeDevice->StartRecording();
123 
124  if(renderingOff)
125  {
126  Sleep(1500);
127  WinProbeDevice->ARFIPush(); // in case we are in ARFI mode, invoke it
128  Sleep(8000); //allow some time to buffer frames
129 
130  vtkPlusChannel* bChannel(nullptr);
131  if(WinProbeDevice->GetOutputChannelByName(bChannel, "VideoStream") != PLUS_SUCCESS)
132  {
133  LOG_ERROR("Unable to locate the channel with Id=\"VideoStream\". Check config file.");
134  return EXIT_FAILURE;
135  }
136 
137  vtkPlusChannel* rfChannel(nullptr);
138  if(WinProbeDevice->GetOutputChannelByName(rfChannel, "AdditionalStream") != PLUS_SUCCESS)
139  {
140  LOG_WARNING("Unable to locate the channel with Id=\"AdditionalStream\". Additional mode will not be used.");
141  }
142 
143  WinProbeDevice->FreezeDevice(true);
144 
145  vtkPlusDataSource* bSource(nullptr);
146  bChannel->GetVideoSource(bSource);
147  bSource->WriteToSequenceFile(outputFileName.c_str());
148 
149  if(rfChannel)
150  {
151  vtkPlusDataSource* rfSource(nullptr);
152  rfChannel->GetVideoSource(rfSource);
153  rfSource->WriteToSequenceFile((outputFileName + "_RF.mha").c_str());
154  }
155 
156  //update and write configuration
157  WinProbeDevice->WriteConfiguration(configRootElement);
158  bool success = vtkXMLUtilities::WriteElementToFile(configRootElement, (outputFileName + ".xml").c_str());
159  if(!success)
160  {
161  LOG_ERROR("Unable to write configuration to: " << outputFileName + ".xml");
162  }
163  else
164  {
165  LOG_INFO("Configuration file written to: " << outputFileName + ".xml");
166  }
167 
168  WinProbeDevice->FreezeDevice(false);
169  }
170  else
171  {
172  vtkSmartPointer<vtkImageViewer> viewer = vtkSmartPointer<vtkImageViewer>::New();
173  viewer->SetInputConnection(WinProbeDevice->GetOutputPort(0)); //set image to the render and window
174  viewer->SetColorWindow(255);
175  viewer->SetColorLevel(127.5);
176  viewer->SetZSlice(0);
177  viewer->SetSize(256, 640);
178 
179  //Create the interactor that handles the event loop
180  vtkSmartPointer<vtkRenderWindowInteractor> iren = vtkSmartPointer<vtkRenderWindowInteractor>::New();
181  iren->SetRenderWindow(viewer->GetRenderWindow());
182  viewer->SetupInteractor(iren);
183 
184  viewer->Render(); //must be called after iren and viewer are linked or there will be problems
185 
186  // Establish timer event and create timer to update the live image
187  vtkSmartPointer<vtkMyCallback> call = vtkSmartPointer<vtkMyCallback>::New();
188  call->m_Interactor = iren;
189  call->m_Viewer = viewer;
190  iren->AddObserver(vtkCommand::TimerEvent, call);
191  iren->CreateTimer(VTKI_TIMER_FIRST);
192 
193  //iren must be initialized so that it can handle events
194  iren->Initialize();
195  iren->Start();
196  }
197 
198  WinProbeDevice->StopRecording();
199  WinProbeDevice->Disconnect();
200 
201  return EXIT_SUCCESS;
202 }
virtual PlusStatus WriteToSequenceFile(const char *filename, bool useCompression=false)
vtkRenderWindowInteractor * iren
#define PLUS_FAIL
Definition: PlusCommon.h:43
int main(int argc, char *argv[])
#define PLUS_SUCCESS
Definition: PlusCommon.h:44
vtkImageViewer * viewer
static vtkIGSIOLogger * Instance()
Contains an optional timestamped circular buffer containing the video images and a number of timestam...
PlusStatus GetVideoSource(vtkPlusDataSource *&aVideoSource) const
static PlusStatus ReadDeviceSetConfigurationFromFile(vtkXMLDataElement *config, const char *filename)
Definition: PlusXmlUtils.h:23
Interface to a 3D positioning tool, video source, or generalized data stream.