Visual Servoing Platform  version 3.6.1 under development (2024-12-17)
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 
53 BEGIN_VISP_NAMESPACE
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 
403 {
404  // cP is expressed in homogeneous coordinates
405  // we devide by the fourth coordinate
406  s[0] = p.cP[0] / p.cP[3];
407  s[1] = p.cP[1] / p.cP[3];
408  s[2] = p.cP[2] / p.cP[3];
409 
410  double Z = s[2];
411  if (Z < 0) {
412  vpERROR_TRACE("Point is behind the camera ");
413  std::cout << "Z = " << Z << std::endl;
414 
415  throw(vpFeatureException(vpFeatureException::badInitializationError, "Point is behind the camera "));
416  }
417 
418  if (fabs(Z) < 1e-6) {
419  vpERROR_TRACE("Point Z coordinates is null ");
420  std::cout << "Z = " << Z << std::endl;
421 
422  throw(vpFeatureException(vpFeatureException::badInitializationError, "Point Z coordinates is null"));
423  }
424 
425  for (unsigned int i = 0; i < nbParameters; ++i) {
426  flags[i] = true;
427  }
428  return *this;
429 }
430 
447 vpFeaturePoint3D &vpFeaturePoint3D::buildFrom(const double &X, const double &Y, const double &Z)
448 {
449  s[0] = X;
450  s[1] = Y;
451  s[2] = Z;
452 
453  if (Z < 0) {
454  vpERROR_TRACE("Point is behind the camera ");
455  std::cout << "Z = " << Z << std::endl;
456 
457  throw(vpFeatureException(vpFeatureException::badInitializationError, "Point is behind the camera "));
458  }
459 
460  if (fabs(Z) < 1e-6) {
461  vpERROR_TRACE("Point Z coordinates is null ");
462  std::cout << "Z = " << Z << std::endl;
463 
464  throw(vpFeatureException(vpFeatureException::badInitializationError, "Point Z coordinates is null"));
465  }
466 
467  for (unsigned int i = 0; i < nbParameters; ++i) {
468  flags[i] = true;
469  }
470  return *this;
471 }
472 
496 void vpFeaturePoint3D::print(unsigned int select) const
497 {
498 
499  std::cout << "Point3D: ";
500  if (vpFeaturePoint3D::selectX() & select)
501  std::cout << " X=" << get_X();
502  if (vpFeaturePoint3D::selectY() & select)
503  std::cout << " Y=" << get_Y();
504  if (vpFeaturePoint3D::selectZ() & select)
505  std::cout << " Z=" << get_Z();
506  std::cout << std::endl;
507 }
508 
521 {
522  vpFeaturePoint3D *feature = new vpFeaturePoint3D;
523  return feature;
524 }
525 
531  const vpColor & /* color */, unsigned int /* thickness */) const
532 {
533  static int firsttime = 0;
534 
535  if (firsttime == 0) {
536  firsttime = 1;
537  vpERROR_TRACE("not implemented");
538  // Do not throw and error since it is not subject
539  // to produce a failure
540  }
541 }
542 
547 void vpFeaturePoint3D::display(const vpCameraParameters & /*cam*/, const vpImage<vpRGBa> & /* I */,
548  const vpColor & /* color */, unsigned int /* thickness */) const
549 {
550  static int firsttime = 0;
551 
552  if (firsttime == 0) {
553  firsttime = 1;
554  vpERROR_TRACE("not implemented");
555  // Do not throw and error since it is not subject
556  // to produce a failure
557  }
558 }
584 unsigned int vpFeaturePoint3D::selectX() { return FEATURE_LINE[0]; }
585 
611 unsigned int vpFeaturePoint3D::selectY() { return FEATURE_LINE[1]; }
612 
637 unsigned int vpFeaturePoint3D::selectZ() { return FEATURE_LINE[2]; }
638 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.
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
vpFeaturePoint3D & buildFrom(const vpPoint &p)
void print(unsigned int select=FEATURE_ALL) const VP_OVERRIDE
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