Visual Servoing Platform  version 3.6.1 under development (2024-10-14)
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 
45 BEGIN_VISP_NAMESPACE
53 void vpMomentCentered::set(unsigned int i, unsigned int j, double value)
54 {
55  vpMomentObject mobj = getObject();
56  assert(i + j <= mobj.getOrder());
57  if (i + j > mobj.getOrder())
58  throw vpException(vpException::badValue, "You cannot set that value.");
59  values[j * (mobj.getOrder() + 1) + i] = value;
60 }
61 
67 {
68  bool found_moment_gravity;
69  values.resize((getObject().getOrder() + 1) * (getObject().getOrder() + 1));
70 
71  const vpMomentGravityCenter &momentGravity =
72  static_cast<const vpMomentGravityCenter &>(getMoments().get("vpMomentGravityCenter", found_moment_gravity));
73  if (!found_moment_gravity)
74  throw vpException(vpException::notInitialized, "vpMomentGravityCenter not found");
75 
76  unsigned int order = getObject().getOrder() + 1;
77  for (unsigned int j = 0; j < (order); j++) {
78  for (unsigned int i = 0; i < order - j; i++) {
79  unsigned int c = order * j + i;
80  values[c] = 0;
81  for (unsigned int k = 0; k <= i; k++) {
82  double Xg_i_k = pow(-momentGravity.get()[0], (int)(i - k));
83  double comb_i_k = static_cast<double>(vpMath::comb(i, k));
84  for (unsigned int l = 0; l <= j; l++) {
85  values[c] += static_cast<double>(comb_i_k * vpMath::comb(j, l) * Xg_i_k *
86  pow(-momentGravity.get()[1], (int)(j - l)) * getObject().get(k, l));
87  }
88  }
89  }
90  }
91 }
92 
97 
104 double vpMomentCentered::get(unsigned int i, unsigned int j) const
105 {
106  unsigned int order = getObject().getOrder();
107  assert(i + j <= order);
108  if (i + j > order)
109  throw vpException(vpException::badValue, "The requested value has not "
110  "been computed, you should "
111  "specify a higher order.");
112 
113  return values[j * (order + 1) + i];
114 }
115 
119 void vpMomentCentered::printWithIndices(std::ostream &os) const
120 {
121  unsigned int orderp1 = getObject().getOrder() + 1;
122  for (unsigned int k = 0; k < orderp1; k++) {
123  for (unsigned int l = 0; l < orderp1 - k; l++) {
124  os << "mu[" << k << "," << l << "] = " << this->get(k, l) << "\t";
125  }
126  os << std::endl;
127  }
128  os << std::endl;
129 }
130 
137 void vpMomentCentered::printDependencies(std::ostream &os) const
138 {
139  os << (__FILE__) << std::endl;
140  /*
141  Retreive the raw moments
142  */
143  const vpMomentObject objt = getObject();
145 
146  /*
147  Get xg,yg
148  */
149  bool found_moment_gravity;
150  const vpMomentGravityCenter &momentGravity =
151  static_cast<const vpMomentGravityCenter &>(getMoments().get("vpMomentGravityCenter", found_moment_gravity));
152  if (!found_moment_gravity)
153  throw vpException(vpException::notInitialized, "vpMomentGravityCenter not found");
154  os << "Xg = " << momentGravity.getXg() << "\t"
155  << "Yg = " << momentGravity.getYg() << std::endl;
156 }
157 
176 VISP_EXPORT std::ostream &operator<<(std::ostream &os, const vpMomentCentered &m)
177 {
178  for (unsigned int i = 0; i < m.values.size(); i++) {
179  if (i % (m.getObject().getOrder() + 1) == 0)
180  os << std::endl;
181 
182  if ((i % (m.getObject().getOrder() + 1) + i / (m.getObject().getOrder() + 1)) < m.getObject().getOrder() + 1)
183  os << m.values[i];
184  else
185  os << "x";
186 
187  os << "\t";
188  }
189  os << std::endl;
190  m.printWithIndices(os);
191  return os;
192 }
193 END_VISP_NAMESPACE
friend std::ostream & operator<<(std::ostream &s, const vpArray2D< Type > &A)
Definition: vpArray2D.h:611
error that can be emitted by ViSP classes.
Definition: vpException.h:60
@ badValue
Used to indicate that a value is not in the allowed range.
Definition: vpException.h:73
@ notInitialized
Used to indicate that a parameter is not initialized.
Definition: vpException.h:74
static long double comb(unsigned int n, unsigned int p)
Definition: vpMath.h:395
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:106
std::vector< double > values
Definition: vpMoment.h:113
const vpMomentObject & getObject() const
Definition: vpMoment.h:145
vpMomentDatabase & getMoments() const
Definition: vpMoment.h:118