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