brainrat-private  5.1.2
measurevector.h
Go to the documentation of this file.
1 /* Copyright (C) 2000-2013 CEA
2  *
3  * This software and supporting documentation were developed by
4  * bioPICSEL
5  * CEA/DSV/I²BM/MIRCen/LMN, Batiment 61,
6  * 18, route du Panorama
7  * 92265 Fontenay-aux-Roses
8  * France
9  */
10 #ifndef BRAINRAT_DATA_MEASUREVECTOR_H
11 #define BRAINRAT_DATA_MEASUREVECTOR_H
12 
13 #include <sstream>
14 #include <vector>
15 #include <math.h>
16 
17 #include <aims/def/assert.h>
18 #include <cartobase/object/object.h>
19 #include <cartobase/object/property.h>
20 #include <cartobase/smart/rcptr.h>
21 #include <brainrat/object/array.h>
22 
23 namespace bio {
24 
25  template <typename T>
27  {
28  public:
29  typedef T value_type;
31  typedef T* pointer;
33  typedef T* iterator;
35  typedef const T* const_iterator;
37  typedef T& reference;
39  typedef const T& const_reference;
41  typedef size_t size_type;
43  typedef ptrdiff_t difference_type;
44 
45  // Constructors
46  BaseMeasureVector( const size_type size, const T* = NULL );
48 
49 
50  iterator begin();
51  const_iterator begin() const;
52  iterator end();
53  const_iterator end() const;
54 
55  // Destructor does nothing
57  delete [] _data;
58  }
59 
60 
63 
64  const size_type size() const;
65  T &operator [] ( unsigned x );
66  T &operator [] ( unsigned x ) const;
68  BaseMeasureVector<T>& operator = (const T &val);
71  BaseMeasureVector<T>& operator *= (const T &val);
72  BaseMeasureVector<T>& operator /= (const T &val);
73 
74  double distance (const BaseMeasureVector<T> &aa) const;
75 
76 
77  // @}
78 
79  private :
80  typename BaseMeasureVector<T>::size_type _size;
81  T * _data;
82  };
83 
84  template <typename T>
85  inline
87  return _size;
88  }
89 
90  template <typename T>
91  inline
92  BaseMeasureVector<T>::BaseMeasureVector ( const size_type size, const T* values )
93  {
94  _size = size;
95  _data = new T[size];
96  for (int32_t i = 0; i < this->size(); i++)
97  if (values)
98  (&*_data)[i] = values[i];
99  else
100  (&*_data)[i] = (T)0;
101  }
102 
103  template <typename T>
104  inline
106  {
107  _size = other.size();
108  _data = new T[other.size()];
109  typename BaseMeasureVector::iterator it2;
110  typename BaseMeasureVector::const_iterator it1, e = other.end();
111 
112  for(it1 = other.begin(), it2 = begin(); it1 < e; it1++, it2++ ) {
113  // Copy data
114  *it2 = *it1;
115  }
116  }
117 
118  template<typename T>
119  inline
121  return (&*_data);
122  }
123 
124 
125  template<typename T>
126  inline
128  return (&*_data);
129  }
130 
131 
132  template<typename T>
133  inline
135  return (&*_data) + size();
136  }
137 
138 
139  template<typename T>
140  inline
142  return (&*_data) + size();
143  }
144 
145  template<typename T>
146  inline
148  ASSERT(x < size());
149  return (&*_data)[x];
150  }
151 
152  template<typename T>
153  inline
154  T &BaseMeasureVector<T>::operator [] ( unsigned x ) const {
155  ASSERT(x < size());
156  return (&*_data)[x];
157  }
158 
159 
160  template<typename T>
161  inline
163  ASSERT(size() == other.size());
164  for (int32_t i = 0; i < size(); i++)
165  (*this)[i] = other[i];
166 
167  return (*this);
168  }
169 
170  template<typename T>
171  inline
173  for (int32_t i = 0; i < size(); i++)
174  (*this)[i] = val;
175 
176  return (*this);
177  }
178 
179  template<typename T>
180  inline
182  ASSERT(size() == other.size());
183  for (int32_t i = 0; i < size(); i++)
184  (*this)[i] += other[i];
185 
186  return (*this);
187  }
188 
189  template<typename T>
190  inline
192  ASSERT(size() == other.size());
193  for (int32_t i = 0; i < size(); i++)
194  (*this)[i] -= other[i];
195 
196  return (*this);
197  }
198 
199  template<typename T>
200  inline
202  for (int32_t i = 0; i < size(); i++)
203  (*this)[i] *= val;
204 
205  return(*this);
206  }
207 
208  template<typename T>
209  inline
211  ASSERT(val != 0);
212  for (int32_t i = 0; i < size(); i++)
213  (*this)[i] /= val;
214 
215  return(*this);
216  }
217 
218  // Euclidian distance
219  template<typename T>
220  inline
222  double result = 0.;
223  for (int32_t i = 0; i < size(); i++) {
224  result += (aa[i] - (*this)[i]) * (aa[i] - (*this)[i]);
225  }
226 
227  return( sqrt( result ) );
228  }
229 
230  template<typename T>
231  inline
233  const BaseMeasureVector<T> &bb) {
234  BaseMeasureVector<T> a(aa);
235  a += bb;
236  return a;
237  }
238 
239  template<typename T>
240  inline
242  const BaseMeasureVector<T> &bb) {
243  BaseMeasureVector<T> a(aa);
244  a -= bb;
245  return a;
246  }
247 
248 
249  template<typename T>
250  inline
252  const T &bb) {
253  BaseMeasureVector<T> a(aa);
254  a *= bb;
255  return a;
256  }
257 
258  template<typename T>
259  inline
261  const BaseMeasureVector<T> &bb) {
262  return (bb * aa);
263  }
264 
265  template<typename T>
266  inline
268  const T &bb) {
269  BaseMeasureVector<T> a(aa);
270  a /= bb;
271  return a;
272  }
273 
275 
276  template<typename T>
277  inline
278  std::ostream& operator << (std::ostream &out, const bio::BaseMeasureVector<T> &aa) {
279  out << '(';
280  for (int32_t i = 0; i < aa.size(); i++) {
281  out << carto::toString(aa[i]);
282  if (i < (aa.size() - 1))
283  out << ',';
284  }
285  out << ')';
286 
287  return out;
288  }
289 
290 } // namespace bio
291 
292 template< typename T >
293 inline
294 void printVector( std::ostream& os, const std::vector<T> & thing )
295 {
296  typename std::vector<T>::const_iterator i, e = thing.end();
297  os << "[ ";
298  for(i = thing.begin(); i < e; ++i)
299  {
300  os << *i;
301  if (i < (e - 1))
302  os << ", ";
303  }
304  os << " ]";
305 }
306 
307 namespace carto {
308 
309  inline std::ostream& operator << ( std::ostream& os, const carto::Array<double> & thing )
310  {
311  printVector<double>(os, thing);
312  return os;
313  }
314 
315  inline std::ostream& operator << ( std::ostream& os, const carto::Array<int> & thing )
316  {
317  printVector<int>(os, thing);
318  return os;
319  }
320 
321  DECLARE_GENERIC_OBJECT_TYPE( carto::Array<double> )
322  DECLARE_GENERIC_OBJECT_TYPE( carto::Array<double>::interfaced_iterator )
323  DECLARE_GENERIC_OBJECT_TYPE( carto::Array<float> )
324  DECLARE_GENERIC_OBJECT_TYPE( carto::Array<float>::interfaced_iterator )
325  DECLARE_GENERIC_OBJECT_TYPE( carto::Array<int> )
326  DECLARE_GENERIC_OBJECT_TYPE( carto::Array<int>::interfaced_iterator )
327  DECLARE_GENERIC_OBJECT_TYPE( carto::Array<carto::Array<int> > )
328  DECLARE_GENERIC_OBJECT_TYPE( carto::Array<carto::Array<int> >::interfaced_iterator )
329  DECLARE_GENERIC_OBJECT_TYPE( carto::Array<carto::Array<carto::Array<int> > > )
330  DECLARE_GENERIC_OBJECT_TYPE( carto::Array<carto::Array<carto::Array<int> > >::interfaced_iterator )
331 }
332 
333 #endif
334 
335 
336 
337 
T & operator[](unsigned x)
const T * const_iterator
basic constant iterator
Definition: measurevector.h:35
double distance(const BaseMeasureVector< T > &aa) const
BaseMeasureVector< T > & operator-=(const BaseMeasureVector< T > &other)
T * pointer
basic pointer
Definition: measurevector.h:31
BaseMeasureVector< T > & operator*=(const T &val)
BaseMeasureVector< T > & operator+=(const BaseMeasureVector< T > &other)
BaseMeasureVector< T > & operator/=(const T &val)
T * iterator
basic iterator
Definition: measurevector.h:33
BaseMeasureVector(const size_type size, const T *=NULL)
Definition: measurevector.h:92
size_t size_type
size of the basic type
Definition: measurevector.h:41
BaseMeasureVector< T > & operator=(const BaseMeasureVector< T > &other)
const T & const_reference
basic constant reference
Definition: measurevector.h:39
ptrdiff_t difference_type
difference type
Definition: measurevector.h:43
T & reference
basic reference
Definition: measurevector.h:37
const size_type size() const
Definition: measurevector.h:86
void printVector(std::ostream &os, const std::vector< T > &thing)
ImageProcessors<AimsRGB, double> p(data, mask, "rgbm", options, ImageProcessorMode::Init); ImageProce...
Definition: classes.h:25
std::ostream & operator<<(std::ostream &os, const bio::BVclass &thing)
bio::BaseMeasureVector< double > MeasureVector
BaseMeasureVector< T > operator/(const BaseMeasureVector< T > &aa, const T &bb)
BaseMeasureVector< T > operator-(const BaseMeasureVector< T > &aa, const BaseMeasureVector< T > &bb)
BaseMeasureVector< T > operator+(const BaseMeasureVector< T > &aa, const BaseMeasureVector< T > &bb)
BaseMeasureVector< T > operator*(const BaseMeasureVector< T > &aa, const T &bb)
std::ostream & operator<<(std::ostream &os, const carto::Array< bio::BVclass > &thing)
Definition: classes.h:62