Visual Servoing Platform version 3.6.0
Loading...
Searching...
No Matches
testMouseEvent.cpp
1/****************************************************************************
2 *
3 * ViSP, open source Visual Servoing Platform software.
4 * Copyright (C) 2005 - 2023 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 https://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 * Read an image sequence from the disk and display it.
33 *
34*****************************************************************************/
45#include <visp3/core/vpConfig.h>
46#include <visp3/core/vpDebug.h>
47#include <visp3/core/vpIoTools.h>
48#include <visp3/io/vpParseArgv.h>
49
50#include <iomanip>
51#include <sstream>
52#include <stdio.h>
53#include <stdlib.h>
54
55#if (defined(VISP_HAVE_GTK) || defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_D3D9))
56
57#include <visp3/core/vpImage.h>
58#include <visp3/io/vpImageIo.h>
59
60#include <visp3/core/vpMouseButton.h>
61#include <visp3/gui/vpDisplayD3D.h>
62#include <visp3/gui/vpDisplayGDI.h>
63#include <visp3/gui/vpDisplayGTK.h>
64#include <visp3/gui/vpDisplayX.h>
65
66#include <visp3/core/vpTime.h>
67
78// List of allowed command line options
79#define GETOPTARGS "cdi:Lp:ht:f:l:s:w"
80typedef enum {
81 vpX11,
82 vpGTK,
83 vpGDI,
84 vpD3D,
85} vpDisplayType;
86
101void usage(const char *name, const char *badparam, std::string ipath, std::string ppath, unsigned first, unsigned last,
102 unsigned step, vpDisplayType &dtype)
103{
104#if VISP_HAVE_DATASET_VERSION >= 0x030600
105 std::string ext("png");
106#else
107 std::string ext("pgm");
108#endif
109
110 fprintf(stdout, "\n\
111Read an image sequence from the disk and display it.\n\
112The sequence is made of separate images. Each image corresponds\n\
113to a PGM file.\n\
114\n\
115SYNOPSIS\n\
116 %s [-i <test image path>] [-p <personal image path>]\n\
117 [-f <first image>] [-l <last image>] [-s <step>] \n\
118 [-t <type of video device>] [-L] [-w] [-c] [-d] [-h]\n\
119 ",
120 name);
121
122 std::string display;
123 switch (dtype) {
124 case vpX11:
125 display = "X11";
126 break;
127 case vpGTK:
128 display = "GTK";
129 break;
130 case vpGDI:
131 display = "GDI";
132 break;
133 case vpD3D:
134 display = "D3D";
135 break;
136 }
137
138 fprintf(stdout, "\n\
139 OPTIONS: Default\n\
140 -i <test image path> %s\n\
141 Set image input path.\n\
142 From this path read \"cube/image.%%04d.%s\"\n\
143 images. These images come from ViSP-images-x.y.z.tar.gz\n\
144 available on the ViSP website.\n\
145 Setting the VISP_INPUT_IMAGE_PATH environment\n\
146 variable produces the same behaviour than using\n\
147 this option.\n\
148 \n\
149 -p <personal image path> %s\n\
150 Specify a personal sequence containing images \n\
151 to process.\n\
152 By image sequence, we mean one file per image.\n\
153 The format is selected by analysing the filename extension.\n\
154 Example : \"/Temp/visp-images/cube/image.%%04d.%s\"\n\
155 %%04d is for the image numbering.\n\
156 \n\
157 -f <first image> %u\n\
158 First image number of the sequence.\n\
159 \n\
160 -l <last image> %u\n\
161 Last image number of the sequence.\n\
162 \n\
163 -s <step> %u\n\
164 Step between two images.\n\
165\n\
166 -t <type of video device> \"%s\"\n\
167 String specifying the video device to use.\n\
168 Possible values:\n\
169 \"X11\": only on UNIX platforms,\n\
170 \"GTK\": on all plaforms,\n\
171 \"GDI\": only on Windows platform (Graphics Device Interface),\n\
172 \"D3D\": only on Windows platform (Direct3D).\n\
173\n\
174 -L\n\
175 Print the list of video-devices available and exit.\n\
176\n\
177 -c\n\
178 Disable mouse click.\n\
179\n\
180 -d\n\
181 Disable the image display. This can be useful \n\
182 for automatic tests using crontab under Unix or \n\
183 using the task manager under Windows.\n\
184\n\
185 -w\n\
186 Wait for a mouse click between two images.\n\
187 If the image display is disabled (using -d)\n\
188 this option is without effect.\n\
189\n\
190 -h\n\
191 Print the help.\n\n",
192 ipath.c_str(), ext.c_str(), ppath.c_str(), ext.c_str(), first, last, step, display.c_str());
193
194 if (badparam)
195 fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
196}
221bool getOptions(int argc, const char **argv, std::string &ipath, std::string &ppath, unsigned &first, unsigned &last,
222 unsigned &step, vpDisplayType &dtype, bool &list, bool &display, bool &click, bool &wait)
223{
224 const char *optarg_;
225 int c;
226 std::string sDisplayType;
227 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
228
229 switch (c) {
230 case 'c':
231 click = false;
232 break;
233 case 'd':
234 display = false;
235 break;
236 case 't':
237 sDisplayType = optarg_;
238 // Parse the display type option
239 if (sDisplayType.compare("X11") == 0) {
240 dtype = vpX11;
241 } else if (sDisplayType.compare("GTK") == 0) {
242 dtype = vpGTK;
243 } else if (sDisplayType.compare("GDI") == 0) {
244 dtype = vpGDI;
245 } else if (sDisplayType.compare("D3D") == 0) {
246 dtype = vpD3D;
247 }
248
249 break;
250 case 'i':
251 ipath = optarg_;
252 break;
253 case 'L':
254 list = true;
255 break;
256 case 'p':
257 ppath = optarg_;
258 break;
259 case 'f':
260 first = (unsigned)atoi(optarg_);
261 break;
262 case 'l':
263 last = (unsigned)atoi(optarg_);
264 break;
265 case 's':
266 step = (unsigned)atoi(optarg_);
267 break;
268 case 'w':
269 wait = true;
270 break;
271 case 'h':
272 usage(argv[0], NULL, ipath, ppath, first, last, step, dtype);
273 return false;
274 break;
275
276 default:
277 usage(argv[0], optarg_, ipath, ppath, first, last, step, dtype);
278 return false;
279 break;
280 }
281 }
282
283 if ((c == 1) || (c == -1)) {
284 // standalone param or error
285 usage(argv[0], NULL, ipath, ppath, first, last, step, dtype);
286 std::cerr << "ERROR: " << std::endl;
287 std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
288 return false;
289 }
290
291 return true;
292}
293
294int main(int argc, const char **argv)
295{
296 std::string env_ipath;
297 std::string opt_ipath;
298 std::string ipath;
299 std::string opt_ppath;
300 std::string dirname;
301 std::string filename;
302 unsigned opt_first = 30;
303 unsigned opt_last = 40;
304 unsigned opt_step = 1;
305 vpDisplayType opt_dtype; // Type of display to use
306 bool opt_list = false; // To print the list of video devices
307 bool opt_display = true;
308 bool opt_click = true;
309 bool opt_click_blocking = false;
310
311#if VISP_HAVE_DATASET_VERSION >= 0x030600
312 std::string ext("png");
313#else
314 std::string ext("pgm");
315#endif
316
317// Default display is one available
318#if defined(VISP_HAVE_GTK)
319 opt_dtype = vpGTK;
320#elif defined(VISP_HAVE_X11)
321 opt_dtype = vpX11;
322#elif defined(VISP_HAVE_GDI)
323 opt_dtype = vpGDI;
324#elif defined(VISP_HAVE_D3D9)
325 opt_dtype = vpD3D;
326#endif
327
328 // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
329 // environment variable value
331
332 // Set the default input path
333 if (!env_ipath.empty())
334 ipath = env_ipath;
335
336 // Read the command line options
337 if (getOptions(argc, argv, opt_ipath, opt_ppath, opt_first, opt_last, opt_step, opt_dtype, opt_list, opt_display,
338 opt_click, opt_click_blocking) == false) {
339 return EXIT_FAILURE;
340 }
341 // Print the list of video-devices available
342 if (opt_list) {
343 unsigned nbDevices = 0;
344 std::cout << "List of video-devices available: \n";
345#if defined(VISP_HAVE_GTK)
346 std::cout << " GTK (use \"-t GTK\" option to use it)\n";
347 nbDevices++;
348#endif
349#if defined(VISP_HAVE_X11)
350 std::cout << " X11 (use \"-t X11\" option to use it)\n";
351 nbDevices++;
352#endif
353#if defined(VISP_HAVE_GDI)
354
355 std::cout << " GDI (use \"-t GDI\" option to use it)\n";
356 nbDevices++;
357#endif
358#if defined(VISP_HAVE_D3D9)
359 std::cout << " D3D (use \"-t D3D\" option to use it)\n";
360 nbDevices++;
361#endif
362 if (!nbDevices) {
363 std::cout << " No display is available\n";
364 }
365 return EXIT_FAILURE;
366 }
367
368 if (!opt_display)
369 opt_click_blocking = false; // turn off the waiting
370
371 // Get the option values
372 if (!opt_ipath.empty())
373 ipath = opt_ipath;
374
375 // Compare ipath and env_ipath. If they differ, we take into account
376 // the input path comming from the command line option
377 if (!opt_ipath.empty() && !env_ipath.empty() && opt_ppath.empty()) {
378 if (ipath != env_ipath) {
379 std::cout << std::endl << "WARNING: " << std::endl;
380 std::cout << " Since -i <visp image path=" << ipath << "> "
381 << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
382 << " we skip the environment variable." << std::endl;
383 }
384 }
385
386 // Test if an input path is set
387 if (opt_ipath.empty() && env_ipath.empty() && opt_ppath.empty()) {
388 usage(argv[0], NULL, ipath, opt_ppath, opt_first, opt_last, opt_step, opt_dtype);
389 std::cerr << std::endl << "ERROR:" << std::endl;
390 std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
391 << " environment variable to specify the location of the " << std::endl
392 << " image path where test images are located." << std::endl
393 << " Use -p <personal image path> option if you want to " << std::endl
394 << " use personal images." << std::endl
395 << std::endl;
396
397 return EXIT_FAILURE;
398 }
399
400 // Declare an image, this is a gray level image (unsigned char)
401 // it size is not defined yet, it will be defined when the image will
402 // read on the disk
404
405 unsigned iter = opt_first;
406 std::ostringstream s;
407 char cfilename[FILENAME_MAX];
408
409 if (opt_ppath.empty()) {
410 // Warning : the datset is available on https://visp.inria.fr/download/
411 dirname = vpIoTools::createFilePath(ipath, "cube");
412
413 // Build the name of the image file
414 s.setf(std::ios::right, std::ios::adjustfield);
415 s << "image." << std::setw(4) << std::setfill('0') << iter << "." << ext;
416 filename = vpIoTools::createFilePath(dirname, s.str());
417 } else {
418
419 snprintf(cfilename, FILENAME_MAX, opt_ppath.c_str(), iter);
420 filename = cfilename;
421 }
422 // Read image named "filename" and put the bitmap in I
423 try {
424 vpImageIo::read(I, filename);
425 } catch (...) {
426 std::cerr << std::endl << "ERROR:" << std::endl;
427 std::cerr << " Cannot read " << filename << std::endl;
428 std::cerr << " Check your -i " << ipath << " option, " << std::endl
429 << " or your -p " << opt_ppath << " option " << std::endl
430 << " or VISP_INPUT_IMAGE_PATH environment variable" << std::endl;
431 return EXIT_FAILURE;
432 }
433 // Create a display for the image
434 vpDisplay *display = NULL;
435
436 switch (opt_dtype) {
437 case vpX11:
438 std::cout << "Requested X11 display functionalities..." << std::endl;
439#if defined(VISP_HAVE_X11)
440 display = new vpDisplayX;
441#else
442 std::cout << " Sorry, X11 video device is not available.\n";
443 std::cout << "Use \"" << argv[0] << " -l\" to print the list of available devices.\n";
444 return EXIT_FAILURE;
445#endif
446 break;
447 case vpGTK:
448 std::cout << "Requested GTK display functionalities..." << std::endl;
449#if defined(VISP_HAVE_GTK)
450 display = new vpDisplayGTK;
451#else
452 std::cout << " Sorry, GTK video device is not available.\n";
453 std::cout << "Use \"" << argv[0] << " -l\" to print the list of available devices.\n";
454 return EXIT_FAILURE;
455#endif
456 break;
457 case vpGDI:
458 std::cout << "Requested GDI display functionalities..." << std::endl;
459#if defined(VISP_HAVE_GDI)
460
461 display = new vpDisplayGDI;
462#else
463 std::cout << " Sorry, GDI video device is not available.\n";
464 std::cout << "Use \"" << argv[0] << " -l\" to print the list of available devices.\n";
465 return EXIT_FAILURE;
466#endif
467 break;
468 case vpD3D:
469 std::cout << "Requested D3D display functionalities..." << std::endl;
470#if defined(VISP_HAVE_D3D9)
471 display = new vpDisplayD3D;
472#else
473 std::cout << " Sorry, D3D video device is not available.\n";
474 std::cout << "Use \"" << argv[0] << " -l\" to print the list of available devices.\n";
475 return EXIT_FAILURE;
476#endif
477 break;
478 }
479
480 if (opt_display) {
481 try {
482 // We open a window using either X11 or GTK or GDI.
483 // Its size is automatically defined by the image (I) size
484 display->init(I, 100, 100, "Display...");
485
486 // Display the image
487 // The image class has a member that specify a pointer toward
488 // the display that has been initialized in the display declaration
489 // therefore is is no longer necessary to make a reference to the
490 // display variable.
493 } catch (...) {
494 vpERROR_TRACE("Error while displaying the image");
495 delete display;
496 return EXIT_FAILURE;
497 }
498 }
499
500 // this is the loop over the image sequence
501 while (iter < opt_last) {
502 try {
503 double tms = vpTime::measureTimeMs();
504
505 // set the new image name
506 if (opt_ppath.empty()) {
507 s.str("");
508 s << "image." << std::setw(4) << std::setfill('0') << iter << "." << ext;
509 filename = vpIoTools::createFilePath(dirname, s.str());
510 } else {
511 snprintf(cfilename, FILENAME_MAX, opt_ppath.c_str(), iter);
512 filename = cfilename;
513 }
514
515 std::cout << "read : " << filename << std::endl;
516 // read the image
517 vpImageIo::read(I, filename);
518 if (opt_display) {
519 // Display the image
521 // Flush the display
523
524 if (opt_click_blocking) {
525 std::cout << "A click in the image to continue..." << std::endl;
526 }
527 vpImagePoint ip;
528
529 if (opt_click) {
531 bool pressed = vpDisplay::getClick(I, ip, button, opt_click_blocking);
532 if (pressed) {
533 switch (button) {
535 std::cout << "Left button was pressed." << std::endl;
536 break;
538 std::cout << "Middle button was pressed." << std::endl;
539 break;
541 std::cout << "Right button was pressed. Bye. " << std::endl;
542 delete display;
543 return EXIT_SUCCESS;
544 break;
546 break;
547 }
548 }
549 }
550 vpTime::wait(tms, 1000);
551 } else {
552 // Synchronise the loop to 40 ms
553 vpTime::wait(tms, 40);
554 }
555 } catch (...) {
556 delete display;
557 return EXIT_FAILURE;
558 }
559 iter += opt_step;
560 }
561 delete display;
562}
563#else
564int main() { vpERROR_TRACE("You do not have X11 or GTK display functionalities..."); }
565
566#endif
Display for windows using Direct3D 3rd party. Thus to enable this class Direct3D should be installed....
Display for windows using GDI (available on any windows 32 platform).
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition vpDisplayX.h:132
Class that defines generic functionalities for display.
Definition vpDisplay.h:173
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
static void flush(const vpImage< unsigned char > &I)
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition of the vpImage class member functions.
Definition vpImage.h:135
static std::string getViSPImagesDataPath()
static std::string createFilePath(const std::string &parent, const std::string &child)
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
#define vpERROR_TRACE
Definition vpDebug.h:388
VISP_EXPORT int wait(double t0, double t)
VISP_EXPORT double measureTimeMs()