OTB  9.0.0
Orfeo Toolbox
otbAttributesMapLabelObject.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 otbAttributesMapLabelObject_h
22 #define otbAttributesMapLabelObject_h
23 
24 #if defined(__GNUC__) || defined(__clang__)
25 #pragma GCC diagnostic push
26 #pragma GCC diagnostic ignored "-Wunused-parameter"
27 #include "itkShapeLabelObject.h"
28 #pragma GCC diagnostic pop
29 #else
30 #include "itkShapeLabelObject.h"
31 #endif
32 
33 #include "otbPolygon.h"
34 #include <map>
35 #include <string>
36 
37 namespace otb
38 {
39 
40 namespace Functor
41 {
42 
53 template <class TLabelObject>
55 {
56 public:
57  // The Label Object typedef
58  typedef TLabelObject LabelObjectType;
59  typedef typename LabelObjectType::AttributesValueType AttributeValueType;
60 
66  inline const AttributeValueType operator()(LabelObjectType* labelObject) const
67  {
68  return labelObject->GetAttribute(m_AttributeName.c_str());
69  }
70 
72  void SetAttributeName(const char* name)
73  {
74  m_AttributeName = name;
75  }
77  const char* GetAttributeName() const
78  {
79  return m_AttributeName.c_str();
80  }
81 
83  AttributesMapLabelObjectAccessor() : m_AttributeName("")
84  {
85  }
86 
89  {
90  }
91 
92 private:
94  std::string m_AttributeName;
95 };
96 
97 
105 template <class TLabelObject, class TMeasurementVector>
107 {
108 public:
109  typedef std::vector<std::string> AttributesListType;
110 
111  inline TMeasurementVector operator()(const TLabelObject* object) const
112  {
113  TMeasurementVector newSample(m_Attributes.size());
114 
115  unsigned int attrIndex = 0;
116  typename AttributesListType::const_iterator attrIt = m_Attributes.begin();
117  while (attrIt != m_Attributes.end())
118  {
119  newSample[attrIndex] = object->GetAttribute(attrIt->c_str());
120  ++attrIt;
121  ++attrIndex;
122  }
123  return newSample;
124  }
125 
127  void AddAttribute(const char* attr)
128  {
129  m_Attributes.push_back(attr);
130  }
131 
133  void RemoveAttribute(const char* attr)
134  {
135  AttributesListType::iterator elt = std::find(m_Attributes.begin(), m_Attributes.end(), attr);
136  if (elt != m_Attributes.end())
137  {
138  m_Attributes.erase(elt);
139  }
140  }
142 
145  {
146  m_Attributes.clear();
147  }
148 
150  unsigned int GetNumberOfAttributes()
151  {
152  return m_Attributes.size();
153  }
154 
155 private:
157 };
158 
159 } // end namespace Functor
160 
176 template <class TLabel, unsigned int VImageDimension, class TAttributesValue>
177 class ITK_EXPORT AttributesMapLabelObject : public itk::LabelObject<TLabel, VImageDimension>
178 {
179 public:
182  typedef itk::LabelObject<TLabel, VImageDimension> Superclass;
183  typedef typename Superclass::LabelObjectType LabelObjectType;
184  typedef itk::SmartPointer<Self> Pointer;
185  typedef itk::SmartPointer<const Self> ConstPointer;
186  typedef itk::WeakPointer<const Self> ConstWeakPointer;
187 
189  itkNewMacro(Self);
190 
192  itkTypeMacro(AttributesMapLabelObject, LabelObject);
193 
194  itkStaticConstMacro(ImageDimension, unsigned int, VImageDimension);
195 
197  typedef itk::LabelMap<Self> LabelMapType;
198 
200  typedef TLabel LabelType;
201  typedef TAttributesValue AttributesValueType;
202 
203  // Convenient inherited typedefs
204  typedef typename Superclass::IndexType IndexType;
205  typedef typename Superclass::LineType LineType;
206  typedef typename Superclass::LengthType LengthType;
207 
209  typedef std::map<std::string, AttributesValueType> AttributesMapType;
210  typedef typename AttributesMapType::iterator AttributesMapIteratorType;
211  typedef typename AttributesMapType::const_iterator AttributesMapConstIteratorType;
212 
213  // The polygon corresponding to the label object
216 
221  void SetAttribute(const char* name, AttributesValueType value)
222  {
223  m_Attributes[name] = value;
224  }
225 
230  void SetAttribute(const std::string& name, AttributesValueType value)
231  {
232  this->SetAttribute(name.c_str(), value);
233  }
234 
238  AttributesValueType GetAttribute(const char* name) const
239  {
240  AttributesMapConstIteratorType it = m_Attributes.find(name);
241  if (it != m_Attributes.end())
242  {
243  return it->second;
244  }
245  else
246  {
247  itkExceptionMacro(<< "Could not find attribute named " << name);
248  }
249  }
251 
255  unsigned int GetNumberOfAttributes() const
256  {
257  return m_Attributes.size();
258  }
259 
263  std::vector<std::string> GetAvailableAttributes() const
264  {
265  std::vector<std::string> attributesNames;
266 
267  AttributesMapConstIteratorType it = m_Attributes.begin();
268 
269  while (it != m_Attributes.end())
270  {
271  attributesNames.push_back(it->first);
272  ++it;
273  }
274  return attributesNames;
275  }
276 
280  virtual void CopyAttributesFrom(const LabelObjectType* lo)
281  {
282  Superclass::CopyAttributesFrom(lo);
283 
284  // copy the data of the current type if possible
285  const Self* src = dynamic_cast<const Self*>(lo);
286  if (src == nullptr)
287  {
288  return;
289  }
290  m_Attributes = src->m_Attributes;
291  }
292 
294  const PolygonType* GetPolygon() const
295  {
296  return m_Polygon;
297  }
298 
301  {
302  return m_Polygon;
303  }
304 
307  {
308  m_Polygon = p;
309  }
310 
311 protected:
313  AttributesMapLabelObject() : m_Attributes(), m_Polygon(PolygonType::New())
314  {
315  }
316 
319  {
320  }
321 
323  void PrintSelf(std::ostream& os, itk::Indent indent) const override
324  {
325  Superclass::PrintSelf(os, indent);
326  os << indent << "Attributes: " << std::endl;
327  for (AttributesMapConstIteratorType it = m_Attributes.begin(); it != m_Attributes.end(); ++it)
328  {
329  os << indent << indent << it->first << " = " << it->second << std::endl;
330  }
331  }
333 
334 private:
335  AttributesMapLabelObject(const Self&) = delete;
336  void operator=(const Self&) = delete;
337 
340 
344 };
345 
346 } // end namespace otb
347 #endif
otb::AttributesMapLabelObject::GetAvailableAttributes
std::vector< std::string > GetAvailableAttributes() const
Definition: otbAttributesMapLabelObject.h:263
otb::AttributesMapLabelObject::GetPolygon
const PolygonType * GetPolygon() const
Definition: otbAttributesMapLabelObject.h:294
otb::AttributesMapLabelObject::AttributesMapType
std::map< std::string, AttributesValueType > AttributesMapType
Map container typedefs.
Definition: otbAttributesMapLabelObject.h:209
otb::AttributesMapLabelObject::m_Attributes
AttributesMapType m_Attributes
Definition: otbAttributesMapLabelObject.h:339
otb::Functor::AttributesMapLabelObjectAccessor::~AttributesMapLabelObjectAccessor
~AttributesMapLabelObjectAccessor()
Destructor.
Definition: otbAttributesMapLabelObject.h:88
otb::Functor::AttributesMapLabelObjectAccessor::m_AttributeName
std::string m_AttributeName
Name of the attribute to retrieve.
Definition: otbAttributesMapLabelObject.h:94
otb::Functor::AttributesMapMeasurementFunctor::RemoveAttribute
void RemoveAttribute(const char *attr)
Definition: otbAttributesMapLabelObject.h:133
otb::AttributesMapLabelObject::LabelObjectType
Superclass::LabelObjectType LabelObjectType
Definition: otbAttributesMapLabelObject.h:183
otb::AttributesMapLabelObject
A LabelObject with a generic attributes map.
Definition: otbAttributesMapLabelObject.h:177
otbPolygon.h
otb::Functor::AttributesMapMeasurementFunctor::m_Attributes
AttributesListType m_Attributes
Definition: otbAttributesMapLabelObject.h:156
otb::AttributesMapLabelObject::Superclass
itk::LabelObject< TLabel, VImageDimension > Superclass
Definition: otbAttributesMapLabelObject.h:182
otb::AttributesMapLabelObject::AttributesMapConstIteratorType
AttributesMapType::const_iterator AttributesMapConstIteratorType
Definition: otbAttributesMapLabelObject.h:211
otb::find
string_view find(string_view const &haystack, string_view const &needle)
Definition: otbStringUtilities.h:305
otb::AttributesMapLabelObject::LabelMapType
itk::LabelMap< Self > LabelMapType
Type of a label map using an AttributesMapLabelObject.
Definition: otbAttributesMapLabelObject.h:197
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::Functor::AttributesMapLabelObjectAccessor::operator()
const AttributeValueType operator()(LabelObjectType *labelObject) const
Definition: otbAttributesMapLabelObject.h:66
otb::AttributesMapLabelObject::SetAttribute
void SetAttribute(const char *name, AttributesValueType value)
Definition: otbAttributesMapLabelObject.h:221
otb::Functor::AttributesMapLabelObjectAccessor::LabelObjectType
TLabelObject LabelObjectType
Definition: otbAttributesMapLabelObject.h:58
otb::AttributesMapLabelObject::PrintSelf
void PrintSelf(std::ostream &os, itk::Indent indent) const override
Definition: otbAttributesMapLabelObject.h:323
otb::AttributesMapLabelObject::Pointer
itk::SmartPointer< Self > Pointer
Definition: otbAttributesMapLabelObject.h:184
otb::AttributesMapLabelObject::AttributesMapIteratorType
AttributesMapType::iterator AttributesMapIteratorType
Definition: otbAttributesMapLabelObject.h:210
otb::Functor::AttributesMapMeasurementFunctor::operator()
TMeasurementVector operator()(const TLabelObject *object) const
Definition: otbAttributesMapLabelObject.h:111
otb::AttributesMapLabelObject::LineType
Superclass::LineType LineType
Definition: otbAttributesMapLabelObject.h:205
otb::AttributesMapLabelObject::AttributesValueType
TAttributesValue AttributesValueType
Definition: otbAttributesMapLabelObject.h:201
otb::AttributesMapLabelObject::CopyAttributesFrom
virtual void CopyAttributesFrom(const LabelObjectType *lo)
Definition: otbAttributesMapLabelObject.h:280
otb::Functor::AttributesMapLabelObjectAccessor::AttributesMapLabelObjectAccessor
AttributesMapLabelObjectAccessor()
Constructor.
Definition: otbAttributesMapLabelObject.h:83
otb::AttributesMapLabelObject::GetAttribute
AttributesValueType GetAttribute(const char *name) const
Definition: otbAttributesMapLabelObject.h:238
otb::Functor::AttributesMapMeasurementFunctor::AttributesListType
std::vector< std::string > AttributesListType
Definition: otbAttributesMapLabelObject.h:109
otb::AttributesMapLabelObject::LabelType
TLabel LabelType
Template parameters typedef.
Definition: otbAttributesMapLabelObject.h:200
otb::Functor::AttributesMapLabelObjectAccessor
Allows accessing a given field of an AttributesMapLabelObject.
Definition: otbAttributesMapLabelObject.h:54
otb::Functor::AttributesMapMeasurementFunctor
This class allows building a measurement vector from an AttributesMapLabelObject.
Definition: otbAttributesMapLabelObject.h:106
otb::AttributesMapLabelObject::PolygonType
Polygon< double > PolygonType
Definition: otbAttributesMapLabelObject.h:214
otb::Functor::AttributesMapLabelObjectAccessor::AttributeValueType
LabelObjectType::AttributesValueType AttributeValueType
Definition: otbAttributesMapLabelObject.h:59
otb::AttributesMapLabelObject::SetAttribute
void SetAttribute(const std::string &name, AttributesValueType value)
Definition: otbAttributesMapLabelObject.h:230
otb::Functor::AttributesMapMeasurementFunctor::ClearAttributes
void ClearAttributes()
Definition: otbAttributesMapLabelObject.h:144
otb::AttributesMapLabelObject::PolygonPointerType
PolygonType::Pointer PolygonPointerType
Definition: otbAttributesMapLabelObject.h:215
otb::AttributesMapLabelObject::~AttributesMapLabelObject
~AttributesMapLabelObject() override
Definition: otbAttributesMapLabelObject.h:318
otb::AttributesMapLabelObject::GetNumberOfAttributes
unsigned int GetNumberOfAttributes() const
Definition: otbAttributesMapLabelObject.h:255
otb::Polygon::Pointer
itk::SmartPointer< Self > Pointer
Definition: otbPolygon.h:50
otb::Functor::AttributesMapLabelObjectAccessor::SetAttributeName
void SetAttributeName(const char *name)
Set the name of the attribute to retrieve.
Definition: otbAttributesMapLabelObject.h:72
otb::Functor::AttributesMapLabelObjectAccessor::GetAttributeName
const char * GetAttributeName() const
Get the the name of the attribute to retrieve.
Definition: otbAttributesMapLabelObject.h:77
otb::AttributesMapLabelObject::IndexType
Superclass::IndexType IndexType
Definition: otbAttributesMapLabelObject.h:204
otb::AttributesMapLabelObject::GetPolygon
PolygonType * GetPolygon()
Definition: otbAttributesMapLabelObject.h:300
otb::Functor::AttributesMapMeasurementFunctor::GetNumberOfAttributes
unsigned int GetNumberOfAttributes()
Definition: otbAttributesMapLabelObject.h:150
otb::Polygon
This class represent a 2D polygon.
Definition: otbPolygon.h:44
otb::Functor::AttributesMapMeasurementFunctor::AddAttribute
void AddAttribute(const char *attr)
Definition: otbAttributesMapLabelObject.h:127
otb::AttributesMapLabelObject::SetPolygon
void SetPolygon(PolygonType *p)
Definition: otbAttributesMapLabelObject.h:306
otb::AttributesMapLabelObject::Self
AttributesMapLabelObject Self
Definition: otbAttributesMapLabelObject.h:181
otb::AttributesMapLabelObject::LengthType
Superclass::LengthType LengthType
Definition: otbAttributesMapLabelObject.h:206
otb::AttributesMapLabelObject::AttributesMapLabelObject
AttributesMapLabelObject()
Definition: otbAttributesMapLabelObject.h:313
otb::AttributesMapLabelObject::ConstPointer
itk::SmartPointer< const Self > ConstPointer
Definition: otbAttributesMapLabelObject.h:185
otb::AttributesMapLabelObject::ConstWeakPointer
itk::WeakPointer< const Self > ConstWeakPointer
Definition: otbAttributesMapLabelObject.h:186
otb::AttributesMapLabelObject::m_Polygon
PolygonPointerType m_Polygon
Definition: otbAttributesMapLabelObject.h:343