Orfeo ToolBox  4.2
Orfeo ToolBox is not a black box
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 "itkLabelMap.h"
23 #include "otbPolygon.h"
24 #include <map>
25 
26 namespace otb
27 {
28 
29 namespace Functor
30 {
31 
40 template<class TLabelObject>
42 {
43 public:
44  // The Label Object typedef
45  typedef TLabelObject LabelObjectType;
46  typedef typename LabelObjectType::AttributesValueType AttributeValueType;
47 
53  inline const AttributeValueType operator ()(LabelObjectType * labelObject) const
54  {
55  return labelObject->GetAttribute(m_AttributeName.c_str());
56  }
57 
59  void SetAttributeName(const char * name)
60  {
61  m_AttributeName = name;
62  }
64  const char * GetAttributeName() const
65  {
66  return m_AttributeName.c_str();
67  }
68 
70  AttributesMapLabelObjectAccessor() : m_AttributeName("") {}
71 
74 
75 private:
77  std::string m_AttributeName;
78 };
79 
80 
86 template<class TLabelObject, class TMeasurementVector>
88 {
89 public:
90  typedef std::vector<std::string> AttributesListType;
91 
92  inline TMeasurementVector operator()(const TLabelObject * object) const
93  {
94  TMeasurementVector newSample(m_Attributes.size());
95 
96  unsigned int attrIndex = 0;
97  typename AttributesListType::const_iterator attrIt = m_Attributes.begin();
98  while(attrIt != m_Attributes.end())
99  {
100  newSample[attrIndex] = object->GetAttribute(attrIt->c_str());
101  ++attrIt;
102  ++attrIndex;
103  }
104  return newSample;
105  }
106 
108  void AddAttribute(const char * attr)
109  {
110  m_Attributes.push_back(attr);
111  }
112 
114  void RemoveAttribute(const char * attr)
115  {
116  AttributesListType::iterator elt = std::find(m_Attributes.begin(), m_Attributes.end(), attr);
117  if(elt!=m_Attributes.end())
118  {
119  m_Attributes.erase(elt);
120  }
121  }
122 
125  {
126  m_Attributes.clear();
127  }
128 
130  unsigned int GetNumberOfAttributes()
131  {
132  return m_Attributes.size();
133  }
134 
135 private:
137 };
138 
139 } // end namespace Functor
140 
154 template <class TLabel, unsigned int VImageDimension, class TAttributesValue>
155 class ITK_EXPORT AttributesMapLabelObject
156  : public itk::LabelObject<TLabel, VImageDimension>
157 {
158 public:
162  typedef typename Superclass::LabelObjectType LabelObjectType;
166 
168  itkNewMacro(Self);
169 
171  itkTypeMacro(AttributesMapLabelObject, LabelObject);
172 
173  itkStaticConstMacro(ImageDimension, unsigned int, VImageDimension);
174 
177 
179  typedef TLabel LabelType;
180  typedef TAttributesValue AttributesValueType;
181 
182  // Convenient inherited typedefs
183  typedef typename Superclass::IndexType IndexType;
184  typedef typename Superclass::LineType LineType;
185  typedef typename Superclass::LengthType LengthType;
186 
188  typedef std::map<std::string, AttributesValueType> AttributesMapType;
189  typedef typename AttributesMapType::iterator AttributesMapIteratorType;
190  typedef typename AttributesMapType::const_iterator AttributesMapConstIteratorType;
191 
192  // The polygon corresponding to the label object
195 
200  void SetAttribute(const char * name, AttributesValueType value)
201  {
202  m_Attributes[name] = value;
203  }
204 
209  void SetAttribute(const std::string& name, AttributesValueType value)
210  {
211  this->SetAttribute(name.c_str(), value);
212  }
213 
217  AttributesValueType GetAttribute(const char * name) const
218  {
219  AttributesMapConstIteratorType it = m_Attributes.find(name);
220  if (it != m_Attributes.end())
221  {
222  return it->second;
223  }
224  else
225  {
226  itkExceptionMacro(<< "Could not find attribute named " << name);
227  }
228  }
229 
233  unsigned int GetNumberOfAttributes() const
234  {
235  return m_Attributes.size();
236  }
237 
241  std::vector<std::string> GetAvailableAttributes() const
242  {
243  std::vector<std::string> attributesNames;
244 
245  AttributesMapConstIteratorType it = m_Attributes.begin();
246 
247  while (it != m_Attributes.end())
248  {
249  attributesNames.push_back(it->first);
250  ++it;
251  }
252  return attributesNames;
253  }
254 
258  virtual void CopyAttributesFrom(const LabelObjectType * lo)
259  {
260  Superclass::CopyAttributesFrom(lo);
261 
262  // copy the data of the current type if possible
263  const Self * src = dynamic_cast<const Self *>(lo);
264  if (src == NULL)
265  {
266  return;
267  }
268  m_Attributes = src->m_Attributes;
269  }
270 
272  const PolygonType * GetPolygon() const
273  {
274  return m_Polygon;
275  }
276 
279  {
280  return m_Polygon;
281  }
282 
285  {
286  m_Polygon = p;
287  }
288 
289 protected:
291  AttributesMapLabelObject() : m_Attributes(), m_Polygon(PolygonType::New()) {}
294 
296  void PrintSelf(std::ostream& os, itk::Indent indent) const
297  {
298  Superclass::PrintSelf(os, indent);
299  os << indent << "Attributes: " << std::endl;
300  for (AttributesMapConstIteratorType it = m_Attributes.begin();
301  it != m_Attributes.end(); ++it)
302  {
303  os << indent << indent << it->first << " = " << it->second << std::endl;
304  }
305  }
306 private:
307  AttributesMapLabelObject(const Self &); //purposely not implemented
308  void operator =(const Self&); //purposely not implemented
309 
312 
316 };
317 
318 } // end namespace otb
319 #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:39
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)