Orfeo Toolbox  3.16
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 "itkLabelObject.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>
156  : public itk::LabelObject<TLabel, VImageDimension>
157 {
158 public:
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
184  typedef typename Superclass::LineType LineType;
187 
189  typedef std::map<std::string, AttributesValueType> AttributesMapType;
190  typedef typename AttributesMapType::iterator AttributesMapIteratorType;
191  typedef typename AttributesMapType::const_iterator AttributesMapConstIteratorType;
192 
193  // The polygon corresponding to the label object
196 
201  void SetAttribute(const char * name, AttributesValueType value)
202  {
203  m_Attributes[name] = value;
204  }
205 
210  void SetAttribute(const std::string& name, AttributesValueType value)
211  {
212  this->SetAttribute(name.c_str(), value);
213  }
214 
218  AttributesValueType GetAttribute(const char * name) const
219  {
220  AttributesMapConstIteratorType it = m_Attributes.find(name);
221  if (it != m_Attributes.end())
222  {
223  return it->second;
224  }
225  else
226  {
227  itkExceptionMacro(<< "Could not find attribute named " << name << ".");
228  }
229  }
230 
234  unsigned int GetNumberOfAttributes() const
235  {
236  return m_Attributes.size();
237  }
238 
242  std::vector<std::string> GetAvailableAttributes() const
243  {
244  std::vector<std::string> attributesNames;
245 
246  AttributesMapConstIteratorType it = m_Attributes.begin();
247 
248  while (it != m_Attributes.end())
249  {
250  attributesNames.push_back(it->first);
251  ++it;
252  }
253  return attributesNames;
254  }
255 
259  virtual void CopyAttributesFrom(const LabelObjectType * lo)
260  {
261  Superclass::CopyAttributesFrom(lo);
262 
263  // copy the data of the current type if possible
264  const Self * src = dynamic_cast<const Self *>(lo);
265  if (src == NULL)
266  {
267  return;
268  }
269  m_Attributes = src->m_Attributes;
270  }
271 
273  const PolygonType * GetPolygon() const
274  {
275  return m_Polygon;
276  }
277 
279  PolygonType * GetPolygon()
280  {
281  return m_Polygon;
282  }
283 
285  void SetPolygon(PolygonType* p)
286  {
287  m_Polygon = p;
288  }
289 
290 protected:
292  AttributesMapLabelObject() : m_Attributes(), m_Polygon(PolygonType::New()) {}
295 
297  void PrintSelf(std::ostream& os, itk::Indent indent) const
298  {
299  Superclass::PrintSelf(os, indent);
300  os << indent << "Attributes: " << std::endl;
301  for (AttributesMapConstIteratorType it = m_Attributes.begin();
302  it != m_Attributes.end(); ++it)
303  {
304  os << indent << indent << it->first << " = " << it->second << std::endl;
305  }
306  }
307 private:
308  AttributesMapLabelObject(const Self &); //purposely not implemented
309  void operator =(const Self&); //purposely not implemented
310 
313 
317 };
318 
319 } // end namespace otb
320 #endif

Generated at Sun Feb 3 2013 00:16:34 for Orfeo Toolbox with doxygen 1.8.1.1