Visual Servoing Platform  version 3.6.1 under development (2024-04-24)
vpFeatureMomentBasic.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 <limits>
35 #include <vector>
36 #include <visp3/core/vpMomentObject.h>
37 #include <visp3/visual_features/vpFeatureMomentBasic.h>
38 #include <visp3/visual_features/vpFeatureMomentDatabase.h>
47 vpFeatureMomentBasic::vpFeatureMomentBasic(vpMomentDatabase &data_base, double A_, double B_, double C_,
48  vpFeatureMomentDatabase *featureMoments)
49  : vpFeatureMoment(data_base, A_, B_, C_, featureMoments), order(0)
50 { }
51 
57 {
58  int delta;
59  const vpMomentObject &m = moment->getObject();
60  order = m.getOrder() + 1;
62  for (std::vector<vpMatrix>::iterator i = interaction_matrices.begin(); i != interaction_matrices.end(); ++i)
63  i->resize(1, 6);
64  if (m.getType() == vpMomentObject::DISCRETE) {
65  delta = 0;
66  }
67  else {
68  delta = 1;
69  }
70 
71  int VX = 0;
72  int VY = 1;
73  int VZ = 2;
74  int WX = 3;
75  int WY = 4;
76  int WZ = 5;
77 
78  // i=0;j=0
79  interaction_matrices[0][0][VX] = -delta * A * m.get(0, 0);
80  interaction_matrices[0][0][VY] = -delta * B * m.get(0, 0);
81  interaction_matrices[0][0][VZ] =
82  3 * delta * (A * m.get(1, 0) + B * m.get(0, 1) + C * m.get(0, 0)) - delta * C * m.get(0, 0);
83 
84  interaction_matrices[0][0][WX] = 3 * delta * m.get(0, 1);
85  interaction_matrices[0][0][WY] = -3 * delta * m.get(1, 0);
86  interaction_matrices[0][0][WZ] = 0;
87 
88  // int i=0;
89  for (int j = 1; j < (int)order - 1; j++) {
90  unsigned int j_ = (unsigned int)j;
91  unsigned int jm1_ = j_ - 1;
92  unsigned int jp1_ = j_ + 1;
93 
94  interaction_matrices[j_ * order][0][VX] = -delta * A * m.get(0, j_);
95  interaction_matrices[j_ * order][0][VY] =
96  -j * (A * m.get(1, jm1_) + B * m.get(0, j_) + C * m.get(0, jm1_)) - delta * B * m.get(0, j_);
97  interaction_matrices[j_ * order][0][VZ] =
98  (j + 3 * delta) * (A * m.get(1, j_) + B * m.get(0, jp1_) + C * m.get(0, j_)) - delta * C * m.get(0, j_);
99 
100  interaction_matrices[j_ * order][0][WX] = (j + 3 * delta) * m.get(0, jp1_) + j * m.get(0, jm1_);
101  interaction_matrices[j_ * order][0][WY] = -(j + 3 * delta) * m.get(1, j_);
102  interaction_matrices[j_ * order][0][WZ] = -j * m.get(1, jm1_);
103  }
104 
105  // int j=0;
106  for (int i = 1; i < (int)order - 1; i++) {
107  unsigned int i_ = (unsigned int)i;
108  unsigned int im1_ = i_ - 1;
109  unsigned int ip1_ = i_ + 1;
110 
111  interaction_matrices[i_][0][VX] =
112  -i * (A * m.get(i_, 0) + B * m.get(im1_, 1) + C * m.get(im1_, 0)) - delta * A * m.get(i_, 0);
113  interaction_matrices[i_][0][VY] = -delta * B * m.get(i_, 0);
114  interaction_matrices[i_][0][VZ] =
115  (i + 3 * delta) * (A * m.get(ip1_, 0) + B * m.get(i_, 1) + C * m.get(i_, 0)) - delta * C * m.get(i_, 0);
116 
117  interaction_matrices[i_][0][WX] = (i + 3 * delta) * m.get(i_, 1);
118  interaction_matrices[i_][0][WY] = -(i + 3 * delta) * m.get(ip1_, 0) - i * m.get(im1_, 0);
119  interaction_matrices[i_][0][WZ] = i * m.get(im1_, 1);
120  }
121 
122  for (int j = 1; j < (int)order - 1; j++) {
123  unsigned int j_ = (unsigned int)j;
124  unsigned int jm1_ = j_ - 1;
125  unsigned int jp1_ = j_ + 1;
126 
127  for (int i = 1; i < (int)order - j - 1; i++) {
128  unsigned int i_ = (unsigned int)i;
129  unsigned int im1_ = i_ - 1;
130  unsigned int ip1_ = i_ + 1;
131 
132  interaction_matrices[j_ * order + i_][0][VX] =
133  -i * (A * m.get(i_, j_) + B * m.get(im1_, jp1_) + C * m.get(im1_, j_)) - delta * A * m.get(i_, j_);
134  interaction_matrices[j_ * order + i_][0][VY] =
135  -j * (A * m.get(ip1_, jm1_) + B * m.get(i_, j_) + C * m.get(i_, jm1_)) - delta * B * m.get(i_, j_);
136  interaction_matrices[j_ * order + i_][0][VZ] =
137  (i + j + 3 * delta) * (A * m.get(ip1_, j_) + B * m.get(i_, jp1_) + C * m.get(i_, j_)) -
138  delta * C * m.get(i_, j_);
139 
140  interaction_matrices[j_ * order + i_][0][WX] = (i + j + 3 * delta) * m.get(i_, jp1_) + j * m.get(i_, jm1_);
141  interaction_matrices[j_ * order + i_][0][WY] = -(i + j + 3 * delta) * m.get(ip1_, j_) - i * m.get(im1_, j_);
142  interaction_matrices[j_ * order + i_][0][WZ] = i * m.get(im1_, jp1_) - j * m.get(ip1_, jm1_);
143  }
144  }
145 }
146 
153 vpMatrix vpFeatureMomentBasic::interaction(unsigned int select_one, unsigned int select_two) const
154 {
155  if (select_one + select_two > moment->getObject().getOrder())
156  throw vpException(vpException::badValue, "The requested value has not "
157  "been computed, you should "
158  "specify a higher order.");
159  return interaction_matrices[select_two * order + select_one];
160 }
error that can be emitted by ViSP classes.
Definition: vpException.h:59
@ badValue
Used to indicate that a value is not in the allowed range.
Definition: vpException.h:85
void compute_interaction() vp_override
vpMatrix interaction(unsigned int select_one, unsigned int select_two) const
vpFeatureMomentBasic(vpMomentDatabase &moments, double A, double B, double C, vpFeatureMomentDatabase *featureMoments=nullptr)
This class allows to register all feature moments (implemented in vpFeatureMoment....
This class defines shared system methods/attributes for 2D moment features but no functional code....
std::vector< vpMatrix > interaction_matrices
const vpMoment * moment
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:146
This class allows to register all vpMoments so they can access each other according to their dependen...
Class for generic objects.
unsigned int getOrder() const
const std::vector< double > & get() const
vpObjectType getType() const
const vpMomentObject & getObject() const
Definition: vpMoment.h:144