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