Orfeo Toolbox  3.16
itkJoinSeriesImageFilter.txx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Insight Segmentation & Registration Toolkit
4  Module: $RCSfile: itkJoinSeriesImageFilter.txx,v $
5  Language: C++
6  Date: $Date: 2008-10-09 16:20:18 $
7  Version: $Revision: 1.7 $
8 
9  Copyright (c) Insight Software Consortium. All rights reserved.
10  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
11 
12  This software is distributed WITHOUT ANY WARRANTY; without even
13  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14  PURPOSE. See the above copyright notices for more information.
15 
16 =========================================================================*/
17 #ifndef __itkJoinSeriesImageFilter_txx
18 #define __itkJoinSeriesImageFilter_txx
19 
21 #include "itkProgressReporter.h"
23 #include "itkImageRegionIterator.h"
24 
25 namespace itk
26 {
27 
28 template <class TInputImage, class TOutputImage>
31 {
32  m_Spacing = 1.0;
33  m_Origin = 0.0;
34 }
35 
36 
37 template <class TInputImage, class TOutputImage>
38 void
40 ::PrintSelf(std::ostream& os, Indent indent) const
41 {
42  Superclass::PrintSelf(os,indent);
43 
44  os << indent << "Spacing: " << m_Spacing << std::endl;
45  os << indent << "Origin: " << m_Origin << std::endl;
46 }
47 
51 template <class TInputImage, class TOutputImage>
52 void
55 {
56  // do not call the superclass' implementation of this method since
57  // this filter allows the input the output to be of different dimensions
58 
59  // get pointers to the input and output
60  typename Superclass::OutputImagePointer outputPtr = this->GetOutput();
61  typename Superclass::InputImageConstPointer inputPtr = this->GetInput();
62 
63  if ( !outputPtr || !inputPtr)
64  {
65  return;
66  }
67 
68  // Set the output image largest possible region. Use a RegionCopier
69  // so that the input and output images can be different dimensions.
70  OutputImageRegionType outputLargestPossibleRegion;
71  this->CallCopyInputRegionToOutputRegion(outputLargestPossibleRegion,
72  inputPtr->GetLargestPossibleRegion());
73 
74  // for the new dimension, assuming the index has been set 0.
75  outputLargestPossibleRegion.SetSize(InputImageDimension,
76  this->GetNumberOfInputs());
77 
78  outputPtr->SetLargestPossibleRegion( outputLargestPossibleRegion );
79 
80  // Set the output spacing and origin
81  const ImageBase<InputImageDimension> *phyData;
82 
83  phyData
84  = dynamic_cast<const ImageBase<InputImageDimension>*>(this->GetInput());
85 
86  if (phyData)
87  {
88  // Copy what we can from the image from spacing and origin of the input
89  // This logic needs to be augmented with logic that select which
90  // dimensions to copy
91  unsigned int ii;
92  const typename InputImageType::SpacingType&
93  inputSpacing = inputPtr->GetSpacing();
94  const typename InputImageType::PointType&
95  inputOrigin = inputPtr->GetOrigin();
96 
97  typename OutputImageType::SpacingType outputSpacing;
98  typename OutputImageType::PointType outputOrigin;
99 
100  // copy the input to the output and fill the rest of the
101  // output with zeros.
102  for (ii=0; ii < InputImageDimension; ++ii)
103  {
104  outputSpacing[ii] = inputSpacing[ii];
105  outputOrigin[ii] = inputOrigin[ii];
106  }
107  for (; ii < OutputImageDimension; ++ii)
108  {
109  outputSpacing[ii] = 1.0;
110  outputOrigin[ii] = 0.0;
111  }
112 
113  // for the new dimension
114  outputSpacing[InputImageDimension] = this->GetSpacing();
115  outputOrigin[InputImageDimension] = this->GetOrigin();
116 
117  // set the spacing and origin
118  outputPtr->SetSpacing( outputSpacing );
119  outputPtr->SetOrigin( outputOrigin );
120  //
121  // Copy the direction cosines from the input to the output.
122  // On join, the output dim is always >= input dim
123  typedef typename InputImageType::DirectionType InputDirectionType;
124  typedef typename OutputImageType::DirectionType OutputDirectionType;
125  InputDirectionType inputDir = inputPtr->GetDirection();
126  unsigned int inputdim = InputImageType::GetImageDimension();
127  unsigned int outputdim = OutputImageType::GetImageDimension();
128  OutputDirectionType outputDir = outputPtr->GetDirection();
129  for(unsigned int i = 0; i < outputdim; i++)
130  {
131  for(unsigned int j = 0; j < outputdim; j++)
132  {
133  if(j < inputdim && i < inputdim)
134  {
135  outputDir[i][j] = inputDir[i][j];
136  }
137  else
138  {
139  outputDir[i][j] = i == j ? 1.0 : 0.0;
140  }
141  }
142  }
143  outputPtr->SetDirection(outputDir);
144  }
145  else
146  {
147  // pointer could not be cast back down
148  itkExceptionMacro(<< "itk::JoinSeriesImageFilter::GenerateOutputInformation "
149  << "cannot cast input to "
150  << typeid(ImageBase<InputImageDimension>*).name() );
151  }
152 }
153 
154 
155 template <class TInputImage, class TOutputImage>
156 void
159 {
160  Superclass::GenerateInputRequestedRegion();
161 
162  if (!this->GetOutput())
163  {
164  return;
165  }
166  OutputImageRegionType outputRegion = this->GetOutput()->GetRequestedRegion();
167  IndexValueType begin = outputRegion.GetIndex(InputImageDimension);
168  IndexValueType end = begin + outputRegion.GetSize(InputImageDimension);
169  for (IndexValueType idx = 0; idx < this->GetNumberOfInputs(); ++idx)
170  {
171  InputImagePointer inputPtr =
172  const_cast<InputImageType *>(this->GetInput(idx));
173  if ( !inputPtr )
174  {
175  // Because DataObject::PropagateRequestedRegion() allows only
176  // InvalidRequestedRegionError, it's impossible to write simply:
177  // itkExceptionMacro(<< "Missing input " << idx);
178  InvalidRequestedRegionError e(__FILE__, __LINE__);
179  e.SetLocation(ITK_LOCATION);
180  e.SetDescription("Missing input.");
181  e.SetDataObject(this->GetOutput());
182  throw e;
183  }
184 
185  InputImageRegionType inputRegion;
186  if ( begin <= idx && idx < end )
187  {
188  this->CallCopyOutputRegionToInputRegion(inputRegion, outputRegion);
189  }
190  else
191  {
192  // to tell the pipeline that updating this input is unncesseary
193  inputRegion = inputPtr->GetBufferedRegion();
194  }
195  inputPtr->SetRequestedRegion(inputRegion);
196  }
197 }
198 
199 
200 template <class TInputImage, class TOutputImage>
201 void
203 ::ThreadedGenerateData(const OutputImageRegionType& outputRegionForThread,
204  int threadId)
205 {
206  itkDebugMacro(<<"Actually executing");
207 
208  ProgressReporter progress(this, threadId, outputRegionForThread.GetNumberOfPixels());
209 
210  OutputImageRegionType outputRegion = outputRegionForThread;
211  outputRegion.SetSize(InputImageDimension, 1);
212 
213  InputImageRegionType inputRegion;
214  this->CallCopyOutputRegionToInputRegion(inputRegion, outputRegionForThread);
215 
216  IndexValueType begin = outputRegionForThread.GetIndex(InputImageDimension);
217  IndexValueType end =
218  begin + outputRegionForThread.GetSize(InputImageDimension);
219  for (IndexValueType idx = begin; idx < end; ++idx)
220  {
221  outputRegion.SetIndex(InputImageDimension, idx);
223  this->GetOutput(), outputRegion);
225  this->GetInput(idx), inputRegion);
226  outIt.GoToBegin();
227  inIt.GoToBegin();
228  while( !outIt.IsAtEnd() )
229  {
230  outIt.Set( inIt.Get() );
231  ++outIt;
232  ++inIt;
233  progress.CompletedPixel();
234  }
235  }
236 }
237 
238 
239 } // end namespace itk
240 
241 #endif

Generated at Sat Feb 2 2013 23:47:09 for Orfeo Toolbox with doxygen 1.8.1.1