PlusLib  2.9.0
Software library for tracked ultrasound image acquisition, calibration, and processing.
vtkWin32VideoSourceTest.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"
10 #include "vtkImageData.h"
11 #include "vtkImageViewer2.h"
12 #include "vtkPlusDataSource.h"
13 #include "vtkRenderWindowInteractor.h"
14 #include "vtkRenderer.h"
15 #include "vtkSmartPointer.h"
17 #include "vtksys/CommandLineArguments.hxx"
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  viewer->Render();
29 
30  //update the timer so it will trigger again
31  iren->CreateTimer(VTKI_TIMER_UPDATE);
32  }
33  vtkImageViewer2 *viewer;
34  vtkRenderWindowInteractor *iren;
35 protected:
36  vtkMyCallback()
37  {
38  viewer = NULL;
39  iren = NULL;
40  }
41 };
42 
43 int main(int argc, char **argv)
44 {
45 
46  bool printHelp(false);
47  bool renderingOff(false);
48  bool showDialogs(false);
49 
50  vtksys::CommandLineArguments args;
51  args.Initialize(argc, argv);
52 
53  int verboseLevel = vtkPlusLogger::LOG_LEVEL_UNDEFINED;
54 
55  args.AddArgument("--help", vtksys::CommandLineArguments::NO_ARGUMENT, &printHelp, "Print this help.");
56  args.AddArgument("--rendering-off", vtksys::CommandLineArguments::NO_ARGUMENT, &renderingOff, "Run test without rendering.");
57  args.AddArgument("--show-dialogs", vtksys::CommandLineArguments::NO_ARGUMENT, &showDialogs, "Show video source and format dialogs");
58  args.AddArgument("--verbose", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &verboseLevel, "Verbose level (1=error only, 2=warning, 3=info, 4=debug, 5=trace)");
59 
60  if ( !args.Parse() )
61  {
62  std::cerr << "Problem parsing arguments" << std::endl;
63  std::cout << "\n\nvtkWin32VideoSourceTest help:" << args.GetHelp() << std::endl;
64  exit(EXIT_FAILURE);
65  }
66 
67  vtkPlusLogger::Instance()->SetLogLevel(verboseLevel);
68 
69  if ( printHelp )
70  {
71  std::cout << "\n\nvtkWin32VideoSourceTest help:" << args.GetHelp() << std::endl;
72  exit(EXIT_SUCCESS);
73 
74  }
75 
76  vtkSmartPointer<vtkPlusWin32VideoSource2> frameGrabber = vtkSmartPointer<vtkPlusWin32VideoSource2>::New();
77 
78  frameGrabber->CreateDefaultOutputChannel();
79 
80  // Add an observer to warning and error events for redirecting it to the stdout
81  vtkSmartPointer<vtkCallbackCommand> callbackCommand = vtkSmartPointer<vtkCallbackCommand>::New();
82  callbackCommand->SetCallback(PrintLogsCallback);
83  frameGrabber->AddObserver("WarningEvent", callbackCommand);
84  frameGrabber->AddObserver("ErrorEvent", callbackCommand);
85 
86  LOG_INFO("Initialize...");
87  frameGrabber->Connect();
88 
89  if (showDialogs)
90  {
91  frameGrabber->VideoSourceDialog();
92  frameGrabber->VideoFormatDialog();
93  }
94 
95  if ( frameGrabber->GetConnected() )
96  {
97  LOG_INFO("Start recording...");
98  frameGrabber->StartRecording();
99  }
100  else
101  {
102  frameGrabber->Disconnect();
103  LOG_ERROR( "Unable to connect to IC capture device");
104  exit(EXIT_FAILURE);
105  }
106 
107 
108  if (renderingOff)
109  {
110  LOG_INFO("No need for rendering, stop the device...");
111  frameGrabber->StopRecording();
112  frameGrabber->Disconnect();
113  LOG_INFO("Exit successfully");
114  exit(EXIT_SUCCESS);
115  }
116 
117  vtkSmartPointer<vtkImageViewer2> viewer = vtkSmartPointer<vtkImageViewer2>::New();
118 
119  viewer->SetColorWindow(255);
120  viewer->SetColorLevel(100.5);
121  vtkPlusDataSource* videoSource=NULL;
122  if (frameGrabber->GetFirstActiveOutputVideoSource(videoSource) != PLUS_SUCCESS )
123  {
124  LOG_ERROR("Unable to retrieve the video source.");
125  exit(EXIT_FAILURE);
126  }
127  viewer->SetSize(videoSource->GetOutputFrameSize()[0], videoSource->GetOutputFrameSize()[1]);
128 
129  viewer->SetInputConnection(frameGrabber->GetOutputPort());
130 
131  //Create the interactor that handles the event loop
132  vtkSmartPointer<vtkRenderWindowInteractor> iren = vtkSmartPointer<vtkRenderWindowInteractor>::New();
133  iren->SetRenderWindow(viewer->GetRenderWindow());
134  viewer->SetupInteractor(iren);
135 
136  viewer->Render();
137 
138  //establish timer event and create timer
139  vtkSmartPointer<vtkMyCallback> call = vtkSmartPointer<vtkMyCallback>::New();
140  call->iren=iren;
141  call->viewer=viewer;
142  iren->AddObserver(vtkCommand::TimerEvent, call);
143  iren->CreateTimer(VTKI_TIMER_FIRST); //VTKI_TIMER_FIRST = 0
144 
145  //iren must be initialized so that it can handle events
146  iren->Initialize();
147  iren->Start();
148 
149  LOG_INFO("Exit successfully");
150  return EXIT_SUCCESS;
151 
152  }
153 
154  // Callback function for error and warning redirects
155 void PrintLogsCallback(vtkObject* obj, unsigned long eid, void* clientdata, void* calldata)
156 {
157  if ( eid == vtkCommand::GetEventIdFromString("WarningEvent") )
158  {
159  LOG_WARNING((const char*)calldata);
160  }
161  else if ( eid == vtkCommand::GetEventIdFromString("ErrorEvent") )
162  {
163  LOG_ERROR((const char*)calldata);
164  }
165 }
int main(int argc, char **argv)
vtkRenderWindowInteractor * iren
void PrintLogsCallback(vtkObject *obj, unsigned long eid, void *clientdata, void *calldata)
#define PLUS_SUCCESS
Definition: PlusCommon.h:44
vtkImageViewer * viewer
virtual FrameSizeType GetOutputFrameSize() const
static vtkIGSIOLogger * Instance()
Interface to a 3D positioning tool, video source, or generalized data stream.