Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
testClick.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 for mouse click manipulations.
32  */
33 
34 #include <visp3/core/vpConfig.h>
35 #include <visp3/core/vpDebug.h>
36 
37 #include <iostream>
38 #include <stdlib.h>
39 #include <string>
40 
41 #if (defined(VISP_HAVE_GTK) || defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_D3D9) || \
42  defined(VISP_HAVE_OPENCV))
43 
44 #include <visp3/core/vpImage.h>
45 #include <visp3/core/vpIoTools.h>
46 #include <visp3/io/vpImageIo.h>
47 #include <visp3/io/vpParseArgv.h>
48 
49 #include <visp3/gui/vpDisplayD3D.h>
50 #include <visp3/gui/vpDisplayGDI.h>
51 #include <visp3/gui/vpDisplayGTK.h>
52 #include <visp3/gui/vpDisplayOpenCV.h>
53 #include <visp3/gui/vpDisplayX.h>
54 
62 // List of allowed command line options
63 #define GETOPTARGS "i:hlt:dc"
64 
65 #ifdef ENABLE_VISP_NAMESPACE
66 using namespace VISP_NAMESPACE_NAME;
67 #endif
68 
69 typedef enum { vpX11, vpGTK, vpGDI, vpD3D, vpCV } vpDisplayType;
70 
71 void usage(const char *name, const char *badparam, std::string ipath, vpDisplayType &dtype);
72 bool getOptions(int argc, const char **argv, std::string &ipath, vpDisplayType &dtype, bool &list, bool &click_allowed,
73  bool &display);
74 
85 void usage(const char *name, const char *badparam, std::string ipath, vpDisplayType &dtype)
86 {
87  fprintf(stdout, "\n\
88 Test click functionalities in video devices or display.\n\
89 \n\
90 SYNOPSIS\n\
91  %s [-i <input image path>] \n\
92  [-t <type of video device>] [-l] [-c] [-d] [-h]\n\
93 ",
94 name);
95 
96  std::string display;
97  switch (dtype) {
98  case vpX11:
99  display = "X11";
100  break;
101  case vpGTK:
102  display = "GTK";
103  break;
104  case vpGDI:
105  display = "GDI";
106  break;
107  case vpD3D:
108  display = "D3D";
109  break;
110  case vpCV:
111  display = "CV";
112  break;
113  }
114 
115  fprintf(stdout, "\n\
116 OPTIONS: Default\n\
117  -i <input image path> %s\n\
118  Set image input path.\n\
119  From this path read \"Klimt/Klimt.pgm\"\n\
120  and \"Klimt/Klimt.ppm\" images.\n\
121  Setting the VISP_INPUT_IMAGE_PATH environment\n\
122  variable produces the same behaviour than using\n\
123  this option.\n\
124 \n\
125  -t <type of video device> \"%s\"\n\
126  String specifying the video device to use.\n\
127  Possible values:\n\
128  \"X11\": only on UNIX platforms,\n\
129  \"GTK\": on all plaforms,\n\
130  \"GDI\": only on Windows platform (Graphics Device Interface),\n\
131  \"D3D\": only on Windows platform (Direct3D).\n\
132  \"CV\" : (OpenCV).\n\
133 \n\
134  -l\n\
135  Print the list of video-devices available and exit.\n\
136 \n\
137  -c\n\
138  Disable the mouse click. Useful to automate the \n\
139  execution of this program without human intervention.\n\
140 \n\
141  -d \n\
142  Turn off the display.\n\
143 \n\
144  -h\n\
145  Print the help.\n\n",
146  ipath.c_str(), display.c_str());
147 
148  if (badparam)
149  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
150 }
151 
170 bool getOptions(int argc, const char **argv, std::string &ipath, vpDisplayType &dtype, bool &list, bool &click_allowed,
171  bool &display)
172 {
173  const char *optarg_;
174  int c;
175  std::string sDisplayType;
176  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
177 
178  switch (c) {
179  case 'i':
180  ipath = optarg_;
181  break;
182  case 'l':
183  list = true;
184  break;
185  case 't':
186  sDisplayType = optarg_;
187  // Parse the display type option
188  if (sDisplayType.compare("X11") == 0) {
189  dtype = vpX11;
190  }
191  else if (sDisplayType.compare("GTK") == 0) {
192  dtype = vpGTK;
193  }
194  else if (sDisplayType.compare("GDI") == 0) {
195  dtype = vpGDI;
196  }
197  else if (sDisplayType.compare("D3D") == 0) {
198  dtype = vpD3D;
199  }
200  else if (sDisplayType.compare("CV") == 0) {
201  dtype = vpCV;
202  }
203 
204  break;
205  case 'h':
206  usage(argv[0], nullptr, ipath, dtype);
207  return false;
208  break;
209  case 'c':
210  click_allowed = false;
211  break;
212  case 'd':
213  display = false;
214  break;
215 
216  default:
217  usage(argv[0], optarg_, ipath, dtype);
218  return false;
219  break;
220  }
221  }
222 
223  if ((c == 1) || (c == -1)) {
224  // standalone param or error
225  usage(argv[0], nullptr, ipath, dtype);
226  std::cerr << "ERROR: " << std::endl;
227  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
228  return false;
229  }
230 
231  return true;
232 }
233 
234 int main(int argc, const char **argv)
235 {
236  try {
237  std::string env_ipath;
238  std::string opt_ipath;
239  bool opt_list = false; // To print the list of video devices
240  vpDisplayType opt_dtype; // Type of display to use
241  std::string ipath;
242  std::string filename;
243  bool opt_click_allowed = true;
244  bool opt_display = true;
245 
246 // Default display is one available
247 #if defined(VISP_HAVE_GTK)
248  opt_dtype = vpGTK;
249 #elif defined(VISP_HAVE_X11)
250  opt_dtype = vpX11;
251 #elif defined(VISP_HAVE_GDI)
252  opt_dtype = vpGDI;
253 #elif defined(VISP_HAVE_D3D9)
254  opt_dtype = vpD3D;
255 #elif defined VISP_HAVE_OPENCV
256  opt_dtype = vpCV;
257 #endif
258 
259  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
260  // environment variable value
261  env_ipath = vpIoTools::getViSPImagesDataPath();
262 
263  // Set the default input path
264  if (!env_ipath.empty())
265  ipath = env_ipath;
266 
267  // Read the command line options
268  if (getOptions(argc, argv, opt_ipath, opt_dtype, opt_list, opt_click_allowed, opt_display) == false) {
269  return EXIT_FAILURE;
270  }
271 
272  // Print the list of video-devices available
273  if (opt_list) {
274  unsigned nbDevices = 0;
275  std::cout << "List of video-devices available: \n";
276 #if defined(VISP_HAVE_GTK)
277  std::cout << " GTK (use \"-t GTK\" option to use it)\n";
278  nbDevices++;
279 #endif
280 #if defined(VISP_HAVE_X11)
281  std::cout << " X11 (use \"-t X11\" option to use it)\n";
282  nbDevices++;
283 #endif
284 #if defined(VISP_HAVE_GDI)
285 
286  std::cout << " GDI (use \"-t GDI\" option to use it)\n";
287  nbDevices++;
288 #endif
289 #if defined(VISP_HAVE_D3D9)
290  std::cout << " D3D (use \"-t D3D\" option to use it)\n";
291  nbDevices++;
292 #endif
293 #if defined VISP_HAVE_OPENCV
294  std::cout << " CV (use \"-t CV\" option to use it)\n";
295  nbDevices++;
296 #endif
297  if (!nbDevices) {
298  std::cout << " No display is available\n";
299  }
300  return EXIT_FAILURE;
301  }
302 
303  // Get the option values
304  if (!opt_ipath.empty())
305  ipath = opt_ipath;
306 
307  // Compare ipath and env_ipath. If they differ, we take into account
308  // the input path coming from the command line option
309  if (!opt_ipath.empty() && !env_ipath.empty()) {
310  if (ipath != env_ipath) {
311  std::cout << std::endl << "WARNING: " << std::endl;
312  std::cout << " Since -i <visp image path=" << ipath << "> "
313  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
314  << " we skip the environment variable." << std::endl;
315  }
316  }
317 
318  // Test if an input path is set
319  if (opt_ipath.empty() && env_ipath.empty()) {
320  usage(argv[0], nullptr, ipath, opt_dtype);
321  std::cerr << std::endl << "ERROR:" << std::endl;
322  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
323  << " environment variable to specify the location of the " << std::endl
324  << " image path where test images are located." << std::endl
325  << std::endl;
326  return EXIT_FAILURE;
327  }
328 
329  // Create a grey level image
331 
332  // Load a grey image from the disk
333  filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.pgm");
334  vpCTRACE << "Load " << filename << std::endl;
335  vpImageIo::read(I, filename);
336 
337  // Create a display for the image
338  vpDisplay *display = nullptr;
339 
340  switch (opt_dtype) {
341  case vpX11:
342  std::cout << "Requested X11 display functionalities..." << std::endl;
343 #if defined(VISP_HAVE_X11)
344  display = new vpDisplayX;
345 #else
346  std::cout << " Sorry, X11 video device is not available.\n";
347  std::cout << "Use \"" << argv[0] << " -l\" to print the list of available devices.\n";
348  return EXIT_FAILURE;
349 #endif
350  break;
351  case vpGTK:
352  std::cout << "Requested GTK display functionalities..." << std::endl;
353 #if defined(VISP_HAVE_GTK)
354  display = new vpDisplayGTK;
355 #else
356  std::cout << " Sorry, GTK video device is not available.\n";
357  std::cout << "Use \"" << argv[0] << " -l\" to print the list of available devices.\n";
358  return EXIT_FAILURE;
359 #endif
360  break;
361  case vpGDI:
362  std::cout << "Requested GDI display functionalities..." << std::endl;
363 #if defined(VISP_HAVE_GDI)
364 
365  display = new vpDisplayGDI;
366 #else
367  std::cout << " Sorry, GDI video device is not available.\n";
368  std::cout << "Use \"" << argv[0] << " -l\" to print the list of available devices.\n";
369  return EXIT_FAILURE;
370 #endif
371  break;
372  case vpD3D:
373  std::cout << "Requested D3D display functionalities..." << std::endl;
374 #if defined(VISP_HAVE_D3D9)
375  display = new vpDisplayD3D;
376 #else
377  std::cout << " Sorry, D3D video device is not available.\n";
378  std::cout << "Use \"" << argv[0] << " -l\" to print the list of available devices.\n";
379  return EXIT_FAILURE;
380 #endif
381  break;
382  case vpCV:
383  std::cout << "Requested OpenCV display functionalities..." << std::endl;
384 #if defined(HAVE_OPENCV_HIGHGUI)
385  display = new vpDisplayOpenCV;
386 #else
387  std::cout << " Sorry, OpenCV video device is not available.\n";
388  std::cout << "Use \"" << argv[0] << " -l\" to print the list of available devices.\n";
389  return EXIT_FAILURE;
390 #endif
391  break;
392  }
393 
394  if (opt_display) {
395 
396  // We open a window using either X11 or GTK or GDI.
397  // Its size is automatically defined by the image (I) size
398  display->init(I, 100, 100, "Display...");
399 
400  // Display the image
401  // The image class has a member that specify a pointer toward
402  // the display that has been initialized in the display declaration
403  // therefore is is no longer necessary to make a reference to the
404  // display variable.
406  // Flush the display
407  vpDisplay::flush(I);
408  if (opt_click_allowed) {
409  std::cout << "Click on a pixel to get his coordinates...\n";
410  vpImagePoint ip;
412  vpDisplay::getClick(I, ip, button);
413  std::cout << " You click down on pixel (" << ip << ") ";
414  switch (button) {
416  std::cout << "with left button.\n";
417  break;
419  std::cout << "with middle button.\n";
420  break;
422  std::cout << "with right button.\n";
423  break;
424  case vpMouseButton::none:
425  break;
426  }
427  vpDisplay::getClickUp(I, ip, button);
428  std::cout << " You click up on pixel (" << ip << ") ";
429  switch (button) {
431  std::cout << "with left button.\n";
432  break;
434  std::cout << "with middle button.\n";
435  break;
437  std::cout << "with right button.\n";
438  break;
439  case vpMouseButton::none:
440  break;
441  }
443  std::cout << " Pointer poisition : " << ip << std::endl;
444  std::cout << "A click to exit...\n";
446  }
447  }
448  delete display;
449  }
450  catch (...) {
451  vpERROR_TRACE("Error while displaying the image");
452  return EXIT_FAILURE;
453  }
454 }
455 
456 #else
457 int main() { vpERROR_TRACE("You do not have display functionalities..."); }
458 
459 #endif
Display for windows using Direct3D 3rd party. Thus to enable this class Direct3D should be installed....
Definition: vpDisplayD3D.h:106
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:130
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:133
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Class that defines generic functionalities for display.
Definition: vpDisplay.h:178
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
static bool getClickUp(const vpImage< unsigned char > &I, vpImagePoint &ip, vpMouseButton::vpMouseButtonType &button, bool blocking=true)
static void flush(const vpImage< unsigned char > &I)
static bool getPointerPosition(const vpImage< unsigned char > &I, vpImagePoint &ip)
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:147
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:82
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1053
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:1427
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:70