PlusLib  2.9.0
Software library for tracked ultrasound image acquisition, calibration, and processing.
vtkMicronTrackerTest.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 "vtksys/CommandLineArguments.hxx"
9 #include "vtkPlusMicronTracker.h"
10 #include "vtkImageData.h"
11 #include "vtkImageViewer2.h"
12 #include "vtkRenderWindowInteractor.h"
13 #include "vtkRenderer.h"
14 #include "vtkSmartPointer.h"
15 #include "vtkCommand.h"
16 #include "vtkCallbackCommand.h"
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  frameGrabber->GetImage(leftImage,NULL);
28  viewer->SetInputData(leftImage);
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  vtkPlusMicronTracker *frameGrabber;
37  vtkImageData *leftImage;
38 protected:
39  vtkMyCallback()
40  {
41  viewer = NULL;
42  iren = NULL;
43  }
44 };
45 
46 int main(int argc, char **argv)
47 {
48  std::string serialNumber;
49  double frameRate=20;
50  bool printHelp(false);
51  bool renderingOff(false);
52 
53  vtksys::CommandLineArguments args;
54  args.Initialize(argc, argv);
55 
56  int verboseLevel = vtkPlusLogger::LOG_LEVEL_UNDEFINED;
57 
58  args.AddArgument("--serial-number", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &serialNumber, "Serial number of the Epiphan device to connect");
59  args.AddArgument("--frame-rate", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &frameRate, "Requested acquisition frame rate (in FPS, default = 30)");
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<vtkPlusMicronTracker> frameGrabber = vtkSmartPointer<vtkPlusMicronTracker>::New();
81 
82  frameGrabber->SetAcquisitionRate(frameRate);
83 
84  // Add an observer to warning and error events for redirecting it to the stdout
85  vtkSmartPointer<vtkCallbackCommand> callbackCommand = vtkSmartPointer<vtkCallbackCommand>::New();
86  callbackCommand->SetCallback(PrintLogsCallback);
87  frameGrabber->AddObserver("WarningEvent", callbackCommand);
88  frameGrabber->AddObserver("ErrorEvent", callbackCommand);
89 
90  LOG_INFO("Initialize...");
91 
92  if ( frameGrabber->Connect()!=PLUS_SUCCESS )
93  {
94  LOG_ERROR( "Unable to connect to device");
95  exit(EXIT_FAILURE);
96  }
97 
98  LOG_INFO("Start recording...");
99  if ( frameGrabber->StartRecording()!=PLUS_SUCCESS )
100  {
101  LOG_ERROR( "Unable to connect to device");
102  exit(EXIT_FAILURE);
103  }
104 
105  if (renderingOff)
106  {
107  LOG_INFO("No need for rendering, stop the device...");
108  frameGrabber->StopRecording();
109  frameGrabber->Disconnect();
110  LOG_INFO("Exit successfully");
111  exit(EXIT_SUCCESS);
112  }
113 
114  vtkSmartPointer<vtkImageViewer2> viewer = vtkSmartPointer<vtkImageViewer2>::New();
115 
116  viewer->SetColorWindow(255);
117  viewer->SetColorLevel(100.5);
118  viewer->SetSize(1024,768);
119 
120  vtkSmartPointer<vtkImageData> leftImage=vtkSmartPointer<vtkImageData>::New();
121  frameGrabber->GetImage(leftImage,NULL);
122  viewer->SetInputData(leftImage);
123 
124  //Create the interactor that handles the event loop
125  vtkSmartPointer<vtkRenderWindowInteractor> iren = vtkSmartPointer<vtkRenderWindowInteractor>::New();
126  iren->SetRenderWindow(viewer->GetRenderWindow());
127  viewer->SetupInteractor(iren);
128 
129  viewer->Render();
130 
131  //establish timer event and create timer
132  vtkSmartPointer<vtkMyCallback> call = vtkSmartPointer<vtkMyCallback>::New();
133  call->iren=iren;
134  call->viewer=viewer;
135  call->frameGrabber=frameGrabber;
136  call->leftImage=leftImage;
137  iren->AddObserver(vtkCommand::TimerEvent, call);
138  iren->CreateTimer(VTKI_TIMER_FIRST); //VTKI_TIMER_FIRST = 0
139 
140  //iren must be initialized so that it can handle events
141  iren->Initialize();
142  iren->Start();
143 
144  LOG_INFO("Exit successfully");
145  return EXIT_SUCCESS;
146 
147 }
148 
149 // Callback function for error and warning redirects
150 void PrintLogsCallback(vtkObject* obj, unsigned long eid, void* clientdata, void* calldata)
151 {
152  if ( eid == vtkCommand::GetEventIdFromString("WarningEvent") )
153  {
154  LOG_WARNING((const char*)calldata);
155  }
156  else if ( eid == vtkCommand::GetEventIdFromString("ErrorEvent") )
157  {
158  LOG_ERROR((const char*)calldata);
159  }
160 }
void PrintLogsCallback(vtkObject *obj, unsigned long eid, void *clientdata, void *calldata)
vtkRenderWindowInteractor * iren
Interface class to Claron MicronTracker optical trackers.
#define PLUS_SUCCESS
Definition: PlusCommon.h:44
vtkImageViewer * viewer
int main(int argc, char **argv)
static vtkIGSIOLogger * Instance()