Visual Servoing Platform  version 3.6.1 under development (2024-04-20)
vpFeatureMomentGravityCenterNormalized.cpp
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
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 https://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  * Implementation for all supported moment features.
32  */
33 
34 #include <visp3/core/vpConfig.h>
35 
36 #ifdef VISP_MOMENTS_COMBINE_MATRICES
37 
38 #include <limits>
39 #include <vector>
40 
41 #include <visp3/core/vpMomentAreaNormalized.h>
42 #include <visp3/core/vpMomentCentered.h>
43 #include <visp3/core/vpMomentGravityCenter.h>
44 #include <visp3/core/vpMomentObject.h>
45 #include <visp3/visual_features/vpFeatureMomentAreaNormalized.h>
46 #include <visp3/visual_features/vpFeatureMomentDatabase.h>
47 #include <visp3/visual_features/vpFeatureMomentGravityCenter.h>
48 #include <visp3/visual_features/vpFeatureMomentGravityCenterNormalized.h>
49 
60 {
61  bool found_moment_gravity;
62  bool found_moment_surface_normalized;
63 
64  bool found_featuremoment_gravity;
65  bool found_featuremoment_surfacenormalized;
66 
67  const vpMomentAreaNormalized &momentSurfaceNormalized = static_cast<const vpMomentAreaNormalized &>(
68  moments.get("vpMomentAreaNormalized", found_moment_surface_normalized));
69  const vpMomentGravityCenter &momentGravity =
70  static_cast<const vpMomentGravityCenter &>(moments.get("vpMomentGravityCenter", found_moment_gravity));
71  vpFeatureMomentGravityCenter &featureMomentGravity = (static_cast<vpFeatureMomentGravityCenter &>(
72  featureMomentsDataBase->get("vpFeatureMomentGravityCenter", found_featuremoment_gravity)));
73  vpFeatureMomentAreaNormalized featureMomentAreaNormalized = (static_cast<vpFeatureMomentAreaNormalized &>(
74  featureMomentsDataBase->get("vpFeatureMomentAreaNormalized", found_featuremoment_surfacenormalized)));
75 
76  if (!found_moment_surface_normalized)
77  throw vpException(vpException::notInitialized, "vpMomentAreaNormalized not found");
78  if (!found_moment_gravity)
79  throw vpException(vpException::notInitialized, "vpMomentGravityCenter not found");
80 
81  if (!found_featuremoment_gravity)
82  throw vpException(vpException::notInitialized, "vpFeatureMomentGravityCenter not found");
83  if (!found_featuremoment_surfacenormalized)
84  throw vpException(vpException::notInitialized, "vpFeatureMomentAreaNormalized not found");
85 
86  interaction_matrices[0].resize(1, 6);
87  interaction_matrices[1].resize(1, 6);
88 
89  interaction_matrices[0] = momentGravity.get()[0] * featureMomentAreaNormalized.interaction(1) +
90  momentSurfaceNormalized.get()[0] * featureMomentGravity.interaction(1);
91  interaction_matrices[1] = momentGravity.get()[1] * featureMomentAreaNormalized.interaction(1) +
92  momentSurfaceNormalized.get()[0] * featureMomentGravity.interaction(2);
93 }
94 
95 #else
96 
97 #include <limits>
98 #include <vector>
99 
100 #include <visp3/core/vpMomentAreaNormalized.h>
101 #include <visp3/core/vpMomentCentered.h>
102 #include <visp3/core/vpMomentGravityCenter.h>
103 #include <visp3/core/vpMomentObject.h>
104 #include <visp3/visual_features/vpFeatureMomentDatabase.h>
105 #include <visp3/visual_features/vpFeatureMomentGravityCenterNormalized.h>
106 
116 {
117  bool found_moment_surface_normalized;
118  bool found_moment_gravity;
119  bool found_moment_centered;
120 
121  const vpMomentCentered &momentCentered =
122  static_cast<const vpMomentCentered &>(moments.get("vpMomentCentered", found_moment_centered));
123  const vpMomentGravityCenter &momentGravity =
124  static_cast<const vpMomentGravityCenter &>(moments.get("vpMomentGravityCenter", found_moment_gravity));
125  const vpMomentAreaNormalized &momentSurfaceNormalized = static_cast<const vpMomentAreaNormalized &>(
126  moments.get("vpMomentAreaNormalized", found_moment_surface_normalized));
127 
128  if (!found_moment_surface_normalized)
129  throw vpException(vpException::notInitialized, "vpMomentAreaNormalized not found");
130  if (!found_moment_gravity)
131  throw vpException(vpException::notInitialized, "vpMomentGravityCenter not found");
132  if (!found_moment_centered)
133  throw vpException(vpException::notInitialized, "vpMomentCentered not found");
134 
135  const vpMomentObject &momentObject = moment->getObject();
136  interaction_matrices[0].resize(1, 6);
137  interaction_matrices[1].resize(1, 6);
138 
139  double n11 = momentCentered.get(1, 1) / momentObject.get(0, 0);
140  double n20 = momentCentered.get(2, 0) / momentObject.get(0, 0);
141  double n02 = momentCentered.get(0, 2) / momentObject.get(0, 0);
142  double n10 = momentCentered.get(1, 0) / momentObject.get(0, 0);
143  double n01 = momentCentered.get(0, 1) / momentObject.get(0, 0);
144  double n03 = momentCentered.get(0, 3) / momentObject.get(0, 0);
145 
146  double Xg = momentGravity.getXg();
147  double Yg = momentGravity.getYg();
148 
149  double An = momentSurfaceNormalized.get()[0];
150 
151  double Xn = An * Xg;
152  double Yn = An * Yg;
153 
154  double Xnvx, Xnvy, Xnvz, Xnwx, Xnwy;
155  double Ynvx, Ynvy, Ynvz, Ynwx, Ynwy;
156 
157  if (momentObject.getType() == vpMomentObject::DISCRETE) {
158  double a = momentCentered.get(2, 0) + momentCentered.get(0, 2);
159 
160  double e11 = momentCentered.get(1, 1) / a;
161  double e12 = momentCentered.get(1, 2) / a;
162  double e21 = momentCentered.get(2, 1) / a;
163  double e30 = momentCentered.get(3, 0) / a;
164 
165  double NA = n20 + n02;
166 
167  Xnvx = B * Xn * e11 - Yn * B - An * C - n02 * A * Xn / NA;
168  Xnvy = A * Xn * e11 + n02 * B * Xn / NA;
169 
170  Xnwx = An * e11 * NA + Yn * n10 - Xn * Xg * e11 + Xn * n01 + Xn * n10 * e11 - Xn * e21 +
171  (-Xn * n03 + (Xn * n01 - Yn * Xg) * n02) / NA;
172  Xnwy = -An * NA + Xn * e12 + Xn * Xg - An + e11 * Xg * Yn - Xn * n01 * e11 - 2 * Xn * n10 + Xn * e30 + n02 * An +
173  (-Xn * Xg + Xn * n10) * n02 / NA;
174 
175  Ynvx = (Yn - n02 * Yn / NA) * A + Yn * e11 * B;
176  Ynvy = (-Xn + e11 * Yn) * A + (-Yn + n02 * Yn / NA) * B - An * C;
177 
178  Ynwx = n02 * An + Yn * n10 * e11 - e11 * Xg * Yn + An - Yn * e21 + Yn * n01 +
179  (-Yn * n03 + (Yn * n01 - Yn * Yg) * n02) / NA;
180  Ynwy = -An * e11 * NA + Yn * e11 * Yg - Yn * n01 * e11 + Yn * Xg + Yn * e12 + Yn * e30 - Xn * n01 - 2 * Yn * n10 +
181  (Yn * n10 - Yn * Xg) * n02 / NA;
182 
183  }
184  else {
185  Xnvx = -An * C - A * Xn - Yn * B;
186  Xnvy = (0.5) * B * Xn;
187 
188  Xnwx = (0.5 * (8. * n10 - Xg)) * Yn + 4. * An * n11 + 4. * n01 * Xn;
189  Xnwy = (0.5 * (-2. - 8. * n20)) * An + (0.5) * Xn * (-8. * n10 + Xg);
190 
191  Ynvx = (0.5) * A * Yn;
192  Ynvy = -(0.5) * B * Yn - C * An - A * Xn;
193 
194  Ynwx = (0.5) * Yn * (8. * n01 - Yg) + (.5 * (2. + 8. * n02)) * An;
195  Ynwy = (0.5 * (-8. * n10 + Xg)) * Yn - 4. * An * n11 - 4. * n01 * Xn;
196  }
197 
198  Ynvz = -A * Ynwy + (-An + Ynwx) * B;
199  Xnvz = -A * An - A * Xnwy + B * Xnwx;
200 
201  int VX = 0;
202  int VY = 1;
203  int VZ = 2;
204  int WX = 3;
205  int WY = 4;
206  int WZ = 5;
207 
208  interaction_matrices[0][0][VX] = Xnvx;
209  interaction_matrices[0][0][VY] = Xnvy;
210  interaction_matrices[0][0][VZ] = Xnvz;
211 
212  interaction_matrices[0][0][WX] = Xnwx;
213  interaction_matrices[0][0][WY] = Xnwy;
214  interaction_matrices[0][0][WZ] = Yn;
215 
216  interaction_matrices[1][0][VX] = Ynvx;
217  interaction_matrices[1][0][VY] = Ynvy;
218  interaction_matrices[1][0][VZ] = Ynvz;
219 
220  interaction_matrices[1][0][WX] = Ynwx;
221  interaction_matrices[1][0][WY] = Ynwy;
222  interaction_matrices[1][0][WZ] = -Xn;
223 }
224 #endif
error that can be emitted by ViSP classes.
Definition: vpException.h:59
@ notInitialized
Used to indicate that a parameter is not initialized.
Definition: vpException.h:86
Functionality computation for normalized surface moment feature. Computes the interaction matrix asso...
vpFeatureMoment & get(const std::string &feature_name, bool &found)
Functionality computation for gravity center moment feature. Computes the interaction matrix associat...
std::vector< vpMatrix > interaction_matrices
vpMatrix interaction(unsigned int select=FEATURE_ALL) vp_override
vpFeatureMomentDatabase * featureMomentsDataBase
vpMomentDatabase & moments
const vpMoment * moment
Class handling the normalized surface moment that is invariant in scale and used to estimate depth.
This class defines the double-indexed centered moment descriptor .
double get(unsigned int i, unsigned int j) const
const vpMoment & get(const std::string &moment_name, bool &found) const
Class describing 2D gravity center moment.
const std::vector< double > & get() const
Class for generic objects.
const std::vector< double > & get() const
vpObjectType getType() const
const vpMomentObject & getObject() const
Definition: vpMoment.h:144
const std::vector< double > & get() const
Definition: vpMoment.h:149