OTB  9.0.0
Orfeo Toolbox
otbImageToGenericRSOutputParameters.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2022 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 otbImageToGenericRSOutputParameters_hxx
22 #define otbImageToGenericRSOutputParameters_hxx
23 
24 
26 #include "itkMacro.h"
27 #include "itkContinuousIndex.h"
28 
29 namespace otb
30 {
31 
32 template <class TImage>
34 {
35  m_Transform = GenericRSTransformType::New();
36  m_ForceSpacing = false;
37  m_ForceSize = false;
38  m_EstimateIsotropicSpacing = false;
39 }
40 
44 template <class TImage>
46 {
47  // Do some checks : exceptions if Null or empty projectionRef and empty keywordlist
48  if (m_Input.IsNull())
49  itkExceptionMacro(<< "The input is null , please set a non null input image");
50 
51  if (!m_Input->GetImageMetadata().HasSensorGeometry() && !m_Input->GetImageMetadata().HasProjectedGeometry())
52  itkExceptionMacro(<< "No information in the metadata, please set an image with non empty metadata");
53 
54  // First Call to UpdateTransform : Initialize with the input image
55  // information
56  this->UpdateTransform();
57 
58  // Estimate the Output image Extent
59  this->EstimateOutputImageExtent();
60 
61  // Estimate the Output Spacing
62  if (!m_ForceSpacing)
63  this->EstimateOutputSpacing();
64 
65  // Finally Estimate the Output Size
66  this->EstimateOutputSize();
67 
68  // Estimate the Output Origin
69  this->EstimateOutputOrigin();
70 }
71 
72 
73 template <class TImage>
75 {
76  m_Transform->SetOutputImageMetadata(&(this->GetInput()->GetImageMetadata()));
77  m_Transform->SetOutputProjectionRef(this->GetInput()->GetProjectionRef());
78  m_Transform->InstantiateTransform();
79 }
80 
81 
86 template <class TImage>
88 {
89  // Get the inverse transform again : used later
90  GenericRSTransformPointerType invTransform = GenericRSTransformType::New();
91  m_Transform->GetInverse(invTransform);
93 
94  // Compute the 4 corners in the cartographic coordinate system
95  std::vector<itk::ContinuousIndex<double, 2>> vindex;
96  std::vector<PointType> voutput;
97 
98  itk::ContinuousIndex<double, 2> index1(m_Input->GetLargestPossibleRegion().GetIndex());
99  index1[0] += -0.5;
100  index1[1] += -0.5;
101  itk::ContinuousIndex<double, 2> index2(index1);
102  itk::ContinuousIndex<double, 2> index3(index1);
103  itk::ContinuousIndex<double, 2> index4(index1);
104 
105  // Image size
106  SizeType size = m_Input->GetLargestPossibleRegion().GetSize();
107 
108  // project the 4 corners
109  index2[0] += size[0];
110  index3[0] += size[0];
111  index3[1] += size[1];
112  index4[1] += size[1];
113 
114  vindex.push_back(index1);
115  vindex.push_back(index2);
116  vindex.push_back(index3);
117  vindex.push_back(index4);
118 
119  for (unsigned int i = 0; i < vindex.size(); ++i)
120  {
121  PointType physicalPoint;
122  m_Input->TransformContinuousIndexToPhysicalPoint(vindex[i], physicalPoint);
123  voutput.push_back(invTransform->TransformPoint(physicalPoint));
124  }
125 
126  // Compute the boundaries
127  double minX = voutput[0][0];
128  double maxX = voutput[0][0];
129  double minY = voutput[0][1];
130  double maxY = voutput[0][1];
131 
132  for (unsigned int i = 0; i < voutput.size(); ++i)
133  {
134  // Origins
135  if (minX > voutput[i][0])
136  minX = voutput[i][0];
137  if (minY > voutput[i][1])
138  minY = voutput[i][1];
139 
140  // Sizes
141  if (maxX < voutput[i][0])
142  maxX = voutput[i][0];
143 
144  if (maxY < voutput[i][1])
145  maxY = voutput[i][1];
146  }
147 
148  // Edit the output image extent type
149  m_OutputExtent.maxX = maxX;
150  m_OutputExtent.minX = minX;
151  m_OutputExtent.maxY = maxY;
152  m_OutputExtent.minY = minY;
153 }
154 
155 
160 template <class TImage>
162 {
163  // Set the output origin in carto
164  // projection
165  PointType origin;
166  origin[0] = m_OutputExtent.minX + 0.5 * this->GetOutputSpacing()[0];
167  origin[1] = m_OutputExtent.maxY + 0.5 * this->GetOutputSpacing()[1];
168  this->SetOutputOrigin(origin);
169 }
171 
176 template <class TImage>
178 {
179  // Compute the output size
180  double sizeCartoX = std::abs(m_OutputExtent.maxX - m_OutputExtent.minX);
181  double sizeCartoY = std::abs(m_OutputExtent.minY - m_OutputExtent.maxY);
183 
184  PointType o, oX, oY;
185  o[0] = m_OutputExtent.minX;
186  o[1] = m_OutputExtent.maxY;
187 
188  oX = o;
189  oY = o;
190 
191  oX[0] += sizeCartoX;
192  oY[1] -= sizeCartoY;
193 
194  // Transform back into the input image
195  PointType io = m_Transform->TransformPoint(o);
196  PointType ioX = m_Transform->TransformPoint(oX);
197  PointType ioY = m_Transform->TransformPoint(oY);
198 
199  // Transform to indices
200  IndexType ioIndex, ioXIndex, ioYIndex;
201  m_Input->TransformPhysicalPointToIndex(io, ioIndex);
202  m_Input->TransformPhysicalPointToIndex(ioX, ioXIndex);
203  m_Input->TransformPhysicalPointToIndex(ioY, ioYIndex);
204 
205  // Evaluate Ox and Oy length in number of pixels
206  double OxLength, OyLength;
207 
208  OxLength = std::sqrt(std::pow((double)ioIndex[0] - (double)ioXIndex[0], 2) + std::pow((double)ioIndex[1] - (double)ioXIndex[1], 2));
209 
210  OyLength = std::sqrt(std::pow((double)ioIndex[0] - (double)ioYIndex[0], 2) + std::pow((double)ioIndex[1] - (double)ioYIndex[1], 2));
211 
212  // Evaluate spacing
213  SpacingType outputSpacing;
214 
215 
216  if (m_EstimateIsotropicSpacing)
217  {
218  double isotropicSpacing = std::min(sizeCartoX / OxLength, sizeCartoY / OyLength);
219  outputSpacing[0] = isotropicSpacing;
220  outputSpacing[1] = -isotropicSpacing;
221  }
222  else
223  {
224  outputSpacing[0] = sizeCartoX / OxLength;
225  outputSpacing[1] = -sizeCartoY / OyLength;
226  }
227 
228  this->SetOutputSpacing(outputSpacing);
229 }
230 
235 template <class TImage>
237 {
238  // Compute the output size
239  double sizeCartoX = std::abs(m_OutputExtent.maxX - m_OutputExtent.minX);
240  double sizeCartoY = std::abs(m_OutputExtent.minY - m_OutputExtent.maxY);
242 
243  // Evaluate output size
244  SizeType outputSize;
245  outputSize[0] = static_cast<unsigned int>(std::floor(std::abs(sizeCartoX / this->GetOutputSpacing()[0])));
246  outputSize[1] = static_cast<unsigned int>(std::floor(std::abs(sizeCartoY / this->GetOutputSpacing()[1])));
247 
248  // if ForceSizeTo used don't update the output size with the value
249  // computed : the value is computed to update the spacing knowing
250  // the forced size and the computed one.
251  if (!m_ForceSize)
252  this->SetOutputSize(outputSize);
253  else
254  {
255  // Compute the spacing knowing the size
256  SpacingType outputSpacing;
257  outputSpacing[0] = this->GetOutputSpacing()[0] * outputSize[0] / this->GetOutputSize()[0];
258  outputSpacing[1] = this->GetOutputSpacing()[1] * outputSize[1] / this->GetOutputSize()[1];
259  this->SetOutputSpacing(outputSpacing);
260  }
261  this->UpdateTransform();
262 }
263 }
264 
265 #endif
otb::ImageToGenericRSOutputParameters::UpdateTransform
void UpdateTransform()
Definition: otbImageToGenericRSOutputParameters.hxx:74
otb::ImageToGenericRSOutputParameters::SizeType
ImageType::SizeType SizeType
Definition: otbImageToGenericRSOutputParameters.h:74
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::ImageToGenericRSOutputParameters::GenericRSTransformPointerType
GenericRSTransformType::Pointer GenericRSTransformPointerType
Definition: otbImageToGenericRSOutputParameters.h:81
otbImageToGenericRSOutputParameters.h
otb::ImageToGenericRSOutputParameters::EstimateOutputImageExtent
void EstimateOutputImageExtent()
Definition: otbImageToGenericRSOutputParameters.hxx:87
otb::ImageToGenericRSOutputParameters::ImageToGenericRSOutputParameters
ImageToGenericRSOutputParameters()
Definition: otbImageToGenericRSOutputParameters.hxx:33
otb::ImageToGenericRSOutputParameters::SpacingType
ImageType::SpacingType SpacingType
Definition: otbImageToGenericRSOutputParameters.h:76
otb::ImageToGenericRSOutputParameters::EstimateOutputSize
void EstimateOutputSize()
Definition: otbImageToGenericRSOutputParameters.hxx:236
otb::ImageToGenericRSOutputParameters::EstimateOutputSpacing
void EstimateOutputSpacing()
Definition: otbImageToGenericRSOutputParameters.hxx:177
otb::ImageToGenericRSOutputParameters::Compute
void Compute()
Definition: otbImageToGenericRSOutputParameters.hxx:45
otb::ImageToGenericRSOutputParameters::EstimateOutputOrigin
void EstimateOutputOrigin()
Definition: otbImageToGenericRSOutputParameters.hxx:161
otb::ImageToGenericRSOutputParameters::IndexType
ImageType::IndexType IndexType
Definition: otbImageToGenericRSOutputParameters.h:75
otb::ImageToGenericRSOutputParameters::PointType
ImageType::PointType PointType
Definition: otbImageToGenericRSOutputParameters.h:77