Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
vpFeatureMoment.cpp
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2025 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
47class 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 = static_cast<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
81int 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
95void 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
124 unsigned int thickness) const
125{
126 (void)cam;
127 (void)I;
128 (void)color;
129 (void)thickness;
130}
131
148void 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 = static_cast<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;
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 unsigned int n = static_cast<unsigned int>(nbParameters);
229 feat->flags = new bool[n];
230 for (unsigned int i = 0; i < n; i++)
231 feat->flags[i] = flags[i];
232
233 return feat;
234}
235
243{
244 m_name = name();
245 this->featureMomentsDataBase = &featureMoments;
246
247 featureMoments.add(*this, m_name);
248}
249
251
259void vpFeatureMoment::printDependencies(std::ostream &os) const
260{
261 os << " WARNING : Falling back to base class version of "
262 "printDependencies() in vpFeatureMoment. To prevent that, this has "
263 "to be implemented in the derived classes!"
264 << std::endl;
265}
266
267VISP_EXPORT std::ostream &operator<<(std::ostream &os, const vpFeatureMoment &featM)
268{
269 /*
270 * - A static_cast is forced here since interaction() defined in vpBasicFeature()
271 * is not const. But introducing const in vpBasicFeature() can break a lot of
272 * client code.
273 * - 6 corresponds to 6 velocities in standard interaction matrix
274 */
275 vpMatrix Lcomplete(static_cast<unsigned int>(featM.getDimension()), 6);
276 Lcomplete = const_cast<vpFeatureMoment &>(featM).interaction(vpBasicFeature::FEATURE_ALL);
277 Lcomplete.matlabPrint(os);
278 return os;
279}
280
281END_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.
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)
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....
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
friend VISP_EXPORT std::ostream & operator<<(std::ostream &os, const vpFeatureMoment &featM)
int getDimension(unsigned int select=FEATURE_ALL) const
const vpMoment * moment
vpFeatureMoment(const vpFeatureMoment &)=delete
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)
Definition of the vpImage class member functions.
Definition vpImage.h:131
Implementation of a matrix and operations on matrices.
Definition vpMatrix.h:175
std::ostream & matlabPrint(std::ostream &os) const
Definition vpMatrix.cpp:959
This class defines a generic feature used for moment feature duplication.