Visual Servoing Platform  version 3.2.0 under development (2019-01-22)
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(int /*argc*/, const char ** /*argv*/)
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((unsigned int)nb_elements, 1.0), 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((unsigned int)nb_elements);
66  robust.setThreshold(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  {
74 
75  vpMbtTukeyEstimator<double> tukey_estimator;
76  std::vector<double> residues(nb_elements);
77  for (size_t i = 0; i < residues.size(); i++) {
78  residues[i] = residues_col[(unsigned int)i];
79  }
80 
81  std::vector<double> weights(nb_elements, 1);
82  double t = vpTime::measureTimeMs();
83  for (int i = 0; i < nb_iterations; i++) {
84  tukey_estimator.MEstimator(residues, weights, noise_threshold);
85  }
86  t = vpTime::measureTimeMs() - t;
87 
88  std::cout << "t_robust=" << t_robust << " ms ; t (double)=" << t << " ; ratio=" << (t_robust / t) << std::endl;
89 
90  for (size_t i = 0; i < weights.size(); i++) {
91  if (!vpMath::equal(weights[i], weights_col[(unsigned int)i], noise_threshold)) {
92  std::cerr << "Difference between vpRobust::TUKEY and "
93  "vpMbtTukeyEstimator (double)!"
94  << std::endl;
95  std::cerr << "weights_col[" << i << "]=" << weights_col[(unsigned int)i] << std::endl;
96  std::cerr << "weights[" << i << "]=" << weights[i] << std::endl;
97  return EXIT_FAILURE;
98  }
99  }
100  }
101 
102  // Generate again for weights != 1
103  for (size_t i = 0; i < nb_elements; i++) {
104  residues_col[(unsigned int)i] = noise();
105  }
106  weights_col_save = weights_col;
107  t_robust = vpTime::measureTimeMs();
108  for (int i = 0; i < nb_iterations; i++) {
109  robust.MEstimator(vpRobust::TUKEY, residues_col, weights_col);
110  }
111  t_robust = vpTime::measureTimeMs() - t_robust;
112 
113  {
114  vpMbtTukeyEstimator<float> tukey_estimator;
115  std::vector<float> residues(nb_elements);
116  std::vector<float> weights(nb_elements);
117  for (size_t i = 0; i < residues.size(); i++) {
118  residues[i] = (float)residues_col[(unsigned int)i];
119  weights[i] = (float)weights_col_save[(unsigned int)i];
120  }
121 
122  double t = vpTime::measureTimeMs();
123  for (int i = 0; i < nb_iterations; i++) {
124  tukey_estimator.MEstimator(residues, weights, (float)noise_threshold);
125  }
126  t = vpTime::measureTimeMs() - t;
127 
128  std::cout << "t_robust=" << t_robust << " ms ; t (float)=" << t << " ; ratio=" << (t_robust / t) << std::endl;
129 
130  for (size_t i = 0; i < weights.size(); i++) {
131  if (!vpMath::equal(weights[i], weights_col[(unsigned int)i], noise_threshold)) {
132  std::cerr << "Difference between vpRobust::TUKEY and "
133  "vpMbtTukeyEstimator (float)!"
134  << std::endl;
135  std::cerr << "weights_col[" << i << "]=" << weights_col[(unsigned int)i] << std::endl;
136  std::cerr << "weights[" << i << "]=" << weights[i] << std::endl;
137  return EXIT_FAILURE;
138  }
139  }
140  }
141 
142  // Generate again for weights != 1 and vpColVector type
143  for (size_t i = 0; i < nb_elements; i++) {
144  residues_col[(unsigned int)i] = noise();
145  }
146  weights_col_save = weights_col;
147  t_robust = vpTime::measureTimeMs();
148  for (int i = 0; i < nb_iterations; i++) {
149  robust.MEstimator(vpRobust::TUKEY, residues_col, weights_col);
150  }
151  t_robust = vpTime::measureTimeMs() - t_robust;
152 
153  {
154  vpMbtTukeyEstimator<double> tukey_estimator;
155  vpColVector residues = residues_col;
156  vpColVector weights = weights_col_save;
157 
158  double t = vpTime::measureTimeMs();
159  for (int i = 0; i < nb_iterations; i++) {
160  tukey_estimator.MEstimator(residues, weights, noise_threshold);
161  }
162  t = vpTime::measureTimeMs() - t;
163 
164  std::cout << "t_robust=" << t_robust << " ms ; t (vpColVector)=" << t << " ; ratio=" << (t_robust / t) << std::endl;
165 
166  for (size_t i = 0; i < weights.size(); i++) {
167  if (!vpMath::equal(weights[(unsigned int)i], weights_col[(unsigned int)i], noise_threshold)) {
168  std::cerr << "Difference between vpRobust::TUKEY and "
169  "vpMbtTukeyEstimator (float)!"
170  << std::endl;
171  std::cerr << "weights_col[" << i << "]=" << weights_col[(unsigned int)i] << std::endl;
172  std::cerr << "weights[" << i << "]=" << weights[(unsigned int)i] << std::endl;
173  return EXIT_FAILURE;
174  }
175  }
176  }
177 
178  std::cout << "vpMbtTukeyEstimator returns the same values than vpRobust::TUKEY." << std::endl;
179  return EXIT_SUCCESS;
180 }
static bool equal(double x, double y, double s=0.001)
Definition: vpMath.h:290
unsigned int size() const
Return the number of elements of the 2D array.
Definition: vpArray2D.h:158
VISP_EXPORT double measureTimeMs()
Definition: vpTime.cpp:88
Class for generating random number with normal probability density.
Definition: vpGaussRand.h:120
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
Contains an M-Estimator and various influence function.
Definition: vpRobust.h:58