OTB  6.1.0
Orfeo Toolbox
otbAttributesMapLabelObject.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2017 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 
36 namespace otb
37 {
38 
39 namespace Functor
40 {
41 
52 template<class TLabelObject>
54 {
55 public:
56  // The Label Object typedef
57  typedef TLabelObject LabelObjectType;
58  typedef typename LabelObjectType::AttributesValueType AttributeValueType;
59 
65  inline const AttributeValueType operator ()(LabelObjectType * labelObject) const
66  {
67  return labelObject->GetAttribute(m_AttributeName.c_str());
68  }
69 
71  void SetAttributeName(const char * name)
72  {
73  m_AttributeName = name;
74  }
76  const char * GetAttributeName() const
77  {
78  return m_AttributeName.c_str();
79  }
80 
82  AttributesMapLabelObjectAccessor() : m_AttributeName("") {}
83 
86 
87 private:
89  std::string m_AttributeName;
90 };
91 
92 
100 template<class TLabelObject, class TMeasurementVector>
102 {
103 public:
104  typedef std::vector<std::string> AttributesListType;
105 
106  inline TMeasurementVector operator()(const TLabelObject * object) const
107  {
108  TMeasurementVector newSample(m_Attributes.size());
109 
110  unsigned int attrIndex = 0;
111  typename AttributesListType::const_iterator attrIt = m_Attributes.begin();
112  while(attrIt != m_Attributes.end())
113  {
114  newSample[attrIndex] = object->GetAttribute(attrIt->c_str());
115  ++attrIt;
116  ++attrIndex;
117  }
118  return newSample;
119  }
120 
122  void AddAttribute(const char * attr)
123  {
124  m_Attributes.push_back(attr);
125  }
126 
128  void RemoveAttribute(const char * attr)
129  {
130  AttributesListType::iterator elt = std::find(m_Attributes.begin(), m_Attributes.end(), attr);
131  if(elt!=m_Attributes.end())
132  {
133  m_Attributes.erase(elt);
134  }
135  }
137 
140  {
141  m_Attributes.clear();
142  }
143 
145  unsigned int GetNumberOfAttributes()
146  {
147  return m_Attributes.size();
148  }
149 
150 private:
152 };
153 
154 } // end namespace Functor
155 
171 template <class TLabel, unsigned int VImageDimension, class TAttributesValue>
172 class ITK_EXPORT AttributesMapLabelObject
173  : public itk::LabelObject<TLabel, VImageDimension>
174 {
175 public:
183 
185  itkNewMacro(Self);
186 
188  itkTypeMacro(AttributesMapLabelObject, LabelObject);
189 
190  itkStaticConstMacro(ImageDimension, unsigned int, VImageDimension);
191 
194 
196  typedef TLabel LabelType;
197  typedef TAttributesValue AttributesValueType;
198 
199  // Convenient inherited typedefs
201  typedef typename Superclass::LineType LineType;
203 
205  typedef std::map<std::string, AttributesValueType> AttributesMapType;
206  typedef typename AttributesMapType::iterator AttributesMapIteratorType;
207  typedef typename AttributesMapType::const_iterator AttributesMapConstIteratorType;
208 
209  // The polygon corresponding to the label object
212 
217  void SetAttribute(const char * name, AttributesValueType value)
218  {
219  m_Attributes[name] = value;
220  }
221 
226  void SetAttribute(const std::string& name, AttributesValueType value)
227  {
228  this->SetAttribute(name.c_str(), value);
229  }
230 
234  AttributesValueType GetAttribute(const char * name) const
235  {
236  AttributesMapConstIteratorType it = m_Attributes.find(name);
237  if (it != m_Attributes.end())
238  {
239  return it->second;
240  }
241  else
242  {
243  itkExceptionMacro(<< "Could not find attribute named " << name);
244  }
245  }
247 
251  unsigned int GetNumberOfAttributes() const
252  {
253  return m_Attributes.size();
254  }
255 
259  std::vector<std::string> GetAvailableAttributes() const
260  {
261  std::vector<std::string> attributesNames;
262 
263  AttributesMapConstIteratorType it = m_Attributes.begin();
264 
265  while (it != m_Attributes.end())
266  {
267  attributesNames.push_back(it->first);
268  ++it;
269  }
270  return attributesNames;
271  }
272 
276  virtual void CopyAttributesFrom(const LabelObjectType * lo)
277  {
278  Superclass::CopyAttributesFrom(lo);
279 
280  // copy the data of the current type if possible
281  const Self * src = dynamic_cast<const Self *>(lo);
282  if (src == ITK_NULLPTR)
283  {
284  return;
285  }
286  m_Attributes = src->m_Attributes;
287  }
288 
290  const PolygonType * GetPolygon() const
291  {
292  return m_Polygon;
293  }
294 
297  {
298  return m_Polygon;
299  }
300 
303  {
304  m_Polygon = p;
305  }
306 
307 protected:
309  AttributesMapLabelObject() : m_Attributes(), m_Polygon(PolygonType::New()) {}
310 
312  ~AttributesMapLabelObject() ITK_OVERRIDE {}
313 
315  void PrintSelf(std::ostream& os, itk::Indent indent) const ITK_OVERRIDE
316  {
317  Superclass::PrintSelf(os, indent);
318  os << indent << "Attributes: " << std::endl;
319  for (AttributesMapConstIteratorType it = m_Attributes.begin();
320  it != m_Attributes.end(); ++it)
321  {
322  os << indent << indent << it->first << " = " << it->second << std::endl;
323  }
324  }
325 private:
326  AttributesMapLabelObject(const Self &); //purposely not implemented
327  void operator =(const Self&); //purposely not implemented
329 
332 
336 };
337 
338 } // end namespace otb
339 #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:44
itk::LabelObject< TLabel, VImageDimension > Superclass
virtual void CopyAttributesFrom(const LabelObjectType *lo)
Superclass::LabelObjectType LabelObjectType
itk::WeakPointer< const Self > ConstWeakPointer
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)
void PrintSelf(std::ostream &os, itk::Indent indent) const ITK_OVERRIDE