OTB  10.0.0
Orfeo Toolbox
otbAttributesMapLabelObject.h
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 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
A LabelObject with a generic attributes map.
Superclass::LabelObjectType LabelObjectType
void operator=(const Self &)=delete
AttributesMapType::const_iterator AttributesMapConstIteratorType
AttributesValueType GetAttribute(const char *name) const
AttributesMapLabelObject(const Self &)=delete
itk::SmartPointer< const Self > ConstPointer
std::vector< std::string > GetAvailableAttributes() const
itk::LabelMap< Self > LabelMapType
Type of a label map using an AttributesMapLabelObject.
virtual void CopyAttributesFrom(const LabelObjectType *lo)
void SetAttribute(const std::string &name, AttributesValueType value)
itk::LabelObject< TLabel, VImageDimension > Superclass
std::map< std::string, AttributesValueType > AttributesMapType
Map container typedefs.
const PolygonType * GetPolygon() const
void PrintSelf(std::ostream &os, itk::Indent indent) const override
TLabel LabelType
Template parameters typedef.
AttributesMapType::iterator AttributesMapIteratorType
itk::WeakPointer< const Self > ConstWeakPointer
void SetAttribute(const char *name, AttributesValueType value)
Allows accessing a given field of an AttributesMapLabelObject.
const char * GetAttributeName() const
Get the the name of the attribute to retrieve.
const AttributeValueType operator()(LabelObjectType *labelObject) const
void SetAttributeName(const char *name)
Set the name of the attribute to retrieve.
LabelObjectType::AttributesValueType AttributeValueType
std::string m_AttributeName
Name of the attribute to retrieve.
This class allows building a measurement vector from an AttributesMapLabelObject.
TMeasurementVector operator()(const TLabelObject *object) const
This class represent a 2D polygon.
Definition: otbPolygon.h:45
itk::SmartPointer< Self > Pointer
Definition: otbPolygon.h:50
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
string_view find(string_view const &haystack, string_view const &needle)