7 #include "PlusConfigure.h" 8 #include "igsioTrackedFrame.h" 9 #include "vtkActorCollection.h" 10 #include "vtkCallbackCommand.h" 11 #include "vtkCollectionIterator.h" 12 #include "vtkCommand.h" 13 #include "vtkImageActor.h" 14 #include "vtkImageData.h" 15 #include "vtkImageImport.h" 16 #ifdef PLUS_RENDERING_ENABLED 17 #include "vtkImageViewer2.h" 19 #include "vtkMatrix4x4.h" 21 #include "vtkRenderWindow.h" 22 #include "vtkRenderWindow.h" 23 #include "vtkRenderWindowInteractor.h" 24 #include "vtkRenderer.h" 25 #include "vtkRenderer.h" 26 #include "vtkIGSIOSequenceIO.h" 27 #include "vtkSmartPointer.h" 28 #include "vtkTextActor.h" 29 #include "vtkTextActor3D.h" 30 #include "vtkTextProperty.h" 31 #include "vtkIGSIOTrackedFrameList.h" 32 #include "vtkTransform.h" 33 #include "vtkIGSIOTransformRepository.h" 34 #include "vtkXMLUtilities.h" 35 #include "vtksys/CommandLineArguments.hxx" 40 class vtkMyCallback :
public vtkCommand
43 static vtkMyCallback* New() {
return new vtkMyCallback;}
45 void Initialize(vtkRenderWindow* renderWindow,
46 vtkRenderWindowInteractor* renderWindowInteractor,
47 vtkTextActor* textActor,
48 vtkCollection* imageActors,
49 std::vector<vtkTransform*>* imageTransforms)
51 this->RenderWindow = renderWindow;
52 this->RenderWindowInteractor = renderWindowInteractor;
53 this->TextActor = textActor;
54 this->ImageActors = imageActors;
55 this->ImageTransforms = imageTransforms;
58 virtual void Execute(vtkObject* caller,
unsigned long callerEvent,
void*)
60 if (callerEvent == vtkCommand::CharEvent)
62 char keycode = this->RenderWindowInteractor->GetKeyCode();
67 if (++this->FrameNum >= this->ImageActors->GetNumberOfItems())
72 if (this->CurrentActor != NULL)
74 this->CurrentActor->VisibilityOff();
75 static_cast<vtkImageActor*>(this->ImageActors->GetItemAsObject(0))->VisibilityOn();
78 this->CurrentActor = static_cast<vtkImageActor*>(this->ImageActors->GetItemAsObject(this->FrameNum));
79 this->CurrentActor->VisibilityOn();
84 if (--this->FrameNum < 0)
86 this->FrameNum = this->ImageActors->GetNumberOfItems() - 1;
89 if (this->CurrentActor != NULL)
91 this->CurrentActor->VisibilityOff();
92 static_cast<vtkImageActor*>(this->ImageActors->GetItemAsObject(0))->VisibilityOn();
95 this->CurrentActor = static_cast<vtkImageActor*>(this->ImageActors->GetItemAsObject(this->FrameNum));
96 this->CurrentActor->VisibilityOn();
102 double*
position = (*this->ImageTransforms)[this->FrameNum]->GetPosition();
103 std::ostringstream ss;
105 ss <<
"Frame " << this->FrameNum <<
"\nImage position: " << std::fixed <<
position[0] <<
" " <<
position[1] <<
" " <<
position[2] << std::ends;
106 this->TextActor->SetInput(ss.str().c_str());
108 this->RenderWindow->Render();
110 this->RenderWindowInteractor->CreateTimer(VTKI_TIMER_UPDATE);
117 this->CurrentActor = NULL;
118 this->RenderWindow = NULL;
119 this->RenderWindowInteractor = NULL;
120 this->TextActor = NULL;
121 this->ImageActors = NULL;
122 this->ImageTransforms = NULL;
125 virtual ~vtkMyCallback()
130 vtkImageActor* CurrentActor;
131 vtkRenderWindow* RenderWindow;
132 vtkRenderWindowInteractor* RenderWindowInteractor;
133 vtkTextActor* TextActor;
134 vtkCollection* ImageActors;
135 std::vector<vtkTransform*>* ImageTransforms;
138 int main(
int argc,
char** argv)
140 bool printHelp(
false);
141 std::string inputSequenceFilename;
142 std::string inputConfigFileName;
143 std::string outputModelFilename;
144 std::string imageToReferenceTransformNameStr;
145 bool renderingOff(
false);
147 int verboseLevel = vtkPlusLogger::LOG_LEVEL_UNDEFINED;
149 vtksys::CommandLineArguments args;
150 args.Initialize(argc, argv);
152 args.AddArgument(
"--image-to-reference-transform", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &imageToReferenceTransformNameStr,
"Transform name used for displaying the slices");
153 args.AddArgument(
"--source-seq-file", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &inputSequenceFilename,
"Tracked ultrasound recorded by Plus (e.g., by the TrackedUltrasoundCapturing application) in a sequence file (.mha/.nrrd)");
154 args.AddArgument(
"--config-file", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &inputConfigFileName,
"Config file containing coordinate system definitions");
155 args.AddArgument(
"--rendering-off", vtksys::CommandLineArguments::NO_ARGUMENT, &renderingOff,
"Run in test mode, without rendering.");
156 args.AddArgument(
"--verbose", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &verboseLevel,
"Verbose level (1=error only, 2=warning, 3=info, 4=debug, 5=trace)");
157 args.AddArgument(
"--help", vtksys::CommandLineArguments::NO_ARGUMENT, &printHelp,
"Print this help.");
161 std::cerr <<
"Problem parsing arguments" << std::endl;
162 std::cout <<
"Help: " << args.GetHelp() << std::endl;
168 std::cout << args.GetHelp() << std::endl;
174 if (inputSequenceFilename.empty())
176 std::cerr <<
"--source-seq-file is required" << std::endl;
182 vtkSmartPointer<vtkRenderWindow> renWin = vtkSmartPointer<vtkRenderWindow>::New();
184 vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
185 renWin->AddRenderer(renderer);
188 vtkSmartPointer<vtkCollection> imageActors = vtkSmartPointer<vtkCollection>::New();
191 vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
192 renderWindowInteractor->SetRenderWindow(renWin);
195 LOG_DEBUG(
"Reading input... ");
196 vtkSmartPointer< vtkIGSIOTrackedFrameList > trackedFrameList = vtkSmartPointer< vtkIGSIOTrackedFrameList >::New();
198 if (vtkIGSIOSequenceIO::Read(inputSequenceFilename, trackedFrameList) !=
PLUS_SUCCESS)
200 LOG_ERROR(
"Unable to load input sequences file.");
203 LOG_DEBUG(
"Reading input done.");
204 LOG_DEBUG(
"Number of frames: " << trackedFrameList->GetNumberOfTrackedFrames());
207 vtkSmartPointer<vtkIGSIOTransformRepository> transformRepository = vtkSmartPointer<vtkIGSIOTransformRepository>::New();
208 if (!inputConfigFileName.empty())
210 LOG_DEBUG(
"Reading config file...");
211 vtkSmartPointer<vtkXMLDataElement> configRootElement = vtkSmartPointer<vtkXMLDataElement>::New();
214 LOG_ERROR(
"Unable to read configuration from file " << inputConfigFileName.c_str());
217 if (transformRepository->ReadConfiguration(configRootElement) !=
PLUS_SUCCESS)
219 LOG_ERROR(
"Failed to read transforms for transform repository!");
225 LOG_INFO(
"Configuration file is not specified. Only those transforms are available that are defined in the sequence metafile");
228 LOG_INFO(
"Adding frames to actors...");
230 std::vector<vtkTransform*> imageTransforms;
232 igsioTransformName imageToReferenceTransformName;
233 if (!imageToReferenceTransformNameStr.empty())
236 if (imageToReferenceTransformName.SetTransformName(imageToReferenceTransformNameStr.c_str()) !=
PLUS_SUCCESS)
238 LOG_ERROR(
"Invalid image to reference transform name: " << imageToReferenceTransformNameStr);
243 int numberOfFrames = trackedFrameList->GetNumberOfTrackedFrames();
244 for (
int frameIndex = 0; frameIndex < numberOfFrames; frameIndex++)
246 vtkPlusLogger::PrintProgressbar((100.0 * frameIndex) / numberOfFrames);
247 igsioTrackedFrame* frame = trackedFrameList->GetTrackedFrame(frameIndex);
250 if (transformRepository->SetTransforms(*frame) !=
PLUS_SUCCESS)
252 LOG_ERROR(
"Failed to set repository transforms from tracked frame!");
256 vtkSmartPointer<vtkImageData> frameImageData = vtkSmartPointer<vtkImageData>::New();
257 frameImageData->DeepCopy(frame->GetImageData()->GetImage());
259 vtkSmartPointer<vtkImageActor> imageActor = vtkSmartPointer<vtkImageActor>::New();
260 imageActor->SetInputData(frameImageData);
262 vtkSmartPointer< vtkTransform > imageToReferenceTransform = vtkSmartPointer< vtkTransform >::New();
263 if (imageToReferenceTransformName.IsValid())
265 vtkSmartPointer<vtkMatrix4x4> imageToReferenceTransformMatrix = vtkSmartPointer<vtkMatrix4x4>::New();
266 if (transformRepository->GetTransform(imageToReferenceTransformName, imageToReferenceTransformMatrix) !=
PLUS_SUCCESS)
268 std::string strTransformName;
269 imageToReferenceTransformName.GetTransformName(strTransformName);
270 LOG_ERROR(
"Failed to get transform from repository: " << strTransformName);
273 imageToReferenceTransform->SetMatrix(imageToReferenceTransformMatrix);
276 imageActor->SetUserTransform(imageToReferenceTransform);
277 imageActor->VisibilityOff();
278 imageTransforms.push_back(imageToReferenceTransform);
279 imageActors->AddItem(imageActor);
282 vtkPlusLogger::PrintProgressbar(100);
283 std::cout << std::endl;
285 for (
int i = 0;
i < imageActors->GetNumberOfItems();
i++)
287 vtkImageActor* imgActor = static_cast<vtkImageActor*>(imageActors->GetItemAsObject(
i));
288 imgActor->SetVisibility(
i == 0);
289 renderer->AddActor(imgActor);
294 LOG_INFO(
"No need for rendering...");
299 vtkSmartPointer<vtkTextActor> textActor = vtkSmartPointer<vtkTextActor>::New();
300 vtkSmartPointer<vtkTextProperty> textprop = textActor->GetTextProperty();
301 textprop->SetColor(1, 0, 0);
302 textprop->SetFontFamilyToArial();
303 textprop->SetFontSize(15);
304 textprop->SetJustificationToLeft();
305 textprop->SetVerticalJustificationToTop();
307 textActor->VisibilityOn();
308 textActor->SetDisplayPosition(10, 50);
310 renderer->AddActor(textActor);
312 renderer->SetBackground(0.1, 0.2, 0.4);
313 renWin->SetSize(800, 600);
317 vtkSmartPointer<vtkMyCallback> call = vtkSmartPointer<vtkMyCallback>::New();
318 call->Initialize(renWin, renderWindowInteractor, textActor, imageActors, &imageTransforms);
319 renderWindowInteractor->AddObserver(vtkCommand::TimerEvent, call);
320 renderWindowInteractor->AddObserver(vtkCommand::CharEvent, call);
321 renderWindowInteractor->CreateTimer(VTKI_TIMER_FIRST);
324 renderWindowInteractor->Initialize();
325 renderWindowInteractor->Start();
328 std::cout <<
"MetaImageSequenceViewer completed successfully!" << std::endl;
int main(int argc, char **argv)
static vtkIGSIOLogger * Instance()
static PlusStatus ReadDeviceSetConfigurationFromFile(vtkXMLDataElement *config, const char *filename)