OTB  6.3.0
Orfeo Toolbox
otbStreamingResampleImageFilter.txx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2017 Centre National d'Etudes Spatiales (CNES)
3  *
4  * This file is part of Orfeo Toolbox
5  *
6  * https://www.orfeo-toolbox.org/
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 #ifndef otbStreamingResampleImageFilter_txx
22 #define otbStreamingResampleImageFilter_txx
23 
25 #include "itkProgressAccumulator.h"
26 #include "otbImage.h"
27 
28 namespace otb
29 {
30 
31 template <class TInputImage, class TOutputImage, class TInterpolatorPrecisionType>
34 {
35  // internal filters instantiation
36  m_DisplacementFilter = DisplacementFieldGeneratorType::New();
37  m_WarpFilter = WarpImageFilterType::New();
38  m_SignedOutputSpacing = m_DisplacementFilter->GetOutputSpacing();
39  // Initialize the displacement field spacing to zero : inconsistent
40  // value
41  this->SetDisplacementFieldSpacing(itk::NumericTraits<SpacingType>::ZeroValue());
42 
43  // Wire minipipeline
44  m_WarpFilter->SetDisplacementField(m_DisplacementFilter->GetOutput());
45 }
46 
47 template <class TInputImage, class TOutputImage, class TInterpolatorPrecisionType>
48 void
51 {
52  // Set up progress reporting
54  progress->SetMiniPipelineFilter(this);
55  progress->RegisterInternalFilter(m_WarpFilter, 1.f);
56 
57  m_WarpFilter->GraftOutput(this->GetOutput());
58  m_WarpFilter->UpdateOutputData(m_WarpFilter->GetOutput());
59  this->GraftOutput(m_WarpFilter->GetOutput());
60 }
61 
65 template <class TInputImage, class TOutputImage, class TInterpolatorPrecisionType>
66 void
69 {
70  // check the output spacing of the displacement field
71  if(this->GetDisplacementFieldSpacing()== itk::NumericTraits<SpacingType>::ZeroValue())
72  {
73  this->SetDisplacementFieldSpacing(2.*this->GetOutputSpacing());
74  }
75 
76  // Retrieve output largest region
77  SizeType largestSize = this->GetOutputSize();
78 
79  // Set up displacement field filter
80  SizeType displacementFieldLargestSize;
81 
82  for(unsigned int dim = 0; dim < InputImageType::ImageDimension; ++dim)
83  {
84  // vcl_ceil to avoid numerical problems due to division of
85  // spacings
86  // + 1 : We need to enlarge the displacement field size cause
87  // itk::WarpImageFilter::EvaluateDisplacementAtPhysicalPoint needs
88  // 4 neighbors and in the edges we can need 1 neighbor pixel
89  // outside the field
90  displacementFieldLargestSize[dim] = static_cast<unsigned int>(
91  vcl_ceil( largestSize[dim]*
92  vcl_abs(this->GetOutputSpacing()[dim] /
93  this->GetDisplacementFieldSpacing()[dim]))) + 1;
94  }
95  m_DisplacementFilter->SetOutputSize(displacementFieldLargestSize);
96  m_DisplacementFilter->SetOutputIndex(this->GetOutputStartIndex());
97 
98  m_WarpFilter->SetInput(this->GetInput());
99  m_WarpFilter->GraftOutput(this->GetOutput());
100  m_WarpFilter->UpdateOutputInformation();
101  this->GraftOutput(m_WarpFilter->GetOutput());
102 }
103 
104 template <class TInputImage, class TOutputImage, class TInterpolatorPrecisionType>
105 void
108 {
109  if (this->m_Updating) return;
110 
111  m_WarpFilter->GetOutput()->SetRequestedRegion(output);
112  m_WarpFilter->GetOutput()->PropagateRequestedRegion();
113 }
114 
119 template <class TInputImage, class TOutputImage, class TInterpolatorPrecisionType>
120 void
123 {
124  this->SetOutputOrigin ( image->GetOrigin() );
125  this->SetOutputSpacing ( internal::GetSignedSpacing( image ) );
126  this->SetOutputStartIndex ( image->GetLargestPossibleRegion().GetIndex() );
127  this->SetOutputSize ( image->GetLargestPossibleRegion().GetSize() );
128 }
130 
131 template <class TInputImage, class TOutputImage, class TInterpolatorPrecisionType>
132 void
135 {
136  m_SignedOutputSpacing = outputSpacing;
137  typename TInputImage::DirectionType direction = this->m_DisplacementFilter->GetOutputDirection();
138  for(unsigned int i = 0; i < TInputImage::ImageDimension; ++i)
139  {
140  if ( outputSpacing[i] < 0 )
141  {
142  if ( direction[i][i] > 0 )
143  {
144  for(unsigned int j = 0; j < TInputImage::ImageDimension; ++j)
145  {
146  direction[j][i] = - direction[j][i];
147  }
148  }
149  outputSpacing[i] = - outputSpacing[i];
150  }
151  }
152  this->m_DisplacementFilter->SetOutputSpacing( outputSpacing );
153  this->m_DisplacementFilter->SetOutputDirection( direction );
154  this->Modified();
155 }
156 
157 template <class TInputImage, class TOutputImage, class TInterpolatorPrecisionType>
158 void
160 ::PrintSelf(std::ostream& os, itk::Indent indent) const
161 {
162  Superclass::PrintSelf(os, indent);
163  os << indent << "OutputOrigin: " << this->GetOutputOrigin() << std::endl;
164  os << indent << "OutputSpacing: " << this->GetOutputSpacing() << std::endl;
165  os << indent << "OutputStartIndex: " << this->GetOutputStartIndex() << std::endl;
166  os << indent << "OutputSize: " << this->GetOutputSize() << std::endl;
167 }
168 
169 
170 }
171 #endif
void SetOutputParametersFromImage(const ImageBaseType *image)
const IndexType & GetIndex() const
virtual const RegionType & GetLargestPossibleRegion() const
static Pointer New()
ImageType::SpacingType GetSignedSpacing(const ImageType *input)
Definition: otbImage.h:40
void PropagateRequestedRegion(itk::DataObject *output) ITK_OVERRIDE
DisplacementFieldGeneratorType::SpacingType SpacingType
const SizeType & GetSize() const
void PrintSelf(std::ostream &os, itk::Indent indent) const ITK_OVERRIDE
DisplacementFieldGeneratorType::SizeType SizeType
virtual const PointType & GetOrigin() const