OTB  5.0.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 #include "itkShapeLabelObject.h"
22 #include "otbPolygon.h"
23 #include <map>
24 
25 namespace otb
26 {
27 
28 namespace Functor
29 {
30 
41 template<class TLabelObject>
43 {
44 public:
45  // The Label Object typedef
46  typedef TLabelObject LabelObjectType;
47  typedef typename LabelObjectType::AttributesValueType AttributeValueType;
48 
54  inline const AttributeValueType operator ()(LabelObjectType * labelObject) const
55  {
56  return labelObject->GetAttribute(m_AttributeName.c_str());
57  }
58 
60  void SetAttributeName(const char * name)
61  {
62  m_AttributeName = name;
63  }
65  const char * GetAttributeName() const
66  {
67  return m_AttributeName.c_str();
68  }
69 
71  AttributesMapLabelObjectAccessor() : m_AttributeName("") {}
72 
75 
76 private:
78  std::string m_AttributeName;
79 };
80 
81 
89 template<class TLabelObject, class TMeasurementVector>
91 {
92 public:
93  typedef std::vector<std::string> AttributesListType;
94 
95  inline TMeasurementVector operator()(const TLabelObject * object) const
96  {
97  TMeasurementVector newSample(m_Attributes.size());
98 
99  unsigned int attrIndex = 0;
100  typename AttributesListType::const_iterator attrIt = m_Attributes.begin();
101  while(attrIt != m_Attributes.end())
102  {
103  newSample[attrIndex] = object->GetAttribute(attrIt->c_str());
104  ++attrIt;
105  ++attrIndex;
106  }
107  return newSample;
108  }
109 
111  void AddAttribute(const char * attr)
112  {
113  m_Attributes.push_back(attr);
114  }
115 
117  void RemoveAttribute(const char * attr)
118  {
119  AttributesListType::iterator elt = std::find(m_Attributes.begin(), m_Attributes.end(), attr);
120  if(elt!=m_Attributes.end())
121  {
122  m_Attributes.erase(elt);
123  }
124  }
126 
129  {
130  m_Attributes.clear();
131  }
132 
134  unsigned int GetNumberOfAttributes()
135  {
136  return m_Attributes.size();
137  }
138 
139 private:
141 };
142 
143 } // end namespace Functor
144 
160 template <class TLabel, unsigned int VImageDimension, class TAttributesValue>
161 class ITK_EXPORT AttributesMapLabelObject
162  : public itk::LabelObject<TLabel, VImageDimension>
163 {
164 public:
172 
174  itkNewMacro(Self);
175 
177  itkTypeMacro(AttributesMapLabelObject, LabelObject);
178 
179  itkStaticConstMacro(ImageDimension, unsigned int, VImageDimension);
180 
183 
185  typedef TLabel LabelType;
186  typedef TAttributesValue AttributesValueType;
187 
188  // Convenient inherited typedefs
190  typedef typename Superclass::LineType LineType;
192 
194  typedef std::map<std::string, AttributesValueType> AttributesMapType;
195  typedef typename AttributesMapType::iterator AttributesMapIteratorType;
196  typedef typename AttributesMapType::const_iterator AttributesMapConstIteratorType;
197 
198  // The polygon corresponding to the label object
201 
206  void SetAttribute(const char * name, AttributesValueType value)
207  {
208  m_Attributes[name] = value;
209  }
210 
215  void SetAttribute(const std::string& name, AttributesValueType value)
216  {
217  this->SetAttribute(name.c_str(), value);
218  }
219 
223  AttributesValueType GetAttribute(const char * name) const
224  {
225  AttributesMapConstIteratorType it = m_Attributes.find(name);
226  if (it != m_Attributes.end())
227  {
228  return it->second;
229  }
230  else
231  {
232  itkExceptionMacro(<< "Could not find attribute named " << name);
233  }
234  }
236 
240  unsigned int GetNumberOfAttributes() const
241  {
242  return m_Attributes.size();
243  }
244 
248  std::vector<std::string> GetAvailableAttributes() const
249  {
250  std::vector<std::string> attributesNames;
251 
252  AttributesMapConstIteratorType it = m_Attributes.begin();
253 
254  while (it != m_Attributes.end())
255  {
256  attributesNames.push_back(it->first);
257  ++it;
258  }
259  return attributesNames;
260  }
261 
265  virtual void CopyAttributesFrom(const LabelObjectType * lo)
266  {
267  Superclass::CopyAttributesFrom(lo);
268 
269  // copy the data of the current type if possible
270  const Self * src = dynamic_cast<const Self *>(lo);
271  if (src == NULL)
272  {
273  return;
274  }
275  m_Attributes = src->m_Attributes;
276  }
277 
279  const PolygonType * GetPolygon() const
280  {
281  return m_Polygon;
282  }
283 
286  {
287  return m_Polygon;
288  }
289 
292  {
293  m_Polygon = p;
294  }
295 
296 protected:
298  AttributesMapLabelObject() : m_Attributes(), m_Polygon(PolygonType::New()) {}
299 
302 
304  void PrintSelf(std::ostream& os, itk::Indent indent) const
305  {
306  Superclass::PrintSelf(os, indent);
307  os << indent << "Attributes: " << std::endl;
308  for (AttributesMapConstIteratorType it = m_Attributes.begin();
309  it != m_Attributes.end(); ++it)
310  {
311  os << indent << indent << it->first << " = " << it->second << std::endl;
312  }
313  }
314 private:
315  AttributesMapLabelObject(const Self &); //purposely not implemented
316  void operator =(const Self&); //purposely not implemented
318 
321 
325 };
326 
327 } // end namespace otb
328 #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 to build 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 to acces a given field of an AttributesMapLabelObject.
#define NULL
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)