Visual Servoing Platform  version 3.6.1 under development (2024-03-29)
vpMomentCentered.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  * Centered moment descriptor
33  *
34  * Authors:
35  * Filip Novotny
36  *
37 *****************************************************************************/
38 
39 #include <cassert>
40 #include <exception>
41 #include <visp3/core/vpMomentCentered.h>
42 #include <visp3/core/vpMomentGravityCenter.h>
43 #include <visp3/core/vpMomentObject.h>
44 
52 void vpMomentCentered::set(unsigned int i, unsigned int j, double value)
53 {
54  vpMomentObject mobj = getObject();
55  assert(i + j <= mobj.getOrder());
56  if (i + j > mobj.getOrder())
57  throw vpException(vpException::badValue, "You cannot set that value.");
58  values[j * (mobj.getOrder() + 1) + i] = value;
59 }
60 
66 {
67  bool found_moment_gravity;
68  values.resize((getObject().getOrder() + 1) * (getObject().getOrder() + 1));
69 
70  const vpMomentGravityCenter &momentGravity =
71  static_cast<const vpMomentGravityCenter &>(getMoments().get("vpMomentGravityCenter", found_moment_gravity));
72  if (!found_moment_gravity)
73  throw vpException(vpException::notInitialized, "vpMomentGravityCenter not found");
74 
75  unsigned int order = getObject().getOrder() + 1;
76  for (unsigned int j = 0; j < (order); j++) {
77  for (unsigned int i = 0; i < order - j; i++) {
78  unsigned int c = order * j + i;
79  values[c] = 0;
80  for (unsigned int k = 0; k <= i; k++) {
81  double Xg_i_k = pow(-momentGravity.get()[0], (int)(i - k));
82  double comb_i_k = static_cast<double>(vpMath::comb(i, k));
83  for (unsigned int l = 0; l <= j; l++) {
84  values[c] += static_cast<double>(comb_i_k * vpMath::comb(j, l) * Xg_i_k *
85  pow(-momentGravity.get()[1], (int)(j - l)) * getObject().get(k, l));
86  }
87  }
88  }
89  }
90 }
91 
96 
103 double vpMomentCentered::get(unsigned int i, unsigned int j) const
104 {
105  unsigned int order = getObject().getOrder();
106  assert(i + j <= order);
107  if (i + j > order)
108  throw vpException(vpException::badValue, "The requested value has not "
109  "been computed, you should "
110  "specify a higher order.");
111 
112  return values[j * (order + 1) + i];
113 }
114 
133 VISP_EXPORT std::ostream &operator<<(std::ostream &os, const vpMomentCentered &m)
134 {
135  for (unsigned int i = 0; i < m.values.size(); i++) {
136  if (i % (m.getObject().getOrder() + 1) == 0)
137  os << std::endl;
138 
139  if ((i % (m.getObject().getOrder() + 1) + i / (m.getObject().getOrder() + 1)) < m.getObject().getOrder() + 1)
140  os << m.values[i];
141  else
142  os << "x";
143 
144  os << "\t";
145  }
146  os << std::endl;
147  m.printWithIndices(os);
148  return os;
149 }
150 
154 void vpMomentCentered::printWithIndices(std::ostream &os) const
155 {
156  unsigned int orderp1 = getObject().getOrder() + 1;
157  for (unsigned int k = 0; k < orderp1; k++) {
158  for (unsigned int l = 0; l < orderp1 - k; l++) {
159  os << "mu[" << k << "," << l << "] = " << this->get(k, l) << "\t";
160  }
161  os << std::endl;
162  }
163  os << std::endl;
164 }
165 
172 void vpMomentCentered::printDependencies(std::ostream &os) const
173 {
174  os << (__FILE__) << std::endl;
175  /*
176  Retreive the raw moments
177  */
178  const vpMomentObject objt = getObject();
180 
181  /*
182  Get xg,yg
183  */
184  bool found_moment_gravity;
185  const vpMomentGravityCenter &momentGravity =
186  static_cast<const vpMomentGravityCenter &>(getMoments().get("vpMomentGravityCenter", found_moment_gravity));
187  if (!found_moment_gravity)
188  throw vpException(vpException::notInitialized, "vpMomentGravityCenter not found");
189  os << "Xg = " << momentGravity.getXg() << "\t"
190  << "Yg = " << momentGravity.getYg() << std::endl;
191 }
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
static long double comb(unsigned int n, unsigned int p)
Definition: vpMath.h:389
This class defines the double-indexed centered moment descriptor .
const std::vector< double > & get() const
void printDependencies(std::ostream &os) const
void printWithIndices(std::ostream &os) const
void set(unsigned int i, unsigned int j, double value)
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.
static void printWithIndices(const vpMomentObject &momobj, std::ostream &os)
unsigned int getOrder() const
const std::vector< double > & get() const
This class defines shared methods/attributes for 2D moments.
Definition: vpMoment.h:105
std::vector< double > values
Definition: vpMoment.h:112
const vpMomentObject & getObject() const
Definition: vpMoment.h:144
vpMomentDatabase & getMoments() const
Definition: vpMoment.h:117