Visual Servoing Platform version 3.5.0
vpMomentAlpha.cpp
1/****************************************************************************
2 *
3 * ViSP, open source Visual Servoing Platform software.
4 * Copyright (C) 2005 - 2019 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 http://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 * Alpha moment descriptor for in-plane orientation.
33 *
34 * Authors:
35 * Filip Novotny
36 *
37 *****************************************************************************/
38
39#include <cmath>
40#include <visp3/core/vpMomentAlpha.h>
41#include <visp3/core/vpMomentCentered.h>
42#include <visp3/core/vpMomentGravityCenter.h>
43
51vpMomentAlpha::vpMomentAlpha() : m_isRef(true), m_symmetric(false), m_mu3Ref(), m_alphaRef(0.), m_symmetricThreshold(1e-6) { values.resize(1); }
52
64vpMomentAlpha::vpMomentAlpha(const std::vector<double> &mu3_ref, double alpha_ref, double threshold)
65 : vpMoment(), m_isRef(false), m_symmetric(true), m_mu3Ref(mu3_ref), m_alphaRef(alpha_ref), m_symmetricThreshold(threshold)
66{
67 for (std::vector<double>::const_iterator it = mu3_ref.begin(); it != mu3_ref.end(); ++it) {
68 if (std::fabs(*it) > m_symmetricThreshold) {
69 m_symmetric = false;
70 break;
71 }
72 }
73
74 values.resize(1);
75}
76
82{
83 // symmetric = symmetric | this->getObject().isSymmetric();
84 bool found_moment_centered;
85
86 const vpMomentCentered &momentCentered =
87 (static_cast<const vpMomentCentered &>(getMoments().get("vpMomentCentered", found_moment_centered)));
88
89 if (!found_moment_centered)
90 throw vpException(vpException::notInitialized, "vpMomentCentered not found");
91
92 double alpha = 0.5 * atan2(2.0 * momentCentered.get(1, 1), (momentCentered.get(2, 0) - momentCentered.get(0, 2)));
93
94 std::vector<double> rotMu(4);
95
96 if (m_isRef) {
97 m_alphaRef = alpha;
98 } else {
99 if (! m_symmetric) {
100 double r11 = cos(alpha - m_alphaRef);
101 double r12 = sin(alpha - m_alphaRef);
102 double r21 = -r12;
103 double r22 = r11;
104 unsigned int idx = 0;
105 unsigned int order = 4;
106 for (unsigned int c = 0; c < (order) * (order); c++) {
107 unsigned int i = c % order;
108 unsigned int j = c / order;
109
110 if (i + j == 3) {
111 double r11_k = 1.;
112 for (unsigned int k = 0; k <= i; k++) {
113 double r12_i_k = pow(r12, (int)(i - k));
114 double comb_i_k = static_cast<double>(vpMath::comb(i, k));
115 for (unsigned int l = 0; l <= j; l++) {
116 rotMu[idx] += static_cast<double>(comb_i_k * vpMath::comb(j, l) * r11_k * pow(r21, (int)l) * r12_i_k *
117 pow(r22, (int)(j - l)) *
118 momentCentered.get(k + l, (unsigned int)(int)(i + j - k - l)));
119 }
120 r11_k *= r11;
121 }
122 idx++;
123 }
124 }
125
126 double sum = 0.;
127 bool signChange = false;
128 for (unsigned int i = 0; i < 4; i++) {
129 if (std::fabs(rotMu[i]) > m_symmetricThreshold &&
130 std::fabs(m_mu3Ref[i]) > m_symmetricThreshold && rotMu[i] * m_mu3Ref[i] < 0) {
131 signChange = true;
132 }
133 sum += std::fabs(rotMu[i] * m_mu3Ref[i]);
134 }
135
136 if (sum < std::numeric_limits<double>::epsilon()) { // FS: Is this test useful ?
137 signChange = false;
138 }
139
140 if (signChange) {
141 if (alpha < 0) {
142 alpha += M_PI;
143 }
144 else {
145 alpha -= M_PI;
146 }
147 }
148 }
149 }
150 values[0] = alpha;
151}
152
156VISP_EXPORT std::ostream &operator<<(std::ostream &os, const vpMomentAlpha &c)
157{
158 os << (__FILE__) << std::endl;
159 os << "Alpha = " << c.values[0] << "rad = " << vpMath::deg(c.values[0]) << "deg " << std::endl;
160 return os;
161}
162
166void vpMomentAlpha::printDependencies(std::ostream &os) const
167{
168 os << (__FILE__) << std::endl;
169 bool found_moment_centered;
170 const vpMomentCentered &momentCentered =
171 (static_cast<const vpMomentCentered &>(getMoments().get("vpMomentCentered", found_moment_centered)));
172 if (!found_moment_centered)
173 throw vpException(vpException::notInitialized, "vpMomentCentered not found");
174
175 os << "mu11 = " << momentCentered.get(1, 1) << "\t";
176 os << "mu20 = " << momentCentered.get(2, 0) << "\t";
177 os << "mu02 = " << momentCentered.get(0, 2) << std::endl;
178}
friend std::ostream & operator<<(std::ostream &s, const vpArray2D< Type > &A)
Definition: vpArray2D.h:493
error that can be emited by ViSP classes.
Definition: vpException.h:72
@ notInitialized
Used to indicate that a parameter is not initialized.
Definition: vpException.h:98
static long double comb(unsigned int n, unsigned int p)
Definition: vpMath.h:232
static double deg(double rad)
Definition: vpMath.h:103
This class defines the orientation of the object inside the plane parallel to the object.
void printDependencies(std::ostream &os) const
This class defines the double-indexed centered moment descriptor .
double get(unsigned int i, unsigned int j) const
const vpMoment & get(const char *type, bool &found) const
This class defines shared methods/attributes for 2D moments.
Definition: vpMoment.h:111
std::vector< double > values
Definition: vpMoment.h:118
vpMomentDatabase & getMoments() const
Definition: vpMoment.h:123