Visual Servoing Platform  version 3.6.1 under development (2024-06-21)
vpFeatureDepth.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/vpFeatureDepth.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 attributes and members directly related to the vpBasicFeature needs
58 other functionalities ar useful but not mandatory
59 */
60 
66 {
67  // feature dimension
68  dim_s = 1;
69  nbParameters = 3;
70 
71  // memory allocation
72  s.resize(dim_s);
73  if (flags == nullptr)
74  flags = new bool[nbParameters];
75  for (unsigned int i = 0; i < nbParameters; i++)
76  flags[i] = false;
77  x = y = 0.;
78  Z = 1.;
79 }
80 
84 vpFeatureDepth::vpFeatureDepth() : x(0), y(0), Z(1.) { init(); }
85 
92 void vpFeatureDepth::set_LogZoverZstar(double LogZoverZstar) { s[0] = LogZoverZstar; }
93 
100 double vpFeatureDepth::get_LogZoverZstar() const { return s[0]; }
101 
108 void vpFeatureDepth::set_x(double x_)
109 {
110  this->x = x_;
111  flags[0] = true;
112 }
113 
120 double vpFeatureDepth::get_x() const { return x; }
121 
128 void vpFeatureDepth::set_y(double y_)
129 {
130  this->y = y_;
131  flags[1] = true;
132 }
133 
140 double vpFeatureDepth::get_y() const { return y; }
141 
148 void vpFeatureDepth::set_Z(double Z_)
149 {
150  this->Z = Z_;
151  flags[2] = true;
152 }
153 
160 double vpFeatureDepth::get_Z() const { return Z; }
161 
174 void vpFeatureDepth::set_xyZLogZoverZstar(double x_, double y_, double Z_, double LogZoverZstar)
175 {
176  set_x(x_);
177  set_y(y_);
178  set_Z(Z_);
179  set_LogZoverZstar(LogZoverZstar);
180  for (unsigned int i = 0; i < nbParameters; i++)
181  flags[i] = true;
182 }
183 
210 {
211  vpMatrix L;
212 
214  for (unsigned int i = 0; i < nbParameters; i++) {
215  if (flags[i] == false) {
216  switch (i) {
217  case 0:
218  vpTRACE("Warning !!! The interaction matrix is computed but x was "
219  "not set yet");
220  break;
221  case 1:
222  vpTRACE("Warning !!! The interaction matrix is computed but y was "
223  "not set yet");
224  break;
225  case 2:
226  vpTRACE("Warning !!! The interaction matrix is computed but z was "
227  "not set yet");
228  break;
229  default:
230  vpTRACE("Problem during the reading of the variable flags");
231  }
232  }
233  }
234  resetFlags();
235  }
236 
237  L.resize(1, 6);
238 
239  double x_ = get_x();
240  double y_ = get_y();
241  double Z_ = get_Z();
242 
243  if (Z_ < 0) {
244  vpERROR_TRACE("Point is behind the camera ");
245  std::cout << "Z = " << Z_ << std::endl;
246 
247  throw(vpFeatureException(vpFeatureException::badInitializationError, "Point is behind the camera "));
248  }
249 
250  if (fabs(Z_) < 1e-6) {
251  vpERROR_TRACE("Point Z coordinates is null ");
252  std::cout << "Z = " << Z_ << std::endl;
253 
254  throw(vpFeatureException(vpFeatureException::badInitializationError, "Point Z coordinates is null"));
255  }
256 
257  if (FEATURE_ALL & select) {
258  L = 0;
259  L[0][0] = 0;
260  L[0][1] = 0;
261  L[0][2] = -1 / Z_;
262  L[0][3] = -y_;
263  L[0][4] = x_;
264  L[0][5] = 0;
265  }
266 
267  return L;
268 }
269 
306 vpColVector vpFeatureDepth::error(const vpBasicFeature &s_star, unsigned int select)
307 {
308 
309  if (fabs(s_star.get_s().sumSquare()) > 1e-6) {
310  vpERROR_TRACE("s* should be zero ! ");
311  throw(vpFeatureException(vpFeatureException::badInitializationError, "s* should be zero !"));
312  }
313 
314  vpColVector e(1);
315  if (FEATURE_ALL & select) {
316  e[0] = s[0];
317  }
318 
319  return e;
320 }
321 
337 void vpFeatureDepth::print(unsigned int select) const
338 {
339  if (FEATURE_ALL & select) {
340  std::cout << "Point: x=" << get_x();
341  std::cout << " Point: y=" << get_y();
342  std::cout << " Point: Z=" << get_Z();
343 
344  std::cout << " log(Z/Z*)=" << get_LogZoverZstar();
345 
346  std::cout << std::endl;
347  }
348 }
349 
350 #ifdef VISP_BUILD_DEPRECATED_FUNCTIONS
363 void vpFeatureDepth::buildFrom(double x_, double y_, double Z_, double LogZoverZstar)
364 {
365  build(x_, y_, Z_, LogZoverZstar);
366 }
367 #endif
368 
380 vpFeatureDepth &vpFeatureDepth::build(const double &x_, const double &y_, const double &Z_, const double &LogZoverZstar)
381 {
382 
383  s[0] = LogZoverZstar;
384 
385  this->x = x_;
386  this->y = y_;
387  this->Z = Z_;
388 
389  if (Z < 0) {
390  vpERROR_TRACE("Point is behind the camera ");
391  std::cout << "Z = " << Z << std::endl;
392 
393  throw(vpFeatureException(vpFeatureException::badInitializationError, "Point is behind the camera "));
394  }
395 
396  if (fabs(Z) < 1e-6) {
397  vpERROR_TRACE("Point Z coordinates is null ");
398  std::cout << "Z = " << Z << std::endl;
399 
400  throw(vpFeatureException(vpFeatureException::badInitializationError, "Point Z coordinates is null"));
401  }
402 
403  for (unsigned int i = 0; i < nbParameters; ++i) {
404  flags[i] = true;
405  }
406  return *this;
407 }
408 
420 {
421  vpFeatureDepth *feature = new vpFeatureDepth;
422  return feature;
423 }
424 
430 void vpFeatureDepth::display(const vpCameraParameters & /* cam */, const vpImage<unsigned char> & /* I */,
431  const vpColor & /* color */, unsigned int /* thickness */) const
432 {
433  static int firsttime = 0;
434 
435  if (firsttime == 0) {
436  firsttime = 1;
437  vpERROR_TRACE("not implemented");
438  // Do not throw and error since it is not subject
439  // to produce a failure
440  }
441 }
447 void vpFeatureDepth::display(const vpCameraParameters & /* cam */, const vpImage<vpRGBa> & /* I */,
448  const vpColor & /* color */, unsigned int /* thickness */) const
449 {
450  static int firsttime = 0;
451 
452  if (firsttime == 0) {
453  firsttime = 1;
454  vpERROR_TRACE("not implemented");
455  // Do not throw and error since it is not subject
456  // to produce a failure
457  }
458 }
460 /*
461  * Local variables:
462  * c-basic-offset: 2
463  * End:
464  */
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.
vpColVector get_s(unsigned int select=FEATURE_ALL) const
Get the feature vector .
unsigned int dim_s
Dimension of the visual feature.
vpBasicFeatureDeallocatorType deallocate
Generic class defining intrinsic camera parameters.
Implementation of column vector and the associated operations.
Definition: vpColVector.h:171
double sumSquare() const
void resize(unsigned int i, bool flagNullify=true)
Definition: vpColVector.h:1066
Class to define RGB colors available for display functionalities.
Definition: vpColor.h:153
Class that defines a 3D point visual feature which is composed by one parameters that is that defin...
void print(unsigned int select=FEATURE_ALL) const vp_override
void set_x(double x)
void init() vp_override
void buildFrom(double x, double y, double Z, double LogZoverZstar)
double get_y() const
vpFeatureDepth & build(const double &x, const double &y, const double &Z, const double &LogZoverZstar)
void set_y(double y)
double get_LogZoverZstar() const
void set_LogZoverZstar(double LogZoverZstar)
void set_xyZLogZoverZstar(double x, double y, double Z, double logZZs)
void display(const vpCameraParameters &cam, const vpImage< unsigned char > &I, const vpColor &color=vpColor::green, unsigned int thickness=1) const vp_override
double get_Z() const
vpColVector error(const vpBasicFeature &s_star, unsigned int select=FEATURE_ALL) vp_override
void set_Z(double Z)
double get_x() const
vpFeatureDepth * duplicate() const vp_override
vpMatrix interaction(unsigned int select=FEATURE_ALL) vp_override
Error that can be emitted by the vpBasicFeature class and its derivates.
@ badInitializationError
Wrong feature initialization.
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:151
#define vpTRACE
Definition: vpDebug.h:408
#define vpERROR_TRACE
Definition: vpDebug.h:385