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