14 #include "PlusConfigure.h" 19 #include "igsioMath.h" 20 #ifdef PLUS_RENDERING_ENABLED 25 #include <vtkCallbackCommand.h> 26 #include <vtkCamera.h> 27 #include <vtkCommand.h> 28 #include <vtkInteractorStyleTrackballCamera.h> 29 #include <vtkMatrix4x4.h> 30 #include <vtkRenderWindow.h> 31 #include <vtkRenderWindowInteractor.h> 32 #include <vtkRenderer.h> 33 #include <vtkSmartPointer.h> 34 #include <vtkTextActor.h> 35 #include <vtkTextProperty.h> 36 #include <vtkTimerLog.h> 37 #include <vtkTransform.h> 38 #include <vtkXMLUtilities.h> 39 #include <vtksys/CommandLineArguments.hxx> 40 #include <vtksys/SystemTools.hxx> 42 class vtkMyCallback :
public vtkCommand
45 static vtkMyCallback* New()
47 return new vtkMyCallback;
53 this->StepperTextActor = vtkTextActor::New();
56 virtual ~vtkMyCallback()
58 this->StepperTextActor->Delete();
59 this->StepperTextActor = NULL;
60 #ifdef PLUS_RENDERING_ENABLED 61 for (std::map<std::string, vtkPlusToolAxesActor*>::iterator it = this->ToolActors.begin(); it != this->ToolActors.end(); ++it)
73 vtkTextProperty* textprop = this->StepperTextActor->GetTextProperty();
74 textprop->SetColor(1, 0, 0);
75 textprop->SetFontFamilyToArial();
76 textprop->SetFontSize(15);
77 textprop->SetJustificationToLeft();
78 textprop->SetVerticalJustificationToTop();
79 this->StepperTextActor->VisibilityOn();
80 this->StepperTextActor->SetDisplayPosition(20, 65);
81 this->Renderer->AddActor(this->StepperTextActor);
84 this->DataCollector->GetDevice(aDevice, DeviceId);
92 AddNewToolActor(tool->GetId());
93 SetToolVisible(tool->GetId(),
true);
96 this->RenderWindowInteractor->AddObserver(vtkCommand::TimerEvent,
this);
98 this->RenderWindowInteractor->AddObserver(vtkCommand::EndInteractionEvent,
this);
100 this->TimerId = this->RenderWindowInteractor->CreateOneShotTimer(100);
103 void AddNewToolActor(
const std::string& aToolId)
105 #ifdef PLUS_RENDERING_ENABLED 107 this->Renderer->AddActor(actor);
108 actor->SetVisibility(
false);
109 this->ToolActors[aToolId] = actor;
112 LOG_WARNING(
"Cannot add actor when VTK_RENDERING_BACKEND is None!");
116 void SetToolVisible(
const std::string& aToolId,
bool visible)
118 #ifdef PLUS_RENDERING_ENABLED 119 this->ToolActors[aToolId]->SetVisibility(visible);
121 LOG_WARNING(
"Cannot change tool visibility when VTK_RENDERING_BACKEND is None!");
125 void SetToolToTrackerTransform(
const std::string& aToolId, vtkMatrix4x4* toolToTrackerTransform)
127 #ifdef PLUS_RENDERING_ENABLED 128 vtkSmartPointer<vtkTransform> normalizedTransform = vtkSmartPointer<vtkTransform>::New();
129 normalizedTransform->SetMatrix(toolToTrackerTransform);
130 this->ToolActors[aToolId]->SetUserTransform(normalizedTransform);
132 LOG_WARNING(
"Cannot set tool transform when VTK_RENDERING_BACKEND is None!");
136 virtual void Execute(vtkObject* caller,
unsigned long,
void*)
138 std::ostringstream ss;
141 igsioTrackedFrame trackedFrame;
142 if (this->BroadcastChannel->GetTrackedFrame(trackedFrame) !=
PLUS_SUCCESS)
144 LOG_ERROR(
"Failed to get tracked frame!");
145 this->TimerId = this->RenderWindowInteractor->CreateOneShotTimer(100);
150 this->DataCollector->GetDevice(aDevice, DeviceId);
156 std::vector<igsioTransformName> transformNameList;
157 trackedFrame.GetFrameTransformNameList(transformNameList);
158 for (std::vector<igsioTransformName>::iterator it = transformNameList.begin(); it != transformNameList.end(); ++it)
160 igsioTransformName transformName = *it;
165 LOG_ERROR(
"Failed to get tool: " << transformName.From());
169 std::string strTransformName;
172 ss << strTransformName <<
": ";
174 vtkSmartPointer<vtkMatrix4x4> toolToTrackerTransform = vtkSmartPointer<vtkMatrix4x4>::New();
175 if (trackedFrame.GetFrameTransform(transformName, toolToTrackerTransform) !=
PLUS_SUCCESS)
177 ss <<
"failed to get transform\n";
178 SetToolVisible(tool->GetId(),
false);
182 ToolStatus status(TOOL_INVALID);
183 trackedFrame.GetFrameTransformStatus(transformName, status);
185 if (status != TOOL_OK)
187 ss <<
"missing or out of view\n";
188 SetToolVisible(tool->GetId(),
false);
193 SetToolToTrackerTransform(tool->GetId(), toolToTrackerTransform);
194 SetToolVisible(tool->GetId(),
true);
196 << toolToTrackerTransform->GetElement(0, 0) <<
" " << toolToTrackerTransform->GetElement(0, 1) <<
" " << toolToTrackerTransform->GetElement(0, 2) <<
" " << toolToTrackerTransform->GetElement(0, 3) <<
" / " 197 << toolToTrackerTransform->GetElement(1, 0) <<
" " << toolToTrackerTransform->GetElement(1, 1) <<
" " << toolToTrackerTransform->GetElement(1, 2) <<
" " << toolToTrackerTransform->GetElement(1, 3) <<
" / " 198 << toolToTrackerTransform->GetElement(2, 0) <<
" " << toolToTrackerTransform->GetElement(2, 1) <<
" " << toolToTrackerTransform->GetElement(2, 2) <<
" " << toolToTrackerTransform->GetElement(2, 3) <<
" / " 199 << toolToTrackerTransform->GetElement(3, 0) <<
" " << toolToTrackerTransform->GetElement(3, 1) <<
" " << toolToTrackerTransform->GetElement(3, 2) <<
" " << toolToTrackerTransform->GetElement(3, 3) <<
"\n";
202 this->StepperTextActor->SetInput(ss.str().c_str());
203 this->StepperTextActor->Modified();
205 this->Renderer->GetRenderWindow()->Render();
207 static bool firstUpdate =
true;
210 this->Renderer->ResetCamera();
214 this->TimerId = this->RenderWindowInteractor->CreateOneShotTimer(100);
219 std::string DeviceId;
220 vtkRenderer* Renderer;
221 vtkRenderWindowInteractor* RenderWindowInteractor;
222 vtkTextActor* StepperTextActor;
223 #ifdef PLUS_RENDERING_ENABLED 224 std::map<std::string, vtkPlusToolAxesActor*> ToolActors;
229 int main(
int argc,
char** argv)
231 bool printHelp(
false);
232 std::string inputConfigFileName;
233 std::string inputToolSourceId;
234 double inputAcqTimeLength(60);
235 std::string outputTrackerBufferSequenceFileName;
236 bool renderingOff(
false);
238 int verboseLevel = vtkPlusLogger::LOG_LEVEL_UNDEFINED;
240 vtksys::CommandLineArguments args;
241 args.Initialize(argc, argv);
243 args.AddArgument(
"--help", vtksys::CommandLineArguments::NO_ARGUMENT, &printHelp,
"Print this help.");
244 args.AddArgument(
"--config-file", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &inputConfigFileName,
"Name of the input configuration file.");
245 args.AddArgument(
"--tool-name", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &inputToolSourceId,
"Will print the actual transform of this tool (names were defined in the config file, default is the first active tool)");
246 args.AddArgument(
"--acq-time-length", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &inputAcqTimeLength,
"Length of acquisition time in seconds (Default: 60s)");
247 args.AddArgument(
"--output-tracker-buffer-seq-file-name", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &outputTrackerBufferSequenceFileName,
"Filename of the output tracker bufffer sequence metafile (Default: TrackerBufferMetafile)");
248 args.AddArgument(
"--rendering-off", vtksys::CommandLineArguments::NO_ARGUMENT, &renderingOff,
"Run test without rendering.");
249 args.AddArgument(
"--verbose", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &verboseLevel,
"Verbose level (1=error only, 2=warning, 3=info, 4=debug, 5=trace)");
253 std::cerr <<
"Problem parsing arguments" << std::endl;
254 std::cout <<
"Help: " << args.GetHelp() << std::endl;
260 std::cout << args.GetHelp() << std::endl;
266 if (inputConfigFileName.empty())
268 std::cerr <<
"--config-file is required" << std::endl;
274 vtkSmartPointer<vtkXMLDataElement> configRootElement = vtkSmartPointer<vtkXMLDataElement>::New();
277 LOG_ERROR(
"Unable to read configuration from file " << inputConfigFileName.c_str());
281 vtkSmartPointer<vtkPlusDataCollector> dataCollector = vtkSmartPointer<vtkPlusDataCollector>::New();
282 if (dataCollector->ReadConfiguration(configRootElement) !=
PLUS_SUCCESS)
284 LOG_ERROR(
"Unable to parse data collection XML tag.");
287 std::string deviceId;
288 vtkXMLDataElement* dataCollectionElement = configRootElement->FindNestedElementWithName(
"DataCollection");
289 vtkXMLDataElement* deviceElement = dataCollectionElement->FindNestedElementWithName(
"Device");
290 if (deviceElement != NULL)
292 deviceId = std::string(deviceElement->GetAttribute(
"Id"));
295 dataCollector->Connect();
296 dataCollector->Start();
299 dataCollector->GetDevice(aDevice, deviceId);
302 LOG_ERROR(
"Unable to retrieve device \'" << deviceId <<
"\'.");
307 LOG_ERROR(
"No channels to retrieve data from. Check config file.");
312 const double acqStartTime = vtkTimerLog::GetUniversalTime();
316 LOG_ERROR(
"Tracking is not enabled!");
321 if (!inputToolSourceId.empty())
325 LOG_ERROR(
"Failed to get tool with name: " << inputToolSourceId);
333 LOG_ERROR(
"There is no active tool!");
343 LOG_ERROR(
"Tool does not exist anymore!");
350 LOG_DEBUG(
"Rendering is disabled");
353 vtkSmartPointer<vtkMatrix4x4> matrix = vtkSmartPointer<vtkMatrix4x4>::New();
355 while (acqStartTime + inputAcqTimeLength > vtkTimerLog::GetUniversalTime())
361 LOG_ERROR(
"Failed to get matrix from buffer item!");
365 std::string transformParameters = igsioMath::GetTransformParametersString(matrix);
366 std::string status = igsioCommon::ConvertToolStatusToString(bufferItem.
GetStatus());
369 message <<
"Tool name: " << tool->GetId() <<
"Transform: ";
370 for (
int r = 0; r < 4; r++)
372 for (
int c = 0; c < 4; c++)
374 message <<
" " << std::fixed << std::setprecision(5) << std::setw(8) << std::setfill(
' ') << matrix->GetElement(r, c);
378 message <<
" Status: " << status;
381 vtksys::SystemTools::Delay(200);
386 #ifdef PLUS_RENDERING_ENABLED 388 vtkSmartPointer<vtkRenderWindow> renWin = vtkSmartPointer<vtkRenderWindow>::New();
389 vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
390 renWin->AddRenderer(renderer);
393 vtkSmartPointer<vtkRenderWindowInteractor>
iren = vtkSmartPointer<vtkRenderWindowInteractor>::New();
394 iren->SetRenderWindow(renWin);
397 vtkSmartPointer<vtkInteractorStyleTrackballCamera> style = vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();
398 iren->SetInteractorStyle(style);
407 vtkSmartPointer<vtkMyCallback> transformDisplayUpdater = vtkSmartPointer<vtkMyCallback>::New();
408 transformDisplayUpdater->DataCollector = dataCollector;
409 transformDisplayUpdater->BroadcastChannel = aChannel;
410 transformDisplayUpdater->Renderer = renderer;
411 transformDisplayUpdater->DeviceId = deviceId;
412 transformDisplayUpdater->RenderWindowInteractor =
iren;
413 transformDisplayUpdater->Init();
416 vtkSmartPointer<vtkPlusToolAxesActor> originActor = vtkSmartPointer<vtkPlusToolAxesActor>::New();
417 originActor->SetName(
"origin");
418 renderer->AddActor(originActor);
421 renderer->GetActiveCamera()->ParallelProjectionOn();
425 LOG_WARNING(
"Cannot render the scene when when VTK_RENDERING_BACKEND is None!");
429 dataCollector->Disconnect();
431 if (!outputTrackerBufferSequenceFileName.empty())
433 LOG_INFO(
"Copy tracker...");
434 vtkSmartPointer<vtkPlusDevice> tracker = vtkSmartPointer<vtkPlusDevice>::New();
435 tracker->DeepCopy(*aDevice);
437 LOG_INFO(
"Write tracker to " << fullPath);
438 tracker->WriteToolsToSequenceFile(fullPath.c_str(),
true);
441 std::cout <<
"Test completed successfully!" << std::endl;
DataSourceContainer::const_iterator DataSourceContainerConstIterator
ToolStatus GetStatus() const
std::string GetOutputPath(const std::string &subPath)
Abstract interface for tracker and video devices.
std::string GetTransformName() const
DataSourceContainerIterator GetToolsStartIterator()
PlusStatus GetTool(vtkPlusDataSource *&aTool, const std::string &toolSourceId)
vtkRenderWindowInteractor * iren
int main(int argc, char **argv)
DataSourceContainerIterator GetToolsEndIterator()
static vtkPlusConfig * GetInstance()
Manages devices that record image or positional data.
ChannelContainerConstIterator GetOutputChannelsStart() const
PlusStatus GetMatrix(vtkMatrix4x4 *outputMatrix)
bool GetTrackingEnabled() const
virtual ItemStatus GetLatestStreamBufferItem(StreamBufferItem *bufferItem)
static vtkIGSIOLogger * Instance()
DataSourceContainerConstIterator GetToolIteratorBegin() const
Contains an optional timestamped circular buffer containing the video images and a number of timestam...
virtual int OutputChannelCount() const
PlusStatus GetTool(const char *aToolSourceId, vtkPlusDataSource *&aTool) const
DataSourceContainerConstIterator GetToolIteratorEnd() const
static PlusStatus ReadDeviceSetConfigurationFromFile(vtkXMLDataElement *config, const char *filename)
Interface to a 3D positioning tool, video source, or generalized data stream.