Visual Servoing Platform version 3.6.0
Loading...
Searching...
No Matches
testClick.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 * Test for mouse click manipulations.
33 *
34*****************************************************************************/
35
36#include <visp3/core/vpConfig.h>
37#include <visp3/core/vpDebug.h>
38
39#include <iostream>
40#include <stdlib.h>
41#include <string>
42
43#if (defined(VISP_HAVE_GTK) || defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_D3D9) || \
44 defined(VISP_HAVE_OPENCV))
45
46#include <visp3/core/vpImage.h>
47#include <visp3/core/vpIoTools.h>
48#include <visp3/io/vpImageIo.h>
49#include <visp3/io/vpParseArgv.h>
50
51#include <visp3/gui/vpDisplayD3D.h>
52#include <visp3/gui/vpDisplayGDI.h>
53#include <visp3/gui/vpDisplayGTK.h>
54#include <visp3/gui/vpDisplayOpenCV.h>
55#include <visp3/gui/vpDisplayX.h>
56
64// List of allowed command line options
65#define GETOPTARGS "i:hlt:dc"
66
67typedef enum { vpX11, vpGTK, vpGDI, vpD3D, vpCV } vpDisplayType;
68
69void usage(const char *name, const char *badparam, std::string ipath, vpDisplayType &dtype);
70bool getOptions(int argc, const char **argv, std::string &ipath, vpDisplayType &dtype, bool &list, bool &click_allowed,
71 bool &display);
72
83void usage(const char *name, const char *badparam, std::string ipath, vpDisplayType &dtype)
84{
85 fprintf(stdout, "\n\
86Test click functionalities in video devices or display.\n\
87\n\
88SYNOPSIS\n\
89 %s [-i <input image path>] \n\
90 [-t <type of video device>] [-l] [-c] [-d] [-h]\n\
91",
92 name);
93
94 std::string display;
95 switch (dtype) {
96 case vpX11:
97 display = "X11";
98 break;
99 case vpGTK:
100 display = "GTK";
101 break;
102 case vpGDI:
103 display = "GDI";
104 break;
105 case vpD3D:
106 display = "D3D";
107 break;
108 case vpCV:
109 display = "CV";
110 break;
111 }
112
113 fprintf(stdout, "\n\
114OPTIONS: Default\n\
115 -i <input image path> %s\n\
116 Set image input path.\n\
117 From this path read \"Klimt/Klimt.pgm\"\n\
118 and \"Klimt/Klimt.ppm\" images.\n\
119 Setting the VISP_INPUT_IMAGE_PATH environment\n\
120 variable produces the same behaviour than using\n\
121 this option.\n\
122\n\
123 -t <type of video device> \"%s\"\n\
124 String specifying the video device to use.\n\
125 Possible values:\n\
126 \"X11\": only on UNIX platforms,\n\
127 \"GTK\": on all plaforms,\n\
128 \"GDI\": only on Windows platform (Graphics Device Interface),\n\
129 \"D3D\": only on Windows platform (Direct3D).\n\
130 \"CV\" : (OpenCV).\n\
131\n\
132 -l\n\
133 Print the list of video-devices available and exit.\n\
134\n\
135 -c\n\
136 Disable the mouse click. Useful to automate the \n\
137 execution of this program without human intervention.\n\
138\n\
139 -d \n\
140 Turn off the display.\n\
141\n\
142 -h\n\
143 Print the help.\n\n",
144 ipath.c_str(), display.c_str());
145
146 if (badparam)
147 fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
148}
149
168bool getOptions(int argc, const char **argv, std::string &ipath, vpDisplayType &dtype, bool &list, bool &click_allowed,
169 bool &display)
170{
171 const char *optarg_;
172 int c;
173 std::string sDisplayType;
174 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
175
176 switch (c) {
177 case 'i':
178 ipath = optarg_;
179 break;
180 case 'l':
181 list = true;
182 break;
183 case 't':
184 sDisplayType = optarg_;
185 // Parse the display type option
186 if (sDisplayType.compare("X11") == 0) {
187 dtype = vpX11;
188 } else if (sDisplayType.compare("GTK") == 0) {
189 dtype = vpGTK;
190 } else if (sDisplayType.compare("GDI") == 0) {
191 dtype = vpGDI;
192 } else if (sDisplayType.compare("D3D") == 0) {
193 dtype = vpD3D;
194 } else if (sDisplayType.compare("CV") == 0) {
195 dtype = vpCV;
196 }
197
198 break;
199 case 'h':
200 usage(argv[0], NULL, ipath, dtype);
201 return false;
202 break;
203 case 'c':
204 click_allowed = false;
205 break;
206 case 'd':
207 display = false;
208 break;
209
210 default:
211 usage(argv[0], optarg_, ipath, dtype);
212 return false;
213 break;
214 }
215 }
216
217 if ((c == 1) || (c == -1)) {
218 // standalone param or error
219 usage(argv[0], NULL, ipath, dtype);
220 std::cerr << "ERROR: " << std::endl;
221 std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
222 return false;
223 }
224
225 return true;
226}
227
228int main(int argc, const char **argv)
229{
230 try {
231 std::string env_ipath;
232 std::string opt_ipath;
233 bool opt_list = false; // To print the list of video devices
234 vpDisplayType opt_dtype; // Type of display to use
235 std::string ipath;
236 std::string filename;
237 bool opt_click_allowed = true;
238 bool opt_display = true;
239
240// Default display is one available
241#if defined(VISP_HAVE_GTK)
242 opt_dtype = vpGTK;
243#elif defined(VISP_HAVE_X11)
244 opt_dtype = vpX11;
245#elif defined(VISP_HAVE_GDI)
246 opt_dtype = vpGDI;
247#elif defined(VISP_HAVE_D3D9)
248 opt_dtype = vpD3D;
249#elif defined VISP_HAVE_OPENCV
250 opt_dtype = vpCV;
251#endif
252
253 // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
254 // environment variable value
256
257 // Set the default input path
258 if (!env_ipath.empty())
259 ipath = env_ipath;
260
261 // Read the command line options
262 if (getOptions(argc, argv, opt_ipath, opt_dtype, opt_list, opt_click_allowed, opt_display) == false) {
263 return EXIT_FAILURE;
264 }
265
266 // Print the list of video-devices available
267 if (opt_list) {
268 unsigned nbDevices = 0;
269 std::cout << "List of video-devices available: \n";
270#if defined(VISP_HAVE_GTK)
271 std::cout << " GTK (use \"-t GTK\" option to use it)\n";
272 nbDevices++;
273#endif
274#if defined(VISP_HAVE_X11)
275 std::cout << " X11 (use \"-t X11\" option to use it)\n";
276 nbDevices++;
277#endif
278#if defined(VISP_HAVE_GDI)
279
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 EXIT_FAILURE;
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 return EXIT_FAILURE;
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 functionalities..." << 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 EXIT_FAILURE;
343#endif
344 break;
345 case vpGTK:
346 std::cout << "Requested GTK display functionalities..." << 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 EXIT_FAILURE;
353#endif
354 break;
355 case vpGDI:
356 std::cout << "Requested GDI display functionalities..." << std::endl;
357#if defined(VISP_HAVE_GDI)
358
359 display = new vpDisplayGDI;
360#else
361 std::cout << " Sorry, GDI video device is not available.\n";
362 std::cout << "Use \"" << argv[0] << " -l\" to print the list of available devices.\n";
363 return EXIT_FAILURE;
364#endif
365 break;
366 case vpD3D:
367 std::cout << "Requested D3D display functionalities..." << std::endl;
368#if defined(VISP_HAVE_D3D9)
369 display = new vpDisplayD3D;
370#else
371 std::cout << " Sorry, D3D video device is not available.\n";
372 std::cout << "Use \"" << argv[0] << " -l\" to print the list of available devices.\n";
373 return EXIT_FAILURE;
374#endif
375 break;
376 case vpCV:
377 std::cout << "Requested OpenCV display functionalities..." << std::endl;
378#if defined(HAVE_OPENCV_HIGHGUI)
379 display = new vpDisplayOpenCV;
380#else
381 std::cout << " Sorry, OpenCV video device is not available.\n";
382 std::cout << "Use \"" << argv[0] << " -l\" to print the list of available devices.\n";
383 return EXIT_FAILURE;
384#endif
385 break;
386 }
387
388 if (opt_display) {
389
390 // We open a window using either X11 or GTK or GDI.
391 // Its size is automatically defined by the image (I) size
392 display->init(I, 100, 100, "Display...");
393
394 // Display the image
395 // The image class has a member that specify a pointer toward
396 // the display that has been initialized in the display declaration
397 // therefore is is no longer necessary to make a reference to the
398 // display variable.
400 // Flush the display
402 if (opt_click_allowed) {
403 std::cout << "Click on a pixel to get his coordinates...\n";
404 vpImagePoint ip;
406 vpDisplay::getClick(I, ip, button);
407 std::cout << " You click down on pixel (" << ip << ") ";
408 switch (button) {
410 std::cout << "with left button.\n";
411 break;
413 std::cout << "with middle button.\n";
414 break;
416 std::cout << "with right button.\n";
417 break;
419 break;
420 }
421 vpDisplay::getClickUp(I, ip, button);
422 std::cout << " You click up on pixel (" << ip << ") ";
423 switch (button) {
425 std::cout << "with left button.\n";
426 break;
428 std::cout << "with middle button.\n";
429 break;
431 std::cout << "with right button.\n";
432 break;
434 break;
435 }
437 std::cout << " Pointer poisition : " << ip << std::endl;
438 std::cout << "A click to exit...\n";
440 }
441 }
442 delete display;
443 } catch (...) {
444 vpERROR_TRACE("Error while displaying the image");
445 return EXIT_FAILURE;
446 }
447}
448
449#else
450int main() { vpERROR_TRACE("You do not have display functionalities..."); }
451
452#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...
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: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 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)
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 vpCTRACE
Definition vpDebug.h:333
#define vpERROR_TRACE
Definition vpDebug.h:388