Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
testDisplays.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 image display.
32  */
33 
40 #include <visp3/core/vpConfig.h>
41 #include <visp3/core/vpDebug.h>
42 
43 #include <iostream>
44 #include <stdlib.h>
45 #include <string>
46 
47 #if defined(VISP_HAVE_GTK) || defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_D3D9) || defined(VISP_HAVE_OPENCV)
48 
49 #include <visp3/core/vpImage.h>
50 #include <visp3/core/vpIoTools.h>
51 #include <visp3/core/vpRect.h>
52 #include <visp3/io/vpImageIo.h>
53 #include <visp3/io/vpParseArgv.h>
54 
55 #include <visp3/gui/vpDisplayD3D.h>
56 #include <visp3/gui/vpDisplayGDI.h>
57 #include <visp3/gui/vpDisplayGTK.h>
58 #include <visp3/gui/vpDisplayOpenCV.h>
59 #include <visp3/gui/vpDisplayX.h>
60 
61 // List of allowed command line options
62 #define GETOPTARGS "hldc"
63 
64 #ifdef ENABLE_VISP_NAMESPACE
65 using namespace VISP_NAMESPACE_NAME;
66 #endif
67 
74 static void usage(const char *name, const char *badparam)
75 {
76  fprintf(stdout, "\n\
77 Test video devices or display.\n\
78 \n\
79 SYNOPSIS\n\
80  %s [-l] [-c] [-d] [-h]\n\
81 ",
82 name);
83 
84  fprintf(stdout, "\n\
85 OPTIONS: Default\n\
86  -c\n\
87  Disable the mouse click. Useful to automate the \n\
88  execution of this program without human intervention.\n\
89 \n\
90  -d \n\
91  Turn off the display.\n\
92 \n\
93  -l\n\
94  Print the list of video-devices available and exit.\n\
95 \n\
96  -h\n\
97  Print the help.\n\n");
98 
99  if (badparam)
100  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
101 }
102 
113 static bool getOptions(int argc, const char **argv, bool &list, bool &click_allowed, bool &display)
114 {
115  const char *optarg_;
116  int c;
117  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
118 
119  switch (c) {
120  case 'l':
121  list = true;
122  break;
123  case 'h':
124  usage(argv[0], nullptr);
125  return false;
126  break;
127  case 'c':
128  click_allowed = false;
129  break;
130  case 'd':
131  display = false;
132  break;
133 
134  default:
135  usage(argv[0], optarg_);
136  return false;
137  break;
138  }
139  }
140 
141  if ((c == 1) || (c == -1)) {
142  // standalone param or error
143  usage(argv[0], nullptr);
144  std::cerr << "ERROR: " << std::endl;
145  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
146  return false;
147  }
148 
149  return true;
150 }
151 
152 template <typename Type> static void draw(vpImage<Type> &I)
153 {
154  vpImagePoint iP1, iP2;
155  unsigned int w, h;
156 
157  iP1.set_i(20);
158  iP1.set_j(10);
159  iP2.set_i(20);
160  iP2.set_j(30);
161  vpDisplay::displayArrow(I, iP1, iP2, vpColor::green, 4, 2, 3);
162 
163  iP1.set_i(20);
164  iP1.set_j(60);
165  vpDisplay::displayText(I, iP1, "Test...", vpColor::black);
166 
167  iP1.set_i(80);
168  iP1.set_j(220);
169  iP2.set_i(80);
170  iP2.set_j(480);
171  vpDisplay::displayCircle(I, iP1, 30, vpColor::red, false, 3);
172  vpDisplay::displayCircle(I, iP2, 30, vpColor::red, true, 3);
173 
174  iP1.set_i(20);
175  iP1.set_j(220);
176  vpDisplay::displayCross(I, iP1, 5, vpColor::blue, 1);
177 
178  iP1.set_i(140);
179  iP1.set_j(10);
180  iP2.set_i(140);
181  iP2.set_j(50);
182  vpDisplay::displayDotLine(I, iP1, iP2, vpColor::blue, 3);
183 
184  iP1.set_i(120);
185  iP1.set_j(180);
186  iP2.set_i(160);
187  iP2.set_j(250);
188  vpDisplay::displayDotLine(I, iP1, iP2, vpColor::blue, 3);
189 
190  iP1.set_i(160);
191  iP1.set_j(280);
192  iP2.set_i(120);
193  iP2.set_j(340);
194  vpDisplay::displayDotLine(I, iP1, iP2, vpColor::blue, 3);
195 
196  iP1.set_i(220);
197  iP1.set_j(400);
198  iP2.set_i(120);
199  iP2.set_j(400);
200  vpDisplay::displayDotLine(I, iP1, iP2, vpColor::cyan, 3);
201 
202  iP1.set_i(220);
203  iP1.set_j(480);
204  iP2.set_i(120);
205  iP2.set_j(450);
206  vpDisplay::displayDotLine(I, iP1, iP2, vpColor::green, 3);
207 
208  vpHomogeneousMatrix cMo(vpTranslationVector(0.15, -0.07, 0.37), vpRotationMatrix(vpRxyzVector(0.1, -0.4, 0.41)));
209  vpCameraParameters cam(600, 600, 320, 240);
210  vpDisplay::displayFrame(I, cMo, cam, 0.05, vpColor::none, 3);
211 
212  iP1.set_i(140);
213  iP1.set_j(80);
214  iP2.set_i(140);
215  iP2.set_j(150);
216  vpDisplay::displayLine(I, iP1, iP2, vpColor::orange, 3);
217 
218  iP1.set_i(140);
219  iP1.set_j(400);
221 
222  iP1.set_i(350);
223  iP1.set_j(20);
224  w = 60;
225  h = 50;
226  vpDisplay::displayRectangle(I, iP1, w, h, vpColor::red, false, 3);
227 
228  iP1.set_i(350);
229  iP1.set_j(110);
230  vpDisplay::displayRectangle(I, iP1, w, h, vpColor::red, true, 3);
231 
232  iP1.set_i(350);
233  iP1.set_j(200);
234  iP2.set_i(400);
235  iP2.set_j(260);
236  vpDisplay::displayRectangle(I, iP1, iP2, vpColor::orange, false, 3);
237 
238  iP1.set_i(350);
239  iP1.set_j(290);
240  iP2.set_i(400);
241  iP2.set_j(350);
242  vpRect rectangle(iP1, iP2);
243  vpDisplay::displayRectangle(I, rectangle, vpColor::yellow, false, 3);
244 
245  iP1.set_i(380);
246  iP1.set_j(400);
247  vpDisplay::displayRectangle(I, iP1, 45, w, h, vpColor::green, 3);
248 
249  std::vector<vpImagePoint> vip;
250  vip.push_back(vpImagePoint(250, 500));
251  vip.push_back(vpImagePoint(350, 600));
252  vip.push_back(vpImagePoint(450, 500));
253  vip.push_back(vpImagePoint(350, 400));
255 
256  vip.clear();
257  vip.push_back(vpImagePoint(300, 500));
258  vip.push_back(vpImagePoint(350, 550));
259  vip.push_back(vpImagePoint(400, 500));
260  vip.push_back(vpImagePoint(350, 450));
261  vpDisplay::displayPolygon(I, vip, vpColor::cyan, 3, false);
262 
263  vip.clear();
264  vip.push_back(vpImagePoint(250, 300));
265  vip.push_back(vpImagePoint(350, 400));
266  vip.push_back(vpImagePoint(450, 300));
267  vip.push_back(vpImagePoint(350, 200));
268  vpPolygon polygon(vip);
270 
271  vip.clear();
272  vip.push_back(vpImagePoint(300, 300));
273  vip.push_back(vpImagePoint(350, 350));
274  vip.push_back(vpImagePoint(400, 300));
275  vip.push_back(vpImagePoint(350, 250));
276  polygon.buildFrom(vip);
277  vpDisplay::displayPolygon(I, polygon, vpColor::cyan, 3, false);
278 }
279 
280 template <typename Type>
281 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, Type(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, Type(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, Type(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, Type(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, Type(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:217
static const vpColor cyan
Definition: vpColor.h:226
static const vpColor none
Definition: vpColor.h:229
static const vpColor orange
Definition: vpColor.h:227
static const vpColor blue
Definition: vpColor.h:223
static const vpColor yellow
Definition: vpColor.h:225
static const vpColor black
Definition: vpColor.h:211
static const vpColor green
Definition: vpColor.h:220
Display for windows using Direct3D 3rd party. Thus to enable this class Direct3D should be installed....
Definition: vpDisplayD3D.h:106
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:130
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:133
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
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:60
const char * getMessage() const
Definition: vpException.cpp:65
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:309
void set_i(double ii)
Definition: vpImagePoint.h:298
Definition of the vpImage class member functions.
Definition: vpImage.h:131
void init(unsigned int height, unsigned int width)
Set the size of the image.
Definition: vpImage.h:385
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:70
Defines a generic 2D polygon.
Definition: vpPolygon.h:103
Defines a rectangle in the plane.
Definition: vpRect.h:79
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:183
Class that consider the case of a translation vector.