OTB  9.0.0
Orfeo Toolbox
otbGroundSpacingImageFunction.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 
22 #ifndef otbGroundSpacingImageFunction_hxx
23 #define otbGroundSpacingImageFunction_hxx
24 
25 #include "otbMath.h"
26 #include "itkConstNeighborhoodIterator.h"
28 
29 namespace otb
30 {
31 
35 template <class TInputImage, class TCoordRep>
37 {
38  m_R = 6371000;
39  m_Deg2radCoef = CONST_PI / 180;
40 }
41 
45 template <class TInputImage, class TCoordRep>
46 void GroundSpacingImageFunction<TInputImage, TCoordRep>::PrintSelf(std::ostream& os, itk::Indent indent) const
47 {
48  this->Superclass::PrintSelf(os, indent);
49 }
50 
54 template <class TInputImage, class TCoordRep>
57 {
58  FloatType var;
59 
60  if (!this->GetInputImage())
61  {
62  var.Fill(itk::NumericTraits<ValueType>::min());
63  return var;
64  }
65 
66  PointType point = this->GetPixelLocation(index);
67 
68  IndexType indexSrcX, indexSrcY;
69  indexSrcX[0] =
70  static_cast<IndexValueType>(std::fabs(static_cast<ValueType>(this->GetInputImage()->GetLargestPossibleRegion().GetSize()[0] - index[0]))); // x position
71  indexSrcX[1] = index[1]; // y position
72 
73  indexSrcY[0] = index[0]; // x position
74  indexSrcY[1] = static_cast<IndexValueType>(std::fabs(static_cast<ValueType>(this->GetInputImage()->GetLargestPossibleRegion().GetSize()[1] - index[1])));
75 
76  PointType pointSrcX = this->GetPixelLocation(indexSrcX);
77  PointType pointSrcY = this->GetPixelLocation(indexSrcY);
78 
79  ValueType dLatX = (std::fabs(pointSrcX[1] - point[1])) * m_Deg2radCoef;
80  ValueType dLonX = (std::fabs(pointSrcX[0] - point[0])) * m_Deg2radCoef;
81 
82  const ValueType One = itk::NumericTraits<ValueType>::One;
83  const ValueType Two = One + One;
84 
85  ValueType aX = std::sin(dLatX / Two) * std::sin(dLatX / Two) +
86  std::cos(point[1] * m_Deg2radCoef) * std::cos(pointSrcX[1] * m_Deg2radCoef) * std::sin(dLonX / Two) * std::sin(dLonX / Two);
87  ValueType cX = Two * std::atan2(std::sqrt(aX), std::sqrt(One - aX));
88  ValueType dX = m_R * cX;
89 
90  ValueType dLatY = (std::fabs(pointSrcY[1] - point[1])) * m_Deg2radCoef;
91  ValueType dLonY = (std::fabs(pointSrcY[0] - point[0])) * m_Deg2radCoef;
92 
93  ValueType aY = std::sin(dLatY / Two) * std::sin(dLatY / Two) +
94  std::cos(point[1] * m_Deg2radCoef) * std::cos(pointSrcY[1] * m_Deg2radCoef) * std::sin(dLonY / Two) * std::sin(dLonY / Two);
95  ValueType cY = Two * std::atan2(std::sqrt(aY), std::sqrt(One - aY));
96  ValueType dY = m_R * cY;
97 
98  // FloatType var;
99  var[0] = dX / (std::fabs(static_cast<ValueType>(indexSrcX[0] - index[0])));
100  var[1] = dY / (std::fabs(static_cast<ValueType>(indexSrcY[1] - index[1])));
101 
102  return var;
103 }
104 
105 template <class TInputImage, class TCoordRep>
108 {
109  PointType inputPoint;
110  inputPoint[0] = index[0];
111  inputPoint[1] = index[1];
112 
113  if (!this->GetInputImage())
114  {
115  itkExceptionMacro(<< "No input image!");
116  }
117 
118  TransformType::Pointer transform = TransformType::New();
119  transform->SetInputImageMetadata(&(this->GetInputImage()->GetImageMetadata()));
120  transform->SetInputOrigin(this->GetInputImage()->GetOrigin());
121  transform->SetInputSpacing(this->GetInputImage()->GetSignedSpacing());
122 
123  transform->InstantiateTransform();
124 
125  return transform->TransformPoint(inputPoint);
126 }
127 
128 } // end namespace otb
129 
130 #endif
otb::GroundSpacingImageFunction::PrintSelf
void PrintSelf(std::ostream &os, itk::Indent indent) const override
Definition: otbGroundSpacingImageFunction.hxx:46
otb::GenericRSTransform::Pointer
itk::SmartPointer< Self > Pointer
Definition: otbGenericRSTransform.h:66
otb::CONST_PI
constexpr double CONST_PI
Definition: otbMath.h:49
otb::GroundSpacingImageFunction::FloatType
itk::Vector< ValueType, 2 > FloatType
Definition: otbGroundSpacingImageFunction.h:55
otb::GroundSpacingImageFunction::GroundSpacingImageFunction
GroundSpacingImageFunction()
Definition: otbGroundSpacingImageFunction.hxx:36
otb::var
Definition: otbParserXPlugins.h:282
otb::GroundSpacingImageFunction::ValueType
float ValueType
Definition: otbGroundSpacingImageFunction.h:53
otb::GroundSpacingImageFunction::PointType
Superclass::PointType PointType
Definition: otbGroundSpacingImageFunction.h:73
otbMath.h
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otbGroundSpacingImageFunction.h
otb::GroundSpacingImageFunction::GetPixelLocation
PointType GetPixelLocation(const IndexType &index) const
Definition: otbGroundSpacingImageFunction.hxx:107
otb::GroundSpacingImageFunction::EvaluateAtIndex
FloatType EvaluateAtIndex(const IndexType &index) const override
Definition: otbGroundSpacingImageFunction.hxx:56
otb::GroundSpacingImageFunction::IndexValueType
IndexType::IndexValueType IndexValueType
Definition: otbGroundSpacingImageFunction.h:77
otb::GroundSpacingImageFunction::IndexType
Superclass::IndexType IndexType
Definition: otbGroundSpacingImageFunction.h:71
otb::internal::GetSignedSpacing
ImageType::SpacingType GetSignedSpacing(const ImageType *input)
Definition: otbImage.h:41