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