Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
testRobust.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 some vpMath functionalities.
32  */
33 
40 #include <fstream>
41 #include <iostream>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string>
45 #include <visp3/core/vpIoTools.h>
46 #include <visp3/core/vpRobust.h>
47 #include <visp3/io/vpParseArgv.h>
48 // List of allowed command line options
49 #define GETOPTARGS "cdho:"
50 
51 #ifdef ENABLE_VISP_NAMESPACE
52 using namespace VISP_NAMESPACE_NAME;
53 #endif
54 
55 void usage(const char *name, const char *badparam, std::string ofilename);
56 bool getOptions(int argc, const char **argv, std::string &ofilename);
57 
66 void usage(const char *name, const char *badparam, std::string ofilename)
67 {
68  fprintf(stdout, "\n\
69 Test some vpMath functionalities. Compute weights and print\n\
70 them in an output file.\n\
71 \n\
72 Using gnuplot the content of the output file can be printed by:\n\
73 set style data line\n\
74 set ylabel \"weight\"\n\
75 set yr [0:1.19]\n\
76 set xlabel \"Normalized residuals\"\n\
77 plot '%s' title \"Tukey Estimator\" lw 2, 1 title \"Least-Squares\" lw 2\n\
78 \n\
79 \n\
80 SYNOPSIS\n\
81  %s [-o <output filename>] [-h]\n",
82  ofilename.c_str(), name);
83 
84  fprintf(stdout, "\n\
85 OPTIONS: Default\n\
86  -o <output filename> %s\n\
87  Name and path of the file containing computed \n\
88  weights.\n\
89 \n\
90  -h\n\
91  Print the help.\n",
92  ofilename.c_str());
93 
94  if (badparam)
95  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
96 }
107 bool getOptions(int argc, const char **argv, std::string &ofilename)
108 {
109  const char *optarg_;
110  int c;
111  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
112 
113  switch (c) {
114  case 'o':
115  ofilename = optarg_;
116  break;
117  case 'h':
118  usage(argv[0], nullptr, ofilename);
119  return false;
120  break;
121 
122  case 'c':
123  case 'd':
124  break;
125  default:
126  usage(argv[0], optarg_, ofilename);
127  return false;
128  break;
129  }
130  }
131 
132  if ((c == 1) || (c == -1)) {
133  // standalone param or error
134  usage(argv[0], nullptr, ofilename);
135  std::cerr << "ERROR: " << std::endl;
136  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
137  return false;
138  }
139 
140  return true;
141 }
142 
143 int main(int argc, const char **argv)
144 {
145  try {
146  std::string ofilename;
147  std::string username;
148 
149 // Set the default output filename
150 #if defined(_WIN32)
151  ofilename = "C:/temp";
152 #else
153  ofilename = "/tmp";
154 #endif
155 
156  // Get the user login name
157  vpIoTools::getUserName(username);
158 
159  // Append to the output filename string, the login name of the user
160  ofilename = ofilename + "/" + username;
161 
162  // Test if the output path exist. If no try to create it
163  if (vpIoTools::checkDirectory(ofilename) == false) {
164  try {
165  // Create the dirname
166  vpIoTools::makeDirectory(ofilename);
167  }
168  catch (...) {
169  usage(argv[0], nullptr, ofilename);
170  std::cerr << std::endl << "ERROR:" << std::endl;
171  std::cerr << " Cannot create " << ofilename << std::endl;
172  std::cerr << " Check your -o " << ofilename << " option " << std::endl;
173  return EXIT_FAILURE;
174  }
175  }
176 
177  // Append to the output filename string, the name of the file
178  ofilename = ofilename + "/w.dat";
179 
180  // Read the command line options
181  if (getOptions(argc, argv, ofilename) == false) {
182  return EXIT_FAILURE;
183  }
184 
185  double sig = 1;
186 
187  double w;
188  std::ofstream f;
189  std::cout << "Create file: " << ofilename << std::endl;
190  f.open(ofilename.c_str());
191  if (f.fail()) {
192  usage(argv[0], nullptr, ofilename);
193  std::cerr << std::endl << "ERROR:" << std::endl;
194  std::cerr << " Cannot create the file: " << ofilename << std::endl;
195  std::cerr << " Check your -o " << ofilename << " option " << std::endl;
196  return EXIT_FAILURE;
197  }
198  double x = -10;
199  while (x < 10) {
200  if (fabs(x / sig) <= (4.6851)) {
201  w = vpMath::sqr(1 - vpMath::sqr(x / (sig * 4.6851)));
202  }
203  else {
204  w = 0;
205  }
206  f << x << " " << w << std::endl;
207  x += 0.01;
208  }
209  return EXIT_SUCCESS;
210  }
211  catch (const vpException &e) {
212  std::cout << "Catch an exception: " << e << std::endl;
213  return EXIT_FAILURE;
214  }
215 }
error that can be emitted by ViSP classes.
Definition: vpException.h:60
static bool checkDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:396
static std::string getUserName()
Definition: vpIoTools.cpp:285
static void makeDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:550
static double sqr(double x)
Definition: vpMath.h:203
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:70