Visual Servoing Platform  version 3.5.0 under development (2022-02-15)
vpFeatureDepth.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 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 http://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  * Authors:
35  * Nicolas Melchior
36  *
37  *****************************************************************************/
38 
44 #include <visp3/visual_features/vpBasicFeature.h>
45 #include <visp3/visual_features/vpFeatureDepth.h>
46 
47 // Exception
48 #include <visp3/core/vpException.h>
49 #include <visp3/visual_features/vpFeatureException.h>
50 
51 // Debug trace
52 #include <visp3/core/vpDebug.h>
53 
54 // math
55 #include <visp3/core/vpMath.h>
56 
57 #include <visp3/core/vpFeatureDisplay.h>
58 
59 /*
60 
61 
62 
63 attributes and members directly related to the vpBasicFeature needs
64 other functionalities ar useful but not mandatory
65 
66 
67 
68 
69 
70 */
71 
76 {
77  // feature dimension
78  dim_s = 1;
79  nbParameters = 3;
80 
81  // memory allocation
82  s.resize(dim_s);
83  if (flags == NULL)
84  flags = new bool[nbParameters];
85  for (unsigned int i = 0; i < nbParameters; i++)
86  flags[i] = false;
87  x = y = 0.;
88  Z = 1.;
89 }
90 
94 vpFeatureDepth::vpFeatureDepth() : x(0), y(0), Z(1.) { init(); }
95 
102 void vpFeatureDepth::set_LogZoverZstar(double LogZoverZstar) { s[0] = LogZoverZstar; }
103 
110 double vpFeatureDepth::get_LogZoverZstar() const { return s[0]; }
111 
118 void vpFeatureDepth::set_x(double x_)
119 {
120  this->x = x_;
121  flags[0] = true;
122 }
123 
130 double vpFeatureDepth::get_x() const { return x; }
131 
138 void vpFeatureDepth::set_y(double y_)
139 {
140  this->y = y_;
141  flags[1] = true;
142 }
143 
150 double vpFeatureDepth::get_y() const { return y; }
151 
158 void vpFeatureDepth::set_Z(double Z_)
159 {
160  this->Z = Z_;
161  flags[2] = true;
162 }
163 
170 double vpFeatureDepth::get_Z() const { return Z; }
171 
184 void vpFeatureDepth::set_xyZLogZoverZstar(double x_, double y_, double Z_, double LogZoverZstar)
185 {
186  set_x(x_);
187  set_y(y_);
188  set_Z(Z_);
189  set_LogZoverZstar(LogZoverZstar);
190  for (unsigned int i = 0; i < nbParameters; i++)
191  flags[i] = true;
192 }
193 
220 {
221  vpMatrix L;
222 
224  for (unsigned int i = 0; i < nbParameters; i++) {
225  if (flags[i] == false) {
226  switch (i) {
227  case 0:
228  vpTRACE("Warning !!! The interaction matrix is computed but x was "
229  "not set yet");
230  break;
231  case 1:
232  vpTRACE("Warning !!! The interaction matrix is computed but y was "
233  "not set yet");
234  break;
235  case 2:
236  vpTRACE("Warning !!! The interaction matrix is computed but z was "
237  "not set yet");
238  break;
239  default:
240  vpTRACE("Problem during the reading of the variable flags");
241  }
242  }
243  }
244  resetFlags();
245  }
246 
247  L.resize(1, 6);
248 
249  double x_ = get_x();
250  double y_ = get_y();
251  double Z_ = get_Z();
252 
253  if (Z_ < 0) {
254  vpERROR_TRACE("Point is behind the camera ");
255  std::cout << "Z = " << Z_ << std::endl;
256 
257  throw(vpFeatureException(vpFeatureException::badInitializationError, "Point is behind the camera "));
258  }
259 
260  if (fabs(Z_) < 1e-6) {
261  vpERROR_TRACE("Point Z coordinates is null ");
262  std::cout << "Z = " << Z_ << std::endl;
263 
264  throw(vpFeatureException(vpFeatureException::badInitializationError, "Point Z coordinates is null"));
265  }
266 
267  if (FEATURE_ALL & select) {
268  L = 0;
269  L[0][0] = 0;
270  L[0][1] = 0;
271  L[0][2] = -1 / Z_;
272  L[0][3] = -y_;
273  L[0][4] = x_;
274  L[0][5] = 0;
275  }
276 
277  return L;
278 }
279 
316 vpColVector vpFeatureDepth::error(const vpBasicFeature &s_star, unsigned int select)
317 {
318 
319  if (fabs(s_star.get_s().sumSquare()) > 1e-6) {
320  vpERROR_TRACE("s* should be zero ! ");
321  throw(vpFeatureException(vpFeatureException::badInitializationError, "s* should be zero !"));
322  }
323 
324  vpColVector e(1);
325  if (FEATURE_ALL & select) {
326  e[0] = s[0];
327  }
328 
329  return e;
330 }
331 
347 void vpFeatureDepth::print(unsigned int select) const
348 {
349  if (FEATURE_ALL & select) {
350  std::cout << "Point: x=" << get_x();
351  std::cout << " Point: y=" << get_y();
352  std::cout << " Point: Z=" << get_Z();
353 
354  std::cout << " log(Z/Z*)=" << get_LogZoverZstar();
355 
356  std::cout << std::endl;
357  }
358 }
359 
371 void vpFeatureDepth::buildFrom(double x_, double y_, double Z_, double LogZoverZstar)
372 {
373 
374  s[0] = LogZoverZstar;
375 
376  this->x = x_;
377  this->y = y_;
378  this->Z = Z_;
379 
380  if (Z < 0) {
381  vpERROR_TRACE("Point is behind the camera ");
382  std::cout << "Z = " << Z << std::endl;
383 
384  throw(vpFeatureException(vpFeatureException::badInitializationError, "Point is behind the camera "));
385  }
386 
387  if (fabs(Z) < 1e-6) {
388  vpERROR_TRACE("Point Z coordinates is null ");
389  std::cout << "Z = " << Z << std::endl;
390 
391  throw(vpFeatureException(vpFeatureException::badInitializationError, "Point Z coordinates is null"));
392  }
393 
394  for (unsigned int i = 0; i < nbParameters; i++)
395  flags[i] = true;
396 }
397 
409 {
410  vpFeatureDepth *feature = new vpFeatureDepth;
411  return feature;
412 }
413 
419 void vpFeatureDepth::display(const vpCameraParameters & /* cam */, const vpImage<unsigned char> & /* I */,
420  const vpColor & /* color */, unsigned int /* thickness */) const
421 {
422  static int firsttime = 0;
423 
424  if (firsttime == 0) {
425  firsttime = 1;
426  vpERROR_TRACE("not implemented");
427  // Do not throw and error since it is not subject
428  // to produce a failure
429  }
430 }
436 void vpFeatureDepth::display(const vpCameraParameters & /* cam */, const vpImage<vpRGBa> & /* I */,
437  const vpColor & /* color */, unsigned int /* thickness */) const
438 {
439  static int firsttime = 0;
440 
441  if (firsttime == 0) {
442  firsttime = 1;
443  vpERROR_TRACE("not implemented");
444  // Do not throw and error since it is not subject
445  // to produce a failure
446  }
447 }
448 
449 /*
450  * Local variables:
451  * c-basic-offset: 2
452  * End:
453  */
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:153
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify=true, bool recopy_=true)
Definition: vpArray2D.h:304
void set_x(double x)
#define vpERROR_TRACE
Definition: vpDebug.h:393
Class to define RGB colors available for display functionnalities.
Definition: vpColor.h:157
void buildFrom(double x, double y, double Z, double LogZoverZstar)
Class that defines a 3D point visual feature which is composed by one parameters that is that defin...
unsigned int dim_s
Dimension of the visual feature.
double get_y() const
vpColVector get_s(unsigned int select=FEATURE_ALL) const
Get the feature vector .
void set_y(double y)
class that defines what is a visual feature
void print(unsigned int select=FEATURE_ALL) const
void set_Z(double Z)
#define vpTRACE
Definition: vpDebug.h:416
Error that can be emited by the vpBasicFeature class and its derivates.
Generic class defining intrinsic camera parameters.
void set_xyZLogZoverZstar(double x, double y, double Z, double logZZs)
void set_LogZoverZstar(double LogZoverZstar)
vpFeatureDepth * duplicate() const
double get_Z() const
vpColVector error(const vpBasicFeature &s_star, unsigned int select=FEATURE_ALL)
void display(const vpCameraParameters &cam, const vpImage< unsigned char > &I, const vpColor &color=vpColor::green, unsigned int thickness=1) const
void resize(unsigned int i, bool flagNullify=true)
Definition: vpColVector.h:310
vpBasicFeatureDeallocatorType deallocate
double get_LogZoverZstar() const
Implementation of column vector and the associated operations.
Definition: vpColVector.h:130
double sumSquare() const
double get_x() const
vpMatrix interaction(unsigned int select=FEATURE_ALL)
unsigned int nbParameters
Number of parameters needed to compute the interaction matrix.
vpColVector s
State of the visual feature.