Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
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
61
62// List of allowed command line options
63#define GETOPTARGS "i:hlt:dc"
64
65#ifdef ENABLE_VISP_NAMESPACE
66using namespace VISP_NAMESPACE_NAME;
67#endif
68
69typedef enum { vpX11, vpGTK, vpGDI, vpD3D, vpCV } vpDisplayType;
70
71void usage(const char *name, const char *badparam, const std::string &ipath, vpDisplayType &dtype);
72bool getOptions(int argc, const char **argv, std::string &ipath, vpDisplayType &dtype, bool &list, bool &click_allowed,
73 bool &display);
74
85void usage(const char *name, const char *badparam, const std::string &ipath, vpDisplayType &dtype)
86{
87 fprintf(stdout, "\n\
88Test click functionalities in video devices or display.\n\
89\n\
90SYNOPSIS\n\
91 %s [-i <input image path>] \n\
92 [-t <type of video device>] [-l] [-c] [-d] [-h]\n\
93",
94name);
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\
116OPTIONS: 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
170bool 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 case 'c':
209 click_allowed = false;
210 break;
211 case 'd':
212 display = false;
213 break;
214
215 default:
216 usage(argv[0], optarg_, ipath, dtype);
217 return false;
218 }
219 }
220
221 if ((c == 1) || (c == -1)) {
222 // standalone param or error
223 usage(argv[0], nullptr, ipath, dtype);
224 std::cerr << "ERROR: " << std::endl;
225 std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
226 return false;
227 }
228
229 return true;
230}
231
232int main(int argc, const char **argv)
233{
234 try {
235 std::string env_ipath;
236 std::string opt_ipath;
237 bool opt_list = false; // To print the list of video devices
238 vpDisplayType opt_dtype; // Type of display to use
239 std::string ipath;
240 std::string filename;
241 bool opt_click_allowed = true;
242 bool opt_display = true;
243
244// Default display is one available
245#if defined(VISP_HAVE_GTK)
246 opt_dtype = vpGTK;
247#elif defined(VISP_HAVE_X11)
248 opt_dtype = vpX11;
249#elif defined(VISP_HAVE_GDI)
250 opt_dtype = vpGDI;
251#elif defined(VISP_HAVE_D3D9)
252 opt_dtype = vpD3D;
253#elif defined VISP_HAVE_OPENCV
254 opt_dtype = vpCV;
255#endif
256
257 // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
258 // environment variable value
260
261 // Set the default input path
262 if (!env_ipath.empty())
263 ipath = env_ipath;
264
265 // Read the command line options
266 if (getOptions(argc, argv, opt_ipath, opt_dtype, opt_list, opt_click_allowed, opt_display) == false) {
267 return EXIT_FAILURE;
268 }
269
270 // Print the list of video-devices available
271 if (opt_list) {
272 unsigned nbDevices = 0;
273 std::cout << "List of video-devices available: \n";
274#if defined(VISP_HAVE_GTK)
275 std::cout << " GTK (use \"-t GTK\" option to use it)\n";
276 nbDevices++;
277#endif
278#if defined(VISP_HAVE_X11)
279 std::cout << " X11 (use \"-t X11\" option to use it)\n";
280 nbDevices++;
281#endif
282#if defined(VISP_HAVE_GDI)
283
284 std::cout << " GDI (use \"-t GDI\" option to use it)\n";
285 nbDevices++;
286#endif
287#if defined(VISP_HAVE_D3D9)
288 std::cout << " D3D (use \"-t D3D\" option to use it)\n";
289 nbDevices++;
290#endif
291#if defined VISP_HAVE_OPENCV
292 std::cout << " CV (use \"-t CV\" option to use it)\n";
293 nbDevices++;
294#endif
295 if (!nbDevices) {
296 std::cout << " No display is available\n";
297 }
298 return EXIT_FAILURE;
299 }
300
301 // Get the option values
302 if (!opt_ipath.empty())
303 ipath = opt_ipath;
304
305 // Compare ipath and env_ipath. If they differ, we take into account
306 // the input path coming from the command line option
307 if (!opt_ipath.empty() && !env_ipath.empty()) {
308 if (ipath != env_ipath) {
309 std::cout << std::endl << "WARNING: " << std::endl;
310 std::cout << " Since -i <visp image path=" << ipath << "> "
311 << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
312 << " we skip the environment variable." << std::endl;
313 }
314 }
315
316 // Test if an input path is set
317 if (opt_ipath.empty() && env_ipath.empty()) {
318 usage(argv[0], nullptr, ipath, opt_dtype);
319 std::cerr << std::endl << "ERROR:" << std::endl;
320 std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
321 << " environment variable to specify the location of the " << std::endl
322 << " image path where test images are located." << std::endl
323 << std::endl;
324 return EXIT_FAILURE;
325 }
326
327 // Create a grey level image
329
330 // Load a grey image from the disk
331 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.pgm");
332 vpCTRACE << "Load " << filename << std::endl;
333 vpImageIo::read(I, filename);
334
335 // Create a display for the image
336 vpDisplay *display = nullptr;
337
338 switch (opt_dtype) {
339 case vpX11:
340 std::cout << "Requested X11 display functionalities..." << std::endl;
341#if defined(VISP_HAVE_X11)
342 display = new vpDisplayX;
343#else
344 std::cout << " Sorry, X11 video device is not available.\n";
345 std::cout << "Use \"" << argv[0] << " -l\" to print the list of available devices.\n";
346 return EXIT_FAILURE;
347#endif
348 break;
349 case vpGTK:
350 std::cout << "Requested GTK display functionalities..." << std::endl;
351#if defined(VISP_HAVE_GTK)
352 display = new vpDisplayGTK;
353#else
354 std::cout << " Sorry, GTK video device is not available.\n";
355 std::cout << "Use \"" << argv[0] << " -l\" to print the list of available devices.\n";
356 return EXIT_FAILURE;
357#endif
358 break;
359 case vpGDI:
360 std::cout << "Requested GDI display functionalities..." << std::endl;
361#if defined(VISP_HAVE_GDI)
362
363 display = new vpDisplayGDI;
364#else
365 std::cout << " Sorry, GDI video device is not available.\n";
366 std::cout << "Use \"" << argv[0] << " -l\" to print the list of available devices.\n";
367 return EXIT_FAILURE;
368#endif
369 break;
370 case vpD3D:
371 std::cout << "Requested D3D display functionalities..." << std::endl;
372#if defined(VISP_HAVE_D3D9)
373 display = new vpDisplayD3D;
374#else
375 std::cout << " Sorry, D3D video device is not available.\n";
376 std::cout << "Use \"" << argv[0] << " -l\" to print the list of available devices.\n";
377 return EXIT_FAILURE;
378#endif
379 break;
380 case vpCV:
381 std::cout << "Requested OpenCV display functionalities..." << std::endl;
382#if defined(HAVE_OPENCV_HIGHGUI)
383 display = new vpDisplayOpenCV;
384#else
385 std::cout << " Sorry, OpenCV video device is not available.\n";
386 std::cout << "Use \"" << argv[0] << " -l\" to print the list of available devices.\n";
387 return EXIT_FAILURE;
388#endif
389 break;
390 }
391
392 if (opt_display) {
393
394 // We open a window using either X11 or GTK or GDI.
395 // Its size is automatically defined by the image (I) size
396 display->init(I, 100, 100, "Display...");
397
398 // Display the image
399 // The image class has a member that specify a pointer toward
400 // the display that has been initialized in the display declaration
401 // therefore is is no longer necessary to make a reference to the
402 // display variable.
404 // Flush the display
406 if (opt_click_allowed) {
407 std::cout << "Click on a pixel to get his coordinates...\n";
408 vpImagePoint ip;
410 vpDisplay::getClick(I, ip, button);
411 std::cout << " You click down on pixel (" << ip << ") ";
412 switch (button) {
414 std::cout << "with left button.\n";
415 break;
417 std::cout << "with middle button.\n";
418 break;
420 std::cout << "with right button.\n";
421 break;
423 break;
424 }
425 vpDisplay::getClickUp(I, ip, button);
426 std::cout << " You click up on pixel (" << ip << ") ";
427 switch (button) {
429 std::cout << "with left button.\n";
430 break;
432 std::cout << "with middle button.\n";
433 break;
435 std::cout << "with right button.\n";
436 break;
438 break;
439 }
441 std::cout << " Pointer position: " << ip << std::endl;
442 std::cout << "A click to exit...\n";
444 }
445 }
446 delete display;
447 }
448 catch (...) {
449 vpERROR_TRACE("Error while displaying the image");
450 return EXIT_FAILURE;
451 }
452}
453
454#else
455int main() { vpERROR_TRACE("You do not have display functionalities..."); }
456
457#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: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 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:131
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:362
#define vpERROR_TRACE
Definition vpDebug.h:423