Visual Servoing Platform  version 3.6.1 under development (2024-07-27)
vpFeatureMoment.cpp
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See https://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Base for all moment features
32  */
33 
34 #include <visp3/core/vpMath.h>
35 #include <visp3/core/vpMoment.h>
36 #include <visp3/core/vpMomentDatabase.h>
37 #include <visp3/visual_features/vpFeatureMoment.h>
38 #include <visp3/visual_features/vpFeatureMomentDatabase.h>
39 
40 #include <visp3/core/vpException.h>
41 #include <visp3/visual_features/vpFeatureException.h>
42 
43 #include <vector>
44 #include <visp3/core/vpDebug.h>
45 
47 class vpBasicFeature;
48 
53 {
54  // feature dimension
55  /*
56  * The dimension of the visual feature is set according to the size of the
57  * vpMoment associated to it. This partly explains why vpFeatureMomentBasic
58  * cannot be used directly as a visual feature.
59  */
60  if (this->moment != nullptr)
61  dim_s = (unsigned int)this->moment->get().size();
62  else
63  dim_s = 0;
64 
65  nbParameters = 1;
66 
67  // memory allocation
68  s.resize(dim_s);
69  for (unsigned int i = 0; i < dim_s; i++)
70  s[i] = 0;
71 
72  if (flags == nullptr)
73  flags = new bool[nbParameters];
74  for (unsigned int i = 0; i < nbParameters; i++)
75  flags[i] = false;
76 }
77 
81 int vpFeatureMoment::getDimension(unsigned int select) const
82 {
83  int dim = 0;
84 
85  for (unsigned int i = 0; i < dim_s; ++i)
86  if (vpBasicFeature::FEATURE_LINE[i] & select)
87  dim++;
88 
89  return dim;
90 }
91 
95 void vpFeatureMoment::print(unsigned int select) const
96 {
97  for (unsigned int i = 0; i < dim_s; ++i) {
98  if (vpBasicFeature::FEATURE_LINE[i] & select) {
99  std::cout << s[i] << ",";
100  }
101  }
102 
103  std::cout << std::endl;
104 }
105 
111  unsigned int thickness) const
112 {
113  // visual representation of a moment doesn't often make sense
114  (void)cam;
115  (void)I;
116  (void)color;
117  (void)thickness;
118 }
119 
123 void vpFeatureMoment::display(const vpCameraParameters &cam, const vpImage<vpRGBa> &I, const vpColor &color,
124  unsigned int thickness) const
125 {
126  (void)cam;
127  (void)I;
128  (void)color;
129  (void)thickness;
130 }
131 
148 void vpFeatureMoment::update(double A_, double B_, double C_)
149 {
150  this->A = A_;
151  this->B = B_;
152  this->C = C_;
153 
154  if (moment == nullptr) {
155  bool found;
156  this->moment = &(moments.get(momentName(), found));
157  if (!found)
158  throw vpException(vpException::notInitialized, "Moment not found for feature");
159  }
160  nbParameters = 1;
161  if (this->moment != nullptr) {
162  dim_s = (unsigned int)this->moment->get().size();
163 
164  s.resize(dim_s);
165 
166  for (unsigned int i = 0; i < dim_s; i++)
167  s[i] = this->moment->get()[i];
168 
169  if (flags == nullptr)
170  flags = new bool[nbParameters];
171  for (unsigned int i = 0; i < nbParameters; i++)
172  flags[i] = false;
173  }
174  else
175  dim_s = 0;
176 
178 }
179 
196 {
197  vpMatrix L(0, 0);
198 
199  for (unsigned int i = 0; i < dim_s; ++i) {
200  if (vpBasicFeature::FEATURE_LINE[i] & select) {
201  L.stack(interaction_matrices[i]);
202  }
203  }
204 
205  return L;
206 }
207 
218 {
221  feat->dim_s = dim_s;
222  feat->nbParameters = nbParameters;
223  // memory allocation
224  feat->s.resize(dim_s);
225  for (unsigned int i = 0; i < dim_s; i++)
226  feat->s[i] = this->s[i];
227 
228  feat->flags = new bool[(unsigned int)nbParameters];
229  for (unsigned int i = 0; i < (unsigned int)nbParameters; i++)
230  feat->flags[i] = flags[i];
231 
232  return feat;
233 }
234 
242 {
243  m_name = name();
244  this->featureMomentsDataBase = &featureMoments;
245 
246  featureMoments.add(*this, m_name);
247 }
248 
250 
258 void vpFeatureMoment::printDependencies(std::ostream &os) const
259 {
260  os << " WARNING : Falling back to base class version of "
261  "printDependencies() in vpFeatureMoment. To prevent that, this has "
262  "to be implemented in the derived classes!"
263  << std::endl;
264 }
265 
266 VISP_EXPORT std::ostream &operator<<(std::ostream &os, const vpFeatureMoment &featM)
267 {
268  /*
269  * - A static_cast is forced here since interaction() defined in vpBasicFeature()
270  * is not const. But introducing const in vpBasicFeature() can break a lot of
271  * client code.
272  * - 6 corresponds to 6 velocities in standard interaction matrix
273  */
274  vpMatrix Lcomplete(static_cast<unsigned int>(featM.getDimension()), 6);
275  Lcomplete = const_cast<vpFeatureMoment &>(featM).interaction(vpBasicFeature::FEATURE_ALL);
276  Lcomplete.matlabPrint(os);
277  return os;
278 }
279 
280 END_VISP_NAMESPACE
friend std::ostream & operator<<(std::ostream &s, const vpArray2D< Type > &A)
Definition: vpArray2D.h:611
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.
virtual vpMatrix interaction(unsigned int select=FEATURE_ALL)=0
Compute the interaction matrix from a subset of the possible features.
unsigned int dim_s
Dimension of the visual feature.
static const unsigned int FEATURE_LINE[32]
Generic class defining intrinsic camera parameters.
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 ViSP classes.
Definition: vpException.h:60
@ notInitialized
Used to indicate that a parameter is not initialized.
Definition: vpException.h:74
This class allows to register all feature moments (implemented in vpFeatureMoment....
This class defines shared system methods/attributes for 2D moment features but no functional code....
std::string m_name
virtual void printDependencies(std::ostream &os) const
std::vector< vpMatrix > interaction_matrices
vpFeatureMomentDatabase * featureMomentsDataBase
vpMomentDatabase & moments
vpBasicFeature * duplicate() const VP_OVERRIDE
vpMatrix interaction(unsigned int select=FEATURE_ALL) VP_OVERRIDE
int getDimension(unsigned int select=FEATURE_ALL) const
const vpMoment * moment
void init(void) VP_OVERRIDE
void display(const vpCameraParameters &cam, const vpImage< unsigned char > &I, const vpColor &color=vpColor::green, unsigned int thickness=1) const VP_OVERRIDE
void print(unsigned int select=FEATURE_ALL) const VP_OVERRIDE
virtual const std::string name() const =0
void update(double A, double B, double C)
virtual const std::string momentName() const =0
virtual void compute_interaction(void)
void linkTo(vpFeatureMomentDatabase &featureMoments)
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:169
std::ostream & matlabPrint(std::ostream &os) const
Definition: vpMatrix.cpp:947
const vpMoment & get(const std::string &moment_name, bool &found) const
This class defines a generic feature used for moment feature duplication.
const std::vector< double > & get() const
Definition: vpMoment.h:150