Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
testRobust.cpp
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2024 by Inria. All rights reserved.
4 *
5 * This software is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
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 https://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
39
40#include <fstream>
41#include <iostream>
42#include <stdio.h>
43#include <stdlib.h>
44#include <string>
45#include <visp3/core/vpIoTools.h>
46#include <visp3/core/vpRobust.h>
47#include <visp3/io/vpParseArgv.h>
48// List of allowed command line options
49#define GETOPTARGS "cdho:"
50
51#ifdef ENABLE_VISP_NAMESPACE
52using namespace VISP_NAMESPACE_NAME;
53#endif
54
55void usage(const char *name, const char *badparam, std::string ofilename);
56bool getOptions(int argc, const char **argv, std::string &ofilename);
57
66void usage(const char *name, const char *badparam, std::string ofilename)
67{
68 fprintf(stdout, "\n\
69Test some vpMath functionalities. Compute weights and print\n\
70them in an output file.\n\
71\n\
72Using gnuplot the content of the output file can be printed by:\n\
73set style data line\n\
74set ylabel \"weight\"\n\
75set yr [0:1.19]\n\
76set xlabel \"Normalized residuals\"\n\
77plot '%s' title \"Tukey Estimator\" lw 2, 1 title \"Least-Squares\" lw 2\n\
78\n\
79\n\
80SYNOPSIS\n\
81 %s [-o <output filename>] [-h]\n",
82 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",
92 ofilename.c_str());
93
94 if (badparam)
95 fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
96}
107bool 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':
115 ofilename = optarg_;
116 break;
117 case 'h':
118 usage(argv[0], nullptr, ofilename);
119 return false;
120
121 case 'c':
122 case 'd':
123 break;
124 default:
125 usage(argv[0], optarg_, ofilename);
126 return false;
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
141int 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], nullptr, 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 return EXIT_FAILURE;
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 return EXIT_FAILURE;
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], nullptr, 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 return EXIT_FAILURE;
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 }
201 else {
202 w = 0;
203 }
204 f << x << " " << w << std::endl;
205 x += 0.01;
206 }
207 return EXIT_SUCCESS;
208 }
209 catch (const vpException &e) {
210 std::cout << "Catch an exception: " << e << std::endl;
211 return EXIT_FAILURE;
212 }
213}
error that can be emitted by ViSP classes.
Definition vpException.h:60
static bool checkDirectory(const std::string &dirname)
static std::string getUserName()
static void makeDirectory(const std::string &dirname)
static double sqr(double x)
Definition vpMath.h:203
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)