Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
vpMomentAlpha.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 * Alpha moment descriptor for in-plane orientation.
32 *
33 * Authors:
34 * Filip Novotny
35 */
36
37#include <cmath>
38#include <visp3/core/vpMomentAlpha.h>
39#include <visp3/core/vpMomentCentered.h>
40#include <visp3/core/vpMomentGravityCenter.h>
41
51 : m_isRef(true), m_symmetric(false), m_mu3Ref(), m_alphaRef(0.), m_symmetricThreshold(1e-6)
52{
53 values.resize(1);
54}
55
67vpMomentAlpha::vpMomentAlpha(const std::vector<double> &mu3_ref, double alpha_ref, double threshold)
68 : vpMoment(), m_isRef(false), m_symmetric(true), m_mu3Ref(mu3_ref), m_alphaRef(alpha_ref),
69 m_symmetricThreshold(threshold)
70{
71 for (std::vector<double>::const_iterator it = mu3_ref.begin(); it != mu3_ref.end(); ++it) {
72 if (std::fabs(*it) > m_symmetricThreshold) {
73 m_symmetric = false;
74 break;
75 }
76 }
77
78 values.resize(1);
79}
80
86{
87 // symmetric = symmetric | this->getObject().isSymmetric();
88 bool found_moment_centered;
89
90 const vpMomentCentered &momentCentered =
91 (static_cast<const vpMomentCentered &>(getMoments().get("vpMomentCentered", found_moment_centered)));
92
93 if (!found_moment_centered)
94 throw vpException(vpException::notInitialized, "vpMomentCentered not found");
95
96 double alpha = 0.5 * atan2(2.0 * momentCentered.get(1, 1), (momentCentered.get(2, 0) - momentCentered.get(0, 2)));
97
98 std::vector<double> rotMu(4);
99
100 if (m_isRef) {
101 m_alphaRef = alpha;
102 }
103 else {
104 if (!m_symmetric) {
105 double r11 = cos(alpha - m_alphaRef);
106 double r12 = sin(alpha - m_alphaRef);
107 double r21 = -r12;
108 double r22 = r11;
109 unsigned int idx = 0;
110 unsigned int order = 4;
111 for (unsigned int c = 0; c < (order) * (order); c++) {
112 unsigned int i = c % order;
113 unsigned int j = c / order;
114
115 if (i + j == 3) {
116 double r11_k = 1.;
117 for (unsigned int k = 0; k <= i; k++) {
118 double r12_i_k = pow(r12, static_cast<int>(i - k));
119 double comb_i_k = static_cast<double>(vpMath::comb(i, k));
120 for (unsigned int l = 0; l <= j; l++) {
121 rotMu[idx] += static_cast<double>(comb_i_k * vpMath::comb(j, l) * r11_k * pow(r21, static_cast<int>(l)) * r12_i_k *
122 pow(r22, static_cast<int>(j - l)) *
123 momentCentered.get(k + l, static_cast<unsigned int>(static_cast<int>(i + j - k - l))));
124 }
125 r11_k *= r11;
126 }
127 idx++;
128 }
129 }
130
131 double sum = 0.;
132 bool signChange = false;
133 for (unsigned int i = 0; i < 4; i++) {
134 if (std::fabs(rotMu[i]) > m_symmetricThreshold && std::fabs(m_mu3Ref[i]) > m_symmetricThreshold &&
135 rotMu[i] * m_mu3Ref[i] < 0) {
136 signChange = true;
137 }
138 sum += std::fabs(rotMu[i] * m_mu3Ref[i]);
139 }
140
141 if (sum < std::numeric_limits<double>::epsilon()) { // FS: Is this test useful ?
142 signChange = false;
143 }
144
145 if (signChange) {
146 if (alpha < 0) {
147 alpha += M_PI;
148 }
149 else {
150 alpha -= M_PI;
151 }
152 }
153 }
154 }
155 values[0] = alpha;
156}
157
161void vpMomentAlpha::printDependencies(std::ostream &os) const
162{
163 os << (__FILE__) << std::endl;
164 bool found_moment_centered;
165 const vpMomentCentered &momentCentered =
166 (static_cast<const vpMomentCentered &>(getMoments().get("vpMomentCentered", found_moment_centered)));
167 if (!found_moment_centered)
168 throw vpException(vpException::notInitialized, "vpMomentCentered not found");
169
170 os << "mu11 = " << momentCentered.get(1, 1) << "\t";
171 os << "mu20 = " << momentCentered.get(2, 0) << "\t";
172 os << "mu02 = " << momentCentered.get(0, 2) << std::endl;
173}
174
178VISP_EXPORT std::ostream &operator<<(std::ostream &os, const vpMomentAlpha &c)
179{
180#ifdef ENABLE_VISP_NAMESPACE
181 using namespace VISP_NAMESPACE_NAME;
182#endif
183 os << (__FILE__) << std::endl;
184 os << "Alpha = " << c.values[0] << "rad = " << vpMath::deg(c.values[0]) << "deg " << std::endl;
185 return os;
186}
187
188END_VISP_NAMESPACE
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
static long double comb(unsigned int n, unsigned int p)
Definition vpMath.h:398
static double deg(double rad)
Definition vpMath.h:119
friend VISP_EXPORT std::ostream & operator<<(std::ostream &os, const vpMomentAlpha &v)
void compute() VP_OVERRIDE
void printDependencies(std::ostream &os) const VP_OVERRIDE
This class defines the double-indexed centered moment descriptor .
double get(unsigned int i, unsigned int j) const
const vpMoment & get(const std::string &moment_name, bool &found) const
std::vector< double > values
Definition vpMoment.h:113
vpMomentDatabase & getMoments() const
Definition vpMoment.h:118
vpMoment(const vpMoment &)=delete