OTB  6.7.0
Orfeo Toolbox
otbGroundSpacingImageFunction.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2019 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"
28 
29 namespace otb
30 {
31 
35 template <class TInputImage, class TCoordRep>
38 {
39  m_R = 6371000;
40  m_Deg2radCoef = CONST_PI / 180;
41 }
42 
46 template <class TInputImage, class TCoordRep>
47 void
49 ::PrintSelf(std::ostream& os, itk::Indent indent) const
50 {
51  this->Superclass::PrintSelf(os, indent);
52 }
53 
57 template <class TInputImage, class TCoordRep>
59 ::FloatType
61 ::EvaluateAtIndex(const IndexType& index) const
62 {
63  FloatType var;
64 
65  if (!this->GetInputImage())
66  {
68  return var;
69  }
70 
71  PointType point = this->GetPixelLocation(index);
72 
73  IndexType indexSrcX, indexSrcY;
74  indexSrcX[0] =
75  static_cast<IndexValueType>(std::fabs(static_cast<ValueType>(this->GetInputImage()->GetLargestPossibleRegion().
76  GetSize()[0] -
77  index[0]))); // x position
78  indexSrcX[1] = index[1]; // y position
79 
80  indexSrcY[0] = index[0]; // x position
81  indexSrcY[1] =
82  static_cast<IndexValueType>(std::fabs(static_cast<ValueType>(this->GetInputImage()->GetLargestPossibleRegion().
83  GetSize()[1] -
84  index[1])));
85 
86  PointType pointSrcX = this->GetPixelLocation(indexSrcX);
87  PointType pointSrcY = this->GetPixelLocation(indexSrcY);
88 
89  ValueType dLatX = (std::fabs(pointSrcX[1] - point[1])) * m_Deg2radCoef;
90  ValueType dLonX = (std::fabs(pointSrcX[0] - point[0])) * m_Deg2radCoef;
91 
93  const ValueType Two = One + One;
94 
95  ValueType aX = std::sin(dLatX / Two) * std::sin(dLatX / Two) + std::cos(point[1] * m_Deg2radCoef) * std::cos(
96  pointSrcX[1] * m_Deg2radCoef) * std::sin(dLonX / Two) * std::sin(dLonX / Two);
97  ValueType cX = Two * std::atan2(std::sqrt(aX), std::sqrt(One - aX));
98  ValueType dX = m_R * cX;
99 
100  ValueType dLatY = (std::fabs(pointSrcY[1] - point[1])) * m_Deg2radCoef;
101  ValueType dLonY = (std::fabs(pointSrcY[0] - point[0])) * m_Deg2radCoef;
102 
103  ValueType aY = std::sin(dLatY / Two) * std::sin(dLatY / Two) + std::cos(point[1] * m_Deg2radCoef) * std::cos(
104  pointSrcY[1] * m_Deg2radCoef) * std::sin(dLonY / Two) * std::sin(dLonY / Two);
105  ValueType cY = Two * std::atan2(std::sqrt(aY), std::sqrt(One - aY));
106  ValueType dY = m_R * cY;
107 
108  //FloatType var;
109  var[0] = dX / (std::fabs(static_cast<ValueType>(indexSrcX[0] - index[0])));
110  var[1] = dY / (std::fabs(static_cast<ValueType>(indexSrcY[1] - index[1])));
111 
112  return var;
113 }
114 
115 template <class TInputImage, class TCoordRep>
119 ::GetPixelLocation(const IndexType& index) const
120 {
121  PointType inputPoint;
122  inputPoint[0] = index[0];
123  inputPoint[1] = index[1];
124 
125  if (!this->GetInputImage())
126  {
127  itkExceptionMacro(<< "No input image!");
128  }
129 
130  TransformType::Pointer transform = TransformType::New();
131  const itk::MetaDataDictionary& inputDict = this->GetInputImage()->GetMetaDataDictionary();
132  transform->SetInputDictionary(inputDict);
133  transform->SetInputOrigin(this->GetInputImage()->GetOrigin());
134  transform->SetInputSpacing(this->GetInputImage()->GetSignedSpacing());
135 
136  transform->InstantiateTransform();
137  return transform->TransformPoint(inputPoint);
138 }
139 
140 } // end namespace otb
141 
142 #endif
Calculate the approximate ground spacing in X and Y directions.
void Fill(const ValueType &)
constexpr double CONST_PI
Definition: otbMath.h:48
void PrintSelf(std::ostream &os, itk::Indent indent) const override
ImageType::SpacingType GetSignedSpacing(const ImageType *input)
Definition: otbImage.h:41
PointType GetPixelLocation(const IndexType &index) const
VectorImageType::PointType PointType
Definition: mvdTypes.h:189
FloatType EvaluateAtIndex(const IndexType &index) const override