File: itkExtractOrthogonalSwath2DImageFilter.txx

package info (click to toggle)
insighttoolkit 3.6.0-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 94,956 kB
  • ctags: 74,981
  • sloc: cpp: 355,621; ansic: 195,070; fortran: 28,713; python: 3,802; tcl: 1,996; sh: 1,175; java: 583; makefile: 415; csh: 184; perl: 175
file content (245 lines) | stat: -rw-r--r-- 7,004 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*=========================================================================

  Program:   Insight Segmentation & Registration Toolkit
  Module:    $RCSfile: itkExtractOrthogonalSwath2DImageFilter.txx,v $
  Language:  C++
  Date:      $Date: 2005-07-26 15:06:24 $
  Version:   $Revision: 1.13 $

  Copyright (c) Insight Software Consortium. All rights reserved.
  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even 
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/
#ifndef _itkExtractOrthogonalSwath2DImageFilter_txx
#define _itkExtractOrthogonalSwath2DImageFilter_txx
#include "itkExtractOrthogonalSwath2DImageFilter.h"
#include "itkImageRegionIteratorWithIndex.h"
#include "itkLinearInterpolateImageFunction.h"
#include "itkProgressReporter.h"
#include "itkNumericTraits.h"

namespace itk
{

template <class TImage>
void
ExtractOrthogonalSwath2DImageFilter<TImage>
::SetSpacing(const double* spacing)
{
  unsigned int i; 
  for (i=0; i<ImageDimension; i++)
    {
    if ( spacing[i] != m_Spacing[i] )
      {
      break;
      }
    } 
  if ( i < ImageDimension ) 
    { 
    for (i=0; i<ImageDimension; i++)
      {
      m_Spacing[i] = spacing[i];
      }
    }
}

template <class TImage>
void
ExtractOrthogonalSwath2DImageFilter<TImage>
::SetSpacing(const float* spacing)
{
  unsigned int i; 
  for (i=0; i<ImageDimension; i++)
    {
    if ( (double)spacing[i] != m_Spacing[i] )
      {
      break;
      }
    } 
  if ( i < ImageDimension ) 
    { 
    for (i=0; i<ImageDimension; i++)
      {
      m_Spacing[i] = spacing[i];
      }
    }
}

template <class TImage>
const double * 
ExtractOrthogonalSwath2DImageFilter<TImage>
::GetSpacing() const
{
  return m_Spacing;
}

//----------------------------------------------------------------------------
template <class TImage>
void
ExtractOrthogonalSwath2DImageFilter<TImage>
::SetOrigin(const double* origin)
{
  unsigned int i; 
  for (i=0; i<ImageDimension; i++)
    {
    if ( origin[i] != m_Origin[i] )
      {
      break;
      }
    } 
  if ( i < ImageDimension ) 
    { 
    for (i=0; i<ImageDimension; i++)
      {
      m_Origin[i] = origin[i];
      }
    }
}

template <class TImage>
void
ExtractOrthogonalSwath2DImageFilter<TImage>
::SetOrigin(const float* origin)
{
  unsigned int i; 
  for (i=0; i<ImageDimension; i++)
    {
    if ( (double)origin[i] != m_Origin[i] )
      {
      break;
      }
    } 
  if ( i < ImageDimension ) 
    { 
    for (i=0; i<ImageDimension; i++)
      {
      m_Origin[i] = origin[i];
      }
    }
}

template <class TImage>
const double * 
ExtractOrthogonalSwath2DImageFilter<TImage>
::GetOrigin() const
{
  return m_Origin;
}

//----------------------------------------------------------------------------

template <class TImage>
void
ExtractOrthogonalSwath2DImageFilter<TImage>
::GenerateOutputInformation( void )
{
  ImagePointer      outputPtr     = this->GetOutput(0);

  ImageRegionType   outputRegion;
  ImageIndexType    index;
  index.Fill(0);
  outputRegion.SetSize( this->m_Size );
  outputRegion.SetIndex( index );
  outputPtr->SetLargestPossibleRegion( outputRegion );
  outputPtr->SetSpacing(this->m_Spacing);
  outputPtr->SetOrigin(this->m_Origin);
}

/**
 * GenerateData Performs the reflection
 */
template <class TImage>
void
ExtractOrthogonalSwath2DImageFilter<TImage>
::GenerateData( void )
{
  ImageConstPointer inputImagePtr = this->GetImageInput();
  PathConstPointer  inputPathPtr  = this->GetPathInput();
  ImagePointer      outputPtr     = this->GetOutput(0);

  // Generate the output image
  ImageRegionType   outputRegion = outputPtr->GetRequestedRegion();
  outputPtr->SetBufferedRegion( outputRegion );
  outputPtr->Allocate();

  // support progress methods/callbacks
  ProgressReporter progress(this, 0,  outputRegion.GetNumberOfPixels() );
  
  typedef ImageRegionIteratorWithIndex<ImageType> OutputIterator;
  typedef LinearInterpolateImageFunction<ImageType,double> InterpolatorType;
  ImageIndexType                        index;                     
  double                                orthogonalOffset;          
  PathInputType                         pathInput;                 
  PathContinuousIndexType               continousIndex;            
  PathVectorType                        pathDerivative;              
  typename InterpolatorType::Pointer    interpolator = InterpolatorType::New();
  interpolator->SetInputImage( inputImagePtr );
  
  // Iterate through the output image
  OutputIterator outputIt( outputPtr, outputPtr->GetRequestedRegion() );
  for( outputIt.GoToBegin(); !outputIt.IsAtEnd(); ++outputIt ) 
    {
    index = outputIt.GetIndex();

    // what position along the path coresponds to this column of the swath?
    pathInput = inputPathPtr->StartOfInput()
              + double(inputPathPtr->EndOfInput()-inputPathPtr->StartOfInput())
              * double(index[0]) / double(m_Size[0]);

    // What is the orghogonal offset from the path in the input image for this
    // particular index in the output swath image?
    // Vertically centered swath pixels lie on the path in the input image.
    orthogonalOffset = index[1] - int(m_Size[1]/2); // use signed arithmatic

    // Make continousIndex point to the source pixel in the input image
    continousIndex = inputPathPtr->Evaluate(pathInput);
    pathDerivative = inputPathPtr->EvaluateDerivative(pathInput);
    pathDerivative.Normalize();
    continousIndex[0] -= orthogonalOffset * pathDerivative[1];
    continousIndex[1] += orthogonalOffset * pathDerivative[0];
    
    // set the swath pixel to the interpolated input pixel
    if ( ! interpolator->IsInsideBuffer( continousIndex ) )
      {
      //itkExceptionMacro(<<"Requested input index ["<<continousIndex
      //                  <<"] is not in the input image" );
      outputIt.Set( m_DefaultPixelValue );
      progress.CompletedPixel();
      continue;
      }
    
    // prevent small interpolation error from rounding-down integer types
    if( NumericTraits<ImagePixelType>::is_integer )
      {
      outputIt.Set( static_cast<ImagePixelType>( 0.5 +
          interpolator->EvaluateAtContinuousIndex( continousIndex ) ) );
      }
    else
      {
      outputIt.Set( static_cast<ImagePixelType>(
          interpolator->EvaluateAtContinuousIndex( continousIndex ) ) );
      }
    
    progress.CompletedPixel();
    }
}

template <class TImage>
void
ExtractOrthogonalSwath2DImageFilter<TImage>::
PrintSelf(std::ostream& os, Indent indent) const
{
  Superclass::PrintSelf(os,indent);
  os << indent << "Size:  " << m_Size << std::endl;
  os << indent << "DefaultPixelValue:  "
     << static_cast<typename NumericTraits<ImagePixelType>::PrintType>(m_DefaultPixelValue)
     << std::endl;
}

} // end namespace itk

#endif