PlusLib  2.9.0
Software library for tracked ultrasound image acquisition, calibration, and processing.
vtkICCapturingSourceTest1.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 class vtkMyCallback : public vtkCommand
19 {
20 public:
21  static vtkMyCallback *New() {return new vtkMyCallback;}
22  virtual void Execute(vtkObject *caller, unsigned long, void*)
23  {
24  viewer->Render();
25 
26  //update the timer so it will trigger again
27  iren->CreateTimer(VTKI_TIMER_UPDATE);
28  }
29  vtkImageViewer2* viewer;
30  vtkRenderWindowInteractor *iren;
31 };
32 
33 int main(int argc, char **argv)
34 {
35  bool printHelp(false);
36  bool renderingOff(false);
37  bool listDevices=false;
38  std::string deviceName("DFG/USB2-lt");
39  std::string videoNorm("NTSC_M");
40  std::string videoFormat("Y800 (640x480)");
41  std::string inputChannel("01 Video: SVideo");
42 
43  vtksys::CommandLineArguments args;
44  args.Initialize(argc, argv);
45 
46  int verboseLevel = vtkPlusLogger::LOG_LEVEL_UNDEFINED;
47 
48  args.AddArgument("--help", vtksys::CommandLineArguments::NO_ARGUMENT, &printHelp, "Print this help.");
49  args.AddArgument("--list-devices", vtksys::CommandLineArguments::NO_ARGUMENT, &listDevices, "Show the list of available devices, norms, formats, and frame sizes and exit");
50  args.AddArgument("--rendering-off", vtksys::CommandLineArguments::NO_ARGUMENT, &renderingOff, "Run test without rendering.");
51  args.AddArgument("--device-name", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &deviceName, "IC Capturing device name (Default: DFG/USB2-lt)." );
52  args.AddArgument("--video-norm", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &videoNorm, "IC Capturing device video norm (Default: NTSC_M)." );
53  args.AddArgument("--video-format", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &videoFormat, "IC Capturing device video format (Default: Y800 (640x480))." );
54  args.AddArgument("--input-channel", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &inputChannel, "IC Capturing device input channel (Default: 01 Video: SVideo)." );
55  args.AddArgument("--verbose", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &verboseLevel, "Verbose level (1=error only, 2=warning, 3=info, 4=debug, 5=trace)");
56 
57  if ( !args.Parse() )
58  {
59  std::cerr << "Problem parsing arguments" << std::endl;
60  std::cout << "\n\nvtkPlusICCapturingSourceTest1 help:" << args.GetHelp() << std::endl;
61  exit(EXIT_FAILURE);
62  }
63 
64  vtkPlusLogger::Instance()->SetLogLevel(verboseLevel);
65 
66  if ( printHelp )
67  {
68  std::cout << "\n\nvtkPlusICCapturingSourceTest1 help:" << args.GetHelp() << std::endl;
69  exit(EXIT_SUCCESS);
70  }
71 
72  vtkSmartPointer<vtkPlusICCapturingSource> frameGrabber = vtkSmartPointer<vtkPlusICCapturingSource>::New();
73 
74  if (listDevices)
75  {
76  frameGrabber->LogListOfCaptureDevices();
77  exit(EXIT_SUCCESS);
78  }
79 
80  frameGrabber->SetDeviceName(deviceName.c_str());
81  frameGrabber->SetVideoNorm(videoNorm.c_str());
82  frameGrabber->SetVideoFormat(videoFormat.c_str());
83  frameGrabber->SetInputChannel(inputChannel.c_str());
84 
85  frameGrabber->CreateDefaultOutputChannel();
86 
87  LOG_INFO("Initialize...");
88  frameGrabber->Connect();
89 
90  if ( frameGrabber->GetConnected() )
91  {
92  LOG_INFO("Start recording...");
93  frameGrabber->StartRecording();
94  }
95  else
96  {
97  frameGrabber->Disconnect();
98  LOG_ERROR( "Unable to connect to IC capture device: " << deviceName );
99  exit(EXIT_FAILURE);
100  }
101 
102  if (renderingOff)
103  {
104  LOG_INFO("No need for rendering, stop the device...");
105  frameGrabber->StopRecording();
106  frameGrabber->Disconnect();
107  LOG_INFO("Exit successfully");
108  exit(EXIT_SUCCESS);
109  }
110 
111  vtkSmartPointer<vtkImageViewer2> viewer = vtkSmartPointer<vtkImageViewer2>::New();
112  viewer->SetColorWindow(255);
113  viewer->SetColorLevel(100.5);
114  viewer->SetSize(640,480);
115 
116  viewer->SetInputConnection(frameGrabber->GetOutputPort());
117 
118  //Create the interactor that handles the event loop
119  vtkSmartPointer<vtkRenderWindowInteractor> iren = vtkSmartPointer<vtkRenderWindowInteractor>::New();
120  iren->SetRenderWindow(viewer->GetRenderWindow());
121  viewer->SetupInteractor(iren);
122 
123  viewer->Render();
124 
125  //establish timer event and create timer
126  vtkSmartPointer<vtkMyCallback> call = vtkSmartPointer<vtkMyCallback>::New();
127  call->viewer=viewer;
128  call->iren=iren;
129  iren->AddObserver(vtkCommand::TimerEvent, call);
130  iren->CreateTimer(VTKI_TIMER_FIRST); //VTKI_TIMER_FIRST = 0
131 
132  // iren must be initialized so that it can handle events
133  iren->Initialize();
134  // start the event loop (exit with 'q' key)
135  iren->Start();
136 
137  frameGrabber->StopRecording();
138  frameGrabber->Disconnect();
139 
140  LOG_INFO("Exit successfully");
141  return EXIT_SUCCESS;
142 }
vtkRenderWindowInteractor * iren
const char ** deviceName
Definition: phidget22.h:1316
vtkImageViewer * viewer
int main(int argc, char **argv)
static vtkIGSIOLogger * Instance()