ViSP  2.9.0
vpGenericFeature.cpp
1 /****************************************************************************
2  *
3  * $Id: vpGenericFeature.cpp 4649 2014-02-07 14:57:11Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Generic feature (used to create new feature not implemented in ViSP).
36  *
37  * Authors:
38  * Eric Marchand
39  *
40  *****************************************************************************/
41 
42 
43 #include <visp/vpGenericFeature.h>
44 
45 
46 // Exception
47 #include <visp/vpException.h>
48 #include <visp/vpMatrixException.h>
49 #include <visp/vpFeatureException.h>
50 
51 // Debug trace
52 #include <visp/vpDebug.h>
53 
54 
55 
63 {
64 
65 }
66 
68 {
69  s = 0 ;
70 }
71 
72 
83 vpGenericFeature::vpGenericFeature()
84  : L(), err(), errorStatus(errorNotInitalized)
85 {
86  /*
87  vpERROR_TRACE("You are not allow to use this constructor ") ;
88  vpERROR_TRACE("Please, use vpGenericFeature::vpGenericFeature(int _dim) "
89  "constructor") ;
90  vpERROR_TRACE("And provide the dimension of the visual feature ") ;
91  throw(vpException(vpException::cannotUseConstructorError,
92  "You are not allow to use this constructor ")) ;
93  */
94 }
95 
96 
102 vpGenericFeature::vpGenericFeature(unsigned int dimension_gen_s)
103  : L(), err(), errorStatus(errorNotInitalized)
104 {
105  this->dim_s = dimension_gen_s ;
106  s.resize(dimension_gen_s) ;
107 }
108 
118 void
120 {
121  if (error_vector.getRows() != dim_s)
122  {
123  vpERROR_TRACE("size mismatch between error dimension"
124  "and feature dimension");
126  "size mismatch between error dimension"
127  "and feature dimension"));
128 
129  }
130  errorStatus = errorInitialized ;
131  err = error_vector ;
132 }
133 
134 
192  const unsigned int select)
193 {
194  if (s_star.get_s().getRows() != dim_s)
195  {
196  vpERROR_TRACE("size mismatch between s* dimension "
197  "and feature dimension");
199  "size mismatch between s* dimension "
200  "and feature dimension"));
201 
202  }
203 
204  vpColVector e(0) ;
205 
206  try
207  {
208  if (errorStatus == errorHasToBeUpdated)
209  {
210  vpERROR_TRACE("Error has no been updated since last iteration"
211  "you should have used vpGenericFeature::setError"
212  "in you visual servoing loop") ;
214  "Error has no been updated since last iteration"));
215  }
216  else
217  if (errorStatus == errorInitialized)
218  {
219  vpDEBUG_TRACE(25,"Error init: e=e.");
220  errorStatus = errorHasToBeUpdated ;
221  for (unsigned int i=0 ; i < dim_s ; i++)
222  if (FEATURE_LINE[i] & select )
223  {
224  vpColVector ex(1) ;
225  ex[i] = err[i] ;
226 
227  e = vpMatrix::stackMatrices(e,ex) ;
228  }
229  }
230  else
231  {
232  vpDEBUG_TRACE(25,"Error not init: e=s-s*.");
233 
234  for (unsigned int i=0 ; i < dim_s ; i++)
235  if (FEATURE_LINE[i] & select )
236  {
237  vpColVector ex(1) ;
238  ex[0] = s[i] - s_star[i] ;
239 
240  e = vpMatrix::stackMatrices(e,ex) ;
241  }
242 
243  }
244  }
245  catch(vpMatrixException me)
246  {
247  vpERROR_TRACE("caught a Matric related error") ;
248  std::cout <<std::endl << me << std::endl ;
249  throw(me) ;
250  }
251  catch(vpException me)
252  {
253  vpERROR_TRACE("caught another error") ;
254  std::cout <<std::endl << me << std::endl ;
255  throw(me) ;
256  }
257  return e ;
258 
259 }
260 
261 
262 
304 vpGenericFeature::error( const unsigned int select)
305 {
306  vpColVector e(0) ;
307 
308  try
309  {
310  if (errorStatus == errorHasToBeUpdated)
311  {
312  vpERROR_TRACE("Error has no been updated since last iteration"
313  "you should have used vpGenericFeature::setError"
314  "in you visual servoing loop") ;
316  "Error has no been updated since last iteration"));
317  }
318  else
319  if (errorStatus == errorInitialized)
320  {
321  errorStatus = errorHasToBeUpdated ;
322  for (unsigned int i=0 ; i < dim_s ; i++)
323  if (FEATURE_LINE[i] & select )
324  {
325  vpColVector ex(1) ;
326  ex[i] = err[i] ;
327 
328  e = vpMatrix::stackMatrices(e,ex) ;
329  }
330  }
331  else
332  {
333 
334  for (unsigned int i=0 ; i < dim_s ; i++)
335  if (FEATURE_LINE[i] & select )
336  {
337  vpColVector ex(1) ;
338  ex[i] = s[i] ;
339 
340  e = vpMatrix::stackMatrices(e,ex) ;
341  }
342 
343  }
344  }
345  catch(vpMatrixException me)
346  {
347  vpERROR_TRACE("caught a Matric related error") ;
348  std::cout <<std::endl << me << std::endl ;
349  throw(me) ;
350  }
351  catch(vpException me)
352  {
353  vpERROR_TRACE("caught another error") ;
354  std::cout <<std::endl << me << std::endl ;
355  throw(me) ;
356  }
357 
358  return e ;
359 
360 }
361 
362 
413 vpMatrix
414 vpGenericFeature::interaction(const unsigned int select)
415 {
416  if (L.getRows() == 0)
417  {
418  std::cout << "interaction matrix " << L << std::endl ;
419  vpERROR_TRACE("Interaction has not been initialized");
420  std::cout << "A possible reason (may be) is that you have set" << std::endl ;
421  std::cout << "the interaction matrix for s and compute a control " << std::endl ;
422  std::cout << "with Ls=s* (default) or vice versa" << std::endl ;
423 
425  "size mismatch between s* dimension "
426  "and feature dimension"));
427 
428  }
429 
430  vpMatrix Ls ;
431 
432  Ls.resize(0,6) ;
433 
434  for (unsigned int i=0 ; i < dim_s ; i++)
435  if (FEATURE_LINE[i] & select )
436  {
437  vpMatrix Lx(1,6) ; Lx = 0;
438 
439  for (int j=0 ; j < 6 ; j++)
440  Lx[0][j] = L[i][j] ;
441 
442  Ls = vpMatrix::stackMatrices(Ls,Lx) ;
443  }
444 
445  return Ls ;
446 }
447 
448 
458 void
460 {
461  if (L_.getRows() != dim_s)
462  {
463  std::cout << L_.getRows() <<" " << dim_s << std::endl ;;
464  vpERROR_TRACE("size mismatch between interaction matrix size "
465  "and feature dimension");
467  "size mismatch between interaction matrix size "
468  "and feature dimension"));
469  }
470 
471  this->L = L_ ;
472 }
473 
483 void
485 {
486 
487  if (s_vector.getRows() != dim_s)
488  {
489  vpERROR_TRACE("size mismatch between s dimension"
490  "and feature dimension");
492  "size mismatch between s dimension"
493  "and feature dimension"));
494  }
495  this->s = s_vector ;
496 }
497 
498 
508 void
510 {
511  if (s_vector.getRows() != dim_s)
512  {
513  vpERROR_TRACE("size mismatch between s dimension"
514  "and feature dimension");
516  "size mismatch between s dimension"
517  "and feature dimension"));
518  }
519  s_vector = this->s ;
520 }
521 
522 
536 void
537 vpGenericFeature::set_s(const double s0, const double s1, const double s2)
538 {
539 
540  if (3 != dim_s)
541  {
542  vpERROR_TRACE("size mismatch between number of parameters"
543  "and feature dimension");
545  "size mismatch between number of parameters"
546  "and feature dimension"));
547 
548  }
549  s[0] = s0 ; s[1] = s1 ; s[2] = s2 ;
550 }
551 
552 
566 void
567 vpGenericFeature::get_s(double &s0, double &s1, double &s2) const
568 {
569 
570  if (3 != dim_s)
571  {
572  vpERROR_TRACE("size mismatch between number of parameters"
573  "and feature dimension");
575  "size mismatch between number of parameters"
576  "and feature dimension"));
577 
578  }
579  s0 = s[0] ; s1 = s[1] ; s2 = s[2] ;
580 }
581 
582 
594 void
595 vpGenericFeature::set_s(const double s0, const double s1)
596 {
597 
598  if (2 != dim_s)
599  {
600  vpERROR_TRACE("size mismatch between number of parameters"
601  "and feature dimension");
603  "size mismatch between number of parameters"
604  "and feature dimension"));
605 
606  }
607  s[0] = s0 ; s[1] = s1 ;
608 }
609 
610 
622 void
623 vpGenericFeature::get_s(double &s0, double &s1) const
624 {
625 
626  if (2 != dim_s)
627  {
628  vpERROR_TRACE("size mismatch between number of parameters"
629  "and feature dimension");
631  "size mismatch between number of parameters"
632  "and feature dimension"));
633 
634  }
635  s0 = s[0] ; s1 = s[1] ;
636 }
637 
638 
648 void
649 vpGenericFeature::set_s(const double s0)
650 {
651 
652  if (1 != dim_s)
653  {
654  vpERROR_TRACE("size mismatch between number of parameters"
655  "and feature dimension");
657  "size mismatch between number of parameters"
658  "and feature dimension"));
659 
660  }
661  s[0] = s0 ;
662 }
663 
664 
674 void
675 vpGenericFeature::get_s(double &s0) const
676 {
677 
678  if (1 != dim_s)
679  {
680  vpERROR_TRACE("size mismatch between number of parameters"
681  "and feature dimension");
683  "size mismatch between number of parameters"
684  "and feature dimension"));
685 
686  }
687  s0 = s[0];
688 }
689 
690 
712 void
713 vpGenericFeature::print(const unsigned int select) const
714 {
715 
716  std::cout <<"Generic Feature: " ;
717  for (unsigned int i=0 ; i < dim_s ; i++)
718  if (FEATURE_LINE[i] & select )
719  {
720  std::cout << " s["<<i << "]=" << s[i] ;
721  }
722 
723  std::cout <<std::endl ;
724 }
725 
727 {
728  vpGenericFeature *feature= new vpGenericFeature(dim_s) ;
729 
730  vpTRACE("dims = %d",dim_s) ;
731  return feature ;
732 }
733 
737 void
739  const vpImage<unsigned char> &/* I */,
740  const vpColor &/* color */,
741  unsigned int /* thickness */) const
742 {
743  static int firsttime =0 ;
744 
745  if (firsttime==0)
746  {
747  firsttime=1 ;
748  vpERROR_TRACE("not implemented") ;
749  // Do not throw and error since it is not subject
750  // to produce a failure
751  }
752 }
756 void
758  const vpImage<vpRGBa> &/* I */,
759  const vpColor &/* color */,
760  unsigned int /* thickness */) const
761 {
762  static int firsttime =0 ;
763 
764  if (firsttime==0)
765  {
766  firsttime=1 ;
767  vpERROR_TRACE("not implemented") ;
768  // Do not throw and error since it is not subject
769  // to produce a failure
770  }
771 }
772 
773 /*
774  * Local variables:
775  * c-basic-offset: 2
776  * End:
777  */
#define vpDEBUG_TRACE
Definition: vpDebug.h:482
Definition of the vpMatrix class.
Definition: vpMatrix.h:98
void resize(const unsigned int nrows, const unsigned int ncols, const bool nullify=true)
Definition: vpMatrix.cpp:183
void set_s(const vpColVector &s)
set the value of all the features.
#define vpERROR_TRACE
Definition: vpDebug.h:395
#define vpTRACE
Definition: vpDebug.h:418
void display(const vpCameraParameters &cam, const vpImage< unsigned char > &I, const vpColor &color=vpColor::green, unsigned int thickness=1) const
Class to define colors available for display functionnalities.
Definition: vpColor.h:125
unsigned int dim_s
Dimension of the visual feature.
error that can be emited by ViSP classes.
Definition: vpException.h:76
feature list or desired feature list is empty
vpMatrix interaction(const unsigned int select=FEATURE_ALL)
void get_s(vpColVector &s) const
get the value of all the features.
class that defines what is a visual feature
Error that can be emited by the vpBasicFeature class and its derivates.
virtual ~vpGenericFeature()
Generic class defining intrinsic camera parameters.
void setInteractionMatrix(const vpMatrix &L)
set the value of the interaction matrix.
void stackMatrices(const vpMatrix &A)
Definition: vpMatrix.cpp:3003
static const unsigned int FEATURE_LINE[32]
Class that provides a data structure for the column vectors as well as a set of operations on these v...
Definition: vpColVector.h:72
void print(const unsigned int select=FEATURE_ALL) const
error that can be emited by the vpMatrix class and its derivates
vpGenericFeature * duplicate() const
Class that enables to define a feature or a set of features which are not implemented in ViSP as a sp...
unsigned int getRows() const
Return the number of rows of the matrix.
Definition: vpMatrix.h:161
vpColVector get_s(unsigned int select=FEATURE_ALL) const
Get the feature vector .
vpColVector error(const vpBasicFeature &s_star, const unsigned int select=FEATURE_ALL)
vpColVector s
State of the visual feature.
void resize(const unsigned int i, const bool flagNullify=true)
Definition: vpColVector.h:94
void setError(const vpColVector &error_vector)