Visual Servoing Platform  version 3.6.1 under development (2024-03-28)
vpFeatureMomentCentered.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  * Implementation for all supported moment features.
32  */
33 
34 #include <visp3/core/vpConfig.h>
35 
36 #include <limits>
37 #include <vector>
38 
39 #include <visp3/core/vpMomentCentered.h>
40 #include <visp3/core/vpMomentGravityCenter.h>
41 #include <visp3/core/vpMomentObject.h>
42 #include <visp3/visual_features/vpFeatureMomentBasic.h>
43 #include <visp3/visual_features/vpFeatureMomentCentered.h>
44 #include <visp3/visual_features/vpFeatureMomentDatabase.h>
45 #include <visp3/visual_features/vpFeatureMomentGravityCenter.h>
46 
55 vpFeatureMomentCentered::vpFeatureMomentCentered(vpMomentDatabase &moments_, double A_, double B_, double C_,
56  vpFeatureMomentDatabase *featureMoments)
57  : vpFeatureMoment(moments_, A_, B_, C_, featureMoments), order(0)
58 { }
59 
66 vpMatrix vpFeatureMomentCentered::interaction(unsigned int select_one, unsigned int select_two) const
67 {
68  if (select_one + select_two > moment->getObject().getOrder())
69  throw vpException(vpException::badValue, "The requested value has not "
70  "been computed, you should "
71  "specify a higher order.");
72  return interaction_matrices[select_two * order + select_one];
73 }
74 
80 vpMatrix vpFeatureMomentCentered::compute_Lmu_pq(const unsigned int &p, const unsigned int &q, const double &xg,
81  const double &yg, const vpMatrix &L_xg, const vpMatrix &L_yg,
82  const vpMomentBasic &m,
83  const vpFeatureMomentBasic &feature_moment_m) const
84 {
85  // term1, term2 and Lterm3 (matrix) will be repeatedly computed inside the
86  // innermost loop
87  double term1 = 0.0;
88  double term2 = 0.0;
89  vpMatrix Lterm3(1, 6);
90 
91  double qcombl = 0.0;
92  double pcombkqcombl = 0.0;
93 
94  double mkl = 0.0;
95  vpMatrix L_mkl;
96 
97  int qml = 0; // q-l
98  double minus1pow = 0.; // (-1)^(p+q-k-l)
99  double pintom = 0.;
100 
101  for (unsigned int k = 0; k <= p; ++k) {
102  int pmk = (int)p - (int)k;
103  double pcombk = static_cast<double>(vpMath::comb(p, k));
104  for (unsigned int l = 0; l <= q; ++l) {
105  qml = (int)q - (int)l;
106  qcombl = static_cast<double>(vpMath::comb(q, l));
107  minus1pow = pow((double)-1, (double)(pmk + qml));
108  pcombkqcombl = pcombk * qcombl;
109  mkl = m.get(k, l);
110  pintom = pcombkqcombl * mkl;
111  L_mkl = feature_moment_m.interaction(k, l);
112  if (pmk > 0)
113  term1 += pintom * pmk * pow(xg, pmk - 1) * pow(yg, qml) * minus1pow;
114  if (qml > 0)
115  term2 += pintom * qml * pow(xg, pmk) * pow(yg, qml - 1) * minus1pow;
116  Lterm3 += pcombkqcombl * pow(xg, pmk) * pow(yg, qml) * L_mkl * minus1pow;
117  }
118  }
119 
120  // L_xg and L_yg stay constant with respect to the above loops. Lterm3 is
121  // summed over
122  vpMatrix L_mupq = L_xg * term1 + L_yg * term2 + Lterm3;
123  return L_mupq;
124 }
125 
139 {
140 #ifdef VISP_MOMENTS_COMBINE_MATRICES
141  const vpMomentObject &momentObject = moment->getObject();
142  order = momentObject.getOrder() + 1;
143  interaction_matrices.resize(order * order);
144  for (std::vector<vpMatrix>::iterator i = interaction_matrices.begin(); i != interaction_matrices.end(); ++i)
145  i->resize(1, 6);
146 
147  bool found_moment_gravity;
148  const vpMomentGravityCenter &momentGravity =
149  static_cast<const vpMomentGravityCenter &>(moments.get("vpMomentGravityCenter", found_moment_gravity));
150  if (!found_moment_gravity)
151  throw vpException(vpException::notInitialized, "vpMomentGravityCenter not found");
152  double xg = momentGravity.get()[0];
153  double yg = momentGravity.get()[1];
154 
155  bool found_feature_gravity_center;
156  vpFeatureMomentGravityCenter &featureMomentGravityCenter = (static_cast<vpFeatureMomentGravityCenter &>(
157  featureMomentsDataBase->get("vpFeatureMomentGravityCenter", found_feature_gravity_center)));
158  if (!found_feature_gravity_center)
159  throw vpException(vpException::notInitialized, "vpFeatureMomentGravityCenter not found");
160  vpMatrix Lxg = featureMomentGravityCenter.interaction(1 << 0);
161  vpMatrix Lyg = featureMomentGravityCenter.interaction(1 << 1);
162 
163  bool found_moment_basic;
164  const vpMomentBasic &momentbasic =
165  static_cast<const vpMomentBasic &>(moments.get("vpMomentBasic", found_moment_basic));
166  if (!found_moment_basic)
167  throw vpException(vpException::notInitialized, "vpMomentBasic not found");
168 
169  bool found_featuremoment_basic;
170  vpFeatureMomentBasic &featureMomentBasic = (static_cast<vpFeatureMomentBasic &>(
171  featureMomentsDataBase->get("vpFeatureMomentBasic", found_featuremoment_basic)));
172  if (!found_featuremoment_basic)
173  throw vpException(vpException::notInitialized, "vpFeatureMomentBasic not found");
174 
175  // Calls the main compute_Lmu_pq function for moments upto order-1
176  for (int i = 0; i < (int)order - 1; i++) {
177  for (int j = 0; j < (int)order - 1 - i; j++) {
178  interaction_matrices[(unsigned int)j * order + (unsigned int)i] =
179  compute_Lmu_pq(i, j, xg, yg, Lxg, Lyg, momentbasic, featureMomentBasic);
180  }
181  }
182 #else // #ifdef VISP_MOMENTS_COMBINE_MATRICES
183  bool found_moment_centered;
184  bool found_moment_gravity;
185 
186  const vpMomentCentered &momentCentered =
187  (static_cast<const vpMomentCentered &>(moments.get("vpMomentCentered", found_moment_centered)));
188  const vpMomentGravityCenter &momentGravity =
189  static_cast<const vpMomentGravityCenter &>(moments.get("vpMomentGravityCenter", found_moment_gravity));
190 
191  if (!found_moment_centered)
192  throw vpException(vpException::notInitialized, "vpMomentCentered not found");
193  if (!found_moment_gravity)
194  throw vpException(vpException::notInitialized, "vpMomentGravityCenter not found");
195 
196  int delta;
197  int epsilon;
198  const vpMomentObject &momentObject = moment->getObject();
199  order = momentObject.getOrder() + 1;
200  interaction_matrices.resize(order * order);
201  for (std::vector<vpMatrix>::iterator i = interaction_matrices.begin(); i != interaction_matrices.end(); ++i)
202  i->resize(1, 6);
203  if (momentObject.getType() == vpMomentObject::DISCRETE) {
204  delta = 0;
205  epsilon = 1;
206  }
207  else {
208  delta = 1;
209  epsilon = 4;
210  }
211  double n11 = momentCentered.get(1, 1) / momentObject.get(0, 0);
212  double n20 = momentCentered.get(2, 0) / momentObject.get(0, 0);
213  double n02 = momentCentered.get(0, 2) / momentObject.get(0, 0);
214  double Xg = momentGravity.getXg();
215  double Yg = momentGravity.getYg();
216  double mu00 = momentCentered.get(0, 0);
217 
218  unsigned int VX = 0;
219  unsigned int VY = 1;
220  unsigned int VZ = 2;
221  unsigned int WX = 3;
222  unsigned int WY = 4;
223  unsigned int WZ = 5;
224 
225  interaction_matrices[0][0][VX] = -(delta)*A * mu00;
226  interaction_matrices[0][0][VY] = -(delta)*B * mu00;
227 
228  // Since mu10=0 and mu01=0
229  // interaction_matrices[0][0][WX] = (3*delta)*MU(0,1)+(3*delta)*Yg*mu00;
230  // interaction_matrices[0][0][WY] = -(3*delta)*MU(1,0)-(3*delta)*Xg*mu00;
231  // we get the simplification:
232  interaction_matrices[0][0][WX] = (3 * delta) * Yg * mu00;
233  interaction_matrices[0][0][WY] = -(3 * delta) * Xg * mu00;
234  interaction_matrices[0][0][VZ] =
235  -A * interaction_matrices[0][0][WY] + B * interaction_matrices[0][0][WX] + (2 * delta) * C * mu00;
236  interaction_matrices[0][0][WZ] = 0.;
237 
238  for (int i = 1; i < (int)order - 1; i++) {
239  unsigned int i_ = (unsigned int)i;
240  unsigned int im1_ = i_ - 1;
241  unsigned int ip1_ = i_ + 1;
242 
243  double mu_im10 = momentCentered.get(im1_, 0);
244  double mu_ip10 = momentCentered.get(ip1_, 0);
245  double mu_im11 = momentCentered.get(im1_, 1);
246  double mu_i0 = momentCentered.get(i_, 0);
247  double mu_i1 = momentCentered.get(i_, 1);
248 
249  interaction_matrices[i_][0][VX] = -(i + delta) * A * mu_i0 - (i * B * mu_im11);
250  interaction_matrices[i_][0][VY] = -(delta)*B * mu_i0;
251 
252  interaction_matrices[i_][0][WX] =
253  (i + 3 * delta) * mu_i1 + (i + 3 * delta) * Yg * mu_i0 + i * Xg * mu_im11 - i * epsilon * n11 * mu_im10;
254  interaction_matrices[i_][0][WY] =
255  -(i + 3 * delta) * mu_ip10 - (2 * i + 3 * delta) * Xg * mu_i0 + i * epsilon * n20 * mu_im10;
256  interaction_matrices[i_][0][VZ] =
257  -A * interaction_matrices[i_][0][WY] + B * interaction_matrices[i_][0][WX] + (i + 2 * delta) * C * mu_i0;
258  interaction_matrices[i_][0][WZ] = i * mu_im11;
259  }
260 
261  for (int j = 1; j < (int)order - 1; j++) {
262  unsigned int j_ = (unsigned int)j;
263  unsigned int jm1_ = j_ - 1;
264  unsigned int jp1_ = j_ + 1;
265 
266  double mu_0jm1 = momentCentered.get(0, jm1_);
267  double mu_0jp1 = momentCentered.get(0, jp1_);
268  double mu_1jm1 = momentCentered.get(1, jm1_);
269  double mu_0j = momentCentered.get(0, j_);
270  double mu_1j = momentCentered.get(1, j_);
271 
272  interaction_matrices[j_ * order][0][VX] = -(delta)*A * mu_0j;
273  interaction_matrices[j_ * order][0][VY] = -j * A * mu_1jm1 - (j + delta) * B * mu_0j;
274 
275  interaction_matrices[j_ * order][0][WX] =
276  (j + 3 * delta) * mu_0jp1 + (2 * j + 3 * delta) * Yg * mu_0j - j * epsilon * n02 * mu_0jm1;
277  interaction_matrices[j_ * order][0][WY] =
278  -(j + 3 * delta) * mu_1j - (j + 3 * delta) * Xg * mu_0j - j * Yg * mu_1jm1 + j * epsilon * n11 * mu_0jm1;
279  interaction_matrices[j_ * order][0][VZ] = -A * interaction_matrices[j_ * order][0][WY] +
280  B * interaction_matrices[j_ * order][0][WX] + (j + 2 * delta) * C * mu_0j;
281  interaction_matrices[j_ * order][0][WZ] = -j * mu_1jm1;
282  }
283 
284  for (int j = 1; j < (int)order - 1; j++) {
285  unsigned int j_ = (unsigned int)j;
286  unsigned int jm1_ = j_ - 1;
287  unsigned int jp1_ = j_ + 1;
288  for (int i = 1; i < (int)order - j - 1; i++) {
289  unsigned int i_ = (unsigned int)i;
290  unsigned int im1_ = i_ - 1;
291  unsigned int ip1_ = i_ + 1;
292 
293  double mu_ijm1 = momentCentered.get(i_, jm1_);
294  double mu_ij = momentCentered.get(i_, j_);
295  double mu_ijp1 = momentCentered.get(i_, jp1_);
296  double mu_im1j = momentCentered.get(im1_, j_);
297  double mu_im1jp1 = momentCentered.get(im1_, jp1_);
298  double mu_ip1jm1 = momentCentered.get(ip1_, jm1_);
299  double mu_ip1j = momentCentered.get(ip1_, j_);
300 
301  interaction_matrices[j_ * order + i_][0][VX] = -(i + delta) * A * mu_ij - i * B * mu_im1jp1;
302  interaction_matrices[j_ * order + i_][0][VY] = -j * A * mu_ip1jm1 - (j + delta) * B * mu_ij;
303 
304  interaction_matrices[j_ * order + i_][0][WX] = (i + j + 3 * delta) * mu_ijp1 +
305  (i + 2 * j + 3 * delta) * Yg * mu_ij + i * Xg * mu_im1jp1 -
306  i * epsilon * n11 * mu_im1j - j * epsilon * n02 * mu_ijm1;
307  interaction_matrices[j_ * order + i_][0][WY] = -(i + j + 3 * delta) * mu_ip1j -
308  (2 * i + j + 3 * delta) * Xg * mu_ij - j * Yg * mu_ip1jm1 +
309  i * epsilon * n20 * mu_im1j + j * epsilon * n11 * mu_ijm1;
310  interaction_matrices[j_ * order + i_][0][VZ] = -A * interaction_matrices[j_ * order + i_][0][WY] +
311  B * interaction_matrices[j_ * order + i_][0][WX] +
312  (i + j + 2 * delta) * C * mu_ij;
313  interaction_matrices[j_ * order + i_][0][WZ] = i * mu_im1jp1 - j * mu_ip1jm1;
314  }
315  }
316 #endif // #ifdef VISP_MOMENTS_COMBINE_MATRICES
317 }
318 
323 std::ostream &operator<<(std::ostream &os, const vpFeatureMomentCentered &mu)
324 {
325  vpTRACE(" << Ls - CENTRED MOMENTS >>");
326  unsigned int order_m_1 = (unsigned int)(mu.order - 1);
327  for (unsigned int i = 0; i < order_m_1; i++) {
328  for (unsigned int j = 0; j < order_m_1 - i; j++) {
329  os << "L_mu[" << i << "," << j << "] = ";
330  mu.interaction(i, j).matlabPrint(os);
331  }
332  }
333  return os;
334 }
friend std::ostream & operator<<(std::ostream &s, const vpArray2D< Type > &A)
Definition: vpArray2D.h:547
error that can be emitted by ViSP classes.
Definition: vpException.h:59
@ badValue
Used to indicate that a value is not in the allowed range.
Definition: vpException.h:85
@ notInitialized
Used to indicate that a parameter is not initialized.
Definition: vpException.h:86
Functionality computation for basic moment feature. Computes the interaction matrix associated with v...
vpMatrix interaction(unsigned int select_one, unsigned int select_two) const
Functionality computation for centered moment feature. Computes the interaction matrix associated wit...
void compute_interaction() vp_override
vpFeatureMomentCentered(vpMomentDatabase &moments, double A, double B, double C, vpFeatureMomentDatabase *featureMoments=nullptr)
vpMatrix interaction(unsigned int select_one, unsigned int select_two) const
vpMatrix compute_Lmu_pq(const unsigned int &p, const unsigned int &q, const double &xg, const double &yg, const vpMatrix &L_xg, const vpMatrix &L_yg, const vpMomentBasic &m, const vpFeatureMomentBasic &feature_moment_m) const
This class allows to register all feature moments (implemented in vpFeatureMoment....
vpFeatureMoment & get(const std::string &feature_name, bool &found)
Functionality computation for gravity center moment feature. Computes the interaction matrix associat...
This class defines shared system methods/attributes for 2D moment features but no functional code....
std::vector< vpMatrix > interaction_matrices
vpMatrix interaction(unsigned int select=FEATURE_ALL) vp_override
vpFeatureMomentDatabase * featureMomentsDataBase
vpMomentDatabase & moments
const vpMoment * moment
static long double comb(unsigned int n, unsigned int p)
Definition: vpMath.h:389
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:146
std::ostream & matlabPrint(std::ostream &os) const
Definition: vpMatrix.cpp:5442
This class defines the 2D basic moment . This class is a wrapper for vpMomentObject wich allows to us...
Definition: vpMomentBasic.h:70
const std::vector< double > & get() const
This class defines the double-indexed centered moment descriptor .
double get(unsigned int i, unsigned int j) const
This class allows to register all vpMoments so they can access each other according to their dependen...
const vpMoment & get(const std::string &moment_name, bool &found) const
Class describing 2D gravity center moment.
const std::vector< double > & get() const
Class for generic objects.
unsigned int getOrder() const
const std::vector< double > & get() const
vpObjectType getType() const
const vpMomentObject & getObject() const
Definition: vpMoment.h:144
#define vpTRACE
Definition: vpDebug.h:405