Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
vpMomentCentered.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  * Centered moment descriptor
32  *
33  * Authors:
34  * Filip Novotny
35  *
36  *****************************************************************************/
37 
38 #include <visp3/core/vpMomentCentered.h>
39 #include <visp3/core/vpMomentObject.h>
40 #include <visp3/core/vpMomentGravityCenter.h>
41 #include <exception>
42 #include <cassert>
43 
50 void vpMomentCentered::set(unsigned int i, unsigned int j, double value){
51  vpMomentObject mobj = getObject();
52  assert(i+j<=mobj.getOrder());
53  if(i+j>mobj.getOrder()) throw vpException(vpException::badValue,"You cannot set that value.");
54  values[j*(mobj.getOrder()+1)+i] = value;
55 }
56 
62  bool found_moment_gravity;
63  values.resize((getObject().getOrder()+1)*(getObject().getOrder()+1));
64 
65  const vpMomentGravityCenter& momentGravity = static_cast<const vpMomentGravityCenter&>(getMoments().get("vpMomentGravityCenter",found_moment_gravity));
66  if(!found_moment_gravity) throw vpException(vpException::notInitialized,"vpMomentGravityCenter not found");
67 
68  unsigned int order = getObject().getOrder()+1;
69  for(unsigned int j=0;j<(order);j++){
70  for(unsigned int i=0;i<order-j;i++){
71  unsigned int c = order*j+i;
72  values[c]=0;
73  for(unsigned int k=0;k<=i;k++){
74  double Xg_i_k = pow(-momentGravity.get()[0],(int)(i-k));
75  double comb_i_k = static_cast<double>( vpMath::comb(i,k) );
76  for(unsigned int l=0;l<=j;l++){
77  values[c]+= static_cast<double>( comb_i_k*vpMath::comb(j,l)
78  *Xg_i_k
79  *pow(-momentGravity.get()[1],(int)(j-l))*getObject().get(k,l) );
80  }
81  }
82  }
83  }
84 
85 }
86 
91 
92 }
93 
100 double vpMomentCentered::get(unsigned int i,unsigned int j) const {
101  unsigned int order = getObject().getOrder();
102  assert(i+j<=order);
103  if(i+j>order) throw vpException(vpException::badValue,"The requested value has not been computed, you should specify a higher order.");
104 
105  return values[j*(order+1)+i];
106 }
107 
124 VISP_EXPORT std::ostream & operator<<(std::ostream & os, const vpMomentCentered& m){
125  for(unsigned int i = 0;i<m.values.size();i++){
126  if(i%(m.getObject().getOrder()+1)==0)
127  os << std::endl;
128 
129  if((i%(m.getObject().getOrder()+1)+i/(m.getObject().getOrder()+1))<m.getObject().getOrder()+1)
130  os << m.values[i] ;
131  else
132  os << "x";
133 
134  os << "\t";
135  }
136  os << std::endl;
137  m.printWithIndices(os);
138  return os;
139 }
140 
144 void
145 vpMomentCentered::printWithIndices(std::ostream& os) const {
146  unsigned int orderp1 = getObject().getOrder()+1;
147  for(unsigned int k=0; k<orderp1; k++) {
148  for(unsigned int l=0; l<orderp1-k; l++)
149  {
150  os << "mu[" << k << "," << l << "] = " << this->get(k,l) << "\t";
151  }
152  os << std::endl;
153  }
154  os << std::endl;
155 }
156 
163 void
164 vpMomentCentered::printDependencies(std::ostream& os) const {
165  os << (__FILE__) << std::endl;
166  /*
167  Retreive the raw moments
168  */
169  const vpMomentObject objt = getObject();
171 
172  /*
173  Get xg,yg
174  */
175  bool found_moment_gravity;
176  const vpMomentGravityCenter& momentGravity = static_cast<const vpMomentGravityCenter&>(getMoments().get("vpMomentGravityCenter",found_moment_gravity));
177  if(!found_moment_gravity)
178  throw vpException(vpException::notInitialized,"vpMomentGravityCenter not found");
179  os << "Xg = " << momentGravity.getXg() << "\t" << "Yg = " << momentGravity.getYg() << std::endl;
180 }
static void printWithIndices(const vpMomentObject &momobj, std::ostream &os)
error that can be emited by ViSP classes.
Definition: vpException.h:73
Class for generic objects.
const std::vector< double > & get() const
const std::vector< double > & get() const
void printWithIndices(std::ostream &os) const
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
Class describing 2D gravity center moment.
vpMomentDatabase & getMoments() const
Definition: vpMoment.h:119
const std::vector< double > & get() const
static long double comb(unsigned int n, unsigned int p)
Definition: vpMath.h:234
void printDependencies(std::ostream &os) const
const vpMomentObject & getObject() const
Definition: vpMoment.h:145
std::vector< double > values
Definition: vpMoment.h:114
void set(unsigned int i, unsigned int j, double value)
unsigned int getOrder() const