Visual Servoing Platform  version 3.6.1 under development (2024-07-27)
vpFeaturePoint3D.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  * 3D point visual feature.
33  *
34 *****************************************************************************/
35 
36 #include <visp3/visual_features/vpBasicFeature.h>
37 #include <visp3/visual_features/vpFeaturePoint3D.h>
38 
39 // Exception
40 #include <visp3/core/vpException.h>
41 #include <visp3/visual_features/vpFeatureException.h>
42 
43 // Debug trace
44 #include <visp3/core/vpDebug.h>
45 
46 /*
47 
48  attributes and members directly related to the vpBasicFeature needs
49  other functionalities are useful but not mandatory
50 
51 */
52 
62 {
63  // feature dimension
64  dim_s = 3;
65  nbParameters = 3;
66 
67  // memory allocation
68  s.resize(dim_s);
69  if (flags == nullptr)
70  flags = new bool[nbParameters];
71  for (unsigned int i = 0; i < nbParameters; i++)
72  flags[i] = false;
73 
74  // default value XYZ
75  s[0] = 0;
76  s[1] = 0;
77  s[2] = 1;
78 }
79 
87 
98 {
99  s[0] = X;
100  flags[0] = true;
101 }
102 
113 {
114  s[1] = Y;
115  flags[1] = true;
116 }
117 
128 {
129  s[2] = Z;
130  flags[2] = true;
131 }
132 
141 void vpFeaturePoint3D::set_XYZ(double X, double Y, double Z)
142 {
143  set_X(X);
144  set_Y(Y);
145  set_Z(Z);
146 
147  for (unsigned int i = 0; i < nbParameters; i++)
148  flags[i] = true;
149 }
150 
152 double vpFeaturePoint3D::get_X() const { return s[0]; }
153 
155 double vpFeaturePoint3D::get_Y() const { return s[1]; }
156 
158 double vpFeaturePoint3D::get_Z() const { return s[2]; }
159 
228 {
229  vpMatrix L;
230 
231  L.resize(0, 6);
232 
234  for (unsigned int i = 0; i < nbParameters; i++) {
235  if (flags[i] == false) {
236  switch (i) {
237  case 0:
238  vpTRACE("Warning !!! The interaction matrix is computed but X was "
239  "not set yet");
240  break;
241  case 1:
242  vpTRACE("Warning !!! The interaction matrix is computed but Y was "
243  "not set yet");
244  break;
245  case 2:
246  vpTRACE("Warning !!! The interaction matrix is computed but Z was "
247  "not set yet");
248  break;
249  default:
250  vpTRACE("Problem during the reading of the variable flags");
251  }
252  }
253  }
254  resetFlags();
255  }
256 
257  double X = get_X();
258  double Y = get_Y();
259  double Z = get_Z();
260 
261  if (vpFeaturePoint3D::selectX() & select) {
262  vpMatrix Lx(1, 6);
263  Lx = 0;
264 
265  Lx[0][0] = -1;
266  Lx[0][1] = 0;
267  Lx[0][2] = 0;
268  Lx[0][3] = 0;
269  Lx[0][4] = -Z;
270  Lx[0][5] = Y;
271 
272  L = vpMatrix::stack(L, Lx);
273  }
274 
275  if (vpFeaturePoint3D::selectY() & select) {
276  vpMatrix Ly(1, 6);
277  Ly = 0;
278 
279  Ly[0][0] = 0;
280  Ly[0][1] = -1;
281  Ly[0][2] = 0;
282  Ly[0][3] = Z;
283  Ly[0][4] = 0;
284  Ly[0][5] = -X;
285 
286  L = vpMatrix::stack(L, Ly);
287  }
288  if (vpFeaturePoint3D::selectZ() & select) {
289  vpMatrix Lz(1, 6);
290  Lz = 0;
291 
292  Lz[0][0] = 0;
293  Lz[0][1] = 0;
294  Lz[0][2] = -1;
295  Lz[0][3] = -Y;
296  Lz[0][4] = X;
297  Lz[0][5] = 0;
298 
299  L = vpMatrix::stack(L, Lz);
300  }
301  return L;
302 }
303 
355 vpColVector vpFeaturePoint3D::error(const vpBasicFeature &s_star, unsigned int select)
356 {
357  vpColVector e(0);
358 
359  try {
360  if (vpFeaturePoint3D::selectX() & select) {
361  vpColVector ex(1);
362  ex[0] = s[0] - s_star[0];
363 
364  e = vpColVector::stack(e, ex);
365  }
366 
367  if (vpFeaturePoint3D::selectY() & select) {
368  vpColVector ey(1);
369  ey[0] = s[1] - s_star[1];
370  e = vpColVector::stack(e, ey);
371  }
372 
373  if (vpFeaturePoint3D::selectZ() & select) {
374  vpColVector ez(1);
375  ez[0] = s[2] - s_star[2];
376  e = vpColVector::stack(e, ez);
377  }
378  }
379  catch (...) {
380  throw;
381  }
382 
383  return e;
384 }
385 
386 #ifdef VISP_BUILD_DEPRECATED_FUNCTIONS
404 {
405  build(p);
406 }
407 
424 void vpFeaturePoint3D::buildFrom(double X, double Y, double Z)
425 {
426  build(X, Y, Z);
427 }
428 #endif
429 
447 {
448 
449  // cP is expressed in homogeneous coordinates
450  // we devide by the fourth coordinate
451  s[0] = p.cP[0] / p.cP[3];
452  s[1] = p.cP[1] / p.cP[3];
453  s[2] = p.cP[2] / p.cP[3];
454 
455  double Z = s[2];
456  if (Z < 0) {
457  vpERROR_TRACE("Point is behind the camera ");
458  std::cout << "Z = " << Z << std::endl;
459 
460  throw(vpFeatureException(vpFeatureException::badInitializationError, "Point is behind the camera "));
461  }
462 
463  if (fabs(Z) < 1e-6) {
464  vpERROR_TRACE("Point Z coordinates is null ");
465  std::cout << "Z = " << Z << std::endl;
466 
467  throw(vpFeatureException(vpFeatureException::badInitializationError, "Point Z coordinates is null"));
468  }
469 
470  for (unsigned int i = 0; i < nbParameters; ++i) {
471  flags[i] = true;
472  }
473  return *this;
474 }
475 
492 vpFeaturePoint3D &vpFeaturePoint3D::build(const double &X, const double &Y, const double &Z)
493 {
494  s[0] = X;
495  s[1] = Y;
496  s[2] = Z;
497 
498  if (Z < 0) {
499  vpERROR_TRACE("Point is behind the camera ");
500  std::cout << "Z = " << Z << std::endl;
501 
502  throw(vpFeatureException(vpFeatureException::badInitializationError, "Point is behind the camera "));
503  }
504 
505  if (fabs(Z) < 1e-6) {
506  vpERROR_TRACE("Point Z coordinates is null ");
507  std::cout << "Z = " << Z << std::endl;
508 
509  throw(vpFeatureException(vpFeatureException::badInitializationError, "Point Z coordinates is null"));
510  }
511 
512  for (unsigned int i = 0; i < nbParameters; ++i) {
513  flags[i] = true;
514  }
515  return *this;
516 }
517 
541 void vpFeaturePoint3D::print(unsigned int select) const
542 {
543 
544  std::cout << "Point3D: ";
545  if (vpFeaturePoint3D::selectX() & select)
546  std::cout << " X=" << get_X();
547  if (vpFeaturePoint3D::selectY() & select)
548  std::cout << " Y=" << get_Y();
549  if (vpFeaturePoint3D::selectZ() & select)
550  std::cout << " Z=" << get_Z();
551  std::cout << std::endl;
552 }
553 
566 {
567  vpFeaturePoint3D *feature = new vpFeaturePoint3D;
568  return feature;
569 }
570 
576  const vpColor & /* color */, unsigned int /* thickness */) const
577 {
578  static int firsttime = 0;
579 
580  if (firsttime == 0) {
581  firsttime = 1;
582  vpERROR_TRACE("not implemented");
583  // Do not throw and error since it is not subject
584  // to produce a failure
585  }
586 }
587 
592 void vpFeaturePoint3D::display(const vpCameraParameters & /*cam*/, const vpImage<vpRGBa> & /* I */,
593  const vpColor & /* color */, unsigned int /* thickness */) const
594 {
595  static int firsttime = 0;
596 
597  if (firsttime == 0) {
598  firsttime = 1;
599  vpERROR_TRACE("not implemented");
600  // Do not throw and error since it is not subject
601  // to produce a failure
602  }
603 }
629 unsigned int vpFeaturePoint3D::selectX() { return FEATURE_LINE[0]; }
630 
656 unsigned int vpFeaturePoint3D::selectY() { return FEATURE_LINE[1]; }
657 
682 unsigned int vpFeaturePoint3D::selectZ() { return FEATURE_LINE[2]; }
683 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
Error that can be emitted by the vpBasicFeature class and its derivates.
@ badInitializationError
Wrong feature initialization.
Class that defines the 3D point visual feature.
static unsigned int selectX()
double get_X() const
Return the coordinate in the camera frame of the 3D point.
vpFeaturePoint3D & build(const vpPoint &p)
void set_XYZ(double X, double Y, double Z)
double get_Y() const
Return the coordinate in the camera frame of the 3D point.
vpFeaturePoint3D * duplicate() const VP_OVERRIDE
static unsigned int selectZ()
vpColVector error(const vpBasicFeature &s_star, unsigned int select=FEATURE_ALL) VP_OVERRIDE
void print(unsigned int select=FEATURE_ALL) const VP_OVERRIDE
void buildFrom(const vpPoint &p)
void set_Y(double Y)
static unsigned int selectY()
void init() VP_OVERRIDE
vpMatrix interaction(unsigned int select=FEATURE_ALL) VP_OVERRIDE
void set_Z(double Z)
void set_X(double X)
double get_Z() const
Return the coordinate in the camera frame of the 3D point.
void display(const vpCameraParameters &cam, const vpImage< unsigned char > &I, const vpColor &color=vpColor::green, unsigned int thickness=1) const VP_OVERRIDE
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:169
void stack(const vpMatrix &A)
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition: vpPoint.h:79
vpColVector cP
Definition: vpTracker.h:73
#define vpTRACE
Definition: vpDebug.h:436
#define vpERROR_TRACE
Definition: vpDebug.h:409