OTB  6.7.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 #include <string>
36 
37 namespace otb
38 {
39 
40 namespace Functor
41 {
42 
53 template<class TLabelObject>
55 {
56 public:
57  // The Label Object typedef
58  typedef TLabelObject LabelObjectType;
59  typedef typename LabelObjectType::AttributesValueType AttributeValueType;
60 
66  inline const AttributeValueType operator ()(LabelObjectType * labelObject) const
67  {
68  return labelObject->GetAttribute(m_AttributeName.c_str());
69  }
70 
72  void SetAttributeName(const char * name)
73  {
74  m_AttributeName = name;
75  }
77  const char * GetAttributeName() const
78  {
79  return m_AttributeName.c_str();
80  }
81 
83  AttributesMapLabelObjectAccessor() : m_AttributeName("") {}
84 
87 
88 private:
90  std::string m_AttributeName;
91 };
92 
93 
101 template<class TLabelObject, class TMeasurementVector>
103 {
104 public:
105  typedef std::vector<std::string> AttributesListType;
106 
107  inline TMeasurementVector operator()(const TLabelObject * object) const
108  {
109  TMeasurementVector newSample(m_Attributes.size());
110 
111  unsigned int attrIndex = 0;
112  typename AttributesListType::const_iterator attrIt = m_Attributes.begin();
113  while(attrIt != m_Attributes.end())
114  {
115  newSample[attrIndex] = object->GetAttribute(attrIt->c_str());
116  ++attrIt;
117  ++attrIndex;
118  }
119  return newSample;
120  }
121 
123  void AddAttribute(const char * attr)
124  {
125  m_Attributes.push_back(attr);
126  }
127 
129  void RemoveAttribute(const char * attr)
130  {
131  AttributesListType::iterator elt = std::find(m_Attributes.begin(), m_Attributes.end(), attr);
132  if(elt!=m_Attributes.end())
133  {
134  m_Attributes.erase(elt);
135  }
136  }
138 
141  {
142  m_Attributes.clear();
143  }
144 
146  unsigned int GetNumberOfAttributes()
147  {
148  return m_Attributes.size();
149  }
150 
151 private:
153 };
154 
155 } // end namespace Functor
156 
172 template <class TLabel, unsigned int VImageDimension, class TAttributesValue>
173 class ITK_EXPORT AttributesMapLabelObject
174  : public itk::LabelObject<TLabel, VImageDimension>
175 {
176 public:
184 
186  itkNewMacro(Self);
187 
189  itkTypeMacro(AttributesMapLabelObject, LabelObject);
190 
191  itkStaticConstMacro(ImageDimension, unsigned int, VImageDimension);
192 
195 
197  typedef TLabel LabelType;
198  typedef TAttributesValue AttributesValueType;
199 
200  // Convenient inherited typedefs
202  typedef typename Superclass::LineType LineType;
204 
206  typedef std::map<std::string, AttributesValueType> AttributesMapType;
207  typedef typename AttributesMapType::iterator AttributesMapIteratorType;
208  typedef typename AttributesMapType::const_iterator AttributesMapConstIteratorType;
209 
210  // The polygon corresponding to the label object
213 
218  void SetAttribute(const char * name, AttributesValueType value)
219  {
220  m_Attributes[name] = value;
221  }
222 
227  void SetAttribute(const std::string& name, AttributesValueType value)
228  {
229  this->SetAttribute(name.c_str(), value);
230  }
231 
235  AttributesValueType GetAttribute(const char * name) const
236  {
237  AttributesMapConstIteratorType it = m_Attributes.find(name);
238  if (it != m_Attributes.end())
239  {
240  return it->second;
241  }
242  else
243  {
244  itkExceptionMacro(<< "Could not find attribute named " << name);
245  }
246  }
248 
252  unsigned int GetNumberOfAttributes() const
253  {
254  return m_Attributes.size();
255  }
256 
260  std::vector<std::string> GetAvailableAttributes() const
261  {
262  std::vector<std::string> attributesNames;
263 
264  AttributesMapConstIteratorType it = m_Attributes.begin();
265 
266  while (it != m_Attributes.end())
267  {
268  attributesNames.push_back(it->first);
269  ++it;
270  }
271  return attributesNames;
272  }
273 
277  virtual void CopyAttributesFrom(const LabelObjectType * lo)
278  {
279  Superclass::CopyAttributesFrom(lo);
280 
281  // copy the data of the current type if possible
282  const Self * src = dynamic_cast<const Self *>(lo);
283  if (src == nullptr)
284  {
285  return;
286  }
287  m_Attributes = src->m_Attributes;
288  }
289 
291  const PolygonType * GetPolygon() const
292  {
293  return m_Polygon;
294  }
295 
298  {
299  return m_Polygon;
300  }
301 
304  {
305  m_Polygon = p;
306  }
307 
308 protected:
310  AttributesMapLabelObject() : m_Attributes(), m_Polygon(PolygonType::New()) {}
311 
314 
316  void PrintSelf(std::ostream& os, itk::Indent indent) const override
317  {
318  Superclass::PrintSelf(os, indent);
319  os << indent << "Attributes: " << std::endl;
320  for (AttributesMapConstIteratorType it = m_Attributes.begin();
321  it != m_Attributes.end(); ++it)
322  {
323  os << indent << indent << it->first << " = " << it->second << std::endl;
324  }
325  }
326 private:
327  AttributesMapLabelObject(const Self &) = delete;
328  void operator =(const Self&) = delete;
330 
333 
337 };
338 
339 } // end namespace otb
340 #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
void PrintSelf(std::ostream &os, itk::Indent indent) const override
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)