Visual Servoing Platform  version 3.2.1 under development (2019-05-26)
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(const double LogZoverZstar) { s[0] = LogZoverZstar; }
103 
110 double vpFeatureDepth::get_LogZoverZstar() const { return s[0]; }
111 
118 void vpFeatureDepth::set_x(const 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(const 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(const 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(const double x_, const double y_, const double Z_, const 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 vpMatrix vpFeatureDepth::interaction(const unsigned int select)
221 {
222  vpMatrix L;
223 
225  for (unsigned int i = 0; i < nbParameters; i++) {
226  if (flags[i] == false) {
227  switch (i) {
228  case 0:
229  vpTRACE("Warning !!! The interaction matrix is computed but x was "
230  "not set yet");
231  break;
232  case 1:
233  vpTRACE("Warning !!! The interaction matrix is computed but y was "
234  "not set yet");
235  break;
236  case 2:
237  vpTRACE("Warning !!! The interaction matrix is computed but z was "
238  "not set yet");
239  break;
240  default:
241  vpTRACE("Problem during the reading of the variable flags");
242  }
243  }
244  }
245  resetFlags();
246  }
247 
248  L.resize(1, 6);
249 
250  double x_ = get_x();
251  double y_ = get_y();
252  double Z_ = get_Z();
253 
254  if (Z_ < 0) {
255  vpERROR_TRACE("Point is behind the camera ");
256  std::cout << "Z = " << Z_ << std::endl;
257 
258  throw(vpFeatureException(vpFeatureException::badInitializationError, "Point is behind the camera "));
259  }
260 
261  if (fabs(Z_) < 1e-6) {
262  vpERROR_TRACE("Point Z coordinates is null ");
263  std::cout << "Z = " << Z_ << std::endl;
264 
265  throw(vpFeatureException(vpFeatureException::badInitializationError, "Point Z coordinates is null"));
266  }
267 
268  if (FEATURE_ALL & select) {
269  L = 0;
270  L[0][0] = 0;
271  L[0][1] = 0;
272  L[0][2] = -1 / Z_;
273  L[0][3] = -y_;
274  L[0][4] = x_;
275  L[0][5] = 0;
276  }
277 
278  return L;
279 }
280 
317 vpColVector vpFeatureDepth::error(const vpBasicFeature &s_star, const unsigned int select)
318 {
319 
320  if (fabs(s_star.get_s().sumSquare()) > 1e-6) {
321  vpERROR_TRACE("s* should be zero ! ");
322  throw(vpFeatureException(vpFeatureException::badInitializationError, "s* should be zero !"));
323  }
324 
325  vpColVector e(1);
326  if (FEATURE_ALL & select) {
327  e[0] = s[0];
328  }
329 
330  return e;
331 }
332 
348 void vpFeatureDepth::print(const unsigned int select) const
349 {
350  if (FEATURE_ALL & select) {
351  std::cout << "Point: x=" << get_x();
352  std::cout << " Point: y=" << get_y();
353  std::cout << " Point: Z=" << get_Z();
354 
355  std::cout << " log(Z/Z*)=" << get_LogZoverZstar();
356 
357  std::cout << std::endl;
358  }
359 }
360 
372 void vpFeatureDepth::buildFrom(const double x_, const double y_, const double Z_, const double LogZoverZstar)
373 {
374 
375  s[0] = LogZoverZstar;
376 
377  this->x = x_;
378  this->y = y_;
379  this->Z = Z_;
380 
381  if (Z < 0) {
382  vpERROR_TRACE("Point is behind the camera ");
383  std::cout << "Z = " << Z << std::endl;
384 
385  throw(vpFeatureException(vpFeatureException::badInitializationError, "Point is behind the camera "));
386  }
387 
388  if (fabs(Z) < 1e-6) {
389  vpERROR_TRACE("Point Z coordinates is null ");
390  std::cout << "Z = " << Z << std::endl;
391 
392  throw(vpFeatureException(vpFeatureException::badInitializationError, "Point Z coordinates is null"));
393  }
394 
395  for (unsigned int i = 0; i < nbParameters; i++)
396  flags[i] = true;
397 }
398 
410 {
411  vpFeatureDepth *feature = new vpFeatureDepth;
412  return feature;
413 }
414 
420 void vpFeatureDepth::display(const vpCameraParameters & /* cam */, const vpImage<unsigned char> & /* I */,
421  const vpColor & /* color */, unsigned int /* thickness */) const
422 {
423  static int firsttime = 0;
424 
425  if (firsttime == 0) {
426  firsttime = 1;
427  vpERROR_TRACE("not implemented");
428  // Do not throw and error since it is not subject
429  // to produce a failure
430  }
431 }
437 void vpFeatureDepth::display(const vpCameraParameters & /* cam */, const vpImage<vpRGBa> & /* I */,
438  const vpColor & /* color */, unsigned int /* thickness */) const
439 {
440  static int firsttime = 0;
441 
442  if (firsttime == 0) {
443  firsttime = 1;
444  vpERROR_TRACE("not implemented");
445  // Do not throw and error since it is not subject
446  // to produce a failure
447  }
448 }
449 
450 /*
451  * Local variables:
452  * c-basic-offset: 2
453  * End:
454  */
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:164
void display(const vpCameraParameters &cam, const vpImage< unsigned char > &I, const vpColor &color=vpColor::green, unsigned int thickness=1) const
#define vpERROR_TRACE
Definition: vpDebug.h:393
void resize(const unsigned int nrows, const unsigned int ncols, const bool flagNullify=true, const bool recopy_=true)
Definition: vpArray2D.h:305
Class to define colors available for display functionnalities.
Definition: vpColor.h:120
void buildFrom(const double x, const double y, const double Z, const 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.
void print(const unsigned int select=FEATURE_ALL) const
void set_y(const double y)
double get_Z() const
void set_LogZoverZstar(const double LogZoverZstar)
class that defines what is a visual feature
vpMatrix interaction(const unsigned int select=FEATURE_ALL)
void set_x(const double x)
#define vpTRACE
Definition: vpDebug.h:416
Error that can be emited by the vpBasicFeature class and its derivates.
Generic class defining intrinsic camera parameters.
double get_y() const
double sumSquare() const
vpBasicFeatureDeallocatorType deallocate
Implementation of column vector and the associated operations.
Definition: vpColVector.h:130
void set_Z(const double Z)
double get_LogZoverZstar() const
void set_xyZLogZoverZstar(const double x, const double y, const double Z, const double logZZs)
vpFeatureDepth * duplicate() const
vpColVector error(const vpBasicFeature &s_star, const unsigned int select=FEATURE_ALL)
vpColVector get_s(unsigned int select=FEATURE_ALL) const
Get the feature vector .
double get_x() const
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:310