PlusLib  2.9.0
Software library for tracked ultrasound image acquisition, calibration, and processing.
vtkBkProFocusOemVideoSourceTest.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 
7 #include "PlusConfigure.h"
8 #include "vtkCallbackCommand.h"
9 #include "vtkCommand.h"
11 #include "vtkImageData.h"
12 #include "vtkImageViewer2.h"
13 #include "vtkRenderWindowInteractor.h"
14 #include "vtkRenderer.h"
15 #include "vtkSmartPointer.h"
16 #include "vtksys/CommandLineArguments.hxx"
17 #include "vtkPlusDataSource.h"
18 
19 void PrintLogsCallback(vtkObject* obj, unsigned long eid, void* clientdata, void* calldata);
20 
21 class vtkMyCallback : public vtkCommand
22 {
23 public:
24  static vtkMyCallback* New()
25  {return new vtkMyCallback;}
26  virtual void Execute(vtkObject* caller, unsigned long, void*)
27  {
28 
29  viewer->Render();
30 
31  //update the timer so it will trigger again
32  iren->CreateTimer(VTKI_TIMER_UPDATE);
33  }
34  vtkImageViewer2* viewer;
35  vtkRenderWindowInteractor* iren;
36 protected:
37  vtkMyCallback()
38  {
39  viewer = NULL;
40  iren = NULL;
41  }
42 };
43 
44 int main(int argc, char** argv)
45 {
46  std::string iniFile = "BkSettings/IniFile.ini";
47  double frameRate = 30;
48  bool printHelp(false);
49  bool renderingOff(false);
50 
51  vtksys::CommandLineArguments args;
52  args.Initialize(argc, argv);
53 
54  int verboseLevel = vtkPlusLogger::LOG_LEVEL_UNDEFINED;
55 
56  args.AddArgument("--ini-file", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &iniFile, "Path to the BK ini file, relative to the configuration file directory. Default: BkSettings/IniFile.ini");
57  args.AddArgument("--frame-rate", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &frameRate, "Requested acquisition frame rate (in FPS, default = 30)");
58  args.AddArgument("--help", vtksys::CommandLineArguments::NO_ARGUMENT, &printHelp, "Print this help.");
59  args.AddArgument("--rendering-off", vtksys::CommandLineArguments::NO_ARGUMENT, &renderingOff, "Run test without rendering.");
60  args.AddArgument("--verbose", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &verboseLevel, "Verbose level (1=error only, 2=warning, 3=info, 4=debug, 5=trace)");
61 
62  if (!args.Parse())
63  {
64  std::cerr << "Problem parsing arguments" << std::endl;
65  std::cout << "\n\nvtkPlusBkProFocusOemVideoSource help:" << args.GetHelp() << std::endl;
66  exit(EXIT_FAILURE);
67  }
68 
69  vtkPlusLogger::Instance()->SetLogLevel(verboseLevel);
70 
71  if (printHelp)
72  {
73  std::cout << "\n\nvtkPlusBkProFocusOemVideoSource help:" << args.GetHelp() << std::endl;
74  exit(EXIT_SUCCESS);
75  }
76 
77  vtkSmartPointer<vtkPlusBkProFocusOemVideoSource> frameGrabber = vtkSmartPointer<vtkPlusBkProFocusOemVideoSource>::New();
78 
79  frameGrabber->SetAcquisitionRate(frameRate);
80 
81  frameGrabber->CreateDefaultOutputChannel();
82 
83  // Add an observer to warning and error events for redirecting it to the stdout
84  vtkSmartPointer<vtkCallbackCommand> callbackCommand = vtkSmartPointer<vtkCallbackCommand>::New();
85  callbackCommand->SetCallback(PrintLogsCallback);
86  frameGrabber->AddObserver("WarningEvent", callbackCommand);
87  frameGrabber->AddObserver("ErrorEvent", callbackCommand);
88 
89  LOG_INFO("Initialize...");
90  frameGrabber->Connect();
91 
92  if (frameGrabber->GetConnected())
93  {
94  LOG_INFO("Start recording...");
95  frameGrabber->StartRecording();
96  }
97  else
98  {
99  frameGrabber->Disconnect();
100  LOG_ERROR("Unable to connect to IC capture device");
101  exit(EXIT_FAILURE);
102  }
103 
104  // If we started the viewer now then most likely the first rendering would be done using an empty frame
105  // and so the image position and scaling would be off.
106  // So, wait until at least a frame is acquired and only then connect the video source to the viewer.
107  vtkPlusDataSource* videoSource = NULL;
108  if (frameGrabber->GetFirstActiveOutputVideoSource(videoSource) != PLUS_SUCCESS)
109  {
110  LOG_ERROR("Unable to retrieve the video source.");
111  exit(EXIT_FAILURE);
112  }
113  for (int retries = 0; retries < 50; retries++)
114  {
115  if (videoSource->GetNumberOfItems() >= 1)
116  {
117  // a frame has been acquired
118  break;
119  }
120  vtkIGSIOAccurateTimer::Delay(0.1);
121  }
122  if (videoSource->GetNumberOfItems() < 1)
123  {
124  LOG_ERROR("Frames are not received from the device for several seconds");
125  }
126 
127  if (renderingOff)
128  {
129  LOG_INFO("No need for rendering, stop the device...");
130  frameGrabber->StopRecording();
131  frameGrabber->Disconnect();
132  LOG_INFO("Exit successfully");
133  exit(EXIT_SUCCESS);
134  }
135 
136  vtkSmartPointer<vtkImageViewer2> viewer = vtkSmartPointer<vtkImageViewer2>::New();
137 
138  viewer->SetColorWindow(255);
139  viewer->SetColorLevel(100.5);
140  viewer->SetSize(1024, 768);
141 
142  viewer->SetInputConnection(frameGrabber->GetOutputPort());
143 
144  //Create the interactor that handles the event loop
145  vtkSmartPointer<vtkRenderWindowInteractor> iren = vtkSmartPointer<vtkRenderWindowInteractor>::New();
146  iren->SetRenderWindow(viewer->GetRenderWindow());
147  viewer->SetupInteractor(iren);
148 
149  viewer->Render();
150 
151  //establish timer event and create timer
152  vtkSmartPointer<vtkMyCallback> call = vtkSmartPointer<vtkMyCallback>::New();
153  call->iren = iren;
154  call->viewer = viewer;
155  iren->AddObserver(vtkCommand::TimerEvent, call);
156  iren->CreateTimer(VTKI_TIMER_FIRST); //VTKI_TIMER_FIRST = 0
157 
158  //iren must be initialized so that it can handle events
159  iren->Initialize();
160  iren->Start();
161 
162  LOG_INFO("Exit successfully");
163  return EXIT_SUCCESS;
164 
165 }
166 
167 // Callback function for error and warning redirects
168 void PrintLogsCallback(vtkObject* obj, unsigned long eid, void* clientdata, void* calldata)
169 {
170  if (eid == vtkCommand::GetEventIdFromString("WarningEvent"))
171  {
172  LOG_WARNING((const char*)calldata);
173  }
174  else if (eid == vtkCommand::GetEventIdFromString("ErrorEvent"))
175  {
176  LOG_ERROR((const char*)calldata);
177  }
178 }
vtkRenderWindowInteractor * iren
int main(int argc, char **argv)
#define PLUS_SUCCESS
Definition: PlusCommon.h:44
vtkImageViewer * viewer
static vtkIGSIOLogger * Instance()
void PrintLogsCallback(vtkObject *obj, unsigned long eid, void *clientdata, void *calldata)
virtual int GetNumberOfItems()
Interface to a 3D positioning tool, video source, or generalized data stream.