Visual Servoing Platform version 3.5.0
testRobust.cpp
1/****************************************************************************
2 *
3 * ViSP, open source Visual Servoing Platform software.
4 * Copyright (C) 2005 - 2019 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 http://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 * Authors:
35 * Fabien Spindler
36 *
37 *****************************************************************************/
38
45#include <fstream>
46#include <iostream>
47#include <stdio.h>
48#include <stdlib.h>
49#include <string>
50#include <visp3/core/vpIoTools.h>
51#include <visp3/core/vpRobust.h>
52#include <visp3/io/vpParseArgv.h>
53// List of allowed command line options
54#define GETOPTARGS "cdho:"
55
56void usage(const char *name, const char *badparam, std::string ofilename);
57bool getOptions(int argc, const char **argv, std::string &ofilename);
58
67void usage(const char *name, const char *badparam, std::string ofilename)
68{
69 fprintf(stdout, "\n\
70Test some vpMath functionalities. Compute weights and print\n\
71them in an output file.\n\
72\n\
73Using gnuplot the content of the output file can be printed by:\n\
74set style data line\n\
75set ylabel \"weight\"\n\
76set yr [0:1.19]\n\
77set xlabel \"Normalized residuals\"\n\
78plot '%s' title \"Tukey Estimator\" lw 2, 1 title \"Least-Squares\" lw 2\n\
79\n\
80\n\
81SYNOPSIS\n\
82 %s [-o <output filename>] [-h]\n", ofilename.c_str(), name);
83
84 fprintf(stdout, "\n\
85OPTIONS: 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", ofilename.c_str());
92
93 if (badparam)
94 fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
95}
106bool getOptions(int argc, const char **argv, std::string &ofilename)
107{
108 const char *optarg_;
109 int c;
110 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
111
112 switch (c) {
113 case 'o':
114 ofilename = optarg_;
115 break;
116 case 'h':
117 usage(argv[0], NULL, ofilename);
118 return false;
119 break;
120
121 case 'c':
122 case 'd':
123 break;
124 default:
125 usage(argv[0], optarg_, ofilename);
126 return false;
127 break;
128 }
129 }
130
131 if ((c == 1) || (c == -1)) {
132 // standalone param or error
133 usage(argv[0], NULL, ofilename);
134 std::cerr << "ERROR: " << std::endl;
135 std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
136 return false;
137 }
138
139 return true;
140}
141
142int main(int argc, const char **argv)
143{
144 try {
145 std::string ofilename;
146 std::string username;
147
148// Set the default output filename
149#if defined(_WIN32)
150 ofilename = "C:/temp";
151#else
152 ofilename = "/tmp";
153#endif
154
155 // Get the user login name
156 vpIoTools::getUserName(username);
157
158 // Append to the output filename string, the login name of the user
159 ofilename = ofilename + "/" + username;
160
161 // Test if the output path exist. If no try to create it
162 if (vpIoTools::checkDirectory(ofilename) == false) {
163 try {
164 // Create the dirname
165 vpIoTools::makeDirectory(ofilename);
166 } catch (...) {
167 usage(argv[0], NULL, ofilename);
168 std::cerr << std::endl << "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 << "ERROR:" << std::endl;
192 std::cerr << " Cannot create the file: " << ofilename << std::endl;
193 std::cerr << " Check your -o " << ofilename << " option " << std::endl;
194 exit(-1);
195 }
196 double x = -10;
197 while (x < 10) {
198 if (fabs(x / sig) <= (4.6851)) {
199 w = vpMath::sqr(1 - vpMath::sqr(x / (sig * 4.6851)));
200 } else {
201 w = 0;
202 }
203 f << x << " " << w << std::endl;
204 x += 0.01;
205 }
206 return 0;
207 } catch (const vpException &e) {
208 std::cout << "Catch an exception: " << e << std::endl;
209 return 1;
210 }
211}
error that can be emited by ViSP classes.
Definition: vpException.h:72
static bool checkDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:420
static std::string getUserName()
Definition: vpIoTools.cpp:316
static void makeDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:570
static double sqr(double x)
Definition: vpMath.h:116
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69