Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
vpFeaturePoint.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * 2D point visual feature.
32  *
33  * Authors:
34  * Eric Marchand
35  *
36  *****************************************************************************/
37 
38 
45 #include <visp3/visual_features/vpBasicFeature.h>
46 #include <visp3/visual_features/vpFeaturePoint.h>
47 
48 // Exception
49 #include <visp3/core/vpException.h>
50 #include <visp3/visual_features/vpFeatureException.h>
51 
52 // Debug trace
53 #include <visp3/core/vpDebug.h>
54 
55 // math
56 #include <visp3/core/vpMath.h>
57 
58 #include <visp3/core/vpFeatureDisplay.h>
59 
60 
61 
62 /*
63 
64 attributes and members directly related to the vpBasicFeature needs
65 other functionalities ar useful but not mandatory
66 
67 */
68 
72 void
74 {
75  //feature dimension
76  dim_s = 2 ;
77  nbParameters = 3;
78 
79  // memory allocation
80  s.resize(dim_s) ;
81  if (flags == NULL)
82  flags = new bool[nbParameters];
83  for (unsigned int i = 0; i < nbParameters; i++) flags[i] = false;
84 
85  //default value Z (1 meters)
86  Z = 1;
87 }
88 
93 {
94  init() ;
95 }
96 
97 
103 void
104 vpFeaturePoint::set_Z(const double Z_)
105 {
106  this->Z = Z_ ;
107  flags[2] = true;
108 }
109 
110 
116 double
118 {
119  return Z ;
120 }
121 
122 
130 void
131 vpFeaturePoint::set_x(const double x)
132 {
133  s[0] = x ;
134  flags[0] = true;
135 }
136 
137 
143 double
145 {
146  return s[0] ;
147 }
148 
149 
155 void
156 vpFeaturePoint::set_y(const double y)
157 {
158  s[1] = y ;
159  flags[1] = true;
160 }
161 
162 
168 double
170 {
171  return s[1] ;
172 }
173 
174 
186 void
187 vpFeaturePoint::set_xyZ(const double x_,
188  const double y_,
189  const double Z_)
190 {
191  set_x(x_) ;
192  set_y(y_) ;
193  set_Z(Z_) ;
194  for(unsigned int i = 0; i < nbParameters; i++) flags[i] = true;
195 }
196 
197 
241 vpMatrix
242 vpFeaturePoint::interaction(const unsigned int select)
243 {
244  vpMatrix L ;
245 
246  L.resize(0,6) ;
247 
249  {
250  for (unsigned int i = 0; i < nbParameters; i++)
251  {
252  if (flags[i] == false)
253  {
254  switch(i){
255  case 0:
256  vpTRACE("Warning !!! The interaction matrix is computed but x was not set yet");
257  break;
258  case 1:
259  vpTRACE("Warning !!! The interaction matrix is computed but y was not set yet");
260  break;
261  case 2:
262  vpTRACE("Warning !!! The interaction matrix is computed but Z was not set yet");
263  break;
264  default:
265  vpTRACE("Problem during the reading of the variable flags");
266  }
267  }
268  }
269  resetFlags();
270  }
271 
272  double x_ = get_x() ;
273  double y_ = get_y() ;
274  double Z_ = get_Z() ;
275 
276  if (Z_ < 0)
277  {
278  vpERROR_TRACE("Point is behind the camera ") ;
279  std::cout <<"Z = " << Z_ << std::endl ;
280 
282  "Point is behind the camera ")) ;
283  }
284 
285  if (fabs(Z_) < 1e-6)
286  {
287  vpERROR_TRACE("Point Z coordinates is null ") ;
288  std::cout <<"Z = " << Z_ << std::endl ;
289 
291  "Point Z coordinates is null")) ;
292  }
293 
294  if (vpFeaturePoint::selectX() & select )
295  {
296  vpMatrix Lx(1,6) ; Lx = 0;
297 
298  Lx[0][0] = -1/Z_ ;
299  Lx[0][1] = 0 ;
300  Lx[0][2] = x_/Z_ ;
301  Lx[0][3] = x_*y_ ;
302  Lx[0][4] = -(1+x_*x_) ;
303  Lx[0][5] = y_ ;
304 
305  L = vpMatrix::stack(L,Lx) ;
306  }
307 
308  if (vpFeaturePoint::selectY() & select )
309  {
310  vpMatrix Ly(1,6) ; Ly = 0;
311 
312  Ly[0][0] = 0 ;
313  Ly[0][1] = -1/Z_ ;
314  Ly[0][2] = y_/Z_ ;
315  Ly[0][3] = 1+y_*y_ ;
316  Ly[0][4] = -x_*y_ ;
317  Ly[0][5] = -x_ ;
318 
319  L = vpMatrix::stack(L,Ly) ;
320  }
321  return L ;
322 }
323 
324 
357  const unsigned int select)
358 {
359  vpColVector e(0) ;
360 
361  try{
362  if (vpFeaturePoint::selectX() & select )
363  {
364  vpColVector ex(1) ;
365  ex[0] = s[0] - s_star[0] ;
366 
367  e = vpColVector::stack(e,ex) ;
368  }
369 
370  if (vpFeaturePoint::selectY() & select )
371  {
372  vpColVector ey(1) ;
373  ey[0] = s[1] - s_star[1] ;
374  e = vpColVector::stack(e,ey) ;
375  }
376  }
377  catch(...) {
378  throw ;
379  }
380 
381 
382  return e ;
383 
384 }
385 
386 
405 void
406 vpFeaturePoint::print(const unsigned int select ) const
407 {
408 
409  std::cout <<"Point: Z=" << get_Z() ;
410  if (vpFeaturePoint::selectX() & select )
411  std::cout << " x=" << get_x() ;
412  if (vpFeaturePoint::selectY() & select )
413  std::cout << " y=" << get_y() ;
414  std::cout <<std::endl ;
415 }
416 
417 
427 void
428 vpFeaturePoint::buildFrom(const double x_, const double y_, const double Z_)
429 {
430 
431  s[0] = x_ ;
432  s[1] = y_ ;
433 
434  this->Z = Z_ ;
435 
436  if (Z_ < 0)
437  {
438  vpERROR_TRACE("Point is behind the camera ") ;
439  std::cout <<"Z = " << Z_ << std::endl ;
440 
442  "Point is behind the camera ")) ;
443  }
444 
445  if (fabs(Z_) < 1e-6)
446  {
447  vpERROR_TRACE("Point Z coordinates is null ") ;
448  std::cout <<"Z = " << Z_ << std::endl ;
449 
451  "Point Z coordinates is null")) ;
452  }
453 
454  for(unsigned int i = 0; i < nbParameters; i++) flags[i] = true;
455 
456 }
457 
458 
469 void
471  const vpImage<unsigned char> &I,
472  const vpColor &color,
473  unsigned int thickness) const
474 {
475  try{
476  double x,y ;
477  x = get_x() ;
478  y = get_y() ;
479 
480  vpFeatureDisplay::displayPoint(x, y, cam, I, color, thickness) ;
481 
482  }
483  catch(...)
484  {
485  vpERROR_TRACE("Error caught") ;
486  throw ;
487  }
488 }
489 
500 void
502  const vpImage<vpRGBa> &I,
503  const vpColor &color,
504  unsigned int thickness) const
505 {
506  try{
507  double x,y ;
508  x = get_x() ;
509  y = get_y() ;
510 
511  vpFeatureDisplay::displayPoint(x, y, cam, I, color, thickness) ;
512 
513  }
514  catch(...)
515  {
516  vpERROR_TRACE("Error caught") ;
517  throw ;
518  }
519 }
520 
521 
533 {
534  vpFeaturePoint *feature = new vpFeaturePoint ;
535  return feature ;
536 }
537 
554 unsigned int vpFeaturePoint::selectX() { return FEATURE_LINE[0] ; }
555 
572 unsigned int vpFeaturePoint::selectY() { return FEATURE_LINE[1] ; }
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:97
vpMatrix interaction(const unsigned int select=FEATURE_ALL)
bool * flags
Ensure that all the parameters needed to compute the iteraction matrix are set.
void stack(const double &d)
void resize(const unsigned int nrows, const unsigned int ncols, const bool flagNullify=true)
Definition: vpArray2D.h:167
void set_xyZ(const double x, const double y, const double Z)
vpColVector error(const vpBasicFeature &s_star, const unsigned int select=FEATURE_ALL)
#define vpERROR_TRACE
Definition: vpDebug.h:391
Class to define colors available for display functionnalities.
Definition: vpColor.h:121
void stack(const vpMatrix &A)
Definition: vpMatrix.cpp:2981
unsigned int dim_s
Dimension of the visual feature.
static unsigned int selectY()
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
static unsigned int selectX()
#define vpTRACE
Definition: vpDebug.h:414
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
static const unsigned int FEATURE_LINE[32]
void buildFrom(const double x, const double y, const double Z)
vpFeaturePoint * duplicate() const
vpBasicFeatureDeallocatorType deallocate
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
double get_y() const
double get_x() const
void set_Z(const double Z)
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:225
void print(const unsigned int select=FEATURE_ALL) const