Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
testTukeyEstimator.cpp
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2024 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  * Test Tukey M-Estimator.
32  */
33 
40 #include <cstdlib>
41 #include <iostream>
42 #include <time.h>
43 #include <visp3/core/vpConfig.h>
44 #include <visp3/core/vpGaussRand.h>
45 #include <visp3/core/vpRobust.h>
46 #include <visp3/mbt/vpMbtTukeyEstimator.h>
47 
48 int main()
49 {
50 #ifdef ENABLE_VISP_NAMESPACE
51  using namespace VISP_NAMESPACE_NAME;
52 #endif
53  size_t nb_elements = 1000;
54  int nb_iterations = 100;
55  double stdev = 0.5, mean = 0.0, noise_threshold = 1e-3;
56 
57  vpGaussRand noise(stdev, mean);
58  noise.seed((unsigned int)time(nullptr));
59 
60  vpColVector residues_col((unsigned int)nb_elements);
61  vpColVector weights_col, weights_col_save;
62  for (size_t i = 0; i < nb_elements; i++) {
63  residues_col[(unsigned int)i] = noise();
64  }
65 
66  vpRobust robust;
67  robust.setMinMedianAbsoluteDeviation(noise_threshold);
68  double t_robust = vpTime::measureTimeMs();
69  for (int i = 0; i < nb_iterations; i++) {
70  robust.MEstimator(vpRobust::TUKEY, residues_col, weights_col);
71  }
72  t_robust = vpTime::measureTimeMs() - t_robust;
73  {
74  vpMbtTukeyEstimator<double> tukey_estimator;
75  std::vector<double> residues(nb_elements);
76  for (size_t i = 0; i < residues.size(); i++) {
77  residues[i] = residues_col[(unsigned int)i];
78  }
79 
80  std::vector<double> weights;
81  double t = vpTime::measureTimeMs();
82  for (int i = 0; i < nb_iterations; i++) {
83  tukey_estimator.MEstimator(residues, weights, noise_threshold);
84  }
85  t = vpTime::measureTimeMs() - t;
86 
87  std::cout << "t_robust=" << t_robust << " ms ; t (double)=" << t << " ; ratio=" << (t_robust / t) << std::endl;
88 
89  for (size_t i = 0; i < weights.size(); i++) {
90  if (!vpMath::equal(weights[i], weights_col[(unsigned int)i], noise_threshold)) {
91  std::cerr << "Difference between vpRobust::TUKEY and "
92  "vpMbtTukeyEstimator (double)!"
93  << std::endl;
94  std::cerr << "weights_col[" << i << "]=" << weights_col[(unsigned int)i] << std::endl;
95  std::cerr << "weights[" << i << "]=" << weights[i] << std::endl;
96  return EXIT_FAILURE;
97  }
98  }
99  }
100 
101  // Generate again for weights != 1
102  for (size_t i = 0; i < nb_elements; i++) {
103  residues_col[(unsigned int)i] = noise();
104  }
105  weights_col_save = weights_col;
106  t_robust = vpTime::measureTimeMs();
107  for (int i = 0; i < nb_iterations; i++) {
108  robust.MEstimator(vpRobust::TUKEY, residues_col, weights_col);
109  }
110  t_robust = vpTime::measureTimeMs() - t_robust;
111 
112  {
113  vpMbtTukeyEstimator<float> tukey_estimator;
114  std::vector<float> residues(nb_elements);
115  std::vector<float> weights(nb_elements);
116  for (size_t i = 0; i < residues.size(); i++) {
117  residues[i] = (float)residues_col[(unsigned int)i];
118  weights[i] = (float)weights_col_save[(unsigned int)i];
119  }
120 
121  double t = vpTime::measureTimeMs();
122  for (int i = 0; i < nb_iterations; i++) {
123  tukey_estimator.MEstimator(residues, weights, (float)noise_threshold);
124  }
125  t = vpTime::measureTimeMs() - t;
126 
127  std::cout << "t_robust=" << t_robust << " ms ; t (float)=" << t << " ; ratio=" << (t_robust / t) << std::endl;
128 
129  for (size_t i = 0; i < weights.size(); i++) {
130  if (!vpMath::equal(weights[i], weights_col[(unsigned int)i], noise_threshold)) {
131  std::cerr << "Difference between vpRobust::TUKEY and "
132  "vpMbtTukeyEstimator (float)!"
133  << std::endl;
134  std::cerr << "weights_col[" << i << "]=" << weights_col[(unsigned int)i] << std::endl;
135  std::cerr << "weights[" << i << "]=" << weights[i] << std::endl;
136  return EXIT_FAILURE;
137  }
138  }
139  }
140 
141  // Generate again for weights != 1 and vpColVector type
142  for (size_t i = 0; i < nb_elements; i++) {
143  residues_col[(unsigned int)i] = noise();
144  }
145  weights_col_save = weights_col;
146  t_robust = vpTime::measureTimeMs();
147  for (int i = 0; i < nb_iterations; i++) {
148  robust.MEstimator(vpRobust::TUKEY, residues_col, weights_col);
149  }
150  t_robust = vpTime::measureTimeMs() - t_robust;
151 
152  {
153  vpMbtTukeyEstimator<double> tukey_estimator;
154  vpColVector residues = residues_col;
155  vpColVector weights = weights_col_save;
156 
157  double t = vpTime::measureTimeMs();
158  for (int i = 0; i < nb_iterations; i++) {
159  tukey_estimator.MEstimator(residues, weights, noise_threshold);
160  }
161  t = vpTime::measureTimeMs() - t;
162 
163  std::cout << "t_robust=" << t_robust << " ms ; t (vpColVector)=" << t << " ; ratio=" << (t_robust / t) << std::endl;
164 
165  for (size_t i = 0; i < weights.size(); i++) {
166  if (!vpMath::equal(weights[(unsigned int)i], weights_col[(unsigned int)i], noise_threshold)) {
167  std::cerr << "Difference between vpRobust::TUKEY and "
168  "vpMbtTukeyEstimator (float)!"
169  << std::endl;
170  std::cerr << "weights_col[" << i << "]=" << weights_col[(unsigned int)i] << std::endl;
171  std::cerr << "weights[" << i << "]=" << weights[(unsigned int)i] << std::endl;
172  return EXIT_FAILURE;
173  }
174  }
175  }
176 
177  std::cout << "vpMbtTukeyEstimator returns the same values than vpRobust::TUKEY." << std::endl;
178  return EXIT_SUCCESS;
179 }
unsigned int size() const
Return the number of elements of the 2D array.
Definition: vpArray2D.h:349
Implementation of column vector and the associated operations.
Definition: vpColVector.h:191
Class for generating random number with normal probability density.
Definition: vpGaussRand.h:117
static bool equal(double x, double y, double threshold=0.001)
Definition: vpMath.h:459
Contains an M-estimator and various influence function.
Definition: vpRobust.h:84
@ TUKEY
Tukey influence function.
Definition: vpRobust.h:89
void MEstimator(const vpRobustEstimatorType method, const vpColVector &residues, vpColVector &weights)
Definition: vpRobust.cpp:130
void setMinMedianAbsoluteDeviation(double mad_min)
Definition: vpRobust.h:136
VISP_EXPORT double measureTimeMs()