Visual Servoing Platform  version 3.6.1 under development (2024-04-25)
vpMatrix_covariance.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See https://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Covariance matrix computation.
33  *
34  * Authors:
35  * Aurelien Yol
36  *
37 *****************************************************************************/
38 
39 #include <cmath> // std::fabs()
40 #include <limits> // numeric_limits
41 
42 #include <visp3/core/vpColVector.h>
43 #include <visp3/core/vpConfig.h>
44 #include <visp3/core/vpHomogeneousMatrix.h>
45 #include <visp3/core/vpMatrix.h>
46 #include <visp3/core/vpMatrixException.h>
47 #include <visp3/core/vpTranslationVector.h>
48 
60 {
61  // double denom = ((double)(A.getRows()) - (double)(A.getCols())); // To
62  // consider OLS Estimate for sigma
63  double denom = (static_cast<double>(A.getRows())); // To consider MLE Estimate for sigma
64 
65  if (denom <= std::numeric_limits<double>::epsilon()) {
67  "Impossible to compute covariance matrix: not enough data");
68  }
69 
70  // double sigma2 = ( ((b.t())*b) - ( (b.t())*A*x ) ); // Should be
71  // equivalent to line bellow.
72  double sigma2 = (b - (A * x)).t() * (b - (A * x));
73 
74  sigma2 /= denom;
75 
76  return (A.t() * A).pseudoInverse(A.getCols() * std::numeric_limits<double>::epsilon()) * sigma2;
77 }
78 
93  const vpMatrix &W)
94 {
95  double denom = 0.0;
96  vpMatrix W2(W.getCols(), W.getCols());
97  unsigned int w_cols = W.getCols();
98  for (unsigned int i = 0; i < w_cols; ++i) {
99  denom += W[i][i];
100  W2[i][i] = W[i][i] * W[i][i];
101  }
102 
103  if (denom <= std::numeric_limits<double>::epsilon()) {
105  "Impossible to compute covariance matrix: not enough data");
106  }
107 
108  // double sigma2 = ( ((W*b).t())*W*b - ( ((W*b).t())*W*A*x ) ); // Should
109  // be equivalent to line bellow.
110  double sigma2 = ((W * b) - (W * A * x)).t() * ((W * b) - (W * A * x));
111  sigma2 /= denom;
112 
113  return (A.t() * W2 * A).pseudoInverse(A.getCols() * std::numeric_limits<double>::epsilon()) * sigma2;
114 }
115 
128  const vpMatrix &Ls)
129 {
130  vpMatrix Js;
131  vpColVector deltaP;
132  vpMatrix::computeCovarianceMatrixVVS(cMo, deltaS, Ls, Js, deltaP);
133 
134  return vpMatrix::computeCovarianceMatrix(Js, deltaP, deltaS);
135 }
136 
153  const vpMatrix &Ls, const vpMatrix &W)
154 {
155  vpMatrix Js;
156  vpColVector deltaP;
157  vpMatrix::computeCovarianceMatrixVVS(cMo, deltaS, Ls, Js, deltaP);
158 
159  return vpMatrix::computeCovarianceMatrix(Js, deltaP, deltaS, W);
160 }
161 
162 void vpMatrix::computeCovarianceMatrixVVS(const vpHomogeneousMatrix &cMo, const vpColVector &deltaS, const vpMatrix &Ls,
163  vpMatrix &Js, vpColVector &deltaP)
164 {
165  // building Lp
166  vpMatrix LpInv(6, 6);
167  LpInv = 0;
168  LpInv[0][0] = -1.0;
169  LpInv[1][1] = -1.0;
170  LpInv[2][2] = -1.0;
171 
172  vpTranslationVector ctoInit;
173 
174  cMo.extract(ctoInit);
175  vpMatrix ctoInitSkew = ctoInit.skew();
176 
177  vpThetaUVector thetau;
178  cMo.extract(thetau);
179 
180  vpColVector tu(3);
181  for (unsigned int i = 0; i < 3; ++i) {
182  tu[i] = thetau[i];
183  }
184 
185  double theta = sqrt(tu.sumSquare());
186 
187  // --comment: declare variable Lthetau of three by three of type vpMatrix
188  vpMatrix LthetauInvAnalytic(3, 3);
189  vpMatrix I3(3, 3);
190  I3.eye();
191  // --comment: Lthetau equals -I3;
192  LthetauInvAnalytic = -I3;
193 
194  if ((theta / (2.0 * M_PI)) > std::numeric_limits<double>::epsilon()) {
195  // Computing [theta/2 u]_x
196  vpColVector theta2u(3);
197  for (unsigned int i = 0; i < 3; ++i) {
198  theta2u[i] = tu[i] / 2.0;
199  }
200  vpMatrix theta2u_skew = vpColVector::skew(theta2u);
201 
202  vpColVector u(3);
203  for (unsigned int i = 0; i < 3; ++i) {
204  u[i] = tu[i] / theta;
205  }
206  vpMatrix u_skew = vpColVector::skew(u);
207 
208  LthetauInvAnalytic +=
209  -((vpMath::sqr(vpMath::sinc(theta / 2.0)) * theta2u_skew) - ((1.0 - vpMath::sinc(theta)) * u_skew * u_skew));
210  }
211 
212  // --comment: vpMatrix LthetauInv equals Lthetau dot inverseByLU()
213 
214  ctoInitSkew = ctoInitSkew * LthetauInvAnalytic;
215 
216  for (unsigned int a = 0; a < 3; ++a) {
217  for (unsigned int b = 0; b < 3; ++b) {
218  LpInv[a][b + 3] = ctoInitSkew[a][b];
219  }
220  }
221 
222  for (unsigned int a = 0; a < 3; ++a) {
223  for (unsigned int b = 0; b < 3; ++b) {
224  LpInv[a + 3][b + 3] = LthetauInvAnalytic[a][b];
225  }
226  }
227 
228  // Building Js
229  Js = Ls * LpInv;
230 
231  // building deltaP
232  deltaP = Js.pseudoInverse(Js.getRows() * std::numeric_limits<double>::epsilon()) * deltaS;
233 }
unsigned int getCols() const
Definition: vpArray2D.h:327
unsigned int getRows() const
Definition: vpArray2D.h:337
Implementation of column vector and the associated operations.
Definition: vpColVector.h:163
static vpMatrix skew(const vpColVector &v)
@ divideByZeroError
Division by zero.
Definition: vpException.h:82
Implementation of an homogeneous matrix and operations on such kind of matrices.
void extract(vpRotationMatrix &R) const
static double sinc(double x)
Definition: vpMath.cpp:269
static double sqr(double x)
Definition: vpMath.h:201
error that can be emitted by the vpMatrix class and its derivatives
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:146
static vpMatrix computeCovarianceMatrix(const vpMatrix &A, const vpColVector &x, const vpColVector &b)
vpMatrix t() const
Definition: vpMatrix.cpp:465
static vpMatrix computeCovarianceMatrixVVS(const vpHomogeneousMatrix &cMo, const vpColVector &deltaS, const vpMatrix &Ls, const vpMatrix &W)
vpMatrix pseudoInverse(double svThreshold=1e-6) const
Definition: vpMatrix.cpp:2343
Implementation of a rotation vector as axis-angle minimal representation.
Class that consider the case of a translation vector.