Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkToFImageRecorderFilter.cpp
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
14 #include <mitkImage.h>
15 #include <mitkImageDataItem.h>
16 #include "mitkToFNrrdImageWriter.h"
17 #include "mitkToFImageCsvWriter.h"
18 #include "mitkImageReadAccessor.h"
19 
20 // itk includes
21 #include "itksys/SystemTools.hxx"
22 
23 
24 mitk::ToFImageRecorderFilter::ToFImageRecorderFilter(): m_RecordingStarted(false), m_ToFImageWriter(nullptr)
25 {
26  m_FileExtension = "";
27 }
28 
30 {
31 }
32 
34 {
35  std::string name = fileName;
36  m_FileExtension = itksys::SystemTools::GetFilenameLastExtension( fileName );
37  if(m_FileExtension == ".nrrd")
38  {
40  }
41  else if(m_FileExtension == ".csv")
42  {
44  }
45  else
46  {
47  throw std::logic_error("The specified file type is not supported, standard file type is .nrrd!");
48  }
49  int pos = name.find_last_of(".");
50  name.insert(pos,"_DistanceImage");
51  m_ToFImageWriter->SetDistanceImageFileName(name);
52  name = fileName;
53  name.insert(pos,"_AmplitudeImage");
54  m_ToFImageWriter->SetAmplitudeImageFileName(name);
55  name = fileName;
56  name.insert(pos,"_IntensityImage");
57  m_ToFImageWriter->SetIntensityImageFileName(name);
58 }
59 
61 {
62  m_ToFImageWriter->SetToFImageType(tofImageType);
63 }
64 
66 {
67  mitk::Image::Pointer distanceImageInput = this->GetInput(0);
68  assert(distanceImageInput);
69  mitk::Image::Pointer amplitudeImageInput = this->GetInput(1);
70  assert(amplitudeImageInput);
71  mitk::Image::Pointer intensityImageInput = this->GetInput(2);
72  assert(intensityImageInput);
73  // add current data to file stream
74  ImageReadAccessor distAcc(distanceImageInput, distanceImageInput->GetSliceData(0,0,0));
75  ImageReadAccessor amplAcc(amplitudeImageInput, amplitudeImageInput->GetSliceData(0,0,0));
76  ImageReadAccessor intenAcc(intensityImageInput, intensityImageInput->GetSliceData(0,0,0));
77 
78  float* distanceFloatData = (float*) distAcc.GetData();
79  float* amplitudeFloatData = (float*) amplAcc.GetData();
80  float* intensityFloatData = (float*) intenAcc.GetData();
82  {
83  m_ToFImageWriter->Add(distanceFloatData,amplitudeFloatData,intensityFloatData);
84  }
85 
86  // set outputs to inputs
87  this->SetNthOutput(0,distanceImageInput);
88  this->SetNthOutput(1,amplitudeImageInput);
89  this->SetNthOutput(2,intensityImageInput);
90 }
91 
93 {
94  if(m_ToFImageWriter.IsNull())
95  {
96  throw std::logic_error("ToFImageWriter is unitialized, set filename first!");
97  return;
98  }
99  m_ToFImageWriter->Open();
100  m_RecordingStarted = true;
101 }
102 
104 {
105  m_ToFImageWriter->Close();
106  m_RecordingStarted = false;
107 }
108 
109 mitk::ToFImageWriter::Pointer mitk::ToFImageRecorderFilter::GetToFImageWriter()
110 {
111  return m_ToFImageWriter;
112 }
113 
114 void mitk::ToFImageRecorderFilter::SetToFImageWriter(mitk::ToFImageWriter::Pointer tofImageWriter)
115 {
116  m_ToFImageWriter = tofImageWriter;
117 }
118 
120 {
121  this->SetInput(0,input);
122 }
123 
124 void mitk::ToFImageRecorderFilter::SetInput( unsigned int idx, const InputImageType* input )
125 {
126  if ((input == nullptr) && (idx == this->GetNumberOfInputs() - 1)) // if the last input is set to nullptr, reduce the number of inputs by one
127  {
128  this->SetNumberOfIndexedInputs(this->GetNumberOfInputs() - 1);
129  }
130  else if(idx == 0 || idx == 1 || idx == 2)
131  {
132  this->ProcessObject::SetNthInput(idx, const_cast<InputImageType*>(input)); // Process object is not const-correct so the const_cast is required here
133  unsigned int xDim = input->GetDimension(0);
134  unsigned int yDim = input->GetDimension(1);
135  m_ToFImageWriter->SetToFCaptureWidth(xDim);
136  m_ToFImageWriter->SetToFCaptureWidth(yDim);
137  }
138 
140 }
141 
143 {
144  return this->GetInput(0);
145 }
146 
148 {
149  if (this->GetNumberOfInputs() < 1)
150  return nullptr;
151  return static_cast< mitk::Image*>(this->ProcessObject::GetInput(idx));
152 }
153 
155 {
156  this->SetNumberOfIndexedOutputs(this->GetNumberOfIndexedInputs()); // create outputs for all inputs
157  for (unsigned int idx = 0; idx < this->GetNumberOfOutputs(); ++idx)
158  if (this->GetOutput(idx) == nullptr)
159  {
160  DataObjectPointer newOutput = this->MakeOutput(idx);
161  this->SetNthOutput(idx, newOutput);
162  }
163  this->Modified();
164 }
void SetImageType(ToFImageWriter::ToFImageType tofImageType)
Set image type for recording.
ToFImageRecorderFilter()
standard constructor
itk::DataObject::Pointer MakeOutput(DataObjectPointerArraySizeType idx) override
Make a DataObject of the correct type to used as the specified output.
ToFImageWriter::Pointer m_ToFImageWriter
image writer used for streaming input data to file
void StartRecording()
start recording of data
static Pointer New()
bool m_RecordingStarted
flag indicating if filter is currently recording
void CreateOutputsForAllInputs()
Create an output for each input.
static Pointer New()
void StopRecording()
stop recording of data
void SetInput(const InputImageType *input) override
sets the input of this filter
unsigned int GetDimension() const
Get dimension of the image.
Definition: mitkImage.cpp:106
void GenerateData() override
method generating the output of this filter. Called in the updated process of the pipeline...
void SetFileName(std::string fileName)
Set file name for writing image files This filename will be appended by "_DistanceImage", "_AmplitudeImage", or "_IntensityImage" for the single images.
Image class for storing images.
Definition: mitkImage.h:72
~ToFImageRecorderFilter() override
standard destructor
void SetToFImageWriter(ToFImageWriter::Pointer tofImageWriter)
Sets a pointer to the ToFImageWriter internally used.
ToFImageWriter::Pointer GetToFImageWriter()
Returns a pointer to the ToFImageWriter internally used.
OutputType * GetOutput()
Get the output data of this image source object.
ImageReadAccessor class to get locked read access for a particular image part.
Image * GetInput()
returns the input of this filter
const void * GetData() const
Gives const access to the data.