PlusLib  2.9.0
Software library for tracked ultrasound image acquisition, calibration, and processing.
vtkGenericSensorTrackerTest.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 "vtkPlusDataSource.h"
11 #include "vtkSmartPointer.h"
13 #include "vtksys/CommandLineArguments.hxx"
14 #include "vtksys/SystemTools.hxx"
15 #include "igtlOSUtil.h"
16 
17 void PrintLogsCallback(vtkObject* obj, unsigned long eid, void* clientdata, void* calldata);
18 
19 int main(int argc, char** argv)
20 {
21 
22  bool printHelp(false);
23  std::string inputConfigFileName = "Testing/PlusDeviceSet_DataCollectionOnly_GenericSensor.xml";
24 
25  vtksys::CommandLineArguments args;
26  args.Initialize(argc, argv);
27 
28  int verboseLevel = vtkPlusLogger::LOG_LEVEL_UNDEFINED;
29  args.AddArgument("--help", vtksys::CommandLineArguments::NO_ARGUMENT, &printHelp, "Print this help.");
30  args.AddArgument("--verbose", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &verboseLevel, "Verbose level (1=error only, 2=warning, 3=info, 4=debug, 5=trace)");
31  args.AddArgument("--config-file", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &inputConfigFileName, "Config file containing the device configuration.");
32 
33  if (!args.Parse())
34  {
35  std::cerr << "Problem parsing arguments" << std::endl;
36  std::cout << "\n\nHelp:" << args.GetHelp() << std::endl;
37  exit(EXIT_FAILURE);
38  }
39 
40  vtkPlusLogger::Instance()->SetLogLevel(verboseLevel);
41 
42  if (printHelp)
43  {
44  std::cout << "\n\nHelp:" << args.GetHelp() << std::endl;
45  exit(EXIT_SUCCESS);
46  }
47 
48  vtkNew<vtkPlusGenericSensorTracker> sensorTracker;
49  sensorTracker->SetDeviceId("TrackerDevice");
50  if (!vtksys::SystemTools::FileExists(inputConfigFileName))
51  {
52  LOG_ERROR("Bad configuration file: " << inputConfigFileName);
53  exit(EXIT_FAILURE);
54  }
55 
56  LOG_DEBUG("Reading config file: " << inputConfigFileName);
57  auto* configRead = vtkXMLUtilities::ReadElementFromFile(inputConfigFileName.c_str());
58 
59  if (!configRead)
60  {
61  LOG_ERROR("Failed to read configuration file");
62  exit(EXIT_FAILURE);
63  }
64 
65  LOG_TRACE("Config file: " << *configRead);
66  if (sensorTracker->ReadConfiguration(configRead) != PLUS_SUCCESS)
67  {
68  LOG_ERROR("Failed to read configuration");
69  exit(EXIT_FAILURE);
70  }
71 
72  sensorTracker->CreateDefaultOutputChannel(NULL, true);
73 
74  // Add an observer to warning and error events for redirecting it to the stdout
75  vtkNew<vtkCallbackCommand> callbackCommand;
76  callbackCommand->SetCallback(PrintLogsCallback);
77  sensorTracker->AddObserver("WarningEvent", callbackCommand);
78  sensorTracker->AddObserver("ErrorEvent", callbackCommand);
79 
80  sensorTracker->StartRecording();
81  igtl::Sleep(1000);
82  sensorTracker->StopRecording();
83 
84  LOG_INFO("Exit successfully");
85  exit(EXIT_SUCCESS);
86 }
87 
88 
89 // Callback function for error and warning redirects
90 void PrintLogsCallback(vtkObject* obj, unsigned long eid, void* clientdata, void* calldata)
91 {
92  if (eid == vtkCommand::GetEventIdFromString("WarningEvent"))
93  {
94  LOG_WARNING((const char*)calldata);
95  }
96  else if (eid == vtkCommand::GetEventIdFromString("ErrorEvent"))
97  {
98  LOG_ERROR((const char*)calldata);
99  }
100 }
int main(int argc, char **argv)
#define PLUS_SUCCESS
Definition: PlusCommon.h:44
static vtkIGSIOLogger * Instance()
void PrintLogsCallback(vtkObject *obj, unsigned long eid, void *clientdata, void *calldata)