Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
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 
44 BEGIN_VISP_NAMESPACE
53  : m_isRef(true), m_symmetric(false), m_mu3Ref(), m_alphaRef(0.), m_symmetricThreshold(1e-6)
54 {
55  values.resize(1);
56 }
57 
69 vpMomentAlpha::vpMomentAlpha(const std::vector<double> &mu3_ref, double alpha_ref, double threshold)
70  : vpMoment(), m_isRef(false), m_symmetric(true), m_mu3Ref(mu3_ref), m_alphaRef(alpha_ref),
71  m_symmetricThreshold(threshold)
72 {
73  for (std::vector<double>::const_iterator it = mu3_ref.begin(); it != mu3_ref.end(); ++it) {
74  if (std::fabs(*it) > m_symmetricThreshold) {
75  m_symmetric = false;
76  break;
77  }
78  }
79 
80  values.resize(1);
81 }
82 
88 {
89  // symmetric = symmetric | this->getObject().isSymmetric();
90  bool found_moment_centered;
91 
92  const vpMomentCentered &momentCentered =
93  (static_cast<const vpMomentCentered &>(getMoments().get("vpMomentCentered", found_moment_centered)));
94 
95  if (!found_moment_centered)
96  throw vpException(vpException::notInitialized, "vpMomentCentered not found");
97 
98  double alpha = 0.5 * atan2(2.0 * momentCentered.get(1, 1), (momentCentered.get(2, 0) - momentCentered.get(0, 2)));
99 
100  std::vector<double> rotMu(4);
101 
102  if (m_isRef) {
103  m_alphaRef = alpha;
104  }
105  else {
106  if (!m_symmetric) {
107  double r11 = cos(alpha - m_alphaRef);
108  double r12 = sin(alpha - m_alphaRef);
109  double r21 = -r12;
110  double r22 = r11;
111  unsigned int idx = 0;
112  unsigned int order = 4;
113  for (unsigned int c = 0; c < (order) * (order); c++) {
114  unsigned int i = c % order;
115  unsigned int j = c / order;
116 
117  if (i + j == 3) {
118  double r11_k = 1.;
119  for (unsigned int k = 0; k <= i; k++) {
120  double r12_i_k = pow(r12, (int)(i - k));
121  double comb_i_k = static_cast<double>(vpMath::comb(i, k));
122  for (unsigned int l = 0; l <= j; l++) {
123  rotMu[idx] += static_cast<double>(comb_i_k * vpMath::comb(j, l) * r11_k * pow(r21, (int)l) * r12_i_k *
124  pow(r22, (int)(j - l)) *
125  momentCentered.get(k + l, (unsigned int)(int)(i + j - k - l)));
126  }
127  r11_k *= r11;
128  }
129  idx++;
130  }
131  }
132 
133  double sum = 0.;
134  bool signChange = false;
135  for (unsigned int i = 0; i < 4; i++) {
136  if (std::fabs(rotMu[i]) > m_symmetricThreshold && std::fabs(m_mu3Ref[i]) > m_symmetricThreshold &&
137  rotMu[i] * m_mu3Ref[i] < 0) {
138  signChange = true;
139  }
140  sum += std::fabs(rotMu[i] * m_mu3Ref[i]);
141  }
142 
143  if (sum < std::numeric_limits<double>::epsilon()) { // FS: Is this test useful ?
144  signChange = false;
145  }
146 
147  if (signChange) {
148  if (alpha < 0) {
149  alpha += M_PI;
150  }
151  else {
152  alpha -= M_PI;
153  }
154  }
155  }
156  }
157  values[0] = alpha;
158 }
159 
163 void vpMomentAlpha::printDependencies(std::ostream &os) const
164 {
165  os << (__FILE__) << std::endl;
166  bool found_moment_centered;
167  const vpMomentCentered &momentCentered =
168  (static_cast<const vpMomentCentered &>(getMoments().get("vpMomentCentered", found_moment_centered)));
169  if (!found_moment_centered)
170  throw vpException(vpException::notInitialized, "vpMomentCentered not found");
171 
172  os << "mu11 = " << momentCentered.get(1, 1) << "\t";
173  os << "mu20 = " << momentCentered.get(2, 0) << "\t";
174  os << "mu02 = " << momentCentered.get(0, 2) << std::endl;
175 }
176 
180 VISP_EXPORT std::ostream &operator<<(std::ostream &os, const vpMomentAlpha &c)
181 {
182 #ifdef ENABLE_VISP_NAMESPACE
183  using namespace VISP_NAMESPACE_NAME;
184 #endif
185  os << (__FILE__) << std::endl;
186  os << "Alpha = " << c.values[0] << "rad = " << vpMath::deg(c.values[0]) << "deg " << std::endl;
187  return os;
188 }
189 
190 END_VISP_NAMESPACE
friend std::ostream & operator<<(std::ostream &s, const vpArray2D< Type > &A)
Definition: vpArray2D.h:614
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:395
static double deg(double rad)
Definition: vpMath.h:119
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 std::string &moment_name, 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