OTB  9.0.0
Orfeo Toolbox
otbHillShadingFunctor.h
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 otbHillShadingFunctor_h
22 #define otbHillShadingFunctor_h
23 
24 #include "itkNumericTraits.h"
25 #include "otbMath.h"
26 
27 namespace otb
28 {
29 namespace Functor
30 {
43 template <class TInput1, class TInput2 = TInput1, class TOutput = TInput1>
45 {
46 public:
48  {
49  }
51  {
52  }
53 
54  inline TOutput operator()(const TInput1& A, const TInput2& B) const
55  {
56  TOutput out;
57  out.SetRed(static_cast<typename TOutput::ValueType>(static_cast<double>(A.GetRed()) * static_cast<double>(B)));
58  out.SetGreen(static_cast<typename TOutput::ValueType>(static_cast<double>(A.GetGreen()) * static_cast<double>(B)));
59  out.SetBlue(static_cast<typename TOutput::ValueType>(static_cast<double>(A.GetBlue()) * static_cast<double>(B)));
60  return out;
61  }
62 };
63 
76 template <class TNeighIter, class TInputImage, class TOutput>
78 {
79 public:
81  typedef TNeighIter IteratorType;
82  typedef typename IteratorType::PixelType PixelType;
83 
85  {
86  m_SinElev = std::sin(m_ElevationLight);
87  m_CosElev = std::cos(m_ElevationLight);
88  m_SinAz = std::sin(m_AzimuthLight);
89  m_CosAz = std::cos(m_AzimuthLight);
90  }
92  {
93  }
94 
95  double GetXRes() const
96  {
97  return m_XRes;
98  }
99 
100  double GetYRes() const
101  {
102  return m_YRes;
103  }
104 
105  void SetXRes(double res)
106  {
107  m_XRes = std::abs(res);
108  }
109 
110  void SetYRes(double res)
111  {
112  m_YRes = std::abs(res);
113  }
114 
115  double GetScale() const
116  {
117  return m_Scale;
118  }
119 
120  void SetScale(double scale)
121  {
122  m_Scale = scale;
123  }
124 
125  double GetAzimuthLight() const
126  {
127  return m_AzimuthLight;
128  }
129 
130  void SetAzimuthLight(double az)
131  {
132  m_AzimuthLight = az;
133  m_SinAz = std::sin(m_AzimuthLight);
134  m_CosAz = std::cos(m_AzimuthLight);
135  }
136 
137  double GetElevationLight() const
138  {
139  return m_ElevationLight;
140  }
141 
142  void SetElevationLight(double el)
143  {
144  m_ElevationLight = el;
145  m_SinElev = std::sin(m_ElevationLight);
146  m_CosElev = std::cos(m_ElevationLight);
147  }
148 
149  inline TOutput operator()(const TNeighIter& it) const
150  {
151  const typename IteratorType::OffsetType LEFT = {{-1, 0}};
152  const typename IteratorType::OffsetType RIGHT = {{1, 0}};
153  const typename IteratorType::OffsetType UP = {{0, -1}};
154  const typename IteratorType::OffsetType DOWN = {{0, 1}};
155  const typename IteratorType::OffsetType LEFTUP = {{-1, -1}};
156  const typename IteratorType::OffsetType RIGHTDOWN = {{1, 1}};
157  const typename IteratorType::OffsetType RIGHTUP = {{1, -1}};
158  const typename IteratorType::OffsetType LEFTDOWN = {{-1, 1}};
159  // const typename IteratorType::OffsetType CENTER ={{0, 0}};
160 
161  float xSlope = ((makeValid(it.GetPixel(LEFTUP)) + 2 * makeValid(it.GetPixel(LEFT)) + makeValid(it.GetPixel(LEFTDOWN))) -
162  (makeValid(it.GetPixel(RIGHTUP)) + 2 * makeValid(it.GetPixel(RIGHT)) + makeValid(it.GetPixel(RIGHTDOWN)))) /
163  (m_XRes * m_Scale);
164  // - as the azimuth is given compared to y axis pointing up
165  float ySlope = -((makeValid(it.GetPixel(LEFTUP)) + 2 * makeValid(it.GetPixel(UP)) + makeValid(it.GetPixel(RIGHTUP))) -
166  (makeValid(it.GetPixel(LEFTDOWN)) + 2 * makeValid(it.GetPixel(DOWN)) + makeValid(it.GetPixel(RIGHTDOWN)))) /
167  (m_YRes * m_Scale);
168 
169  // permutation between x and y as the azimuth angle is given compared to the north-south axis
170  float lambertian = ((m_CosElev * m_CosAz * ySlope) + (m_CosElev * m_SinAz * xSlope) + m_SinElev) / std::sqrt(xSlope * xSlope + ySlope * ySlope + 1);
171 
172  return (lambertian + 1) / 2; // normalize between 0 and 1
173  }
174 
175 private:
176  inline PixelType makeValid(PixelType v) const
177  {
178  return v < itk::NumericTraits<PixelType>::Zero ? itk::NumericTraits<PixelType>::Zero : v;
179  }
180 
181  double m_AzimuthLight; // in radian
182  double m_ElevationLight; // in radian
183  double m_XRes; // assumed to be positive provided in m
184  double m_YRes; // assumed to be positive provided in m
185  double m_Scale;
186 
187  // precomputed parameters to avoid the sin() cos() call for each pixel
188  double m_SinElev;
189  double m_CosElev;
190  double m_SinAz;
191  double m_CosAz;
192 };
193 }
194 }
195 
196 #endif
otb::Functor::HillShadingFunctor
Unary neighborhood functor to compute the lambertian of a surface.
Definition: otbHillShadingFunctor.h:77
otb::Functor::HillShadingFunctor::GetElevationLight
double GetElevationLight() const
Definition: otbHillShadingFunctor.h:137
otb::Functor::HillShadingFunctor::m_SinElev
double m_SinElev
Definition: otbHillShadingFunctor.h:188
otb::Functor::HillShadingFunctor::SetYRes
void SetYRes(double res)
Definition: otbHillShadingFunctor.h:110
otb::Functor::HillShadingFunctor::GetXRes
double GetXRes() const
Definition: otbHillShadingFunctor.h:95
otb::Functor::HillShadingFunctor::IteratorType
TNeighIter IteratorType
Definition: otbHillShadingFunctor.h:81
otbMath.h
otb::Functor::HillShadingFunctor::operator()
TOutput operator()(const TNeighIter &it) const
Definition: otbHillShadingFunctor.h:149
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::Functor::HillShadingFunctor::~HillShadingFunctor
~HillShadingFunctor()
Definition: otbHillShadingFunctor.h:91
otb::Functor::HillShadingFunctor::GetAzimuthLight
double GetAzimuthLight() const
Definition: otbHillShadingFunctor.h:125
otb::Functor::HillShadingFunctor::GetYRes
double GetYRes() const
Definition: otbHillShadingFunctor.h:100
otb::Functor::HillShadingFunctor::SetScale
void SetScale(double scale)
Definition: otbHillShadingFunctor.h:120
otb::Functor::HillShadingFunctor::m_YRes
double m_YRes
Definition: otbHillShadingFunctor.h:184
otb::Functor::HillShadeModulationFunctor::operator()
TOutput operator()(const TInput1 &A, const TInput2 &B) const
Definition: otbHillShadingFunctor.h:54
otb::Functor::HillShadingFunctor::m_ElevationLight
double m_ElevationLight
Definition: otbHillShadingFunctor.h:182
otb::Functor::HillShadingFunctor::PixelType
IteratorType::PixelType PixelType
Definition: otbHillShadingFunctor.h:82
otb::CONST_PI_180
constexpr double CONST_PI_180
Definition: otbMath.h:56
otb::Functor::HillShadingFunctor::HillShadingFunctor
HillShadingFunctor()
Definition: otbHillShadingFunctor.h:84
otb::Functor::HillShadingFunctor::m_CosAz
double m_CosAz
Definition: otbHillShadingFunctor.h:191
otb::Functor::HillShadeModulationFunctor
Modulate an image with hill shading.
Definition: otbHillShadingFunctor.h:44
otb::Functor::HillShadingFunctor::GetScale
double GetScale() const
Definition: otbHillShadingFunctor.h:115
otb::Functor::HillShadingFunctor::m_CosElev
double m_CosElev
Definition: otbHillShadingFunctor.h:189
otb::Functor::HillShadingFunctor::Self
HillShadingFunctor Self
Definition: otbHillShadingFunctor.h:80
otb::Functor::HillShadeModulationFunctor::~HillShadeModulationFunctor
~HillShadeModulationFunctor()
Definition: otbHillShadingFunctor.h:50
otb::Functor::HillShadingFunctor::SetAzimuthLight
void SetAzimuthLight(double az)
Definition: otbHillShadingFunctor.h:130
otb::Functor::HillShadingFunctor::SetElevationLight
void SetElevationLight(double el)
Definition: otbHillShadingFunctor.h:142
otb::Functor::HillShadingFunctor::makeValid
PixelType makeValid(PixelType v) const
Definition: otbHillShadingFunctor.h:176
otb::Functor::HillShadingFunctor::m_SinAz
double m_SinAz
Definition: otbHillShadingFunctor.h:190
otb::Functor::HillShadeModulationFunctor::HillShadeModulationFunctor
HillShadeModulationFunctor()
Definition: otbHillShadingFunctor.h:47
otb::Functor::HillShadingFunctor::m_Scale
double m_Scale
Definition: otbHillShadingFunctor.h:185
otb::Functor::HillShadingFunctor::m_XRes
double m_XRes
Definition: otbHillShadingFunctor.h:183
otb::Functor::HillShadingFunctor::m_AzimuthLight
double m_AzimuthLight
Definition: otbHillShadingFunctor.h:181
otb::Functor::HillShadingFunctor::SetXRes
void SetXRes(double res)
Definition: otbHillShadingFunctor.h:105