<commit_before>/*=========================================================================

  Program:   ORFEO Toolbox
  Language:  C++
  Date:      $Date$
  Version:   $Revision$


  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
  See OTBCopyright.txt for details.


     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/
#ifndef __otbAttributesMapLabelObject_h
#define __otbAttributesMapLabelObject_h

#include "itkLabelObject.h"
#include "itkLabelMap.h"
#include "otbPolygon.h"
#include <map>

namespace otb
{

namespace Functor
{

/** \class AttributesMapLabelObjectAccessor
 *  \brief Allows to acces a given field of an AttributesMapLabelObject
 *
 * The name of the attribute to retrieve can be set by using the
 * SetAttributeName method.
 *
 * \sa AttributesMapLabelObject
 */
template< class TLabelObject >
class ITK_EXPORT AttributesMapLabelObjectAccessor
{
public:
  // The Label Object typedef
  typedef TLabelObject                                  LabelObjectType;
  typedef typename LabelObjectType::AttributesValueType AttributeValueType;

  /**
   * Please note that no exception catching is done here.
   * \param labelObject The pointer to the label object
   * \return The attribute value.
   */
  inline const AttributeValueType operator()( LabelObjectType * labelObject ) const
    {
    return labelObject->GetAttribute(m_AttributeName.c_str());
    }
  
  /// Set the name of the attribute to retrieve
  void SetAttributeName(const char * name)
  {
    m_AttributeName = name;
  }
  /// Get the the name of the attribute to retrieve
  const char * GetAttributeName() const
  {
    return m_AttributeName.c_str();
  }

  /// Constructor
  AttributesMapLabelObjectAccessor() : m_AttributeName("") {}

  /// Destructor
  ~AttributesMapLabelObjectAccessor() {}

private:
  /// Name of the attribute to retrieve
  std::string m_AttributeName;
};

} // end namespace Functor

/** \class AttributesMapLabelObject
 *  \brief A LabelObject with a generic attributes map
 *
 *  This class derives from itk::LabelObject and extends it to
 *  store pairs of key,value (of type TAttributesValue) in an internal
 *  map container.
 *
 * As such it allows to store any custom attributes as necessary.
 *
 *\sa LabelObject, ShapeLabelObject, StatisticsLabelObject
 *
 * \ingroup DataRepresentation
 */
