OTB  10.0.0
Orfeo Toolbox
otbCompacityPathFunction.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2024 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 otbCompacityPathFunction_hxx
22 #define otbCompacityPathFunction_hxx
23 
25 #include "itkNumericTraits.h"
26 #include "otbMacro.h"
27 #include "otbMath.h"
28 
29 namespace otb
30 {
31 
32 template <class TInputPath, class TOutput>
33 void CompacityPathFunction<TInputPath, TOutput>::PrintSelf(std::ostream& os, itk::Indent indent) const
34 {
35  this->Superclass::PrintSelf(os, indent);
36 }
37 
38 template <class TInputPath, class TOutput>
40 {
41  VertexListPointer vertexList;
42  VertexType cindex;
43  int nbPath;
44  RealType Surface = 0.0;
45  RealType Perimeter = 0.0;
46  RealType Compacity;
47  RealType x1, x2, y1, y2;
48 
49  vertexList = path.GetVertexList();
50  nbPath = vertexList->Size();
51 
52  if (nbPath > 2)
53  {
54  for (int i = 0; i < nbPath; ++i)
55  {
56  cindex = vertexList->GetElement(i);
57  x1 = cindex[0];
58  y1 = cindex[1];
59 
60  if (i == (nbPath - 1))
61  {
62  cindex = vertexList->GetElement(0);
63  }
64  else
65  {
66  cindex = vertexList->GetElement(i + 1);
67  }
68  x2 = cindex[0];
69  y2 = cindex[1];
70 
71  RealType Norm;
72 
73  Norm = std::sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
74 
75  Perimeter += Norm;
76 
77  RealType P = x2 * (y2 - y1);
78  RealType Q = y2 * (x2 - x1);
79 
80  Surface += (P - Q);
81  } // FOR loop
82  } // IF loop
83  else
84  {
85  itkExceptionMacro(<< "CompacityPathFunction::Evaluate() FAILED -- path must contains at least 3 points");
86  }
87  Surface /= 2.0;
88 
89  Compacity = Surface / (Perimeter * Perimeter);
90  Compacity *= (4. * acos(-1.0));
91 
92  return (static_cast<OutputType>(Compacity));
93 }
94 
95 template <class TInputPath, class TOutput>
97 {
98  if (!this->GetInputPath())
99  {
100  otbMsgDevMacro(<< "Problem with GetInputPath");
101  return static_cast<OutputType>(itk::NumericTraits<OutputType>::max());
102  }
103 
104  OutputType Result = Evaluate(*(this->GetInputPath()));
105 
106  return Result;
107 }
108 
109 } // namespace otb
110 
111 #endif
VertexListType::ConstPointer VertexListPointer
void PrintSelf(std::ostream &os, itk::Indent indent) const override
Superclass::InputPathType PathType
PathType::ContinuousIndexType VertexType
virtual OutputType Evaluate() const
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
#define otbMsgDevMacro(x)
Definition: otbMacro.h:116