PlusLib  2.9.0
Software library for tracked ultrasound image acquisition, calibration, and processing.
vtkVirtualTextRecognizerTest.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 "vtkPlusDataCollector.h"
10 #include "vtksys/CommandLineArguments.hxx"
11 #include <map>
12 
13 int main(int argc, char** argv)
14 {
15  bool printHelp(false);
16  std::string inputConfigFileName;
17  std::string deviceId;
18  std::string fieldValue;
19 
20  vtksys::CommandLineArguments args;
21  args.Initialize(argc, argv);
22 
23  int verboseLevel = vtkPlusLogger::LOG_LEVEL_UNDEFINED;
24 
25  args.AddArgument("--help", vtksys::CommandLineArguments::NO_ARGUMENT, &printHelp, "Print this help.");
26  args.AddArgument("--config-file", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &inputConfigFileName, "Config file to test with.");
27  args.AddArgument("--device-id", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &deviceId, "Id of the text recognizer device.");
28  args.AddArgument("--field-value", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &fieldValue, "Value of the first field.");
29  args.AddArgument("--verbose", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &verboseLevel, "Verbose level (1=error only, 2=warning, 3=info, 4=debug, 5=trace)");
30 
31  if (!args.Parse())
32  {
33  std::cerr << "Problem parsing arguments" << std::endl;
34  std::cout << "\n\nvtkPlusVirtualTextRecognizerTest help:" << args.GetHelp() << std::endl;
35  return EXIT_FAILURE;
36  }
37 
38  vtkPlusLogger::Instance()->SetLogLevel(verboseLevel);
39 
40  if (printHelp)
41  {
42  std::cout << "\n\nvtkPlusVirtualTextRecognizerTest help:" << args.GetHelp() << std::endl;
43  return EXIT_SUCCESS;
44  }
45 
46  if (!vtksys::SystemTools::FileExists(inputConfigFileName))
47  {
48  LOG_ERROR("Invalid config file sent to test.");
49  return EXIT_FAILURE;
50  }
51 
52  vtkSmartPointer<vtkXMLDataElement> configRootElement = vtkSmartPointer<vtkXMLDataElement>::New();
53  if (PlusXmlUtils::ReadDeviceSetConfigurationFromFile(configRootElement, inputConfigFileName.c_str()) == PLUS_FAIL)
54  {
55  LOG_ERROR("Unable to read configuration from file " << inputConfigFileName.c_str());
56  return EXIT_FAILURE;
57  }
58 
60 
61  vtkSmartPointer<vtkPlusDataCollector> dataCollector = vtkSmartPointer<vtkPlusDataCollector>::New();
62 
63  if (dataCollector->ReadConfiguration(configRootElement) != PLUS_SUCCESS)
64  {
65  LOG_ERROR("Configuration incorrect for vtkPlusVirtualTextRecognizerTest.");
66  return EXIT_FAILURE;
67  }
68 
69  vtkPlusDevice* device(NULL);
70  if (dataCollector->GetDevice(device, deviceId) != PLUS_SUCCESS)
71  {
72  LOG_ERROR("Unable to retrieve recognizer device by Id: " << deviceId);
73  return EXIT_FAILURE;
74  }
75 
77  if (textRecognizer == NULL)
78  {
79  LOG_ERROR("Unable to retrieve recognizer device by Id: " << deviceId);
80  return EXIT_FAILURE;
81  }
82  std::stringstream tessDataPathSS;
83  tessDataPathSS << vtkPlusConfig::GetInstance()->GetImagePath("../../tessdata");
84  textRecognizer->SetTessdataDirectory(vtkPlusConfig::GetInstance()->GetAbsolutePath("", tessDataPathSS.str()));
85 
86  if (dataCollector->Connect() != PLUS_SUCCESS)
87  {
88  LOG_ERROR("Failed to connect to devices!");
89  return EXIT_FAILURE;
90  }
91 
92  if (dataCollector->Start() != PLUS_SUCCESS)
93  {
94  LOG_ERROR("Failed to start data collection!");
95  return EXIT_FAILURE;
96  }
97 
98  textRecognizer->SetMissingInputGracePeriodSec(0);
99 
100 #ifdef _WIN32
101  Sleep(500);
102 #else
103  usleep(500000);
104 #endif
105 
106  vtkPlusVirtualTextRecognizer::ChannelFieldListMap map = textRecognizer->GetRecognitionFields();
107  vtkPlusVirtualTextRecognizer::FieldListIterator it = map.begin()->second.begin();
108  if ((*it)->LatestParameterValue != fieldValue)
109  {
110  LOG_ERROR("Direct: Parameter \"" << (*it)->ParameterName << "\" value=\"" << (*it)->LatestParameterValue << "\" does not match expected value=\"" << fieldValue << "\"");
111  return EXIT_FAILURE;
112  }
113 
114  igsioTrackedFrame frame;
115  (*device->GetOutputChannelsStart())->GetTrackedFrame(frame);
116 
117  if (frame.GetFrameField((*it)->ParameterName).empty() || !igsioCommon::IsEqualInsensitive(frame.GetFrameField((*it)->ParameterName), fieldValue))
118  {
119  LOG_ERROR("Tracked Frame: Parameter \"" << (*it)->ParameterName << "\" value=\"" << (*it)->LatestParameterValue << "\" does not match expected value=\"" << fieldValue << "\"");
120  return EXIT_FAILURE;
121  }
122 
123  LOG_INFO("Exit successfully");
124  return EXIT_SUCCESS;
125 }
Abstract interface for tracker and video devices.
Definition: vtkPlusDevice.h:60
int main(int argc, char **argv)
std::string GetImagePath(const std::string &subPath)
#define PLUS_FAIL
Definition: PlusCommon.h:43
virtual void SetMissingInputGracePeriodSec(double)
static vtkPlusConfig * GetInstance()
#define PLUS_SUCCESS
Definition: PlusCommon.h:44
ChannelContainerConstIterator GetOutputChannelsStart() const
static vtkIGSIOLogger * Instance()
std::map< vtkPlusChannel *, FieldList > ChannelFieldListMap
void SetDeviceSetConfigurationData(vtkXMLDataElement *deviceSetConfigurationData)
static PlusStatus ReadDeviceSetConfigurationFromFile(vtkXMLDataElement *config, const char *filename)
Definition: PlusXmlUtils.h:23
static vtkPlusVirtualTextRecognizer * SafeDownCast(vtkObject *o)