ViSP
 All Classes Functions Variables Enumerations Enumerator Friends Groups Pages
testRobust.cpp
1 /****************************************************************************
2  *
3  * $Id: testRobust.cpp 4056 2013-01-05 13:04:42Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2013 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 
68 void usage(const char *name, const char *badparam, std::string ofilename)
69 {
70  fprintf(stdout, "\n\
71 Test some vpMath functionalities. Compute weights and print\n\
72 them in an output file.\n\
73 \n\
74 Using gnuplot the content of the output file can be printed by:\n\
75 set style data line\n\
76 set ylabel \"weight\"\n\
77 set yr [0:1.19]\n\
78 set xlabel \"Normalized residuals\"\n\
79 plot '%s' title \"Tukey Estimator\" lw 2, 1 title \"Least-Squares\" lw 2\n\
80 \n\
81 \n\
82 SYNOPSIS\n\
83  %s [-o <output filename>] [-h]\n", ofilename.c_str(), name);
84 
85  fprintf(stdout, "\n\
86 OPTIONS: Default\n\
87  -o <output filename> %s\n\
88  Name and path of the file containing computed \n\
89  weights.\n\
90 \n\
91  -h\n\
92  Print the help.\n",
93  ofilename.c_str());
94 
95  if (badparam)
96  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
97 }
108 bool getOptions(int argc, const char **argv, std::string &ofilename)
109 {
110  const char *optarg;
111  int c;
112  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
113 
114  switch (c) {
115  case 'o': ofilename = optarg; break;
116  case 'h': usage(argv[0], NULL, ofilename); return false; break;
117 
118  default:
119  usage(argv[0], optarg, ofilename);
120  return false; break;
121  }
122  }
123 
124  if ((c == 1) || (c == -1)) {
125  // standalone param or error
126  usage(argv[0], NULL, ofilename);
127  std::cerr << "ERROR: " << std::endl;
128  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
129  return false;
130  }
131 
132  return true;
133 }
134 
135 
136 
137 int
138 main(int argc, const char ** argv)
139 {
140  std::string ofilename;
141  std::string username;
142 
143  // Set the default output filename
144 #ifdef WIN32
145  ofilename = "C:/temp";
146 #else
147  ofilename = "/tmp";
148 #endif
149 
150  // Get the user login name
151  vpIoTools::getUserName(username);
152 
153  // Append to the output filename string, the login name of the user
154  ofilename = ofilename + "/" + username;
155 
156  // Test if the output path exist. If no try to create it
157  if (vpIoTools::checkDirectory(ofilename) == false) {
158  try {
159  // Create the dirname
160  vpIoTools::makeDirectory(ofilename);
161  }
162  catch (...) {
163  usage(argv[0], NULL, ofilename);
164  std::cerr << std::endl
165  << "ERROR:" << std::endl;
166  std::cerr << " Cannot create " << ofilename << std::endl;
167  std::cerr << " Check your -o " << ofilename << " option " << std::endl;
168  exit(-1);
169  }
170  }
171 
172  // Append to the output filename string, the name of the file
173  ofilename = ofilename + "/w.dat";
174 
175  // Read the command line options
176  if (getOptions(argc, argv, ofilename) == false) {
177  exit (-1);
178  }
179 
180  double sig = 1 ;
181 
182  double w ;
183  std::ofstream f;
184  std::cout << "Create file: " << ofilename << std::endl;
185  f.open(ofilename.c_str());
186  if (f == NULL) {
187  usage(argv[0], NULL, ofilename);
188  std::cerr << std::endl
189  << "ERROR:" << std::endl;
190  std::cerr << " Cannot create the file: " << ofilename << std::endl;
191  std::cerr << " Check your -o " << ofilename << " option " << std::endl;
192  exit(-1);
193 
194  }
195  double x = -10 ;
196  while (x<10)
197  {
198  if (fabs(x/sig)<=(4.6851))
199  {
200  w = vpMath::sqr(1-vpMath::sqr(x/(sig*4.6851)));
201  }
202  else
203  {
204  w = 0;
205  }
206  f << x <<" "<<w <<std::endl ;
207  x+= 0.01 ;
208  }
209 }
210 
static bool checkDirectory(const char *dirname)
Definition: vpIoTools.cpp:335
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