iRoCS Toolbox  1.1.0
BasicFV.hh
Go to the documentation of this file.
1 /**************************************************************************
2  *
3  * Copyright (C) 2004-2015 Olaf Ronneberger, Florian Pigorsch, Jörg Mechnich,
4  * Thorsten Falk
5  *
6  * Image Analysis Lab, University of Freiburg, Germany
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  *
22  **************************************************************************/
23 
24 /**************************************************************************
25 ** Title: basic feature vector
26 ** $RCSfile$
27 ** $Revision: 5057 $$Name$
28 ** $Date: 2013-04-05 13:34:53 +0200 (Fri, 05 Apr 2013) $
29 ** Copyright: LGPL $Author: tschmidt $
30 ** Description:
31 **
32 **
33 **
34 **-------------------------------------------------------------------------
35 **
36 ** $Log$
37 ** Revision 1.2 2006/04/10 15:16:42 ronneber
38 ** - added Constructor from std::vector<double>
39 ** - added operators +=, *=, +, -
40 **
41 ** Revision 1.1 2004/08/26 08:36:58 ronneber
42 ** initital import
43 **
44 ** Revision 1.7 2003/05/19 10:49:17 ronneber
45 ** - added readWithoutLabel()
46 **
47 ** Revision 1.6 2002/06/06 07:00:20 ronneber
48 ** - made again compilable for gcc-2.95.4 (has no std::isspace ...)
49 **
50 ** Revision 1.5 2002/06/05 17:18:31 mechnich
51 ** Made minor corrections for compilation under Win32
52 **
53 ** Revision 1.4 2002/05/14 10:11:19 ronneber
54 ** - added writeWithoutLabel()
55 **
56 ** Revision 1.3 2002/05/13 16:15:00 ronneber
57 ** - removed unused tmpLabel stuff
58 **
59 ** Revision 1.2 2002/04/25 06:27:44 pigorsch
60 ** apapted to new directory structure
61 **
62 ** Revision 1.1 2002/03/26 12:44:02 ronneber
63 ** restructured for autoconf
64 **
65 ** Revision 1.6 2002/03/13 14:20:32 pigorsch
66 ** *** empty log message ***
67 **
68 ** Revision 1.5 2002/03/11 17:15:47 mechnich
69 ** modified some expressions to increase standard conformity (for compilation under Win32)
70 **
71 ** Revision 1.4 2002/01/21 13:47:36 pigorsch
72 ** * changed type of label from double to int
73 ** * fixed input function
74 **
75 ** Revision 1.3 2002/01/17 09:27:41 pigorsch
76 ** * fixed input/output functions
77 **
78 ** Revision 1.2 2002/01/17 09:20:07 pigorsch
79 ** * renamed p_features to pFeatures
80 ** p_tmplabel to pTmpLabel
81 ** p_label to pLabel
82 ** p_squareValid to pSquareValid
83 ** p_square to pSquare
84 **
85 ** Revision 1.1 2002/01/17 09:06:53 pigorsch
86 ** * initial revision
87 ** * BasicFeatureVector is renamed to BasicFV
88 ** * added input/output support
89 **
90 **
91 **
92 **
93 **
94 **************************************************************************/
95 
96 #ifndef BASICFV_HH
97 #define BASICFV_HH
98 
99 #ifdef HAVE_CONFIG_H
100 #include <config.hh>
101 #endif
102 
103 #include <list>
104 #include <string>
105 #include <vector>
106 #include <iostream>
107 #include <cctype>
108 using std::isspace; // sorry gcc 2.95.4 has no isspace in namespace std
109 
110 #include "adjustUniqueIDs.hh"
111 #include "svm_defines.hh"
112 
113 #ifdef _OPENMP
114 #include <omp.h>
115 #endif
116 
117 namespace svt
118 {
119  class BasicFV
120  {
121  public:
122  typedef std::vector<double>::iterator iterator;
123  typedef std::vector<double>::const_iterator const_iterator;
124  typedef std::vector<double>::reference reference;
125  typedef std::vector<double>::const_reference const_reference;
126  typedef std::vector<double>::size_type size_type;
127 
129  : pLabel(0),
130  _uniqueID( MAX_BELIEVABLE_UNIQUE_ID + 1), /* ensure that user specifies
131  unique ID before it is used */
132  pSquareValid(false),
133  pSquare(0)
134  {
135 #ifdef _OPENMP
136  omp_init_lock(&_pSquareMutexLock);
137 #endif
138  };
139 
140  BasicFV( std::vector<double> features,
141  double label = 0,
143  : pFeatures( features),
144  pLabel( label),
145  _uniqueID( uniqueID),
146  pSquareValid( false),
147  pSquare(0)
148  {
149 #ifdef _OPENMP
150  omp_init_lock(&_pSquareMutexLock);
151 #endif
152  }
153 
154  BasicFV(BasicFV const &fv)
155  : pFeatures(fv.pFeatures),
156  pLabel(fv.pLabel),
157  _uniqueID(fv._uniqueID),
158  pSquareValid(fv.pSquareValid),
159  pSquare(fv.pSquare)
160  {
161 #ifdef _OPENMP
162  omp_init_lock(&_pSquareMutexLock);
163 #endif
164  };
165 
167  {
168 #ifdef _OPENMP
169  omp_set_lock(&_pSquareMutexLock);
170 #endif
171  pFeatures = fv.pFeatures;
172  pLabel = fv.pLabel;
173  _uniqueID = fv._uniqueID;
174  pSquareValid = fv.pSquareValid;
175  pSquare = fv.pSquare;
176 #ifdef _OPENMP
177  omp_unset_lock(&_pSquareMutexLock);
178 #endif
179  return *this;
180  };
181 
183  {
184 #ifdef _OPENMP
185  omp_destroy_lock(&_pSquareMutexLock);
186 #endif
187  }
188 
189  void setLabel( double value)
190  {
191  pLabel=value;
192  };
193 
194  double getLabel() const
195  {
196  return pLabel;
197  };
198 
199  void setUniqueID( unsigned int uid)
200  {
201  _uniqueID = uid;
202  }
203 
204 
205  unsigned int uniqueID() const
206  {
207  return _uniqueID;
208  }
209 
210 
211  reference operator[](
212  int index)
213  {
214 #ifdef _OPENMP
215  omp_set_lock(&_pSquareMutexLock);
216 #endif
217  pSquareValid=false;
218 #ifdef _OPENMP
219  omp_unset_lock(&_pSquareMutexLock);
220 #endif
221  return pFeatures[index];
222  };
223 
224  const_reference operator[](
225  int index) const
226  {
227  return pFeatures[index];
228  };
229 
230  const_iterator begin() const
231  {
232  return pFeatures.begin();
233  };
234  iterator begin()
235  {
236 #ifdef _OPENMP
237  omp_set_lock(&_pSquareMutexLock);
238 #endif
239  pSquareValid=false;
240 #ifdef _OPENMP
241  omp_unset_lock(&_pSquareMutexLock);
242 #endif
243  return pFeatures.begin();
244  };
245 
246  const_iterator end() const
247  {
248  return pFeatures.end();
249  };
250  iterator end()
251  {
252 #ifdef _OPENMP
253  omp_set_lock(&_pSquareMutexLock);
254 #endif
255  pSquareValid=false;
256 #ifdef _OPENMP
257  omp_unset_lock(&_pSquareMutexLock);
258 #endif
259  return pFeatures.end();
260  };
261 
262  size_type size() const
263  {
264  return pFeatures.size();
265  };
266 
267  void resize(
268  size_type newSize)
269  {
270 #ifdef _OPENMP
271  omp_set_lock(&_pSquareMutexLock);
272 #endif
273  pFeatures.resize(newSize);
274  pSquareValid=false;
275 #ifdef _OPENMP
276  omp_unset_lock(&_pSquareMutexLock);
277 #endif
278  };
279 
280  void setZero() // fixme: STL name nachgucken
281  {
282 #ifdef _OPENMP
283  omp_set_lock(&_pSquareMutexLock);
284 #endif
285  for (iterator p=begin(); p!=end(); )
286  {
287  *p=0.;
288  ++p;
289  }
290  pSquareValid=false;
291 #ifdef _OPENMP
292  omp_unset_lock(&_pSquareMutexLock);
293 #endif
294  };
295 
296  double square() const
297  {
298 #ifdef _OPENMP
299  omp_set_lock(&_pSquareMutexLock);
300 #endif
301  if(!pSquareValid)
302  {
303  pSquare=0.;
304  for (const_iterator p=begin();
305  p!=end();
306  )
307  {
308  pSquare+=*p * *p;
309  ++p;
310  }
311 
312  pSquareValid=true;
313  }
314 #ifdef _OPENMP
315  omp_unset_lock(&_pSquareMutexLock);
316 #endif
317 
318  return pSquare;
319  };
320 
321  double dotProduct(
322  const svt::BasicFV& fv) const
323  {
324  double sum=0.;
325 
326  const_iterator i=begin();
327  const_iterator j=fv.begin();
328  for (; i!=end(); )
329  {
330  sum+=(*i) * (*j);
331  ++i;
332  ++j;
333  }
334 
335  return sum;
336  };
337 
338  void readWithoutLabel( std::istream& is)
339  {
340 #ifdef _OPENMP
341  omp_set_lock(&_pSquareMutexLock);
342 #endif
343  pFeatures.clear();
344  pSquareValid=false;
345 
346  while( is.good())
347  {
348  //skip whitespace, stop if non-number character or newline occurs
349  char c;
350  do
351  {
352  if( is.rdbuf()->sgetc() == EOF)
353  {
354 #ifdef _OPENMP
355  omp_unset_lock(&_pSquareMutexLock);
356 #endif
357  return;
358  }
359  is.get(c);
360  if( !is)
361  {
362 #ifdef _OPENMP
363  omp_unset_lock(&_pSquareMutexLock);
364 #endif
365  return;
366  }
367  } while( isspace(c) && c != '\n');
368 
369  is.putback(c);
370  if( !isdigit(c) && c != '-' && c != '+' && c != '.')
371  {
372 #ifdef _OPENMP
373  omp_unset_lock(&_pSquareMutexLock);
374 #endif
375  return;
376  }
377  double value;
378  is >> value;
379  pFeatures.push_back(value);
380  }
381 #ifdef _OPENMP
382  omp_unset_lock(&_pSquareMutexLock);
383 #endif
384  }
385 
386 
387  void writeWithoutLabel( std::ostream& os) const
388  {
389  for (svt::BasicFV::const_iterator p = pFeatures.begin();
390  p != pFeatures.end(); ++p )
391  {
392  os << " " << *p;
393  }
394  }
395 
396  static const char* helpPipeFormat()
397  {
398  return "<label><ws><feature_0><ws><feature_1>...<ws><feature_n>\n"
399  "where <ws> is any number of white spaces except for newline\n"
400  "example:\n"
401  "4 0.123 2.432 42.0 137.0815 24.35";
402  }
403 
404 
405  bool operator==( const BasicFV& fv) const
406  {
407  return( pFeatures == fv.pFeatures);
408  }
409 
410  void operator+=( const BasicFV& fv)
411  {
412 #ifdef _OPENMP
413  omp_set_lock(&_pSquareMutexLock);
414 #endif
415  size_t n = size();
416  BasicFV::iterator resultP = begin();
417  BasicFV::const_iterator fvP = fv.begin();
418  for( size_t i = 0; i < n; ++i)
419  {
420  *resultP += *fvP;
421  ++resultP;
422  ++fvP;
423  }
424  pSquareValid=false;
425 #ifdef _OPENMP
426  omp_unset_lock(&_pSquareMutexLock);
427 #endif
428  }
429 
430  void operator*=( double factor)
431  {
432 #ifdef _OPENMP
433  omp_set_lock(&_pSquareMutexLock);
434 #endif
435  size_t n = size();
436  BasicFV::iterator resultP = begin();
437  for( size_t i = 0; i < n; ++i)
438  {
439  *resultP *= factor;
440  ++resultP;
441  }
442 #ifdef _OPENMP
443  omp_unset_lock(&_pSquareMutexLock);
444 #endif
445  }
446 
447 
448 
449  friend std::ostream& operator<<(std::ostream& os, const svt::BasicFV& fv);
450  friend std::istream& operator>>(std::istream& is, svt::BasicFV& fv);
451  private:
452  std::vector<double> pFeatures;
453  double pLabel;
454  unsigned int _uniqueID;
455 
456 #ifdef _OPENMP
457  mutable omp_lock_t _pSquareMutexLock;
458 #endif
459  mutable bool pSquareValid;
460  mutable double pSquare;
461  };
462 
463  inline
464  std::ostream& operator<<(std::ostream& os, const svt::BasicFV& fv)
465  {
466  os << fv.pLabel;
467  fv.writeWithoutLabel( os);
468 // os << "\n";
469  return os;
470  }
471 
472  inline
473  std::istream& operator>>(std::istream& is, svt::BasicFV& fv)
474  {
475  // read label
476  double label;
477  is >> label;
478  fv.setLabel(label);
479  fv.readWithoutLabel( is);
480  return is;
481  }
482 
483  inline BasicFV operator+( const BasicFV& fv1, const BasicFV& fv2)
484  {
485  BasicFV result;
486  size_t n = fv1.size();
487  result.resize( n);
488  BasicFV::iterator resultP = result.begin();
489  BasicFV::const_iterator fv1P = fv1.begin();
490  BasicFV::const_iterator fv2P = fv2.begin();
491  for( size_t i = 0; i < n; ++i)
492  {
493  *resultP = *fv1P + *fv2P;
494  ++resultP;
495  ++fv1P;
496  ++fv2P;
497  }
498  return result;
499  }
500 
501  inline BasicFV operator-( const BasicFV& fv1, const BasicFV& fv2)
502  {
503  BasicFV result;
504  size_t n = fv1.size();
505  result.resize( n);
506  BasicFV::iterator resultP = result.begin();
507  BasicFV::const_iterator fv1P = fv1.begin();
508  BasicFV::const_iterator fv2P = fv2.begin();
509  for( size_t i = 0; i < n; ++i)
510  {
511  *resultP = *fv1P - *fv2P;
512  ++resultP;
513  ++fv1P;
514  ++fv2P;
515  }
516  return result;
517  }
518 
519  inline BasicFV operator*( double factor, const BasicFV& fv)
520  {
521  BasicFV result;
522  size_t n = fv.size();
523  result.resize( n);
524  BasicFV::iterator resultP = result.begin();
525  BasicFV::const_iterator fvP = fv.begin();
526  for( size_t i = 0; i < n; ++i)
527  {
528  *resultP = factor * *fvP;
529  ++resultP;
530  ++fvP;
531  }
532  return result;
533  }
534 
535  inline BasicFV operator*( const BasicFV& fv, double factor)
536  {
537  return operator*( factor, fv);
538  }
539 
540 
541 
542 
543 }
544 
545 #endif
546 
547 
548 
const_reference operator[](int index) const
Definition: BasicFV.hh:224
std::vector< double >::reference reference
Definition: BasicFV.hh:124
void operator*=(double factor)
Definition: BasicFV.hh:430
bool operator==(const BasicFV &fv) const
Definition: BasicFV.hh:405
size_type size() const
Definition: BasicFV.hh:262
unsigned int uniqueID() const
Definition: BasicFV.hh:205
BasicFV operator+(const BasicFV &fv1, const BasicFV &fv2)
Definition: BasicFV.hh:483
void operator+=(const BasicFV &fv)
Definition: BasicFV.hh:410
void setZero()
Definition: BasicFV.hh:280
const unsigned int MAX_BELIEVABLE_UNIQUE_ID
Definition: svm_defines.hh:63
BasicFV operator*(double factor, const BasicFV &fv)
Definition: BasicFV.hh:519
reference operator[](int index)
Definition: BasicFV.hh:211
const_iterator end() const
Definition: BasicFV.hh:246
static const char * helpPipeFormat()
Definition: BasicFV.hh:396
iterator end()
Definition: BasicFV.hh:250
std::vector< double >::iterator iterator
Definition: BasicFV.hh:122
void resize(size_type newSize)
Definition: BasicFV.hh:267
void setUniqueID(unsigned int uid)
Definition: BasicFV.hh:199
BasicFV operator-(const BasicFV &fv1, const BasicFV &fv2)
Definition: BasicFV.hh:501
BasicFV(BasicFV const &fv)
Definition: BasicFV.hh:154
std::vector< double >::size_type size_type
Definition: BasicFV.hh:126
void setLabel(double value)
Definition: BasicFV.hh:189
double dotProduct(const svt::BasicFV &fv) const
Definition: BasicFV.hh:321
friend std::ostream & operator<<(std::ostream &os, const svt::BasicFV &fv)
Definition: BasicFV.hh:464
iterator begin()
Definition: BasicFV.hh:234
void writeWithoutLabel(std::ostream &os) const
Definition: BasicFV.hh:387
const_iterator begin() const
Definition: BasicFV.hh:230
friend std::istream & operator>>(std::istream &is, svt::BasicFV &fv)
Definition: BasicFV.hh:473
BasicFV & operator=(BasicFV const &fv)
Definition: BasicFV.hh:166
std::vector< double >::const_reference const_reference
Definition: BasicFV.hh:125
BasicFV(std::vector< double > features, double label=0, int uniqueID=MAX_BELIEVABLE_UNIQUE_ID+1)
Definition: BasicFV.hh:140
double getLabel() const
Definition: BasicFV.hh:194
std::vector< double >::const_iterator const_iterator
Definition: BasicFV.hh:123
double square() const
Definition: BasicFV.hh:296
void readWithoutLabel(std::istream &is)
Definition: BasicFV.hh:338