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