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