Visual Servoing Platform  version 3.6.1 under development (2024-07-27)
vpFeatureTranslation.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 translation visual feature.
33  *
34 *****************************************************************************/
35 
41 #include <visp3/visual_features/vpBasicFeature.h>
42 #include <visp3/visual_features/vpFeatureTranslation.h>
43 
44 #include <visp3/core/vpMath.h>
45 
46 // Exception
47 #include <visp3/core/vpException.h>
48 #include <visp3/visual_features/vpFeatureException.h>
49 
50 // Debug trace
51 #include <visp3/core/vpDebug.h>
52 
53 /*
54 
55 attributes and members directly related to the vpBasicFeature needs
56 other functionalities are useful but not mandatory
57 
58 */
59 
67 {
68  // feature dimension
69  dim_s = 3;
70  nbParameters = 1;
71 
72  // memory allocation
73  s.resize(dim_s);
74  if (flags == nullptr)
75  flags = new bool[nbParameters];
76  for (unsigned int i = 0; i < nbParameters; i++)
77  flags[i] = false;
78 }
79 
87 
96 {
97  init();
98 }
99 
113  : f2Mf1(), translation(r)
114 {
115  init();
116 
117  build(f2Mf1_);
118 }
119 
120 #ifdef VISP_BUILD_DEPRECATED_FUNCTIONS
132 {
133  build(f2Mf1_);
134 }
135 #endif
136 
147 {
148  this->f2Mf1 = f2Mf1_;
149  s[0] = f2Mf1[0][3];
150  s[1] = f2Mf1[1][3];
151  s[2] = f2Mf1[2][3];
152 
153  flags[0] = true;
154  return *this;
155 }
156 
167 {
168  translation = r;
169 }
170 
180 void vpFeatureTranslation::set_Tx(double t_x) { s[0] = t_x; }
190 void vpFeatureTranslation::set_Ty(double t_y) { s[1] = t_y; }
200 void vpFeatureTranslation::set_Tz(double t_z) { s[2] = t_z; }
201 
212 {
213  return translation;
214 }
215 
221 double vpFeatureTranslation::get_Tx() const { return s[0]; }
222 
228 double vpFeatureTranslation::get_Ty() const { return s[1]; }
229 
235 double vpFeatureTranslation::get_Tz() const { return s[2]; }
236 
319 {
320 
321  vpMatrix L;
322  L.resize(0, 6);
323 
325  for (unsigned int i = 0; i < nbParameters; i++) {
326  if (flags[i] == false) {
327  switch (i) {
328  case 0:
329  vpTRACE("Warning !!! The interaction matrix is computed but f2Mf1 "
330  "was not set yet");
331  break;
332  default:
333  vpTRACE("Problem during the reading of the variable flags");
334  }
335  }
336  }
337  resetFlags();
338  }
339 
340  if (translation == cdMc) {
341  // This version is a simplification
342  if (vpFeatureTranslation::selectTx() & select) {
343  vpMatrix Lx(1, 6);
344 
345  for (int i = 0; i < 3; i++)
346  Lx[0][i] = f2Mf1[0][i];
347  Lx[0][3] = 0;
348  Lx[0][4] = 0;
349  Lx[0][5] = 0;
350 
351  L = vpMatrix::stack(L, Lx);
352  }
353 
354  if (vpFeatureTranslation::selectTy() & select) {
355  vpMatrix Ly(1, 6);
356 
357  for (int i = 0; i < 3; i++)
358  Ly[0][i] = f2Mf1[1][i];
359  Ly[0][3] = 0;
360  Ly[0][4] = 0;
361  Ly[0][5] = 0;
362 
363  L = vpMatrix::stack(L, Ly);
364  }
365 
366  if (vpFeatureTranslation::selectTz() & select) {
367  vpMatrix Lz(1, 6);
368 
369  for (int i = 0; i < 3; i++)
370  Lz[0][i] = f2Mf1[2][i];
371  Lz[0][3] = 0;
372  Lz[0][4] = 0;
373  Lz[0][5] = 0;
374 
375  L = vpMatrix::stack(L, Lz);
376  }
377  }
378  if (translation == cMcd) {
379  // This version is a simplification
380  if (vpFeatureTranslation::selectTx() & select) {
381  vpMatrix Lx(1, 6);
382  Lx[0][0] = -1;
383  Lx[0][1] = 0;
384  Lx[0][2] = 0;
385  Lx[0][3] = 0;
386  Lx[0][4] = -s[2];
387  Lx[0][5] = s[1];
388 
389  L = vpMatrix::stack(L, Lx);
390  }
391 
392  if (vpFeatureTranslation::selectTy() & select) {
393  vpMatrix Ly(1, 6);
394  Ly[0][0] = 0;
395  Ly[0][1] = -1;
396  Ly[0][2] = 0;
397  Ly[0][3] = s[2];
398  Ly[0][4] = 0;
399  Ly[0][5] = -s[0];
400 
401  L = vpMatrix::stack(L, Ly);
402  }
403 
404  if (vpFeatureTranslation::selectTz() & select) {
405  vpMatrix Lz(1, 6);
406  Lz[0][0] = 0;
407  Lz[0][1] = 0;
408  Lz[0][2] = -1;
409  Lz[0][3] = -s[1];
410  Lz[0][4] = s[0];
411  Lz[0][5] = 0;
412 
413  L = vpMatrix::stack(L, Lz);
414  }
415  }
416 
417  if (translation == cMo) {
418  // This version is a simplification
419  if (vpFeatureTranslation::selectTx() & select) {
420  vpMatrix Lx(1, 6);
421  Lx[0][0] = -1;
422  Lx[0][1] = 0;
423  Lx[0][2] = 0;
424  Lx[0][3] = 0;
425  Lx[0][4] = -s[2];
426  Lx[0][5] = s[1];
427 
428  L = vpMatrix::stack(L, Lx);
429  }
430 
431  if (vpFeatureTranslation::selectTy() & select) {
432  vpMatrix Ly(1, 6);
433  Ly[0][0] = 0;
434  Ly[0][1] = -1;
435  Ly[0][2] = 0;
436  Ly[0][3] = s[2];
437  Ly[0][4] = 0;
438  Ly[0][5] = -s[0];
439 
440  L = vpMatrix::stack(L, Ly);
441  }
442 
443  if (vpFeatureTranslation::selectTz() & select) {
444  vpMatrix Lz(1, 6);
445  Lz[0][0] = 0;
446  Lz[0][1] = 0;
447  Lz[0][2] = -1;
448  Lz[0][3] = -s[1];
449  Lz[0][4] = s[0];
450  Lz[0][5] = 0;
451 
452  L = vpMatrix::stack(L, Lz);
453  }
454  }
455 
456  return L;
457 }
458 
525 vpColVector vpFeatureTranslation::error(const vpBasicFeature &s_star, unsigned int select)
526 {
527  vpColVector e(0);
528 
529  if (translation == cdMc || translation == cMcd) {
530  if (s_star.get_s().sumSquare() > 1e-6) {
531  vpERROR_TRACE("s* should be zero ! ");
532  throw(vpFeatureException(vpFeatureException::badInitializationError, "s* should be zero !"));
533  }
534  }
535 
536  if (vpFeatureTranslation::selectTx() & select) {
537  vpColVector ex(1);
538  ex[0] = s[0] - s_star[0];
539  e = vpColVector::stack(e, ex);
540  }
541 
542  if (vpFeatureTranslation::selectTy() & select) {
543  vpColVector ey(1);
544  ey[0] = s[1] - s_star[1];
545  e = vpColVector::stack(e, ey);
546  }
547 
548  if (vpFeatureTranslation::selectTz() & select) {
549  vpColVector ez(1);
550  ez[0] = s[2] - s_star[2];
551  e = vpColVector::stack(e, ez);
552  }
553 
554  return e;
555 }
556 
580 void vpFeatureTranslation::print(unsigned int select) const
581 {
582  std::cout << "Translation 3D:";
583  if (vpFeatureTranslation::selectTx() & select) {
584  std::cout << " tx=" << s[0];
585  }
586  if (vpFeatureTranslation::selectTy() & select) {
587  std::cout << " ty=" << s[1];
588  }
589  if (vpFeatureTranslation::selectTz() & select) {
590  std::cout << " tz=" << s[2];
591  }
592  std::cout << std::endl;
593 }
594 
609 {
610  vpFeatureTranslation *feature = nullptr;
611  if (translation == cdMc)
612  feature = new vpFeatureTranslation(cdMc);
613  if (translation == cMo)
614  feature = new vpFeatureTranslation(cMo);
615  if (translation == cMcd)
616  feature = new vpFeatureTranslation(cMcd);
617  return feature;
618 }
619 
626  const vpColor & /* color */, unsigned int /* thickness */) const
627 {
628  static int firsttime = 0;
629 
630  if (firsttime == 0) {
631  firsttime = 1;
632  vpERROR_TRACE("not implemented");
633  // Do not throw and error since it is not subject
634  // to produce a failure
635  }
636 }
642 void vpFeatureTranslation::display(const vpCameraParameters & /* cam */, const vpImage<vpRGBa> & /* I */,
643  const vpColor & /* color */, unsigned int /* thickness */) const
644 {
645  static int firsttime = 0;
646 
647  if (firsttime == 0) {
648  firsttime = 1;
649  vpERROR_TRACE("not implemented");
650  // Do not throw and error since it is not subject
651  // to produce a failure
652  }
653 }
654 
699 unsigned int vpFeatureTranslation::selectTx() { return FEATURE_LINE[0]; }
700 
744 unsigned int vpFeatureTranslation::selectTy() { return FEATURE_LINE[1]; }
745 
789 unsigned int vpFeatureTranslation::selectTz() { return FEATURE_LINE[2]; }
790 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.
vpColVector get_s(unsigned int select=FEATURE_ALL) const
Get the feature vector .
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
double sumSquare() const
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 translation visual feature .
static unsigned int selectTz()
void buildFrom(const vpHomogeneousMatrix &f2Mf1)
void setFeatureTranslationType(const vpFeatureTranslationRepresentationType r)
vpColVector error(const vpBasicFeature &s_star, unsigned int select=FEATURE_ALL) VP_OVERRIDE
static unsigned int selectTx()
void print(unsigned int select=FEATURE_ALL) const VP_OVERRIDE
void display(const vpCameraParameters &cam, const vpImage< unsigned char > &I, const vpColor &color=vpColor::green, unsigned int thickness=1) const VP_OVERRIDE
vpFeatureTranslationRepresentationType getFeatureTranslationType() const
vpFeatureTranslation & build(const vpHomogeneousMatrix &f2Mf1)
vpMatrix interaction(unsigned int select=FEATURE_ALL) VP_OVERRIDE
vpFeatureTranslation * duplicate() const VP_OVERRIDE
Feature duplication.
static unsigned int selectTy()
Implementation of an homogeneous matrix and operations on such kind of matrices.
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:169
void stack(const vpMatrix &A)
#define vpTRACE
Definition: vpDebug.h:436
#define vpERROR_TRACE
Definition: vpDebug.h:409