Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
testRobust.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
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 http://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  * Authors:
34  * Fabien Spindler
35  *
36  *****************************************************************************/
37 
45 #include <visp3/core/vpRobust.h>
46 #include <string>
47 #include <fstream>
48 #include <visp3/core/vpIoTools.h>
49 #include <visp3/io/vpParseArgv.h>
50 #include <stdlib.h>
51 #include <stdio.h>
52 #include <iostream>
53 // List of allowed command line options
54 #define GETOPTARGS "cdho:"
55 
56 void usage(const char *name, const char *badparam, std::string ofilename);
57 bool getOptions(int argc, const char **argv, std::string &ofilename);
58 
67 void usage(const char *name, const char *badparam, std::string ofilename)
68 {
69  fprintf(stdout, "\n\
70 Test some vpMath functionalities. Compute weights and print\n\
71 them in an output file.\n\
72 \n\
73 Using gnuplot the content of the output file can be printed by:\n\
74 set style data line\n\
75 set ylabel \"weight\"\n\
76 set yr [0:1.19]\n\
77 set xlabel \"Normalized residuals\"\n\
78 plot '%s' title \"Tukey Estimator\" lw 2, 1 title \"Least-Squares\" lw 2\n\
79 \n\
80 \n\
81 SYNOPSIS\n\
82  %s [-o <output filename>] [-h]\n", 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': ofilename = optarg_; break;
115  case 'h': usage(argv[0], NULL, ofilename); return false; break;
116 
117  case 'c':
118  case 'd':
119  break;
120  default:
121  usage(argv[0], optarg_, ofilename);
122  return false; break;
123  }
124  }
125 
126  if ((c == 1) || (c == -1)) {
127  // standalone param or error
128  usage(argv[0], NULL, ofilename);
129  std::cerr << "ERROR: " << std::endl;
130  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
131  return false;
132  }
133 
134  return true;
135 }
136 
137 
138 
139 int
140 main(int argc, const char ** argv)
141 {
142  try {
143  std::string ofilename;
144  std::string username;
145 
146  // Set the default output filename
147 #if defined(_WIN32)
148  ofilename = "C:/temp";
149 #else
150  ofilename = "/tmp";
151 #endif
152 
153  // Get the user login name
154  vpIoTools::getUserName(username);
155 
156  // Append to the output filename string, the login name of the user
157  ofilename = ofilename + "/" + username;
158 
159  // Test if the output path exist. If no try to create it
160  if (vpIoTools::checkDirectory(ofilename) == false) {
161  try {
162  // Create the dirname
163  vpIoTools::makeDirectory(ofilename);
164  }
165  catch (...) {
166  usage(argv[0], NULL, ofilename);
167  std::cerr << std::endl
168  << "ERROR:" << std::endl;
169  std::cerr << " Cannot create " << ofilename << std::endl;
170  std::cerr << " Check your -o " << ofilename << " option " << std::endl;
171  exit(-1);
172  }
173  }
174 
175  // Append to the output filename string, the name of the file
176  ofilename = ofilename + "/w.dat";
177 
178  // Read the command line options
179  if (getOptions(argc, argv, ofilename) == false) {
180  exit (-1);
181  }
182 
183  double sig = 1 ;
184 
185  double w ;
186  std::ofstream f;
187  std::cout << "Create file: " << ofilename << std::endl;
188  f.open(ofilename.c_str());
189  if (f.fail()) {
190  usage(argv[0], NULL, ofilename);
191  std::cerr << std::endl
192  << "ERROR:" << std::endl;
193  std::cerr << " Cannot create the file: " << ofilename << std::endl;
194  std::cerr << " Check your -o " << ofilename << " option " << std::endl;
195  exit(-1);
196 
197  }
198  double x = -10 ;
199  while (x<10)
200  {
201  if (fabs(x/sig)<=(4.6851))
202  {
203  w = vpMath::sqr(1-vpMath::sqr(x/(sig*4.6851)));
204  }
205  else
206  {
207  w = 0;
208  }
209  f << x <<" "<<w <<std::endl ;
210  x+= 0.01 ;
211  }
212  return 0;
213  }
214  catch(vpException &e) {
215  std::cout << "Catch an exception: " << e << std::endl;
216  return 1;
217  }
218 }
219 
static bool checkDirectory(const char *dirname)
Definition: vpIoTools.cpp:358
error that can be emited by ViSP classes.
Definition: vpException.h:73
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:76
static void makeDirectory(const char *dirname)
Definition: vpIoTools.cpp:427
static double sqr(double x)
Definition: vpMath.h:110
static std::string getUserName()
Definition: vpIoTools.cpp:177