ViSP  2.8.0
vpMomentAlpha.cpp
1 /****************************************************************************
2  *
3  * $Id: vpMomentAlpha.cpp 4056 2013-01-05 13:04:42Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2013 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Alpha moment descriptor for in-plane orientation.
36  *
37  * Authors:
38  * Filip Novotny
39  *
40  *****************************************************************************/
41 
42 #include <visp/vpMomentAlpha.h>
43 #include <visp/vpMomentGravityCenter.h>
44 #include <visp/vpMomentCentered.h>
45 #include <cmath>
46 
52 vpMomentAlpha::vpMomentAlpha() : vpMoment(),isRef(true), symmetric(false){
53  values.resize(1);
54 }
55 
64 vpMomentAlpha::vpMomentAlpha(std::vector<double>& ref,double alphaRef) : vpMoment(),isRef(false),symmetric(false),ref(ref),alphaRef(alphaRef){
65  for (std::vector<double>::iterator it = ref.begin(); it!=ref.end(); it++)
66  if (*it<=1e-4)
67  symmetric = true;
68 
69  values.resize(1);
70 }
71 
77  //symmetric = symmetric | this->getObject().isSymmetric();
78  bool found_moment_centered;
79 
80  vpMomentCentered& momentCentered = (static_cast<vpMomentCentered&> (getMoments().get("vpMomentCentered",
81  found_moment_centered)));
82 
83  if (!found_moment_centered)
84  throw vpException(vpException::notInitialized, "vpMomentCentered not found");
85 
86  double alpha = 0.5 * atan2(2.0 * momentCentered.get(1, 1), (momentCentered.get(2, 0) - momentCentered.get(0, 2)));
87 
88  unsigned int order = 4;
89  std::vector<double> rotMu(4);
90  std::vector<double> realMu(4);
91 
92  if (isRef)
93  {
94  alphaRef = alpha;
95  }
96  else
97  {
98  if (!symmetric)
99  {
100  double r11 = cos(alpha - alphaRef);
101  double r12 = sin(alpha - alphaRef);
102  double r21 = -sin(alpha - alphaRef);
103  double r22 = cos(alpha - alphaRef);
104  unsigned int idx = 0;
105  for (register unsigned int c = 0; c < (order) * (order); c++)
106  {
107  unsigned int i = c % order;
108  unsigned int j = c / order;
109 
110  if (i + j == 3)
111  {
112  double r11_k = 1.;
113  for (register unsigned int k = 0; k <= i; k++)
114  {
115  double r12_i_k = pow(r12, (int)(i - k));
116  double comb_i_k = static_cast<double> (vpMath::comb(i, k));
117  for (register unsigned int l = 0; l <= j; l++)
118  {
119  rotMu[idx] += static_cast<double> (comb_i_k * vpMath::comb(j, l) * r11_k * pow(r21, (int)l) * r12_i_k
120  * pow(r22, (int)(j - l)) * momentCentered.get(k + l, (unsigned int)(int)(i + j - k - l)));
121  }
122  r11_k *= r11;
123  }
124  realMu[idx] = momentCentered.get(i, j);
125  idx++;
126  }
127  }
128 
129  double sum = 0.;
130  bool signChange = true;
131  for (register unsigned int i = 0; i < 4; i++)
132  {
133  if (std::abs(rotMu[i]) > 1e10 * std::numeric_limits<double>::epsilon() && std::abs(ref[i]) > 1e10
134  * std::numeric_limits<double>::epsilon() && rotMu[i] * ref[i] > 0)
135  signChange = false;
136  sum += std::abs(rotMu[i] * ref[i]);
137  }
138 
139  if (sum < 1e4 * std::numeric_limits<double>::epsilon())
140  signChange = false;
141  if (signChange)
142  alpha = alpha + M_PI;
143  }
144  }
145  values[0] = alpha;
146 }
147 
151 std::ostream & operator<<(std::ostream & os, const vpMomentAlpha& c){
152  os << c.values[0] ;
153  return os;
154 }
155 
156 
157 
158 
vpMomentDatabase & getMoments()
Definition: vpMoment.h:115
double get(unsigned int i, unsigned int j)
error that can be emited by ViSP classes.
Definition: vpException.h:75
vpMoment & get(const char *type, bool &found)
This class defines shared methods/attributes for 2D moments.
Definition: vpMoment.h:104
This class defines the double-indexed centered moment descriptor .
VISP_EXPORT std::ostream & operator<<(std::ostream &os, const vpImagePoint &ip)
Definition: vpImagePoint.h:529
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:213
std::vector< double > values
Definition: vpMoment.h:110