Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
vpMomentCentered.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 * Centered moment descriptor
32 */
33
34#include <cassert>
35#include <exception>
36#include <visp3/core/vpMomentCentered.h>
37#include <visp3/core/vpMomentGravityCenter.h>
38#include <visp3/core/vpMomentObject.h>
39
48void vpMomentCentered::set(unsigned int i, unsigned int j, double value)
49{
51 assert(i + j <= mobj.getOrder());
52 if (i + j > mobj.getOrder())
53 throw vpException(vpException::badValue, "You cannot set that value.");
54 values[j * (mobj.getOrder() + 1) + i] = value;
55}
56
62{
63 bool found_moment_gravity;
64 values.resize((getObject().getOrder() + 1) * (getObject().getOrder() + 1));
65
66 const vpMomentGravityCenter &momentGravity =
67 static_cast<const vpMomentGravityCenter &>(getMoments().get("vpMomentGravityCenter", found_moment_gravity));
68 if (!found_moment_gravity)
69 throw vpException(vpException::notInitialized, "vpMomentGravityCenter not found");
70
71 unsigned int order = getObject().getOrder() + 1;
72 for (unsigned int j = 0; j < (order); j++) {
73 for (unsigned int i = 0; i < order - j; i++) {
74 unsigned int c = order * j + i;
75 values[c] = 0;
76 for (unsigned int k = 0; k <= i; k++) {
77 double Xg_i_k = pow(-momentGravity.get()[0], static_cast<int>(i - k));
78 double comb_i_k = static_cast<double>(vpMath::comb(i, k));
79 for (unsigned int l = 0; l <= j; l++) {
80 values[c] += static_cast<double>(comb_i_k * vpMath::comb(j, l) * Xg_i_k *
81 pow(-momentGravity.get()[1], static_cast<int>(j - l)) * getObject().get(k, l));
82 }
83 }
84 }
85 }
86}
87
92
99double vpMomentCentered::get(unsigned int i, unsigned int j) const
100{
101 unsigned int order = getObject().getOrder();
102 assert(i + j <= order);
103 if (i + j > order)
104 throw vpException(vpException::badValue, "The requested value has not "
105 "been computed, you should "
106 "specify a higher order.");
107
108 return values[j * (order + 1) + i];
109}
110
114void vpMomentCentered::printWithIndices(std::ostream &os) const
115{
116 unsigned int orderp1 = getObject().getOrder() + 1;
117 for (unsigned int k = 0; k < orderp1; k++) {
118 for (unsigned int l = 0; l < orderp1 - k; l++) {
119 os << "mu[" << k << "," << l << "] = " << this->get(k, l) << "\t";
120 }
121 os << std::endl;
122 }
123 os << std::endl;
124}
125
132void vpMomentCentered::printDependencies(std::ostream &os) const
133{
134 os << (__FILE__) << std::endl;
135 /*
136 Retreive the raw moments
137 */
138 const vpMomentObject objt = getObject();
140
141 /*
142 Get xg,yg
143 */
144 bool found_moment_gravity;
145 const vpMomentGravityCenter &momentGravity =
146 static_cast<const vpMomentGravityCenter &>(getMoments().get("vpMomentGravityCenter", found_moment_gravity));
147 if (!found_moment_gravity)
148 throw vpException(vpException::notInitialized, "vpMomentGravityCenter not found");
149 os << "Xg = " << momentGravity.getXg() << "\t"
150 << "Yg = " << momentGravity.getYg() << std::endl;
151}
152
171VISP_EXPORT std::ostream &operator<<(std::ostream &os, const vpMomentCentered &m)
172{
173 for (unsigned int i = 0; i < m.values.size(); i++) {
174 if (i % (m.getObject().getOrder() + 1) == 0)
175 os << std::endl;
176
177 if ((i % (m.getObject().getOrder() + 1) + i / (m.getObject().getOrder() + 1)) < m.getObject().getOrder() + 1)
178 os << m.values[i];
179 else
180 os << "x";
181
182 os << "\t";
183 }
184 os << std::endl;
185 m.printWithIndices(os);
186 return os;
187}
188END_VISP_NAMESPACE
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:398
const std::vector< double > & get() const
void compute() VP_OVERRIDE
void printWithIndices(std::ostream &os) const
friend VISP_EXPORT std::ostream & operator<<(std::ostream &os, const vpMomentCentered &v)
void printDependencies(std::ostream &os) const VP_OVERRIDE
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
const vpMomentObject & getObject() const
Definition vpMoment.h:150
std::vector< double > values
Definition vpMoment.h:113
vpMomentDatabase & getMoments() const
Definition vpMoment.h:118
vpMoment(const vpMoment &)=delete