Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
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 http://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 
38 #include <visp3/core/vpMomentAlpha.h>
39 #include <visp3/core/vpMomentGravityCenter.h>
40 #include <visp3/core/vpMomentCentered.h>
41 #include <cmath>
42 
48 vpMomentAlpha::vpMomentAlpha() : isRef(true), symmetric(false), ref(), alphaRef(0.) {
49  values.resize(1);
50 }
51 
58 vpMomentAlpha::vpMomentAlpha(std::vector<double>& ref_, double alpha_ref)
59  : vpMoment(),isRef(false),symmetric(false),ref(ref_),alphaRef(alpha_ref)
60 {
61  for (std::vector<double>::iterator it = ref_.begin(); it!=ref_.end(); ++it)
62  if (std::fabs(*it)<=1e-4)
63  symmetric = true;
64 
65  values.resize(1);
66 }
67 
73  //symmetric = symmetric | this->getObject().isSymmetric();
74  bool found_moment_centered;
75 
76  const vpMomentCentered& momentCentered = (static_cast<const vpMomentCentered&> (getMoments().get("vpMomentCentered",
77  found_moment_centered)));
78 
79  if (!found_moment_centered)
80  throw vpException(vpException::notInitialized, "vpMomentCentered not found");
81 
82  double t = 2.0 * momentCentered.get(1, 1) / (momentCentered.get(2, 0) - momentCentered.get(0, 2));
83  //double alpha = 0.5 * atan2(2.0 * momentCentered.get(1, 1), (momentCentered.get(2, 0) - momentCentered.get(0, 2)));
84  double alpha = 0.5 * atan(t);
85 
86  std::vector<double> rotMu(4);
87  //std::vector<double> realMu(4);
88 
89  if (isRef)
90  {
91  alphaRef = alpha;
92  }
93  else
94  {
95  if (!symmetric)
96  {
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  {
105  unsigned int i = c % order;
106  unsigned int j = c / order;
107 
108  if (i + j == 3)
109  {
110  double r11_k = 1.;
111  for (unsigned int k = 0; k <= i; k++)
112  {
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  {
117  rotMu[idx] += static_cast<double> (comb_i_k * vpMath::comb(j, l) * r11_k * pow(r21, (int)l) * r12_i_k
118  * pow(r22, (int)(j - l)) * momentCentered.get(k + l, (unsigned int)(int)(i + j - k - l)));
119  }
120  r11_k *= r11;
121  }
122  //realMu[idx] = momentCentered.get(i, j);
123  idx++;
124  }
125  }
126 
127  double sum = 0.;
128  bool signChange = true;
129  for (unsigned int i = 0; i < 4; i++)
130  {
131  if (std::fabs(rotMu[i]) > 1e10 * std::numeric_limits<double>::epsilon() && std::fabs(ref[i]) > 1e10
132  * std::numeric_limits<double>::epsilon() && rotMu[i] * ref[i] > 0)
133  signChange = false;
134  sum += std::fabs(rotMu[i] * ref[i]);
135  }
136 
137  if (sum < 1e4 * std::numeric_limits<double>::epsilon())
138  signChange = false;
139  if (signChange) {
140  // alpha = alpha + M_PI;
141  if (alpha < 0)
142  alpha += M_PI;
143  else
144  alpha -= M_PI;
145  }
146  }
147  }
148  values[0] = alpha;
149 }
150 
154 VISP_EXPORT std::ostream & operator<<(std::ostream & os, const vpMomentAlpha& c){
155  os << (__FILE__) << std::endl;
156  os << "Alpha = " << c.values[0] << "rad = " << vpMath::deg(c.values[0]) << "deg " << std::endl;
157  return os;
158 }
159 
163 void vpMomentAlpha::printDependencies(std::ostream& os) const{
164  os << (__FILE__) << std::endl;
165  bool found_moment_centered;
166  const vpMomentCentered& momentCentered = (static_cast<const vpMomentCentered&> (getMoments().get("vpMomentCentered",
167  found_moment_centered)));
168  if (!found_moment_centered)
169  throw vpException(vpException::notInitialized, "vpMomentCentered not found");
170 
171  os << "mu11 = " << momentCentered.get(1, 1) << "\t";
172  os << "mu20 = " << momentCentered.get(2, 0) << "\t";
173  os << "mu02 = " << momentCentered.get(0, 2) << std::endl;
174 }
error that can be emited by ViSP classes.
Definition: vpException.h:73
This class defines shared methods/attributes for 2D moments.
Definition: vpMoment.h:106
const vpMoment & get(const char *type, bool &found) const
This class defines the double-indexed centered moment descriptor .
friend std::ostream & operator<<(std::ostream &s, const vpArray2D< Type > &A)
Definition: vpArray2D.h:267
This class defines the orientation of the object inside the plane parallel to the object...
double get(unsigned int i, unsigned int j) const
vpMomentDatabase & getMoments() const
Definition: vpMoment.h:119
void printDependencies(std::ostream &os) const
static long double comb(unsigned int n, unsigned int p)
Definition: vpMath.h:234
static double deg(double rad)
Definition: vpMath.h:97
std::vector< double > values
Definition: vpMoment.h:114