template < class TLabel, unsigned int VImageDimension, class TAttributesValue >
class ITK_EXPORT AttributesMapLabelObject
: public itk::LabelObject< TLabel, VImageDimension >
{
public:
  /** Standard class typedefs */
  typedef AttributesMapLabelObject                    Self;
  typedef itk::LabelObject< TLabel, VImageDimension > Superclass;
  typedef itk::SmartPointer<Self>                     Pointer;
  typedef itk::SmartPointer<const Self>               ConstPointer;
  typedef itk::WeakPointer <const Self>               ConstWeakPointer;

  /** Method for creation through the object factory. */
  itkNewMacro(Self);

  /** Run-time type information (and related methods). */
  itkTypeMacro(AttributesMapLabelObject, LabelObject);

  itkStaticConstMacro(ImageDimension, unsigned int, VImageDimension);

  /// Type of a label map using an AttributesMapLabelObject
  typedef itk::LabelMap< Self >                      LabelMapType;

  /// Template parameters typedef
  typedef TLabel                                     LabelType;
  typedef TAttributesValue                           AttributesValueType;
  
  // Convenient inherited typedefs
  typedef typename Superclass::IndexType             IndexType;
  typedef typename Superclass::LineType              LineType;
  typedef typename Superclass::LengthType            LengthType;
  typedef typename Superclass::LineContainerType     LineContainerType;

  /// Map container typedefs
  typedef std::map<std::string,AttributesValueType>  AttributesMapType;
  typedef typename AttributesMapType::iterator       AttributesMapIteratorType;
  typedef typename AttributesMapType::const_iterator AttributesMapConstIteratorType;

  // The polygon corresponding to the label object
  typedef Polygon<double>                            PolygonType;
  typedef typename PolygonType::Pointer              PolygonPointerType;

  /**
   * Set an attribute value.
   * If the key name already exists in the map, the value is overwritten.
   */
  void SetAttribute(const char * name, AttributesValueType value)
  {
    m_Attributes[name]=value;
  }

  /**
   * Returns the attribute corresponding to name
   */
  AttributesValueType GetAttribute(const char * name) const
  {
    AttributesMapConstIteratorType it = m_Attributes.find(name);
    if (it!=m_Attributes.end())
      {
      return it->second;
      }
    else
      {
      itkExceptionMacro(<<"Could not find attribute named "<<name<<".");
      }
  }

  /**
   * Returns the total number of attributes
   */
  unsigned int GetNumberOfAttributes() const
  {
    return m_Attributes.size();
  }

  /**
   * Returns the list of available attributes
   */
  std::vector<std::string> GetAvailableAttributes() const
  {
    std::vector<std::string> attributesNames;
    
    AttributesMapConstIteratorType it = m_Attributes.begin();

    while(it!=m_Attributes.end())
      {
      attributesNames.push_back(it->first);
      ++it;
      }
    return attributesNames;
  }
  
  /**
  * This method is overloaded to add the copy of the attributes map.
  */
  virtual void CopyAttributesFrom( const Self * src)
    {
    Superclass::CopyAttributesFrom( src );
      m_Attributes = src->m_Attributes;
   }

  /** Return the polygon (const version) */
  const PolygonType * GetPolygon() const
  {
    return m_Polygon;
  }

  /** Return the polygon (non const version) */
  PolygonType * GetPolygon()
  {
    return m_Polygon;
  }

  /** Set the polygon */
  void SetPolygon(PolygonType* p)
  {
    m_Polygon = p;
  }

protected:
  /** Constructor */
  AttributesMapLabelObject() : m_Attributes(), m_Polygon(PolygonType::New())
    {}
  /** Destructor */<commit_msg>BUG : Ensure calling CopyAttributesFrom via polymorphism really calls the overridden method<commit_after>/*=========================================================================

  Program:   ORFEO Toolbox
  Language:  C++
  Date:      $Date$
  Version:   $Revision$


  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
  See OTBCopyright.txt for details.


     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/
#ifndef __otbAttributesMapLabelObject_h
#define __otbAttributesMapLabelObject_h

#include "itkLabelObject.h"
#include "itkLabelMap.h"
#include "otbPolygon.h"
#include <map>

namespace otb
{

namespace Functor
{

/** \class AttributesMapLabelObjectAccessor
 *  \brief Allows to acces a given field of an AttributesMapLabelObject
 *
 * The name of the attribute to retrieve can be set by using the
 * SetAttributeName method.
 *
 * \sa AttributesMapLabelObject
 */
template< class TLabelObject >
class ITK_EXPORT AttributesMapLabelObjectAccessor
{
public:
  // The Label Object typedef
  typedef TLabelObject                                  LabelObjectType;
  typedef typename LabelObjectType::AttributesValueType AttributeValueType;

  /**
   * Please note that no exception catching is done here.
   * \param labelObject The pointer to the label object
   * \return The attribute value.
   */
  inline const AttributeValueType operator()( LabelObjectType * labelObject ) const
    {
    return labelObject->GetAttribute(m_AttributeName.c_str());
    }
  
  /// Set the name of the attribute to retrieve
  void SetAttributeName(const char * name)
  {
    m_AttributeName = name;
  }
  /// Get the the name of the attribute to retrieve
  const char * GetAttributeName() const
  {
    return m_AttributeName.c_str();
  }

  /// Constructor
  AttributesMapLabelObjectAccessor() : m_AttributeName("") {}

  /// Destructor
  ~AttributesMapLabelObjectAccessor() {}

private:
  /// Name of the attribute to retrieve
  std::string m_AttributeName;
};

} // end namespace Functor

/** \class AttributesMapLabelObject
 *  \brief A LabelObject with a generic attributes map
 *
 *  This class derives from itk::LabelObject and extends it to
 *  store pairs of key,value (of type TAttributesValue) in an internal
 *  map container.
 *
 * As such it allows to store any custom attributes as necessary.
 *
 *\sa LabelObject, ShapeLabelObject, StatisticsLabelObject
 *
 * \ingroup DataRepresentation
 */
