PlusLib  2.9.0
Software library for tracked ultrasound image acquisition, calibration, and processing.
vtkEpiphanVideoSourceTest.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 
18 void PrintLogsCallback(vtkObject* obj, unsigned long eid, void* clientdata, void* calldata);
19 
20 class vtkMyCallback : public vtkCommand
21 {
22 public:
23  static vtkMyCallback* New()
24  {return new vtkMyCallback;}
25  virtual void Execute(vtkObject* caller, unsigned long, void*)
26  {
27  viewer->Render();
28 
29  //update the timer so it will trigger again
30  iren->CreateTimer(VTKI_TIMER_UPDATE);
31  }
32  vtkImageViewer2* viewer;
33  vtkRenderWindowInteractor* iren;
34 protected:
35  vtkMyCallback()
36  {
37  viewer = NULL;
38  iren = NULL;
39  }
40 };
41 
42 int main(int argc, char** argv)
43 {
44  std::string grabberLocation;
45  double frameRate = 30;
46  std::vector<int> clipRectOrigin;
47  std::vector<int> clipRectSize;
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("--grabber-location", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &grabberLocation, "Location of the of the Epiphan device to connect (use the format sn:SERIAL for the specification of serial number)");
57  args.AddArgument("--frame-rate", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &frameRate, "Requested acquisition frame rate (in FPS, default = 30)");
58  args.AddArgument("--clip-rect-origin", vtksys::CommandLineArguments::MULTI_ARGUMENT, &clipRectOrigin, "Origin of the clipping rectangle");
59  args.AddArgument("--clip-rect-size", vtksys::CommandLineArguments::MULTI_ARGUMENT, &clipRectSize, "Size of the clipping rectangle");
60  args.AddArgument("--help", vtksys::CommandLineArguments::NO_ARGUMENT, &printHelp, "Print this help.");
61  args.AddArgument("--rendering-off", vtksys::CommandLineArguments::NO_ARGUMENT, &renderingOff, "Run test without rendering.");
62  args.AddArgument("--verbose", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &verboseLevel, "Verbose level (1=error only, 2=warning, 3=info, 4=debug, 5=trace)");
63 
64  if (!args.Parse())
65  {
66  std::cerr << "Problem parsing arguments" << std::endl;
67  std::cout << "\n\nvtkPlusEpiphanVideoSourceTest help:" << args.GetHelp() << std::endl;
68  exit(EXIT_FAILURE);
69  }
70 
71  vtkPlusLogger::Instance()->SetLogLevel(verboseLevel);
72 
73  if (printHelp)
74  {
75  std::cout << "\n\nvtkPlusEpiphanVideoSourceTest help:" << args.GetHelp() << std::endl;
76  exit(EXIT_SUCCESS);
77 
78  }
79 
80  vtkSmartPointer<vtkPlusEpiphanVideoSource> frameGrabber = vtkSmartPointer<vtkPlusEpiphanVideoSource>::New();
81  if (!grabberLocation.empty())
82  {
83  frameGrabber->SetGrabberLocation(grabberLocation.c_str());
84  }
85  frameGrabber->SetAcquisitionRate(frameRate);
86 
87  if (clipRectOrigin.size() > 0 || clipRectSize.size() > 0)
88  {
89  if (clipRectOrigin.size() != 2 || clipRectSize.size() != 2)
90  {
91  LOG_ERROR("Invalid clip rectangle origin and/or size");
92  exit(EXIT_FAILURE);
93  }
94  std::array<int, 3> clipRectangleOrigin = {clipRectOrigin[0], clipRectOrigin[1], igsioCommon::NO_CLIP};
95  frameGrabber->SetClipRectangleOrigin(clipRectangleOrigin);
96  std::array<int, 3> clipRectangleSize = {clipRectSize[0], clipRectSize[1], igsioCommon::NO_CLIP};
97  frameGrabber->SetClipRectangleSize(clipRectangleSize);
98  }
99 
100  frameGrabber->CreateDefaultOutputChannel();
101 
102  // Add an observer to warning and error events for redirecting it to the stdout
103  vtkSmartPointer<vtkCallbackCommand> callbackCommand = vtkSmartPointer<vtkCallbackCommand>::New();
104  callbackCommand->SetCallback(PrintLogsCallback);
105  frameGrabber->AddObserver("WarningEvent", callbackCommand);
106  frameGrabber->AddObserver("ErrorEvent", callbackCommand);
107 
108  LOG_INFO("Initialize...");
109  frameGrabber->Connect();
110 
111  if (frameGrabber->GetConnected())
112  {
113  LOG_INFO("Start recording...");
114  frameGrabber->StartRecording();
115  }
116  else
117  {
118  frameGrabber->Disconnect();
119  LOG_ERROR("Unable to connect to IC capture device");
120  exit(EXIT_FAILURE);
121  }
122 
123 
124  if (renderingOff)
125  {
126  LOG_INFO("No need for rendering, stop the device...");
127  frameGrabber->StopRecording();
128  frameGrabber->Disconnect();
129  LOG_INFO("Exit successfully");
130  exit(EXIT_SUCCESS);
131  }
132 
133  vtkSmartPointer<vtkImageViewer2> viewer = vtkSmartPointer<vtkImageViewer2>::New();
134 
135  viewer->SetColorWindow(255);
136  viewer->SetColorLevel(100.5);
137  viewer->SetSize(1024, 768);
138 
139  viewer->SetInputConnection(frameGrabber->GetOutputPort());
140 
141  //Create the interactor that handles the event loop
142  vtkSmartPointer<vtkRenderWindowInteractor> iren = vtkSmartPointer<vtkRenderWindowInteractor>::New();
143  iren->SetRenderWindow(viewer->GetRenderWindow());
144  viewer->SetupInteractor(iren);
145 
146  viewer->Render();
147 
148  //establish timer event and create timer
149  vtkSmartPointer<vtkMyCallback> call = vtkSmartPointer<vtkMyCallback>::New();
150  call->iren = iren;
151  call->viewer = viewer;
152  iren->AddObserver(vtkCommand::TimerEvent, call);
153  iren->CreateTimer(VTKI_TIMER_FIRST); //VTKI_TIMER_FIRST = 0
154 
155  //iren must be initialized so that it can handle events
156  iren->Initialize();
157  iren->Start();
158 
159  LOG_INFO("Exit successfully");
160  return EXIT_SUCCESS;
161 
162 }
163 
164 // Callback function for error and warning redirects
165 void PrintLogsCallback(vtkObject* obj, unsigned long eid, void* clientdata, void* calldata)
166 {
167  if (eid == vtkCommand::GetEventIdFromString("WarningEvent"))
168  {
169  LOG_WARNING((const char*)calldata);
170  }
171  else if (eid == vtkCommand::GetEventIdFromString("ErrorEvent"))
172  {
173  LOG_ERROR((const char*)calldata);
174  }
175 }
vtkRenderWindowInteractor * iren
int main(int argc, char **argv)
vtkImageViewer * viewer
static vtkIGSIOLogger * Instance()
void PrintLogsCallback(vtkObject *obj, unsigned long eid, void *clientdata, void *calldata)