Visual Servoing Platform  version 3.1.0
vpMomentAlpha.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 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 
51 vpMomentAlpha::vpMomentAlpha() : isRef(true), symmetric(false), ref(), alphaRef(0.) { values.resize(1); }
52 
60 vpMomentAlpha::vpMomentAlpha(const std::vector<double> &ref_, double alpha_ref)
61  : vpMoment(), isRef(false), symmetric(false), ref(ref_), alphaRef(alpha_ref)
62 {
63  for (std::vector<double>::const_iterator it = ref_.begin(); it != ref_.end(); ++it)
64  if (std::fabs(*it) <= 1e-4)
65  symmetric = true;
66 
67  values.resize(1);
68 }
69 
75 {
76  // symmetric = symmetric | this->getObject().isSymmetric();
77  bool found_moment_centered;
78 
79  const vpMomentCentered &momentCentered =
80  (static_cast<const vpMomentCentered &>(getMoments().get("vpMomentCentered", found_moment_centered)));
81 
82  if (!found_moment_centered)
83  throw vpException(vpException::notInitialized, "vpMomentCentered not found");
84 
85  double t = 2.0 * momentCentered.get(1, 1) / (momentCentered.get(2, 0) - momentCentered.get(0, 2));
86  // double alpha = 0.5 * atan2(2.0 * momentCentered.get(1, 1),
87  // (momentCentered.get(2, 0) - momentCentered.get(0, 2)));
88  double alpha = 0.5 * atan(t);
89 
90  std::vector<double> rotMu(4);
91  // std::vector<double> realMu(4);
92 
93  if (isRef) {
94  alphaRef = alpha;
95  } else {
96  if (!symmetric) {
97  double r11 = cos(alpha - alphaRef);
98  double r12 = sin(alpha - alphaRef);
99  double r21 = -r12;
100  double r22 = r11;
101  unsigned int idx = 0;
102  unsigned int order = 4;
103  for (unsigned int c = 0; c < (order) * (order); c++) {
104  unsigned int i = c % order;
105  unsigned int j = c / order;
106 
107  if (i + j == 3) {
108  double r11_k = 1.;
109  for (unsigned int k = 0; k <= i; k++) {
110  double r12_i_k = pow(r12, (int)(i - k));
111  double comb_i_k = static_cast<double>(vpMath::comb(i, k));
112  for (unsigned int l = 0; l <= j; l++) {
113  rotMu[idx] += static_cast<double>(comb_i_k * vpMath::comb(j, l) * r11_k * pow(r21, (int)l) * r12_i_k *
114  pow(r22, (int)(j - l)) *
115  momentCentered.get(k + l, (unsigned int)(int)(i + j - k - l)));
116  }
117  r11_k *= r11;
118  }
119  // realMu[idx] = momentCentered.get(i, j);
120  idx++;
121  }
122  }
123 
124  double sum = 0.;
125  bool signChange = true;
126  for (unsigned int i = 0; i < 4; i++) {
127  if (std::fabs(rotMu[i]) > 1e10 * std::numeric_limits<double>::epsilon() &&
128  std::fabs(ref[i]) > 1e10 * std::numeric_limits<double>::epsilon() && rotMu[i] * ref[i] > 0)
129  signChange = false;
130  sum += std::fabs(rotMu[i] * ref[i]);
131  }
132 
133  if (sum < 1e4 * std::numeric_limits<double>::epsilon())
134  signChange = false;
135  if (signChange) {
136  // alpha = alpha + M_PI;
137  if (alpha < 0)
138  alpha += M_PI;
139  else
140  alpha -= M_PI;
141  }
142  }
143  }
144  values[0] = alpha;
145 }
146 
150 VISP_EXPORT std::ostream &operator<<(std::ostream &os, const vpMomentAlpha &c)
151 {
152  os << (__FILE__) << std::endl;
153  os << "Alpha = " << c.values[0] << "rad = " << vpMath::deg(c.values[0]) << "deg " << std::endl;
154  return os;
155 }
156 
160 void vpMomentAlpha::printDependencies(std::ostream &os) const
161 {
162  os << (__FILE__) << std::endl;
163  bool found_moment_centered;
164  const vpMomentCentered &momentCentered =
165  (static_cast<const vpMomentCentered &>(getMoments().get("vpMomentCentered", found_moment_centered)));
166  if (!found_moment_centered)
167  throw vpException(vpException::notInitialized, "vpMomentCentered not found");
168 
169  os << "mu11 = " << momentCentered.get(1, 1) << "\t";
170  os << "mu20 = " << momentCentered.get(2, 0) << "\t";
171  os << "mu02 = " << momentCentered.get(0, 2) << std::endl;
172 }
double get(unsigned int i, unsigned int j) const
friend VISP_EXPORT std::ostream & operator<<(std::ostream &os, const vpMomentAlpha &v)
const vpMoment & get(const char *type, bool &found) const
void printDependencies(std::ostream &os) const
error that can be emited by ViSP classes.
Definition: vpException.h:71
This class defines shared methods/attributes for 2D moments.
Definition: vpMoment.h:110
This class defines the double-indexed centered moment descriptor .
This class defines the orientation of the object inside the plane parallel to the object...
static long double comb(unsigned int n, unsigned int p)
Definition: vpMath.h:220
static double deg(double rad)
Definition: vpMath.h:95
vpMomentDatabase & getMoments() const
Definition: vpMoment.h:123
std::vector< double > values
Definition: vpMoment.h:118