ViSP  2.9.0
testRobust.cpp
1 /****************************************************************************
2  *
3  * $Id: testRobust.cpp 4658 2014-02-09 09:50:14Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Test some vpMath functionalities.
36  *
37  * Authors:
38  * Fabien Spindler
39  *
40  *****************************************************************************/
41 
49 #include <visp/vpRobust.h>
50 #include <string>
51 #include <fstream>
52 #include <visp/vpIoTools.h>
53 #include <visp/vpParseArgv.h>
54 #include <stdlib.h>
55 #include <stdio.h>
56 #include <iostream>
57 // List of allowed command line options
58 #define GETOPTARGS "ho:"
59 
60 void usage(const char *name, const char *badparam, std::string ofilename);
61 bool getOptions(int argc, const char **argv, std::string &ofilename);
62 
71 void usage(const char *name, const char *badparam, std::string ofilename)
72 {
73  fprintf(stdout, "\n\
74 Test some vpMath functionalities. Compute weights and print\n\
75 them in an output file.\n\
76 \n\
77 Using gnuplot the content of the output file can be printed by:\n\
78 set style data line\n\
79 set ylabel \"weight\"\n\
80 set yr [0:1.19]\n\
81 set xlabel \"Normalized residuals\"\n\
82 plot '%s' title \"Tukey Estimator\" lw 2, 1 title \"Least-Squares\" lw 2\n\
83 \n\
84 \n\
85 SYNOPSIS\n\
86  %s [-o <output filename>] [-h]\n", ofilename.c_str(), name);
87 
88  fprintf(stdout, "\n\
89 OPTIONS: Default\n\
90  -o <output filename> %s\n\
91  Name and path of the file containing computed \n\
92  weights.\n\
93 \n\
94  -h\n\
95  Print the help.\n",
96  ofilename.c_str());
97 
98  if (badparam)
99  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
100 }
111 bool getOptions(int argc, const char **argv, std::string &ofilename)
112 {
113  const char *optarg_;
114  int c;
115  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
116 
117  switch (c) {
118  case 'o': ofilename = optarg_; break;
119  case 'h': usage(argv[0], NULL, ofilename); return false; break;
120 
121  default:
122  usage(argv[0], optarg_, ofilename);
123  return false; break;
124  }
125  }
126 
127  if ((c == 1) || (c == -1)) {
128  // standalone param or error
129  usage(argv[0], NULL, ofilename);
130  std::cerr << "ERROR: " << std::endl;
131  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
132  return false;
133  }
134 
135  return true;
136 }
137 
138 
139 
140 int
141 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  }
166  catch (...) {
167  usage(argv[0], NULL, ofilename);
168  std::cerr << std::endl
169  << "ERROR:" << std::endl;
170  std::cerr << " Cannot create " << ofilename << std::endl;
171  std::cerr << " Check your -o " << ofilename << " option " << std::endl;
172  exit(-1);
173  }
174  }
175 
176  // Append to the output filename string, the name of the file
177  ofilename = ofilename + "/w.dat";
178 
179  // Read the command line options
180  if (getOptions(argc, argv, ofilename) == false) {
181  exit (-1);
182  }
183 
184  double sig = 1 ;
185 
186  double w ;
187  std::ofstream f;
188  std::cout << "Create file: " << ofilename << std::endl;
189  f.open(ofilename.c_str());
190  if (f.fail()) {
191  usage(argv[0], NULL, ofilename);
192  std::cerr << std::endl
193  << "ERROR:" << std::endl;
194  std::cerr << " Cannot create the file: " << ofilename << std::endl;
195  std::cerr << " Check your -o " << ofilename << " option " << std::endl;
196  exit(-1);
197 
198  }
199  double x = -10 ;
200  while (x<10)
201  {
202  if (fabs(x/sig)<=(4.6851))
203  {
204  w = vpMath::sqr(1-vpMath::sqr(x/(sig*4.6851)));
205  }
206  else
207  {
208  w = 0;
209  }
210  f << x <<" "<<w <<std::endl ;
211  x+= 0.01 ;
212  }
213  return 0;
214  }
215  catch(vpException e) {
216  std::cout << "Catch an exception: " << e << std::endl;
217  return 1;
218  }
219 }
220 
static bool checkDirectory(const char *dirname)
Definition: vpIoTools.cpp:335
error that can be emited by ViSP classes.
Definition: vpException.h:76
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:79
static void makeDirectory(const char *dirname)
Definition: vpIoTools.cpp:404
static double sqr(double x)
Definition: vpMath.h:106
static std::string getUserName()
Definition: vpIoTools.cpp:140