OTB  9.0.0
Orfeo Toolbox
otbMergeLabelObjectFunctor.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 otbMergeLabelObjectFunctor_h
22 #define otbMergeLabelObjectFunctor_h
23 
24 #include <algorithm>
25 
26 namespace otb
27 {
28 
29 namespace Functor
30 {
31 
47 template <class TLabelObject>
49 {
50 public:
52  typedef TLabelObject LabelObjectType;
53  typedef typename LabelObjectType::Pointer LabelObjectPointerType;
54  typedef typename LabelObjectType::LineContainerType LineContainerType;
55  typedef typename LabelObjectType::LineType LineType;
56  typedef typename LineType::IndexType IndexType;
57 
58 
66  {
67  // Retrieve the two input line containers
68  LineContainerType lines1 = l1->GetLineContainer();
69  LineContainerType lines2 = l2->GetLineContainer();
71 
72  // Ensure they are sorted according to our criterion
73  stable_sort(lines1.begin(), lines1.end(), &LexicographicalLineCompare);
74  stable_sort(lines2.begin(), lines2.end(), &LexicographicalLineCompare);
75 
76  // Merge the two containers
77  LineContainerType linesOut1(lines1.size() + lines2.size());
78  merge(lines1.begin(), lines1.end(), lines2.begin(), lines2.end(), linesOut1.begin(), &LexicographicalLineCompare);
79 
80  // Merge consecutive and overlapping lines
81  LineContainerType linesOut2;
82 
83  // Initialize output final container
84  typename LineContainerType::const_iterator lit = linesOut1.begin();
85  linesOut2.push_back(*lit);
86  ++lit;
87 
88  // Walk the merged line container
89  while (lit != linesOut1.end())
90  {
91  // Test if next line overlaps with current
92  if (LinesOverlap(linesOut2.back(), *lit))
93  {
94  // Merge lines
95  LineType mline = MergesLines(linesOut2.back(), *lit);
96 
97  // Replace the last line by the merged line
98  linesOut2.pop_back();
99  linesOut2.push_back(mline);
100  }
101  else
102  {
103  // Push back the new line
104  linesOut2.push_back(*lit);
105  }
106 
107  // fetch next line
108  ++lit;
109  }
110 
111  // Create the output label object
112  LabelObjectPointerType resp = LabelObjectType::New();
113  resp->CopyAllFrom(l1);
114  resp->GetLineContainer() = linesOut2;
115 
116  // Return the output label object
117  return resp;
118  }
119 
120 private:
123  static bool LexicographicalLineCompare(const LineType& l1, const LineType& l2)
124  {
125  bool resp = l2.GetIndex()[1] > l1.GetIndex()[1];
126  resp = resp || (l2.GetIndex()[1] == l1.GetIndex()[1] && (l1.GetIndex()[0] < l2.GetIndex()[0]));
127  return resp;
128  }
129 
132  static bool LinesOverlap(const LineType& l1, const LineType& l2)
133  {
134  // Test if we are in same row
135  bool sameRow = l2.GetIndex()[1] == l1.GetIndex()[1];
136  bool leftEndInside = (l2.GetIndex()[0] >= l1.GetIndex()[0] && l2.GetIndex()[0] <= l1.GetIndex()[0] + static_cast<long>(l1.GetLength()));
137  bool rightEndInside = (l1.GetIndex()[0] >= l2.GetIndex()[0] && l1.GetIndex()[0] <= l2.GetIndex()[0] + static_cast<long>(l2.GetLength()));
138 
139  return (sameRow && (leftEndInside || rightEndInside));
140  }
141 
144  static const LineType MergesLines(const LineType& l1, const LineType& l2)
145  {
146  LineType resp;
147  resp.SetIndex(l1.GetIndex());
148  unsigned long length = std::max(l1.GetLength(), l2.GetIndex()[0] + l2.GetLength() - l1.GetIndex()[0]);
149  resp.SetLength(length);
150  return resp;
151  }
152 
153 }; // end class MergeLabelObjectFunctor
154 
155 } // end namespace Functor
156 
157 } // end namespace otb
158 
159 #endif
otb::Functor::MergeLabelObjectFunctor::MergesLines
static const LineType MergesLines(const LineType &l1, const LineType &l2)
Definition: otbMergeLabelObjectFunctor.h:144
otb::Functor::MergeLabelObjectFunctor::operator()
LabelObjectPointerType operator()(const LabelObjectType *l1, const LabelObjectType *l2) const
Definition: otbMergeLabelObjectFunctor.h:65
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::Functor::MergeLabelObjectFunctor::LinesOverlap
static bool LinesOverlap(const LineType &l1, const LineType &l2)
Definition: otbMergeLabelObjectFunctor.h:132
otb::Functor::MergeLabelObjectFunctor::LineType
LabelObjectType::LineType LineType
Definition: otbMergeLabelObjectFunctor.h:55
otb::Functor::MergeLabelObjectFunctor::LabelObjectType
TLabelObject LabelObjectType
Definition: otbMergeLabelObjectFunctor.h:52
otb::Functor::MergeLabelObjectFunctor
Merge two LabelObjects.
Definition: otbMergeLabelObjectFunctor.h:48
otb::Functor::MergeLabelObjectFunctor::LexicographicalLineCompare
static bool LexicographicalLineCompare(const LineType &l1, const LineType &l2)
Definition: otbMergeLabelObjectFunctor.h:123
otb::Functor::MergeLabelObjectFunctor::LineContainerType
LabelObjectType::LineContainerType LineContainerType
Definition: otbMergeLabelObjectFunctor.h:54
otb::Functor::MergeLabelObjectFunctor::IndexType
LineType::IndexType IndexType
Definition: otbMergeLabelObjectFunctor.h:56
otb::Functor::MergeLabelObjectFunctor::LabelObjectPointerType
LabelObjectType::Pointer LabelObjectPointerType
Definition: otbMergeLabelObjectFunctor.h:53