Visual Servoing Platform  version 3.6.1 under development (2024-07-27)
vpFeaturePoint.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See https://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * 2D point visual feature.
33  *
34 *****************************************************************************/
35 
41 #include <visp3/visual_features/vpBasicFeature.h>
42 #include <visp3/visual_features/vpFeaturePoint.h>
43 
44 // Exception
45 #include <visp3/core/vpException.h>
46 #include <visp3/visual_features/vpFeatureException.h>
47 
48 // Debug trace
49 #include <visp3/core/vpDebug.h>
50 
51 // math
52 #include <visp3/core/vpMath.h>
53 
54 #include <visp3/core/vpFeatureDisplay.h>
55 
56 /*
57 
58 attributes and members directly related to the vpBasicFeature needs
59 other functionalities ar useful but not mandatory
60 
61 */
62 
68 {
69  // feature dimension
70  dim_s = 2;
71  nbParameters = 3;
72 
73  // memory allocation
74  s.resize(dim_s);
75  if (flags == nullptr)
76  flags = new bool[nbParameters];
77  for (unsigned int i = 0; i < nbParameters; i++)
78  flags[i] = false;
79 
80  // default value Z (1 meters)
81  Z = 1;
82 }
83 
88 
95 void vpFeaturePoint::set_Z(double Z_)
96 {
97  this->Z = Z_;
98  flags[2] = true;
99 }
100 
107 double vpFeaturePoint::get_Z() const { return Z; }
108 
116 void vpFeaturePoint::set_x(double x)
117 {
118  s[0] = x;
119  flags[0] = true;
120 }
121 
128 double vpFeaturePoint::get_x() const { return s[0]; }
129 
136 void vpFeaturePoint::set_y(double y)
137 {
138  s[1] = y;
139  flags[1] = true;
140 }
141 
148 double vpFeaturePoint::get_y() const { return s[1]; }
149 
161 void vpFeaturePoint::set_xyZ(double x_, double y_, double Z_)
162 {
163  set_x(x_);
164  set_y(y_);
165  set_Z(Z_);
166  for (unsigned int i = 0; i < nbParameters; i++)
167  flags[i] = true;
168 }
169 
214 {
215  vpMatrix L;
216 
217  L.resize(0, 6);
218 
220  for (unsigned int i = 0; i < nbParameters; i++) {
221  if (flags[i] == false) {
222  switch (i) {
223  case 0:
224  vpTRACE("Warning !!! The interaction matrix is computed but x was "
225  "not set yet");
226  break;
227  case 1:
228  vpTRACE("Warning !!! The interaction matrix is computed but y was "
229  "not set yet");
230  break;
231  case 2:
232  vpTRACE("Warning !!! The interaction matrix is computed but Z was "
233  "not set yet");
234  break;
235  default:
236  vpTRACE("Problem during the reading of the variable flags");
237  }
238  }
239  }
240  resetFlags();
241  }
242 
243  double x_ = get_x();
244  double y_ = get_y();
245  double Z_ = get_Z();
246 
247  if (Z_ < 0) {
248  vpERROR_TRACE("Point is behind the camera ");
249  std::cout << "Z = " << Z_ << std::endl;
250 
251  throw(vpFeatureException(vpFeatureException::badInitializationError, "Point is behind the camera "));
252  }
253 
254  if (fabs(Z_) < 1e-6) {
255  vpERROR_TRACE("Point Z coordinates is null ");
256  std::cout << "Z = " << Z_ << std::endl;
257 
258  throw(vpFeatureException(vpFeatureException::badInitializationError, "Point Z coordinates is null"));
259  }
260 
261  if (vpFeaturePoint::selectX() & select) {
262  vpMatrix Lx(1, 6);
263  Lx = 0;
264 
265  Lx[0][0] = -1 / Z_;
266  Lx[0][1] = 0;
267  Lx[0][2] = x_ / Z_;
268  Lx[0][3] = x_ * y_;
269  Lx[0][4] = -(1 + x_ * x_);
270  Lx[0][5] = y_;
271 
272  L = vpMatrix::stack(L, Lx);
273  }
274 
275  if (vpFeaturePoint::selectY() & select) {
276  vpMatrix Ly(1, 6);
277  Ly = 0;
278 
279  Ly[0][0] = 0;
280  Ly[0][1] = -1 / Z_;
281  Ly[0][2] = y_ / Z_;
282  Ly[0][3] = 1 + y_ * y_;
283  Ly[0][4] = -x_ * y_;
284  Ly[0][5] = -x_;
285 
286  L = vpMatrix::stack(L, Ly);
287  }
288  return L;
289 }
290 
327 vpColVector vpFeaturePoint::error(const vpBasicFeature &s_star, unsigned int select)
328 {
329  vpColVector e(0);
330 
331  try {
332  if (vpFeaturePoint::selectX() & select) {
333  vpColVector ex(1);
334  ex[0] = s[0] - s_star[0];
335 
336  e = vpColVector::stack(e, ex);
337  }
338 
339  if (vpFeaturePoint::selectY() & select) {
340  vpColVector ey(1);
341  ey[0] = s[1] - s_star[1];
342  e = vpColVector::stack(e, ey);
343  }
344  }
345  catch (...) {
346  throw;
347  }
348 
349  return e;
350 }
351 
371 void vpFeaturePoint::print(unsigned int select) const
372 {
373 
374  std::cout << "Point: Z=" << get_Z();
375  if (vpFeaturePoint::selectX() & select)
376  std::cout << " x=" << get_x();
377  if (vpFeaturePoint::selectY() & select)
378  std::cout << " y=" << get_y();
379  std::cout << std::endl;
380 }
381 
382 #ifdef VISP_BUILD_DEPRECATED_FUNCTIONS
396 void vpFeaturePoint::buildFrom(double x_, double y_, double Z_)
397 {
398  build(x_, y_, Z_);
399 }
400 #endif
401 
414 vpFeaturePoint &vpFeaturePoint::build(const double &x_, const double &y_, const double &Z_)
415 {
416 
417  s[0] = x_;
418  s[1] = y_;
419 
420  this->Z = Z_;
421 
422  if (Z_ < 0) {
423  vpERROR_TRACE("Point is behind the camera ");
424  std::cout << "Z = " << Z_ << std::endl;
425 
426  throw(vpFeatureException(vpFeatureException::badInitializationError, "Point is behind the camera "));
427  }
428 
429  if (fabs(Z_) < 1e-6) {
430  vpERROR_TRACE("Point Z coordinates is null ");
431  std::cout << "Z = " << Z_ << std::endl;
432 
433  throw(vpFeatureException(vpFeatureException::badInitializationError, "Point Z coordinates is null"));
434  }
435 
436  for (unsigned int i = 0; i < nbParameters; ++i) {
437  flags[i] = true;
438  }
439  return *this;
440 }
441 
453  unsigned int thickness) const
454 {
455  try {
456  double x, y;
457  x = get_x();
458  y = get_y();
459 
460  vpFeatureDisplay::displayPoint(x, y, cam, I, color, thickness);
461 
462  }
463  catch (...) {
464  vpERROR_TRACE("Error caught");
465  throw;
466  }
467 }
468 
479 void vpFeaturePoint::display(const vpCameraParameters &cam, const vpImage<vpRGBa> &I, const vpColor &color,
480  unsigned int thickness) const
481 {
482  try {
483  double x, y;
484  x = get_x();
485  y = get_y();
486 
487  vpFeatureDisplay::displayPoint(x, y, cam, I, color, thickness);
488 
489  }
490  catch (...) {
491  vpERROR_TRACE("Error caught");
492  throw;
493  }
494 }
495 
507 {
508  vpFeaturePoint *feature = new vpFeaturePoint;
509  return feature;
510 }
511 
530 unsigned int vpFeaturePoint::selectX() { return FEATURE_LINE[0]; }
531 
550 unsigned int vpFeaturePoint::selectY() { return FEATURE_LINE[1]; }
551 END_VISP_NAMESPACE
class that defines what is a visual feature
vpColVector s
State of the visual feature.
unsigned int nbParameters
Number of parameters needed to compute the interaction matrix.
unsigned int dim_s
Dimension of the visual feature.
static const unsigned int FEATURE_LINE[32]
vpBasicFeatureDeallocatorType deallocate
Generic class defining intrinsic camera parameters.
Implementation of column vector and the associated operations.
Definition: vpColVector.h:191
void stack(double d)
void resize(unsigned int i, bool flagNullify=true)
Definition: vpColVector.h:1143
Class to define RGB colors available for display functionalities.
Definition: vpColor.h:157
static void displayPoint(double x, double y, const vpCameraParameters &cam, const vpImage< unsigned char > &I, const vpColor &color=vpColor::green, unsigned int thickness=1)
Error that can be emitted by the vpBasicFeature class and its derivates.
@ badInitializationError
Wrong feature initialization.
Class that defines a 2D point visual feature which is composed by two parameters that are the cartes...
void buildFrom(double x, double y, double Z)
static unsigned int selectX()
void set_xyZ(double x, double y, double Z)
vpFeaturePoint * duplicate() const VP_OVERRIDE
vpColVector error(const vpBasicFeature &s_star, unsigned int select=FEATURE_ALL) VP_OVERRIDE
void print(unsigned int select=FEATURE_ALL) const VP_OVERRIDE
static unsigned int selectY()
vpMatrix interaction(unsigned int select=FEATURE_ALL) VP_OVERRIDE
void set_y(double y)
void display(const vpCameraParameters &cam, const vpImage< unsigned char > &I, const vpColor &color=vpColor::green, unsigned int thickness=1) const VP_OVERRIDE
double get_y() const
double get_x() const
void set_x(double x)
vpFeaturePoint & build(const double &x, const double &y, const double &Z)
void init() VP_OVERRIDE
double get_Z() const
void set_Z(double Z)
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:169
void stack(const vpMatrix &A)
#define vpTRACE
Definition: vpDebug.h:436
#define vpERROR_TRACE
Definition: vpDebug.h:409