21 #ifndef otbImageToGenericRSOutputParameters_hxx
22 #define otbImageToGenericRSOutputParameters_hxx
27 #include "itkContinuousIndex.h"
32 template <
class TImage>
35 m_Transform = GenericRSTransformType::New();
36 m_ForceSpacing =
false;
38 m_EstimateIsotropicSpacing =
false;
44 template <
class TImage>
49 itkExceptionMacro(<<
"The input is null , please set a non null input image");
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");
56 this->UpdateTransform();
59 this->EstimateOutputImageExtent();
63 this->EstimateOutputSpacing();
66 this->EstimateOutputSize();
69 this->EstimateOutputOrigin();
73 template <
class TImage>
76 m_Transform->SetOutputImageMetadata(&(this->GetInput()->GetImageMetadata()));
77 m_Transform->SetOutputProjectionRef(this->GetInput()->GetProjectionRef());
78 m_Transform->InstantiateTransform();
86 template <
class TImage>
91 m_Transform->GetInverse(invTransform);
95 std::vector<itk::ContinuousIndex<double, 2>> vindex;
96 std::vector<PointType> voutput;
98 itk::ContinuousIndex<double, 2> index1(m_Input->GetLargestPossibleRegion().GetIndex());
101 itk::ContinuousIndex<double, 2> index2(index1);
102 itk::ContinuousIndex<double, 2> index3(index1);
103 itk::ContinuousIndex<double, 2> index4(index1);
106 SizeType size = m_Input->GetLargestPossibleRegion().GetSize();
109 index2[0] += size[0];
110 index3[0] += size[0];
111 index3[1] += size[1];
112 index4[1] += size[1];
114 vindex.push_back(index1);
115 vindex.push_back(index2);
116 vindex.push_back(index3);
117 vindex.push_back(index4);
119 for (
unsigned int i = 0; i < vindex.size(); ++i)
122 m_Input->TransformContinuousIndexToPhysicalPoint(vindex[i], physicalPoint);
123 voutput.push_back(invTransform->TransformPoint(physicalPoint));
127 double minX = voutput[0][0];
128 double maxX = voutput[0][0];
129 double minY = voutput[0][1];
130 double maxY = voutput[0][1];
132 for (
unsigned int i = 0; i < voutput.size(); ++i)
135 if (minX > voutput[i][0])
136 minX = voutput[i][0];
137 if (minY > voutput[i][1])
138 minY = voutput[i][1];
141 if (maxX < voutput[i][0])
142 maxX = voutput[i][0];
144 if (maxY < voutput[i][1])
145 maxY = voutput[i][1];
149 m_OutputExtent.maxX = maxX;
150 m_OutputExtent.minX = minX;
151 m_OutputExtent.maxY = maxY;
152 m_OutputExtent.minY = minY;
160 template <
class TImage>
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);
176 template <
class TImage>
180 double sizeCartoX = std::abs(m_OutputExtent.maxX - m_OutputExtent.minX);
181 double sizeCartoY = std::abs(m_OutputExtent.minY - m_OutputExtent.maxY);
185 o[0] = m_OutputExtent.minX;
186 o[1] = m_OutputExtent.maxY;
195 PointType io = m_Transform->TransformPoint(o);
196 PointType ioX = m_Transform->TransformPoint(oX);
197 PointType ioY = m_Transform->TransformPoint(oY);
201 m_Input->TransformPhysicalPointToIndex(io, ioIndex);
202 m_Input->TransformPhysicalPointToIndex(ioX, ioXIndex);
203 m_Input->TransformPhysicalPointToIndex(ioY, ioYIndex);
206 double OxLength, OyLength;
208 OxLength = std::sqrt(std::pow((
double)ioIndex[0] - (
double)ioXIndex[0], 2) + std::pow((
double)ioIndex[1] - (
double)ioXIndex[1], 2));
210 OyLength = std::sqrt(std::pow((
double)ioIndex[0] - (
double)ioYIndex[0], 2) + std::pow((
double)ioIndex[1] - (
double)ioYIndex[1], 2));
216 if (m_EstimateIsotropicSpacing)
218 double isotropicSpacing = std::min(sizeCartoX / OxLength, sizeCartoY / OyLength);
219 outputSpacing[0] = isotropicSpacing;
220 outputSpacing[1] = -isotropicSpacing;
224 outputSpacing[0] = sizeCartoX / OxLength;
225 outputSpacing[1] = -sizeCartoY / OyLength;
228 this->SetOutputSpacing(outputSpacing);
235 template <
class TImage>
239 double sizeCartoX = std::abs(m_OutputExtent.maxX - m_OutputExtent.minX);
240 double sizeCartoY = std::abs(m_OutputExtent.minY - m_OutputExtent.maxY);
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])));
252 this->SetOutputSize(outputSize);
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);
261 this->UpdateTransform();