template < class TLabel, unsigned int VImageDimension, class TAttributesValue >
class ITK_EXPORT AttributesMapLabelObject
: public itk::LabelObject< TLabel, VImageDimension >
{
public:
  /** Standard class typedefs */
  typedef AttributesMapLabelObject                    Self;
  typedef itk::LabelObject< TLabel, VImageDimension > Superclass;
  typedef typename Superclass::LabelObjectType        LabelObjectType;
  typedef itk::SmartPointer<Self>                     Pointer;
  typedef itk::SmartPointer<const Self>               ConstPointer;
  typedef itk::WeakPointer <const Self>               ConstWeakPointer;

  /** Method for creation through the object factory. */
  itkNewMacro(Self);

  /** Run-time type information (and related methods). */
  itkTypeMacro(AttributesMapLabelObject, LabelObject);

  itkStaticConstMacro(ImageDimension, unsigned int, VImageDimension);

  /// Type of a label map using an AttributesMapLabelObject
  typedef itk::LabelMap< Self >                      LabelMapType;

  /// Template parameters typedef
  typedef TLabel                                     LabelType;
  typedef TAttributesValue                           AttributesValueType;
  
  // Convenient inherited typedefs
  typedef typename Superclass::IndexType             IndexType;
  typedef typename Superclass::LineType              LineType;
  typedef typename Superclass::LengthType            LengthType;
  typedef typename Superclass::LineContainerType     LineContainerType;

  /// Map container typedefs
  typedef std::map<std::string,AttributesValueType>  AttributesMapType;
  typedef typename AttributesMapType::iterator       AttributesMapIteratorType;
  typedef typename AttributesMapType::const_iterator AttributesMapConstIteratorType;

  // The polygon corresponding to the label object
  typedef Polygon<double>                            PolygonType;
  typedef typename PolygonType::Pointer              PolygonPointerType;

  /**
   * Set an attribute value.
   * If the key name already exists in the map, the value is overwritten.
   */
  void SetAttribute(const char * name, AttributesValueType value)
  {
    m_Attributes[name]=value;
  }

  /**
   * Returns the attribute corresponding to name
   */
  AttributesValueType GetAttribute(const char * name) const
  {
    AttributesMapConstIteratorType it = m_Attributes.find(name);
    if (it!=m_Attributes.end())
      {
      return it->second;
      }
    else
      {
      itkExceptionMacro(<<"Could not find attribute named "<<name<<".");
      }
  }

  /**
   * Returns the total number of attributes
   */
  unsigned int GetNumberOfAttributes() const
  {
    return m_Attributes.size();
  }

  /**
   * Returns the list of available attributes
   */
  std::vector<std::string> GetAvailableAttributes() const
  {
    std::vector<std::string> attributesNames;
    
    AttributesMapConstIteratorType it = m_Attributes.begin();

    while(it!=m_Attributes.end())
      {
      attributesNames.push_back(it->first);
      ++it;
      }
    return attributesNames;
  }
  
  /**
  * This method is overloaded to add the copy of the attributes map.
  */
  virtual void CopyAttributesFrom( const LabelObjectType * lo)
    {
    Superclass::CopyAttributesFrom( lo );

    // copy the data of the current type if possible
    const Self * src = dynamic_cast<const Self *>( lo );
    if( src == NULL )
      {
      return;
      }
      m_Attributes = src->m_Attributes;
    }

  /** Return the polygon (const version) */
  const PolygonType * GetPolygon() const
  {
    return m_Polygon;
  }

  /** Return the polygon (non const version) */
  PolygonType * GetPolygon()
  {
    return m_Polygon;
  }

  /** Set the polygon */
  void SetPolygon(PolygonType* p)
  {
    m_Polygon = p;
  }

protected:
  /** Constructor */
  AttributesMapLabelObject() : m_Attributes(), m_Polygon(PolygonType::New())
    {}
  /** Destructor */
{'fraction_non_alphanumeric': 0.10395807259073843, 'fraction_numerical': 0.0, 'mean_word_length': 3.2236537826230593, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1201624', 'n_tokens_mistral': 3493, 'n_tokens_neox': 3365, 'n_words': 1037}