Visual Servoing Platform  version 3.5.1 under development (2023-09-22)
vpMomentAlpha.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  * 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 
52  : m_isRef(true), m_symmetric(false), m_mu3Ref(), m_alphaRef(0.), m_symmetricThreshold(1e-6)
53 {
54  values.resize(1);
55 }
56 
68 vpMomentAlpha::vpMomentAlpha(const std::vector<double> &mu3_ref, double alpha_ref, double threshold)
69  : vpMoment(), m_isRef(false), m_symmetric(true), m_mu3Ref(mu3_ref), m_alphaRef(alpha_ref),
70  m_symmetricThreshold(threshold)
71 {
72  for (std::vector<double>::const_iterator it = mu3_ref.begin(); it != mu3_ref.end(); ++it) {
73  if (std::fabs(*it) > m_symmetricThreshold) {
74  m_symmetric = false;
75  break;
76  }
77  }
78 
79  values.resize(1);
80 }
81 
87 {
88  // symmetric = symmetric | this->getObject().isSymmetric();
89  bool found_moment_centered;
90 
91  const vpMomentCentered &momentCentered =
92  (static_cast<const vpMomentCentered &>(getMoments().get("vpMomentCentered", found_moment_centered)));
93 
94  if (!found_moment_centered)
95  throw vpException(vpException::notInitialized, "vpMomentCentered not found");
96 
97  double alpha = 0.5 * atan2(2.0 * momentCentered.get(1, 1), (momentCentered.get(2, 0) - momentCentered.get(0, 2)));
98 
99  std::vector<double> rotMu(4);
100 
101  if (m_isRef) {
102  m_alphaRef = alpha;
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, (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, (int)l) * r12_i_k *
122  pow(r22, (int)(j - l)) *
123  momentCentered.get(k + l, (unsigned int)(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  } else {
149  alpha -= M_PI;
150  }
151  }
152  }
153  }
154  values[0] = alpha;
155 }
156 
160 VISP_EXPORT std::ostream &operator<<(std::ostream &os, const vpMomentAlpha &c)
161 {
162  os << (__FILE__) << std::endl;
163  os << "Alpha = " << c.values[0] << "rad = " << vpMath::deg(c.values[0]) << "deg " << std::endl;
164  return os;
165 }
166 
170 void vpMomentAlpha::printDependencies(std::ostream &os) const
171 {
172  os << (__FILE__) << std::endl;
173  bool found_moment_centered;
174  const vpMomentCentered &momentCentered =
175  (static_cast<const vpMomentCentered &>(getMoments().get("vpMomentCentered", found_moment_centered)));
176  if (!found_moment_centered)
177  throw vpException(vpException::notInitialized, "vpMomentCentered not found");
178 
179  os << "mu11 = " << momentCentered.get(1, 1) << "\t";
180  os << "mu20 = " << momentCentered.get(2, 0) << "\t";
181  os << "mu02 = " << momentCentered.get(0, 2) << std::endl;
182 }
friend std::ostream & operator<<(std::ostream &s, const vpArray2D< Type > &A)
Definition: vpArray2D.h:529
error that can be emitted by ViSP classes.
Definition: vpException.h:59
@ 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:309
static double deg(double rad)
Definition: vpMath.h:106
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:106
std::vector< double > values
Definition: vpMoment.h:113
vpMomentDatabase & getMoments() const
Definition: vpMoment.h:118