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