iMSTK
Interactive Medical Simulation Toolkit
imstkImageReslice.cpp
1 /*
2 ** This file is part of the Interactive Medical Simulation Toolkit (iMSTK)
3 ** iMSTK is distributed under the Apache License, Version 2.0.
4 ** See accompanying NOTICE for details.
5 */
6 
7 #include "imstkImageReslice.h"
8 #include "imstkGeometryUtilities.h"
9 #include "imstkImageData.h"
10 #include "imstkLogger.h"
11 
12 #include <vtkImageData.h>
13 #include <vtkImageReslice.h>
14 #include <vtkTransform.h>
15 
16 namespace imstk
17 {
18 ImageReslice::ImageReslice()
19 {
21  setRequiredInputType<ImageData>(0);
22 
24  setOutput(std::make_shared<ImageData>());
25 }
26 
27 std::shared_ptr<ImageData>
28 ImageReslice::getOutputImage() const
29 {
30  return std::dynamic_pointer_cast<ImageData>(getOutput(0));
31 }
32 
33 void
34 ImageReslice::setInputImage(std::shared_ptr<ImageData> inputData)
35 {
36  setInput(inputData, 0);
37 }
38 
39 void
41 {
42  std::shared_ptr<ImageData> inputImage = std::dynamic_pointer_cast<ImageData>(getInput(0));
43  if (inputImage == nullptr)
44  {
45  LOG(WARNING) << "No inputImage to resample";
46  return;
47  }
48 
49  vtkNew<vtkTransform> transform;
50  Mat4d test = m_Transform.transpose();
51  transform->SetMatrix(test.data());
52 
53  vtkNew<vtkImageReslice> reslice;
54  reslice->SetResliceTransform(transform);
55  if (m_InterpolationType == InterpolateType::NearestNeighbor)
56  {
57  reslice->SetInterpolationModeToNearestNeighbor();
58  }
59  else if (m_InterpolationType == InterpolateType::Linear)
60  {
61  reslice->SetInterpolationModeToLinear();
62  }
63  else if (m_InterpolationType == InterpolateType::Cubic)
64  {
65  reslice->SetInterpolationModeToCubic();
66  }
67  vtkSmartPointer<vtkImageData> vtkInputImage = GeometryUtils::copyToVtkImageData(inputImage);
68  reslice->SetInputData(vtkInputImage);
69  reslice->SetResliceTransform(transform);
70  reslice->SetAutoCropOutput(true);
71  reslice->Update();
72 
73  setOutput(GeometryUtils::copyToImageData(reslice->GetOutput()));
74 }
75 } // namespace imstk
void requestUpdate() override
Users can implement this for the logic to be run.
std::shared_ptr< Geometry > getInput(size_t port=0) const
Returns input geometry given port, returns nullptr if doesn&#39;t exist.
Compound Geometry.
void setNumOutputPorts(const size_t numPorts)
Get/Set the amount of output ports.
std::shared_ptr< Geometry > getOutput(size_t port=0) const
Returns output geometry given port, returns nullptr if doesn&#39;t exist.
void setInput(std::shared_ptr< Geometry > inputGeometry, size_t port=0)
Set the input at the port.
void setOutput(std::shared_ptr< Geometry > inputGeometry, const size_t port=0)
Set the output at the port.
void setNumInputPorts(const size_t numPorts)
Get/Set the amount of input ports.
Class to represent 1, 2, or 3D image data (i.e. structured points)