Adaptors.h
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/classification/Adaptors.h
22 
23  \brief Classifiers adaptors.
24 */
25 
26 #ifndef __TERRALIB_CLASSIFICATION_INTERNAL_ADAPTORS_H
27 #define __TERRALIB_CLASSIFICATION_INTERNAL_ADAPTORS_H
28 
29 #include "Config.h"
30 #include "Exception.h"
31 
32 #include <cstddef>
33 #include <vector>
34 
35 namespace te
36 {
37  namespace cl
38  {
39  /*!
40  \class InputAdaptor
41  \brief Classifiers input data adaptor.
42 
43  */
44  template<typename DataType>
46  {
47  public:
48 
50 
52 
53  /*!
54  \brief Returns one feature value.
55  \param elementIndex Element index..
56  \param featureIndex Feature index.
57  \param featureValue The returned feature value.
58  */
59  virtual void getFeature(const unsigned int& elementIndex,
60  const unsigned int& featureIndex, DataType& featureValue ) const = 0;
61 
62  /*!
63  \brief Returns the total elements number.
64  */
65  virtual unsigned int getElementsCount() const = 0;
66 
67  /*!
68  \brief Returns the total features per element number.
69  */
70  virtual unsigned int getFeaturesCount() const = 0;
71  };
72 
73  /*!
74  \class OutputAdaptor
75  \brief Classifiers output data adaptor.
76 
77  */
78  template<typename DataType>
80  {
81  public:
82 
84 
86 
87  /*!
88  \brief Set one feature value.
89  \param elementIndex Element index..
90  \param featureIndex Feature index.
91  */
92  virtual void setFeature(const unsigned int& elementIndex,
93  const unsigned int& featureIndex, const DataType& value ) = 0;
94 
95  /*!
96  \brief Returns the total elements number.
97  */
98  virtual unsigned int getElementsCount() const = 0;
99 
100  /*!
101  \brief Returns the total features per element number.
102  */
103  virtual unsigned int getFeaturesCount() const = 0;
104  };
105 
106  /*!
107  \class ContainerInputAdaptor
108  \brief Classifiers container input data adaptor.
109 
110  */
111  template<typename DataType>
112  class VectorOfVectorsInputAdaptor : public InputAdaptor< DataType >
113  {
114  public:
115 
116  VectorOfVectorsInputAdaptor( const std::vector< std::vector< DataType > >& container )
117  : m_container( container )
118  {
119  // Checking consistency
120 
121  if( !container.empty() )
122  {
123  typename std::vector< std::vector< DataType > >::const_iterator it = container.begin();
124  const typename std::vector< std::vector< DataType > >::const_iterator itEnd = container.end();
125  const std::size_t baseFeaturesCount = container[ 0 ].size();
126 
127  while( it != itEnd )
128  {
129  if( it->size() != baseFeaturesCount )
130  {
131  throw Exception( "Features count inconsistency" );
132  }
133 
134  ++it;
135  }
136  }
137  };
138 
140 
141  // overload
142  void getFeature(const unsigned int& elementIndex, const unsigned int& featureIndex,
143  DataType& featureValue ) const
144  {
145  featureValue = m_container[ elementIndex ][ featureIndex ];
146  };
147 
148  // overload
149  unsigned int getElementsCount() const
150  {
151  return static_cast<unsigned int>(m_container.size());
152  };
153 
154  // overload
155  unsigned int getFeaturesCount() const
156  {
157  if( m_container.empty() )
158  {
159  return 0;
160  }
161  else
162  {
163  return static_cast<unsigned int>(m_container[0].size());
164  }
165  };
166 
167  protected :
168 
169  /*!
170  \brief Internal container reference.
171  */
172  const std::vector< std::vector< DataType > >& m_container;
173  };
174 
175  /*!
176  \class VectorOutputAdaptor
177  \brief Classifiers vector container output data adaptor.
178 
179  */
180  template< typename DataType >
181  class VectorOutputAdaptor : public te::cl::OutputAdaptor< DataType >
182  {
183  public:
184  explicit VectorOutputAdaptor( std::vector< DataType >& container )
185  : m_container( container )
186  {
187  };
188 
190 
191  // overload
192  void setFeature(const unsigned int& elementIndex,
193  const unsigned int&, const DataType& value )
194  {
195  m_container[ elementIndex ] = value;
196  }
197 
198  // overload
199  unsigned int getElementsCount() const
200  {
201  return m_container.size();
202  }
203 
204  // overload
205  unsigned int getFeaturesCount() const
206  {
207  return 1;
208  }
209 
210  protected :
211 
212  /*!
213  \brief Internal container reference.
214  */
215  std::vector< DataType >& m_container;
216  };
217 
218  } // end namespace cl
219 } // end namespace te
220 
221 #endif // __TERRALIB_CLASSIFICATION_INTERNAL_MAP_H
unsigned int getFeaturesCount() const
Returns the total features per element number.
Definition: Adaptors.h:205
Classifiers vector container output data adaptor.
Definition: Adaptors.h:181
Base exception class for plugin module.
Definition: Exception.h:42
std::vector< DataType > & m_container
Internal container reference.
Definition: Adaptors.h:215
const std::vector< std::vector< DataType > > & m_container
Internal container reference.
Definition: Adaptors.h:165
VectorOutputAdaptor(std::vector< DataType > &container)
Definition: Adaptors.h:184
void getFeature(const unsigned int &elementIndex, const unsigned int &featureIndex, DataType &featureValue) const
Returns one feature value.
Definition: Adaptors.h:142
Configuration flags for the Terrralib Classification module.
unsigned int getFeaturesCount() const
Returns the total features per element number.
Definition: Adaptors.h:155
Classifiers input data adaptor.
Definition: Adaptors.h:45
Classifiers output data adaptor.
Definition: Adaptors.h:79
URI C++ Library.
unsigned int getElementsCount() const
Returns the total elements number.
Definition: Adaptors.h:149
virtual unsigned int getElementsCount() const =0
Returns the total elements number.
virtual void getFeature(const unsigned int &elementIndex, const unsigned int &featureIndex, DataType &featureValue) const =0
Returns one feature value.
An exception class for the Classification module.
VectorOfVectorsInputAdaptor(const std::vector< std::vector< DataType > > &container)
Definition: Adaptors.h:116
unsigned int getElementsCount() const
Returns the total elements number.
Definition: Adaptors.h:199
virtual unsigned int getFeaturesCount() const =0
Returns the total features per element number.
void setFeature(const unsigned int &elementIndex, const unsigned int &, const DataType &value)
Set one feature value.
Definition: Adaptors.h:192