PlusLib  2.9.0
Software library for tracked ultrasound image acquisition, calibration, and processing.
vtkURFSavedVideoSourceTest1.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 "vtkImageViewer.h"
12 #include "vtkPlusChannel.h"
13 #include "vtkPlusDataSource.h"
14 #include "vtkRenderWindowInteractor.h"
15 #include "vtkSmartPointer.h"
17 #include "vtkXMLUtilities.h"
18 #include "vtksys/CommandLineArguments.hxx"
19 #include <stdlib.h>
20 
21 vtkSmartPointer<vtkPlusSonixVideoSource> sonixGrabber;
22 vtkImageViewer *viewer = NULL;
23 vtkRenderWindowInteractor *iren = NULL;
24 
25 class vtkMyCallback : public vtkCommand
26 {
27 public:
28  static vtkMyCallback *New()
29  {return new vtkMyCallback;}
30  virtual void Execute(vtkObject *caller, unsigned long, void*)
31  {
32  viewer->Render();
33 
34  //update the timer so it will trigger again
35  //VTKI_TIMER_UPDATE = 1
36  iren->CreateTimer(VTKI_TIMER_UPDATE);
37  }
38 };
39 
40 int main(int argc, char* argv[])
41 {
42 
43  bool printHelp(false);
44  bool renderingOff(false);
45  std::string inputConfigFileName;
46  std::string inputSonixIp;
47 
48  vtksys::CommandLineArguments args;
49  args.Initialize(argc, argv);
50 
51  int verboseLevel = vtkPlusLogger::LOG_LEVEL_INFO;
52 
53  args.AddArgument("--help", vtksys::CommandLineArguments::NO_ARGUMENT, &printHelp, "Print this help.");
54  args.AddArgument("--sonix-ip", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &inputSonixIp, "IP address of the Ultrasonix scanner (overrides the IP address parameter defined in the config file).");
55  args.AddArgument("--config-file", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &inputConfigFileName, "Config file containing the device configuration.");
56  args.AddArgument("--rendering-off", vtksys::CommandLineArguments::NO_ARGUMENT, &renderingOff, "Run test without rendering.");
57  args.AddArgument("--verbose", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &verboseLevel, "Verbose level (Default: 1; 1=error only, 2=warning, 3=info, 4=debug)");
58 
59  if ( !args.Parse() )
60  {
61  std::cerr << "Problem parsing arguments" << std::endl;
62  std::cout << "\n\nvtkPlusSonixVideoSourceTest1 help:" << args.GetHelp() << std::endl;
63  exit(EXIT_FAILURE);
64  }
65 
66  if ( printHelp )
67  {
68  std::cout << "\n\nvtkPlusSonixVideoSourceTest1 help:" << args.GetHelp() << std::endl;
69  exit(EXIT_SUCCESS);
70 
71  }
72 
73  vtkPlusLogger::Instance()->SetLogLevel(verboseLevel);
74 
75  LOG_DEBUG("Reading config file...");
76  vtkSmartPointer<vtkXMLDataElement> configRootElement = vtkSmartPointer<vtkXMLDataElement>::New();
77  if (PlusXmlUtils::ReadDeviceSetConfigurationFromFile(configRootElement, inputConfigFileName.c_str())==PLUS_FAIL)
78  {
79  LOG_ERROR("Unable to read configuration from file " << inputConfigFileName.c_str());
80  return EXIT_FAILURE;
81  }
82  LOG_DEBUG("Reading config file finished.");
83 
84  //Add the video source here
85  sonixGrabber = vtkSmartPointer<vtkPlusSonixVideoSource>::New();
86  sonixGrabber->SetImagingModeDevice(0);
87  sonixGrabber->SetAcquisitionDataTypeDevice(0x00000005);
88  sonixGrabber->ReadConfiguration(configRootElement);
89  if (!inputSonixIp.empty())
90  {
91  sonixGrabber->SetSonixIP(inputSonixIp.c_str());
92  }
93 
94  sonixGrabber->CreateDefaultOutputChannel();
95 
96  sonixGrabber->Connect();
97 
98  if ( sonixGrabber->GetConnected() )
99  {
100  sonixGrabber->StartRecording(); //start recording frame from the video
101  }
102  else
103  {
104  sonixGrabber->Disconnect();
105  if ( sonixGrabber != NULL )
106  {
107  sonixGrabber->Delete();
108  }
109 
110  if ( viewer != NULL )
111  {
112  viewer->Delete();
113  }
114 
115  if ( iren != NULL )
116  {
117  iren->Delete();
118  }
119 
120  LOG_ERROR( "Unable to connect to Sonix RP machine at: " << inputSonixIp );
121  exit(EXIT_FAILURE);
122  }
123 
124  if (renderingOff)
125  {
126  sonixGrabber->StopRecording();
127  sonixGrabber->Disconnect();
128 
129  if ( sonixGrabber != NULL )
130  {
131  sonixGrabber->Delete();
132  }
133 
134  if ( viewer != NULL )
135  {
136  viewer->Delete();
137  }
138 
139  if ( iren != NULL )
140  {
141  iren->Delete();
142  }
143 
144  exit(EXIT_SUCCESS);
145  }
146 
147  viewer = vtkImageViewer::New();
148  viewer->SetInputConnection(sonixGrabber->GetOutputPort());
149  viewer->SetColorWindow(255);
150  viewer->SetColorLevel(127.5);
151  viewer->SetZSlice(0);
152 
153  //Create the interactor that handles the event loop
154  iren = vtkRenderWindowInteractor::New();
155  iren->SetRenderWindow(viewer->GetRenderWindow());
156  viewer->SetupInteractor(iren);
157 
158  viewer->Render(); //must be called after iren and viewer are linked
159  //or there will be problems
160 
161  //establish timer event and create timer
162  vtkMyCallback* call = vtkMyCallback::New();
163  iren->AddObserver(vtkCommand::TimerEvent, call);
164  iren->CreateTimer(VTKI_TIMER_FIRST); //VTKI_TIMER_FIRST = 0
165 
166  //iren must be initialized so that it can handle events
167  iren->Initialize();
168  iren->Start();
169 
170  //delete all instances and release the hold the win32videosource
171  //has on the pci card
172  sonixGrabber->Disconnect();
173  sonixGrabber->Delete();
174 
175  call->Delete();
176  viewer->Delete();
177  iren->Delete();
178 
179  return 0;
180 }
181 
182 
int main(int argc, char *argv[])
vtkRenderWindowInteractor * iren
vtkSmartPointer< vtkPlusSonixVideoSource > sonixGrabber
#define PLUS_FAIL
Definition: PlusCommon.h:43
vtkImageViewer * viewer
static vtkIGSIOLogger * Instance()
static PlusStatus ReadDeviceSetConfigurationFromFile(vtkXMLDataElement *config, const char *filename)
Definition: PlusXmlUtils.h:23