Visual Servoing Platform  version 3.6.1 under development (2024-04-16)
testDisplays.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 image display.
33  *
34  * Authors:
35  * Anthony Saunier
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) || defined(VISP_HAVE_OPENCV)
47 
48 #include <visp3/core/vpImage.h>
49 #include <visp3/core/vpIoTools.h>
50 #include <visp3/core/vpRect.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 
66 // List of allowed command line options
67 #define GETOPTARGS "hldc"
68 
75 static void usage(const char *name, const char *badparam)
76 {
77  fprintf(stdout, "\n\
78 Test video devices or display.\n\
79 \n\
80 SYNOPSIS\n\
81  %s [-l] [-c] [-d] [-h]\n\
82 ",
83 name);
84 
85  fprintf(stdout, "\n\
86 OPTIONS: Default\n\
87  -c\n\
88  Disable the mouse click. Useful to automate the \n\
89  execution of this program without human intervention.\n\
90 \n\
91  -d \n\
92  Turn off the display.\n\
93 \n\
94  -l\n\
95  Print the list of video-devices available and exit.\n\
96 \n\
97  -h\n\
98  Print the help.\n\n");
99 
100  if (badparam)
101  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
102 }
103 
114 static bool getOptions(int argc, const char **argv, bool &list, bool &click_allowed, bool &display)
115 {
116  const char *optarg_;
117  int c;
118  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
119 
120  switch (c) {
121  case 'l':
122  list = true;
123  break;
124  case 'h':
125  usage(argv[0], nullptr);
126  return false;
127  break;
128  case 'c':
129  click_allowed = false;
130  break;
131  case 'd':
132  display = false;
133  break;
134 
135  default:
136  usage(argv[0], optarg_);
137  return false;
138  break;
139  }
140  }
141 
142  if ((c == 1) || (c == -1)) {
143  // standalone param or error
144  usage(argv[0], nullptr);
145  std::cerr << "ERROR: " << std::endl;
146  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
147  return false;
148  }
149 
150  return true;
151 }
152 
153 template <typename Type> static void draw(vpImage<Type> &I)
154 {
155  vpImagePoint iP1, iP2;
156  unsigned int w, h;
157 
158  iP1.set_i(20);
159  iP1.set_j(10);
160  iP2.set_i(20);
161  iP2.set_j(30);
162  vpDisplay::displayArrow(I, iP1, iP2, vpColor::green, 4, 2, 3);
163 
164  iP1.set_i(20);
165  iP1.set_j(60);
166  vpDisplay::displayText(I, iP1, "Test...", vpColor::black);
167 
168  iP1.set_i(80);
169  iP1.set_j(220);
170  iP2.set_i(80);
171  iP2.set_j(480);
172  vpDisplay::displayCircle(I, iP1, 30, vpColor::red, false, 3);
173  vpDisplay::displayCircle(I, iP2, 30, vpColor::red, true, 3);
174 
175  iP1.set_i(20);
176  iP1.set_j(220);
177  vpDisplay::displayCross(I, iP1, 5, vpColor::blue, 1);
178 
179  iP1.set_i(140);
180  iP1.set_j(10);
181  iP2.set_i(140);
182  iP2.set_j(50);
183  vpDisplay::displayDotLine(I, iP1, iP2, vpColor::blue, 3);
184 
185  iP1.set_i(120);
186  iP1.set_j(180);
187  iP2.set_i(160);
188  iP2.set_j(250);
189  vpDisplay::displayDotLine(I, iP1, iP2, vpColor::blue, 3);
190 
191  iP1.set_i(160);
192  iP1.set_j(280);
193  iP2.set_i(120);
194  iP2.set_j(340);
195  vpDisplay::displayDotLine(I, iP1, iP2, vpColor::blue, 3);
196 
197  iP1.set_i(220);
198  iP1.set_j(400);
199  iP2.set_i(120);
200  iP2.set_j(400);
201  vpDisplay::displayDotLine(I, iP1, iP2, vpColor::cyan, 3);
202 
203  iP1.set_i(220);
204  iP1.set_j(480);
205  iP2.set_i(120);
206  iP2.set_j(450);
207  vpDisplay::displayDotLine(I, iP1, iP2, vpColor::green, 3);
208 
209  vpHomogeneousMatrix cMo(vpTranslationVector(0.15, -0.07, 0.37), vpRotationMatrix(vpRxyzVector(0.1, -0.4, 0.41)));
210  vpCameraParameters cam(600, 600, 320, 240);
211  vpDisplay::displayFrame(I, cMo, cam, 0.05, vpColor::none, 3);
212 
213  iP1.set_i(140);
214  iP1.set_j(80);
215  iP2.set_i(140);
216  iP2.set_j(150);
217  vpDisplay::displayLine(I, iP1, iP2, vpColor::orange, 3);
218 
219  iP1.set_i(140);
220  iP1.set_j(400);
222 
223  iP1.set_i(350);
224  iP1.set_j(20);
225  w = 60;
226  h = 50;
227  vpDisplay::displayRectangle(I, iP1, w, h, vpColor::red, false, 3);
228 
229  iP1.set_i(350);
230  iP1.set_j(110);
231  vpDisplay::displayRectangle(I, iP1, w, h, vpColor::red, true, 3);
232 
233  iP1.set_i(350);
234  iP1.set_j(200);
235  iP2.set_i(400);
236  iP2.set_j(260);
237  vpDisplay::displayRectangle(I, iP1, iP2, vpColor::orange, false, 3);
238 
239  iP1.set_i(350);
240  iP1.set_j(290);
241  iP2.set_i(400);
242  iP2.set_j(350);
243  vpRect rectangle(iP1, iP2);
244  vpDisplay::displayRectangle(I, rectangle, vpColor::yellow, false, 3);
245 
246  iP1.set_i(380);
247  iP1.set_j(400);
248  vpDisplay::displayRectangle(I, iP1, 45, w, h, vpColor::green, 3);
249 
250  std::vector<vpImagePoint> vip;
251  vip.push_back(vpImagePoint(250, 500));
252  vip.push_back(vpImagePoint(350, 600));
253  vip.push_back(vpImagePoint(450, 500));
254  vip.push_back(vpImagePoint(350, 400));
256 
257  vip.clear();
258  vip.push_back(vpImagePoint(300, 500));
259  vip.push_back(vpImagePoint(350, 550));
260  vip.push_back(vpImagePoint(400, 500));
261  vip.push_back(vpImagePoint(350, 450));
262  vpDisplay::displayPolygon(I, vip, vpColor::cyan, 3, false);
263 
264  vip.clear();
265  vip.push_back(vpImagePoint(250, 300));
266  vip.push_back(vpImagePoint(350, 400));
267  vip.push_back(vpImagePoint(450, 300));
268  vip.push_back(vpImagePoint(350, 200));
269  vpPolygon polygon(vip);
271 
272  vip.clear();
273  vip.push_back(vpImagePoint(300, 300));
274  vip.push_back(vpImagePoint(350, 350));
275  vip.push_back(vpImagePoint(400, 300));
276  vip.push_back(vpImagePoint(350, 250));
277  polygon.buildFrom(vip);
278  vpDisplay::displayPolygon(I, polygon, vpColor::cyan, 3, false);
279 }
280 
281 template <typename Type> static void runTest(bool opt_display, bool opt_click_allowed)
282 {
283  vpImage<Type> Ix;
284  vpImage<Type> Igtk;
285  vpImage<Type> Icv;
286  vpImage<Type> Igdi;
287  vpImage<Type> Id3d;
288 
289 #if defined(VISP_HAVE_X11)
290  vpDisplayX *displayX = new vpDisplayX;
291  Ix.init(480, 640, 255);
292  if (opt_display) {
293  displayX->init(Ix, 100, 100, "Display X11");
294  vpDisplay::display(Ix);
295  draw(Ix);
296  vpDisplay::flush(Ix);
297  if (opt_click_allowed)
299  }
300 #endif
301 
302 #if defined(HAVE_OPENCV_HIGHGUI)
303  vpDisplayOpenCV *displayCv = new vpDisplayOpenCV;
304  Icv.init(480, 640, 255);
305  if (opt_display) {
306  displayCv->init(Icv, 100, 100, "Display OpenCV");
307  vpDisplay::display(Icv);
308  draw(Icv);
309  vpDisplay::flush(Icv);
310  if (opt_click_allowed)
311  vpDisplay::getClick(Icv);
312  }
313 #endif
314 
315 #if defined(VISP_HAVE_GTK)
316  vpDisplayGTK *displayGtk = new vpDisplayGTK;
317  Igtk.init(480, 640, 255);
318  if (opt_display) {
319  displayGtk->init(Igtk, 100, 100, "Display GTK");
320  vpDisplay::display(Igtk);
321  draw(Igtk);
322  vpDisplay::flush(Igtk);
323  if (opt_click_allowed)
324  vpDisplay::getClick(Igtk);
325  }
326 #endif
327 
328 #if defined(VISP_HAVE_GDI)
329 
330  vpDisplayGDI *displayGdi = new vpDisplayGDI;
331  Igdi.init(480, 640, 255);
332  if (opt_display) {
333  displayGdi->init(Igdi, 100, 100, "Display GDI");
334  vpDisplay::display(Igdi);
335  draw(Igdi);
336  vpDisplay::flush(Igdi);
337  if (opt_click_allowed)
338  vpDisplay::getClick(Igdi);
339  }
340 #endif
341 
342 #if defined(VISP_HAVE_D3D9)
343  vpDisplayD3D *displayD3d = new vpDisplayD3D;
344  Id3d.init(480, 640, 255);
345  if (opt_display) {
346  displayD3d->init(Id3d, 100, 100, "Display Direct 3D");
347  vpDisplay::display(Id3d);
348  draw(Id3d);
349  vpDisplay::flush(Id3d);
350  if (opt_click_allowed)
351  vpDisplay::getClick(Id3d);
352  }
353 #endif
354 
355 #if defined(VISP_HAVE_X11)
356  delete displayX;
357 #endif
358 
359 #if defined(VISP_HAVE_GTK)
360  delete displayGtk;
361 #endif
362 
363 #if defined(HAVE_OPENCV_HIGHGUI)
364  delete displayCv;
365 #endif
366 
367 #if defined(VISP_HAVE_GDI)
368 
369  delete displayGdi;
370 #endif
371 
372 #if defined(VISP_HAVE_D3D9)
373  delete displayD3d;
374 #endif
375 }
376 
377 int main(int argc, const char **argv)
378 {
379  try {
380  bool opt_list = false; // To print the list of video devices
381  bool opt_click_allowed = true;
382  bool opt_display = true;
383 
384  // Read the command line options
385  if (getOptions(argc, argv, opt_list, opt_click_allowed, opt_display) == false) {
386  return EXIT_FAILURE;
387  }
388 
389  // Print the list of video-devices available
390  if (opt_list) {
391  unsigned nbDevices = 0;
392  std::cout << "List of video-devices available: \n";
393 #if defined(VISP_HAVE_GTK)
394  std::cout << " GTK\n";
395  nbDevices++;
396 #endif
397 #if defined(VISP_HAVE_X11)
398  std::cout << " X11\n";
399  nbDevices++;
400 #endif
401 #if defined(VISP_HAVE_GDI)
402 
403  std::cout << " GDI\n";
404  nbDevices++;
405 #endif
406 #if defined(VISP_HAVE_D3D9)
407  std::cout << " D3D\n";
408  nbDevices++;
409 #endif
410 #if defined VISP_HAVE_OPENCV
411  std::cout << " OpenCV\n";
412  nbDevices++;
413 #endif
414  if (!nbDevices) {
415  std::cout << " No display is available\n";
416  }
417  return EXIT_FAILURE;
418  }
419 
420  // Create a color image for each display.
421  runTest<vpRGBa>(opt_display, opt_click_allowed);
422 
423  // Create a grayscale image for each display.
424  runTest<unsigned char>(opt_display, opt_click_allowed);
425 
426  return EXIT_SUCCESS;
427  }
428  catch (const vpException &e) {
429  std::cout << "Catch an exception: " << e.getMessage() << std::endl;
430  return EXIT_FAILURE;
431  }
432 }
433 #else
434 int main()
435 {
436  std::cout << "You do not have display functionalities..." << std::endl;
437  return EXIT_SUCCESS;
438 }
439 #endif
Generic class defining intrinsic camera parameters.
static const vpColor red
Definition: vpColor.h:211
static const vpColor black
Definition: vpColor.h:205
static const vpColor cyan
Definition: vpColor.h:220
static const vpColor none
Definition: vpColor.h:223
static const vpColor orange
Definition: vpColor.h:221
static const vpColor blue
Definition: vpColor.h:217
static const vpColor yellow
Definition: vpColor.h:219
static const vpColor green
Definition: vpColor.h:214
Display for windows using Direct3D 3rd party. Thus to enable this class Direct3D should be installed....
Definition: vpDisplayD3D.h:101
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:128
void init(vpImage< unsigned char > &I, int win_x=-1, int win_y=-1, const std::string &win_title="") vp_override
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="") vp_override
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="") vp_override
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:128
void init(vpImage< unsigned char > &I, int win_x=-1, int win_y=-1, const std::string &win_title="") vp_override
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void displayCircle(const vpImage< unsigned char > &I, const vpImageCircle &circle, const vpColor &color, bool fill=false, unsigned int thickness=1)
static void display(const vpImage< unsigned char > &I)
static void displayLine(const vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1, bool segment=true)
static void displayFrame(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, double size, const vpColor &color=vpColor::none, unsigned int thickness=1, const vpImagePoint &offset=vpImagePoint(0, 0), const std::string &frameName="", const vpColor &textColor=vpColor::black, const vpImagePoint &textOffset=vpImagePoint(15, 15))
static void displayCross(const vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
static void flush(const vpImage< unsigned char > &I)
static void displayArrow(const vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color=vpColor::white, unsigned int w=4, unsigned int h=2, unsigned int thickness=1)
static void displayPoint(const vpImage< unsigned char > &I, const vpImagePoint &ip, const vpColor &color, unsigned int thickness=1)
static void displayDotLine(const vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1)
static void displayRectangle(const vpImage< unsigned char > &I, const vpImagePoint &topLeft, unsigned int width, unsigned int height, const vpColor &color, bool fill=false, unsigned int thickness=1)
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
static void displayPolygon(const vpImage< unsigned char > &I, const std::vector< vpImagePoint > &vip, const vpColor &color, unsigned int thickness=1, bool closed=true)
error that can be emitted by ViSP classes.
Definition: vpException.h:59
const char * getMessage() const
Definition: vpException.cpp:64
Implementation of an homogeneous matrix and operations on such kind of matrices.
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:82
void set_j(double jj)
Definition: vpImagePoint.h:304
void set_i(double ii)
Definition: vpImagePoint.h:293
Definition of the vpImage class member functions.
Definition: vpImage.h:69
void init(unsigned int height, unsigned int width)
Set the size of the image.
Definition: vpImage.h:631
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
Defines a generic 2D polygon.
Definition: vpPolygon.h:97
Defines a rectangle in the plane.
Definition: vpRect.h:76
Implementation of a rotation matrix and operations on such kind of matrices.
Implementation of a rotation vector as Euler angle minimal representation.
Definition: vpRxyzVector.h:176
Class that consider the case of a translation vector.
void display(vpImage< unsigned char > &I, const std::string &title)
Display a gray-scale image.