ViSP  2.9.0
vpFeaturePoint.cpp
1 /****************************************************************************
2  *
3  * $Id: vpFeaturePoint.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  * 2D point visual feature.
36  *
37  * Authors:
38  * Eric Marchand
39  *
40  *****************************************************************************/
41 
42 
49 #include <visp/vpBasicFeature.h>
50 #include <visp/vpFeaturePoint.h>
51 
52 // Exception
53 #include <visp/vpException.h>
54 #include <visp/vpMatrixException.h>
55 #include <visp/vpFeatureException.h>
56 
57 // Debug trace
58 #include <visp/vpDebug.h>
59 
60 // math
61 #include <visp/vpMath.h>
62 
63 #include <visp/vpFeatureDisplay.h>
64 
65 
66 
67 /*
68 
69 attributes and members directly related to the vpBasicFeature needs
70 other functionalities ar useful but not mandatory
71 
72 */
73 
77 void
79 {
80  //feature dimension
81  dim_s = 2 ;
82  nbParameters = 3;
83 
84  // memory allocation
85  s.resize(dim_s) ;
86  if (flags == NULL)
87  flags = new bool[nbParameters];
88  for (unsigned int i = 0; i < nbParameters; i++) flags[i] = false;
89 
90  //default value Z (1 meters)
91  Z = 1;
92 }
93 
98 {
99  init() ;
100 }
101 
102 
108 void
109 vpFeaturePoint::set_Z(const double Z_)
110 {
111  this->Z = Z_ ;
112  flags[2] = true;
113 }
114 
115 
121 double
123 {
124  return Z ;
125 }
126 
127 
135 void
136 vpFeaturePoint::set_x(const double x)
137 {
138  s[0] = x ;
139  flags[0] = true;
140 }
141 
142 
148 double
150 {
151  return s[0] ;
152 }
153 
154 
160 void
161 vpFeaturePoint::set_y(const double y)
162 {
163  s[1] = y ;
164  flags[1] = true;
165 }
166 
167 
173 double
175 {
176  return s[1] ;
177 }
178 
179 
191 void
192 vpFeaturePoint::set_xyZ(const double x_,
193  const double y_,
194  const double Z_)
195 {
196  set_x(x_) ;
197  set_y(y_) ;
198  set_Z(Z_) ;
199  for(unsigned int i = 0; i < nbParameters; i++) flags[i] = true;
200 }
201 
202 
246 vpMatrix
247 vpFeaturePoint::interaction(const unsigned int select)
248 {
249  vpMatrix L ;
250 
251  L.resize(0,6) ;
252 
254  {
255  for (unsigned int i = 0; i < nbParameters; i++)
256  {
257  if (flags[i] == false)
258  {
259  switch(i){
260  case 0:
261  vpTRACE("Warning !!! The interaction matrix is computed but x was not set yet");
262  break;
263  case 1:
264  vpTRACE("Warning !!! The interaction matrix is computed but y was not set yet");
265  break;
266  case 2:
267  vpTRACE("Warning !!! The interaction matrix is computed but Z was not set yet");
268  break;
269  default:
270  vpTRACE("Problem during the reading of the variable flags");
271  }
272  }
273  }
274  resetFlags();
275  }
276 
277  double x_ = get_x() ;
278  double y_ = get_y() ;
279  double Z_ = get_Z() ;
280 
281  if (Z_ < 0)
282  {
283  vpERROR_TRACE("Point is behind the camera ") ;
284  std::cout <<"Z = " << Z_ << std::endl ;
285 
287  "Point is behind the camera ")) ;
288  }
289 
290  if (fabs(Z_) < 1e-6)
291  {
292  vpERROR_TRACE("Point Z coordinates is null ") ;
293  std::cout <<"Z = " << Z_ << std::endl ;
294 
296  "Point Z coordinates is null")) ;
297  }
298 
299  if (vpFeaturePoint::selectX() & select )
300  {
301  vpMatrix Lx(1,6) ; Lx = 0;
302 
303  Lx[0][0] = -1/Z_ ;
304  Lx[0][1] = 0 ;
305  Lx[0][2] = x_/Z_ ;
306  Lx[0][3] = x_*y_ ;
307  Lx[0][4] = -(1+x_*x_) ;
308  Lx[0][5] = y_ ;
309 
310  L = vpMatrix::stackMatrices(L,Lx) ;
311  }
312 
313  if (vpFeaturePoint::selectY() & select )
314  {
315  vpMatrix Ly(1,6) ; Ly = 0;
316 
317  Ly[0][0] = 0 ;
318  Ly[0][1] = -1/Z_ ;
319  Ly[0][2] = y_/Z ;
320  Ly[0][3] = 1+y_*y_ ;
321  Ly[0][4] = -x_*y_ ;
322  Ly[0][5] = -x_ ;
323 
324  L = vpMatrix::stackMatrices(L,Ly) ;
325  }
326  return L ;
327 }
328 
329 
362  const unsigned int select)
363 {
364  vpColVector e(0) ;
365 
366  try{
367  if (vpFeaturePoint::selectX() & select )
368  {
369  vpColVector ex(1) ;
370  ex[0] = s[0] - s_star[0] ;
371 
372  e = vpMatrix::stackMatrices(e,ex) ;
373  }
374 
375  if (vpFeaturePoint::selectY() & select )
376  {
377  vpColVector ey(1) ;
378  ey[0] = s[1] - s_star[1] ;
379  e = vpMatrix::stackMatrices(e,ey) ;
380  }
381  }
382  catch(vpMatrixException me)
383  {
384  vpERROR_TRACE("caught a Matrix related error") ;
385  std::cout <<std::endl << me << std::endl ;
386  throw(me) ;
387  }
388  catch(vpException me)
389  {
390  vpERROR_TRACE("caught another error") ;
391  std::cout <<std::endl << me << std::endl ;
392  throw(me) ;
393  }
394 
395 
396  return e ;
397 
398 }
399 
400 
419 void
420 vpFeaturePoint::print(const unsigned int select ) const
421 {
422 
423  std::cout <<"Point: Z=" << get_Z() ;
424  if (vpFeaturePoint::selectX() & select )
425  std::cout << " x=" << get_x() ;
426  if (vpFeaturePoint::selectY() & select )
427  std::cout << " y=" << get_y() ;
428  std::cout <<std::endl ;
429 }
430 
431 
441 void
442 vpFeaturePoint::buildFrom(const double x_, const double y_, const double Z_)
443 {
444 
445  s[0] = x_ ;
446  s[1] = y_ ;
447 
448  this->Z = Z_ ;
449 
450  if (Z_ < 0)
451  {
452  vpERROR_TRACE("Point is behind the camera ") ;
453  std::cout <<"Z = " << Z_ << std::endl ;
454 
456  "Point is behind the camera ")) ;
457  }
458 
459  if (fabs(Z_) < 1e-6)
460  {
461  vpERROR_TRACE("Point Z coordinates is null ") ;
462  std::cout <<"Z = " << Z_ << std::endl ;
463 
465  "Point Z coordinates is null")) ;
466  }
467 
468  for(unsigned int i = 0; i < nbParameters; i++) flags[i] = true;
469 
470 }
471 
472 
483 void
485  const vpImage<unsigned char> &I,
486  const vpColor &color,
487  unsigned int thickness) const
488 {
489  try{
490  double x,y ;
491  x = get_x() ;
492  y = get_y() ;
493 
494  vpFeatureDisplay::displayPoint(x, y, cam, I, color, thickness) ;
495 
496  }
497  catch(...)
498  {
499  vpERROR_TRACE("Error caught") ;
500  throw ;
501  }
502 }
503 
514 void
516  const vpImage<vpRGBa> &I,
517  const vpColor &color,
518  unsigned int thickness) const
519 {
520  try{
521  double x,y ;
522  x = get_x() ;
523  y = get_y() ;
524 
525  vpFeatureDisplay::displayPoint(x, y, cam, I, color, thickness) ;
526 
527  }
528  catch(...)
529  {
530  vpERROR_TRACE("Error caught") ;
531  throw ;
532  }
533 }
534 
535 
547 {
548  vpFeaturePoint *feature = new vpFeaturePoint ;
549  return feature ;
550 }
551 
552 
553 /*
554  * Local variables:
555  * c-basic-offset: 2
556  * End:
557  */
Definition of the vpMatrix class.
Definition: vpMatrix.h:98
vpMatrix interaction(const unsigned int select=FEATURE_ALL)
bool * flags
Ensure that all the parameters needed to compute the iteraction matrix are set.
void resize(const unsigned int nrows, const unsigned int ncols, const bool nullify=true)
Definition: vpMatrix.cpp:183
#define vpERROR_TRACE
Definition: vpDebug.h:395
#define vpTRACE
Definition: vpDebug.h:418
void set_xyZ(const double x, const double y, const double Z)
vpColVector error(const vpBasicFeature &s_star, const unsigned int select=FEATURE_ALL)
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
Class that defines a 2D point visual feature which is composed by two parameters that are the cartes...
static void displayPoint(double x, double y, const vpCameraParameters &cam, const vpImage< unsigned char > &I, const vpColor &color=vpColor::green, unsigned int thickness=1)
void set_y(const double y)
void set_x(const double x)
class that defines what is a visual feature
Error that can be emited by the vpBasicFeature class and its derivates.
double get_Z() const
Generic class defining intrinsic camera parameters.
void display(const vpCameraParameters &cam, const vpImage< unsigned char > &I, const vpColor &color=vpColor::green, unsigned int thickness=1) const
void stackMatrices(const vpMatrix &A)
Definition: vpMatrix.cpp:3003
void buildFrom(const double x, const double y, const double Z)
vpFeaturePoint * duplicate() const
vpBasicFeatureDeallocatorType deallocate
Class that provides a data structure for the column vectors as well as a set of operations on these v...
Definition: vpColVector.h:72
double get_y() const
double get_x() const
error that can be emited by the vpMatrix class and its derivates
void set_Z(const double Z)
static unsigned int selectX()
static unsigned int selectY()
unsigned int nbParameters
Number of parameters needed to compute the interaction matrix.
vpColVector s
State of the visual feature.
void resize(const unsigned int i, const bool flagNullify=true)
Definition: vpColVector.h:94
void print(const unsigned int select=FEATURE_ALL) const