OTB  5.5.0
Orfeo Toolbox
otbAttributesMapLabelObject.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: ORFEO Toolbox
4  Language: C++
5  Date: $Date$
6  Version: $Revision$
7 
8 
9  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
10  See OTBCopyright.txt for details.
11 
12 
13  This software is distributed WITHOUT ANY WARRANTY; without even
14  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  PURPOSE. See the above copyright notices for more information.
16 
17 =========================================================================*/
18 #ifndef __otbAttributesMapLabelObject_h
19 #define __otbAttributesMapLabelObject_h
20 
21 #if defined(__GNUC__) || defined(__clang__)
22 #pragma GCC diagnostic push
23 #pragma GCC diagnostic ignored "-Wunused-parameter"
24 #include "itkShapeLabelObject.h"
25 #pragma GCC diagnostic pop
26 #else
27 #include "itkShapeLabelObject.h"
28 #endif
29 
30 #include "otbPolygon.h"
31 #include <map>
32 
33 namespace otb
34 {
35 
36 namespace Functor
37 {
38 
49 template<class TLabelObject>
51 {
52 public:
53  // The Label Object typedef
54  typedef TLabelObject LabelObjectType;
55  typedef typename LabelObjectType::AttributesValueType AttributeValueType;
56 
62  inline const AttributeValueType operator ()(LabelObjectType * labelObject) const
63  {
64  return labelObject->GetAttribute(m_AttributeName.c_str());
65  }
66 
68  void SetAttributeName(const char * name)
69  {
70  m_AttributeName = name;
71  }
73  const char * GetAttributeName() const
74  {
75  return m_AttributeName.c_str();
76  }
77 
79  AttributesMapLabelObjectAccessor() : m_AttributeName("") {}
80 
83 
84 private:
86  std::string m_AttributeName;
87 };
88 
89 
97 template<class TLabelObject, class TMeasurementVector>
99 {
100 public:
101  typedef std::vector<std::string> AttributesListType;
102 
103  inline TMeasurementVector operator()(const TLabelObject * object) const
104  {
105  TMeasurementVector newSample(m_Attributes.size());
106 
107  unsigned int attrIndex = 0;
108  typename AttributesListType::const_iterator attrIt = m_Attributes.begin();
109  while(attrIt != m_Attributes.end())
110  {
111  newSample[attrIndex] = object->GetAttribute(attrIt->c_str());
112  ++attrIt;
113  ++attrIndex;
114  }
115  return newSample;
116  }
117 
119  void AddAttribute(const char * attr)
120  {
121  m_Attributes.push_back(attr);
122  }
123 
125  void RemoveAttribute(const char * attr)
126  {
127  AttributesListType::iterator elt = std::find(m_Attributes.begin(), m_Attributes.end(), attr);
128  if(elt!=m_Attributes.end())
129  {
130  m_Attributes.erase(elt);
131  }
132  }
134 
137  {
138  m_Attributes.clear();
139  }
140 
142  unsigned int GetNumberOfAttributes()
143  {
144  return m_Attributes.size();
145  }
146 
147 private:
149 };
150 
151 } // end namespace Functor
152 
168 template <class TLabel, unsigned int VImageDimension, class TAttributesValue>
169 class ITK_EXPORT AttributesMapLabelObject
170  : public itk::LabelObject<TLabel, VImageDimension>
171 {
172 public:
180 
182  itkNewMacro(Self);
183 
185  itkTypeMacro(AttributesMapLabelObject, LabelObject);
186 
187  itkStaticConstMacro(ImageDimension, unsigned int, VImageDimension);
188 
191 
193  typedef TLabel LabelType;
194  typedef TAttributesValue AttributesValueType;
195 
196  // Convenient inherited typedefs
198  typedef typename Superclass::LineType LineType;
200 
202  typedef std::map<std::string, AttributesValueType> AttributesMapType;
203  typedef typename AttributesMapType::iterator AttributesMapIteratorType;
204  typedef typename AttributesMapType::const_iterator AttributesMapConstIteratorType;
205 
206  // The polygon corresponding to the label object
209 
214  void SetAttribute(const char * name, AttributesValueType value)
215  {
216  m_Attributes[name] = value;
217  }
218 
223  void SetAttribute(const std::string& name, AttributesValueType value)
224  {
225  this->SetAttribute(name.c_str(), value);
226  }
227 
231  AttributesValueType GetAttribute(const char * name) const
232  {
233  AttributesMapConstIteratorType it = m_Attributes.find(name);
234  if (it != m_Attributes.end())
235  {
236  return it->second;
237  }
238  else
239  {
240  itkExceptionMacro(<< "Could not find attribute named " << name);
241  }
242  }
244 
248  unsigned int GetNumberOfAttributes() const
249  {
250  return m_Attributes.size();
251  }
252 
256  std::vector<std::string> GetAvailableAttributes() const
257  {
258  std::vector<std::string> attributesNames;
259 
260  AttributesMapConstIteratorType it = m_Attributes.begin();
261 
262  while (it != m_Attributes.end())
263  {
264  attributesNames.push_back(it->first);
265  ++it;
266  }
267  return attributesNames;
268  }
269 
273  virtual void CopyAttributesFrom(const LabelObjectType * lo)
274  {
275  Superclass::CopyAttributesFrom(lo);
276 
277  // copy the data of the current type if possible
278  const Self * src = dynamic_cast<const Self *>(lo);
279  if (src == NULL)
280  {
281  return;
282  }
283  m_Attributes = src->m_Attributes;
284  }
285 
287  const PolygonType * GetPolygon() const
288  {
289  return m_Polygon;
290  }
291 
294  {
295  return m_Polygon;
296  }
297 
300  {
301  m_Polygon = p;
302  }
303 
304 protected:
306  AttributesMapLabelObject() : m_Attributes(), m_Polygon(PolygonType::New()) {}
307 
310 
312  void PrintSelf(std::ostream& os, itk::Indent indent) const
313  {
314  Superclass::PrintSelf(os, indent);
315  os << indent << "Attributes: " << std::endl;
316  for (AttributesMapConstIteratorType it = m_Attributes.begin();
317  it != m_Attributes.end(); ++it)
318  {
319  os << indent << indent << it->first << " = " << it->second << std::endl;
320  }
321  }
322 private:
323  AttributesMapLabelObject(const Self &); //purposely not implemented
324  void operator =(const Self&); //purposely not implemented
326 
329 
333 };
334 
335 } // end namespace otb
336 #endif
std::vector< std::string > GetAvailableAttributes() const
itk::LabelMap< Self > LabelMapType
Type of a label map using an AttributesMapLabelObject.
AttributesValueType GetAttribute(const char *name) const
void SetAttribute(const char *name, AttributesValueType value)
itk::SmartPointer< const Self > ConstPointer
LabelObjectType::AttributesValueType AttributeValueType
void SetAttributeName(const char *name)
Set the name of the attribute to retrieve.
This class allows building a measurement vector from an AttributesMapLabelObject. ...
const char * GetAttributeName() const
Get the the name of the attribute to retrieve.
AttributesMapType::const_iterator AttributesMapConstIteratorType
This class represent a 2D polygon.
Definition: otbPolygon.h:41
itk::LabelObject< TLabel, VImageDimension > Superclass
virtual void CopyAttributesFrom(const LabelObjectType *lo)
Superclass::LabelObjectType LabelObjectType
itk::WeakPointer< const Self > ConstWeakPointer
void PrintSelf(std::ostream &os, itk::Indent indent) const
std::map< std::string, AttributesValueType > AttributesMapType
Map container typedefs.
TMeasurementVector operator()(const TLabelObject *object) const
A LabelObject with a generic attributes map.
Allows accessing a given field of an AttributesMapLabelObject.
const PolygonType * GetPolygon() const
TLabel LabelType
Template parameters typedef.
std::string m_AttributeName
Name of the attribute to retrieve.
AttributesMapType::iterator AttributesMapIteratorType
void SetAttribute(const std::string &name, AttributesValueType value)