Visual Servoing Platform  version 3.6.1 under development (2024-11-14)
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 <vector>
39 
40 #include <visp3/core/vpMomentAreaNormalized.h>
41 #include <visp3/core/vpMomentCentered.h>
42 #include <visp3/core/vpMomentGravityCenter.h>
43 #include <visp3/core/vpMomentObject.h>
44 #include <visp3/visual_features/vpFeatureMomentAreaNormalized.h>
45 #include <visp3/visual_features/vpFeatureMomentDatabase.h>
46 #include <visp3/visual_features/vpFeatureMomentGravityCenter.h>
47 #include <visp3/visual_features/vpFeatureMomentGravityCenterNormalized.h>
48 BEGIN_VISP_NAMESPACE
59 {
60  bool found_moment_gravity;
61  bool found_moment_surface_normalized;
62 
63  bool found_featuremoment_gravity;
64  bool found_featuremoment_surfacenormalized;
65 
66  const vpMomentAreaNormalized &momentSurfaceNormalized = static_cast<const vpMomentAreaNormalized &>(
67  moments.get("vpMomentAreaNormalized", found_moment_surface_normalized));
68  const vpMomentGravityCenter &momentGravity =
69  static_cast<const vpMomentGravityCenter &>(moments.get("vpMomentGravityCenter", found_moment_gravity));
70  vpFeatureMomentGravityCenter &featureMomentGravity = (static_cast<vpFeatureMomentGravityCenter &>(
71  featureMomentsDataBase->get("vpFeatureMomentGravityCenter", found_featuremoment_gravity)));
72  vpFeatureMomentAreaNormalized featureMomentAreaNormalized = (static_cast<vpFeatureMomentAreaNormalized &>(
73  featureMomentsDataBase->get("vpFeatureMomentAreaNormalized", found_featuremoment_surfacenormalized)));
74 
75  if (!found_moment_surface_normalized)
76  throw vpException(vpException::notInitialized, "vpMomentAreaNormalized not found");
77  if (!found_moment_gravity)
78  throw vpException(vpException::notInitialized, "vpMomentGravityCenter not found");
79 
80  if (!found_featuremoment_gravity)
81  throw vpException(vpException::notInitialized, "vpFeatureMomentGravityCenter not found");
82  if (!found_featuremoment_surfacenormalized)
83  throw vpException(vpException::notInitialized, "vpFeatureMomentAreaNormalized not found");
84 
85  interaction_matrices[0].resize(1, 6);
86  interaction_matrices[1].resize(1, 6);
87 
88  interaction_matrices[0] = momentGravity.get()[0] * featureMomentAreaNormalized.interaction(1) +
89  momentSurfaceNormalized.get()[0] * featureMomentGravity.interaction(1);
90  interaction_matrices[1] = momentGravity.get()[1] * featureMomentAreaNormalized.interaction(1) +
91  momentSurfaceNormalized.get()[0] * featureMomentGravity.interaction(2);
92 }
93 END_VISP_NAMESPACE
94 #else
95 
96 #include <vector>
97 
98 #include <visp3/core/vpMomentAreaNormalized.h>
99 #include <visp3/core/vpMomentCentered.h>
100 #include <visp3/core/vpMomentGravityCenter.h>
101 #include <visp3/core/vpMomentObject.h>
102 #include <visp3/visual_features/vpFeatureMomentDatabase.h>
103 #include <visp3/visual_features/vpFeatureMomentGravityCenterNormalized.h>
104 
105 BEGIN_VISP_NAMESPACE
115 {
116  bool found_moment_surface_normalized;
117  bool found_moment_gravity;
118  bool found_moment_centered;
119 
120  const vpMomentCentered &momentCentered =
121  static_cast<const vpMomentCentered &>(moments.get("vpMomentCentered", found_moment_centered));
122  const vpMomentGravityCenter &momentGravity =
123  static_cast<const vpMomentGravityCenter &>(moments.get("vpMomentGravityCenter", found_moment_gravity));
124  const vpMomentAreaNormalized &momentSurfaceNormalized = static_cast<const vpMomentAreaNormalized &>(
125  moments.get("vpMomentAreaNormalized", found_moment_surface_normalized));
126 
127  if (!found_moment_surface_normalized)
128  throw vpException(vpException::notInitialized, "vpMomentAreaNormalized not found");
129  if (!found_moment_gravity)
130  throw vpException(vpException::notInitialized, "vpMomentGravityCenter not found");
131  if (!found_moment_centered)
132  throw vpException(vpException::notInitialized, "vpMomentCentered not found");
133 
134  const vpMomentObject &momentObject = moment->getObject();
135  interaction_matrices[0].resize(1, 6);
136  interaction_matrices[1].resize(1, 6);
137 
138  double n11 = momentCentered.get(1, 1) / momentObject.get(0, 0);
139  double n20 = momentCentered.get(2, 0) / momentObject.get(0, 0);
140  double n02 = momentCentered.get(0, 2) / momentObject.get(0, 0);
141  double n10 = momentCentered.get(1, 0) / momentObject.get(0, 0);
142  double n01 = momentCentered.get(0, 1) / momentObject.get(0, 0);
143  double n03 = momentCentered.get(0, 3) / momentObject.get(0, 0);
144 
145  double Xg = momentGravity.getXg();
146  double Yg = momentGravity.getYg();
147 
148  double An = momentSurfaceNormalized.get()[0];
149 
150  double Xn = An * Xg;
151  double Yn = An * Yg;
152 
153  double Xnvx, Xnvy, Xnvz, Xnwx, Xnwy;
154  double Ynvx, Ynvy, Ynvz, Ynwx, Ynwy;
155 
156  if (momentObject.getType() == vpMomentObject::DISCRETE) {
157  double a = momentCentered.get(2, 0) + momentCentered.get(0, 2);
158 
159  double e11 = momentCentered.get(1, 1) / a;
160  double e12 = momentCentered.get(1, 2) / a;
161  double e21 = momentCentered.get(2, 1) / a;
162  double e30 = momentCentered.get(3, 0) / a;
163 
164  double NA = n20 + n02;
165 
166  Xnvx = B * Xn * e11 - Yn * B - An * C - n02 * A * Xn / NA;
167  Xnvy = A * Xn * e11 + n02 * B * Xn / NA;
168 
169  Xnwx = An * e11 * NA + Yn * n10 - Xn * Xg * e11 + Xn * n01 + Xn * n10 * e11 - Xn * e21 +
170  (-Xn * n03 + (Xn * n01 - Yn * Xg) * n02) / NA;
171  Xnwy = -An * NA + Xn * e12 + Xn * Xg - An + e11 * Xg * Yn - Xn * n01 * e11 - 2 * Xn * n10 + Xn * e30 + n02 * An +
172  (-Xn * Xg + Xn * n10) * n02 / NA;
173 
174  Ynvx = (Yn - n02 * Yn / NA) * A + Yn * e11 * B;
175  Ynvy = (-Xn + e11 * Yn) * A + (-Yn + n02 * Yn / NA) * B - An * C;
176 
177  Ynwx = n02 * An + Yn * n10 * e11 - e11 * Xg * Yn + An - Yn * e21 + Yn * n01 +
178  (-Yn * n03 + (Yn * n01 - Yn * Yg) * n02) / NA;
179  Ynwy = -An * e11 * NA + Yn * e11 * Yg - Yn * n01 * e11 + Yn * Xg + Yn * e12 + Yn * e30 - Xn * n01 - 2 * Yn * n10 +
180  (Yn * n10 - Yn * Xg) * n02 / NA;
181 
182  }
183  else {
184  Xnvx = -An * C - A * Xn - Yn * B;
185  Xnvy = (0.5) * B * Xn;
186 
187  Xnwx = (0.5 * (8. * n10 - Xg)) * Yn + 4. * An * n11 + 4. * n01 * Xn;
188  Xnwy = (0.5 * (-2. - 8. * n20)) * An + (0.5) * Xn * (-8. * n10 + Xg);
189 
190  Ynvx = (0.5) * A * Yn;
191  Ynvy = -(0.5) * B * Yn - C * An - A * Xn;
192 
193  Ynwx = (0.5) * Yn * (8. * n01 - Yg) + (.5 * (2. + 8. * n02)) * An;
194  Ynwy = (0.5 * (-8. * n10 + Xg)) * Yn - 4. * An * n11 - 4. * n01 * Xn;
195  }
196 
197  Ynvz = -A * Ynwy + (-An + Ynwx) * B;
198  Xnvz = -A * An - A * Xnwy + B * Xnwx;
199 
200  int VX = 0;
201  int VY = 1;
202  int VZ = 2;
203  int WX = 3;
204  int WY = 4;
205  int WZ = 5;
206 
207  interaction_matrices[0][0][VX] = Xnvx;
208  interaction_matrices[0][0][VY] = Xnvy;
209  interaction_matrices[0][0][VZ] = Xnvz;
210 
211  interaction_matrices[0][0][WX] = Xnwx;
212  interaction_matrices[0][0][WY] = Xnwy;
213  interaction_matrices[0][0][WZ] = Yn;
214 
215  interaction_matrices[1][0][VX] = Ynvx;
216  interaction_matrices[1][0][VY] = Ynvy;
217  interaction_matrices[1][0][VZ] = Ynvz;
218 
219  interaction_matrices[1][0][WX] = Ynwx;
220  interaction_matrices[1][0][WY] = Ynwy;
221  interaction_matrices[1][0][WZ] = -Xn;
222 }
223 END_VISP_NAMESPACE
224 #endif
error that can be emitted by ViSP classes.
Definition: vpException.h:60
@ notInitialized
Used to indicate that a parameter is not initialized.
Definition: vpException.h:74
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
vpFeatureMomentDatabase * featureMomentsDataBase
vpMomentDatabase & moments
vpMatrix interaction(unsigned int select=FEATURE_ALL) VP_OVERRIDE
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:145
const std::vector< double > & get() const
Definition: vpMoment.h:150