Visual Servoing Platform  version 3.0.0
vpMomentAlpha.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2015 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 
59 vpMomentAlpha::vpMomentAlpha(std::vector<double>& ref_, double alpha_ref)
60  : vpMoment(),isRef(false),symmetric(false),ref(ref_),alphaRef(alpha_ref)
61 {
62  for (std::vector<double>::iterator it = ref_.begin(); it!=ref_.end(); it++)
63  if (*it<=1e-4)
64  symmetric = true;
65 
66  values.resize(1);
67 }
68 
74  //symmetric = symmetric | this->getObject().isSymmetric();
75  bool found_moment_centered;
76 
77  const vpMomentCentered& momentCentered = (static_cast<const vpMomentCentered&> (getMoments().get("vpMomentCentered",
78  found_moment_centered)));
79 
80  if (!found_moment_centered)
81  throw vpException(vpException::notInitialized, "vpMomentCentered not found");
82 
83  double alpha = 0.5 * atan2(2.0 * momentCentered.get(1, 1), (momentCentered.get(2, 0) - momentCentered.get(0, 2)));
84 
85  unsigned int order = 4;
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 = -sin(alpha - alphaRef);
100  double r22 = cos(alpha - alphaRef);
101  unsigned int idx = 0;
102  for (register unsigned int c = 0; c < (order) * (order); c++)
103  {
104  unsigned int i = c % order;
105  unsigned int j = c / order;
106 
107  if (i + j == 3)
108  {
109  double r11_k = 1.;
110  for (register unsigned int k = 0; k <= i; k++)
111  {
112  double r12_i_k = pow(r12, (int)(i - k));
113  double comb_i_k = static_cast<double> (vpMath::comb(i, k));
114  for (register unsigned int l = 0; l <= j; l++)
115  {
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)) * momentCentered.get(k + l, (unsigned int)(int)(i + j - k - l)));
118  }
119  r11_k *= r11;
120  }
121  realMu[idx] = momentCentered.get(i, j);
122  idx++;
123  }
124  }
125 
126  double sum = 0.;
127  bool signChange = true;
128  for (register unsigned int i = 0; i < 4; i++)
129  {
130  if (std::abs(rotMu[i]) > 1e10 * std::numeric_limits<double>::epsilon() && std::abs(ref[i]) > 1e10
131  * std::numeric_limits<double>::epsilon() && rotMu[i] * ref[i] > 0)
132  signChange = false;
133  sum += std::abs(rotMu[i] * ref[i]);
134  }
135 
136  if (sum < 1e4 * std::numeric_limits<double>::epsilon())
137  signChange = false;
138  if (signChange)
139  alpha = alpha + M_PI;
140  }
141  }
142  values[0] = alpha;
143 }
144 
148 VISP_EXPORT std::ostream & operator<<(std::ostream & os, const vpMomentAlpha& c){
149  os << (__FILE__) << std::endl;
150  os << "Alpha = " << c.values[0] << "rad = " << vpMath::deg(c.values[0]) << "deg " << std::endl;
151  return os;
152 }
153 
157 void vpMomentAlpha::printDependencies(std::ostream& os) const{
158  os << (__FILE__) << std::endl;
159  bool found_moment_centered;
160  const vpMomentCentered& momentCentered = (static_cast<const vpMomentCentered&> (getMoments().get("vpMomentCentered",
161  found_moment_centered)));
162  if (!found_moment_centered)
163  throw vpException(vpException::notInitialized, "vpMomentCentered not found");
164 
165  os << "mu11 = " << momentCentered.get(1, 1) << "\t";
166  os << "mu20 = " << momentCentered.get(2, 0) << "\t";
167  os << "mu02 = " << momentCentered.get(0, 2) << std::endl;
168 }
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:233
static double deg(double rad)
Definition: vpMath.h:97
std::vector< double > values
Definition: vpMoment.h:114