Visual Servoing Platform  version 3.3.0 under development (2020-02-17)
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 ", name);
84 
85  fprintf(stdout, "\n\
86 OPTIONS: Default\n\
87  -c\n\
88  Disable the mouse click. Useful to automaze the \n\
89  execution of this program without humain 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], NULL);
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], NULL);
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> polygon;
251  polygon.push_back(vpImagePoint(250, 500));
252  polygon.push_back(vpImagePoint(350, 600));
253  polygon.push_back(vpImagePoint(450, 500));
254  polygon.push_back(vpImagePoint(350, 400));
256 
257  polygon.clear();
258  polygon.push_back(vpImagePoint(300, 500));
259  polygon.push_back(vpImagePoint(350, 550));
260  polygon.push_back(vpImagePoint(400, 500));
261  polygon.push_back(vpImagePoint(350, 450));
262  vpDisplay::displayPolygon(I, polygon, vpColor::cyan, 3, false);
263 }
264 
265 template <typename Type> static void runTest(bool opt_display, bool opt_click_allowed)
266 {
267  vpImage<Type> Ix;
268  vpImage<Type> Igtk;
269  vpImage<Type> Icv;
270  vpImage<Type> Igdi;
271  vpImage<Type> Id3d;
272 
273 #if defined VISP_HAVE_X11
274  vpDisplayX *displayX = new vpDisplayX;
275  Ix.init(480, 640, 255);
276  if (opt_display) {
277  displayX->init(Ix, 100, 100, "Display X11");
278  vpDisplay::display(Ix);
279  draw(Ix);
280  vpDisplay::flush(Ix);
281  if (opt_click_allowed)
283  }
284 #endif
285 
286 #if defined(VISP_HAVE_OPENCV)
287  vpDisplayOpenCV *displayCv = new vpDisplayOpenCV;
288  Icv.init(480, 640, 255);
289  if (opt_display) {
290  displayCv->init(Icv, 100, 100, "Display OpenCV");
291  vpDisplay::display(Icv);
292  draw(Icv);
293  vpDisplay::flush(Icv);
294  if (opt_click_allowed)
295  vpDisplay::getClick(Icv);
296  }
297 #endif
298 
299 #if defined VISP_HAVE_GTK
300  vpDisplayGTK *displayGtk = new vpDisplayGTK;
301  Igtk.init(480, 640, 255);
302  if (opt_display) {
303  displayGtk->init(Igtk, 100, 100, "Display GTK");
304  vpDisplay::display(Igtk);
305  draw(Igtk);
306  vpDisplay::flush(Igtk);
307  if (opt_click_allowed)
308  vpDisplay::getClick(Igtk);
309  }
310 #endif
311 
312 #if defined VISP_HAVE_GDI
313  vpDisplayGDI *displayGdi = new vpDisplayGDI;
314  Igdi.init(480, 640, 255);
315  if (opt_display) {
316  displayGdi->init(Igdi, 100, 100, "Display GDI");
317  vpDisplay::display(Igdi);
318  draw(Igdi);
319  vpDisplay::flush(Igdi);
320  if (opt_click_allowed)
321  vpDisplay::getClick(Igdi);
322  }
323 #endif
324 
325 #if defined VISP_HAVE_D3D9
326  vpDisplayD3D *displayD3d = new vpDisplayD3D;
327  Id3d.init(480, 640, 255);
328  if (opt_display) {
329  displayD3d->init(Id3d, 100, 100, "Display Direct 3D");
330  vpDisplay::display(Id3d);
331  draw(Id3d);
332  vpDisplay::flush(Id3d);
333  if (opt_click_allowed)
334  vpDisplay::getClick(Id3d);
335  }
336 #endif
337 
338 #if defined VISP_HAVE_X11
339  delete displayX;
340 #endif
341 
342 #if defined VISP_HAVE_GTK
343  delete displayGtk;
344 #endif
345 
346 #if defined(VISP_HAVE_OPENCV)
347  delete displayCv;
348 #endif
349 
350 #if defined VISP_HAVE_GDI
351  delete displayGdi;
352 #endif
353 
354 #if defined VISP_HAVE_D3D9
355  delete displayD3d;
356 #endif
357 }
358 
359 int main(int argc, const char **argv)
360 {
361  try {
362  bool opt_list = false; // To print the list of video devices
363  bool opt_click_allowed = true;
364  bool opt_display = true;
365 
366  // Read the command line options
367  if (getOptions(argc, argv, opt_list, opt_click_allowed, opt_display) == false) {
368  exit(-1);
369  }
370 
371  // Print the list of video-devices available
372  if (opt_list) {
373  unsigned nbDevices = 0;
374  std::cout << "List of video-devices available: \n";
375 #if defined VISP_HAVE_GTK
376  std::cout << " GTK\n";
377  nbDevices++;
378 #endif
379 #if defined VISP_HAVE_X11
380  std::cout << " X11\n";
381  nbDevices++;
382 #endif
383 #if defined VISP_HAVE_GDI
384  std::cout << " GDI\n";
385  nbDevices++;
386 #endif
387 #if defined VISP_HAVE_D3D9
388  std::cout << " D3D\n";
389  nbDevices++;
390 #endif
391 #if defined VISP_HAVE_OPENCV
392  std::cout << " OpenCV\n";
393  nbDevices++;
394 #endif
395  if (!nbDevices) {
396  std::cout << " No display is available\n";
397  }
398  return (0);
399  }
400 
401  // Create a color image for each display.
402  runTest<vpRGBa>(opt_display, opt_click_allowed);
403 
404  // Create a grayscale image for each display.
405  runTest<unsigned char>(opt_display, opt_click_allowed);
406 
407  } catch (const vpException &e) {
408  std::cout << "Catch an exception: " << e.getMessage() << std::endl;
409  return (-1);
410  }
411 }
412 #else
413 int main()
414 {
415  std::cout << "You do not have display functionalities..." << std::endl;
416  return 0;
417 }
418 #endif
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="")
Definition: vpDisplayX.cpp:252
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
void init(unsigned int height, unsigned int width)
Set the size of the image.
Definition: vpImage.h:665
Implementation of an homogeneous matrix and operations on such kind of matrices.
static const vpColor black
Definition: vpColor.h:173
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
vpDisplayGDI()
Basic constructor.
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:150
static const vpColor none
Definition: vpColor.h:191
error that can be emited by ViSP classes.
Definition: vpException.h:71
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="")
static void displayPoint(const vpImage< unsigned char > &I, const vpImagePoint &ip, const vpColor &color, unsigned int thickness=1)
static const vpColor green
Definition: vpColor.h:182
static void flush(const vpImage< unsigned char > &I)
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
static const vpColor red
Definition: vpColor.h:179
static const vpColor orange
Definition: vpColor.h:189
Implementation of a rotation matrix and operations on such kind of matrices.
Display for windows using Direct3D 3rd party. Thus to enable this class Direct3D should be installed...
Definition: vpDisplayD3D.h:106
static void displayPolygon(const vpImage< unsigned char > &I, const std::vector< vpImagePoint > &vip, const vpColor &color, unsigned int thickness=1, bool closed=true)
static const vpColor cyan
Definition: vpColor.h:188
void set_i(double ii)
Definition: vpImagePoint.h:167
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 display(const vpImage< unsigned char > &I)
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Generic class defining intrinsic camera parameters.
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:137
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)
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="")
const char * getMessage(void) const
Definition: vpException.cpp:90
static void displayCircle(const vpImage< unsigned char > &I, const vpImagePoint &center, unsigned int radius, const vpColor &color, bool fill=false, unsigned int thickness=1)
void set_j(double jj)
Definition: vpImagePoint.h:178
static void displayCross(const vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned int size, 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))
Implementation of a rotation vector as Euler angle minimal representation.
Definition: vpRxyzVector.h:183
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="")
Defines a rectangle in the plane.
Definition: vpRect.h:78
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:88
static const vpColor yellow
Definition: vpColor.h:187
static void displayLine(const vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1, bool segment=true)
Definition of the vpImage class member functions.
Definition: vpImage.h:124
Class that consider the case of a translation vector.
static void displayDotLine(const vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1)
static const vpColor blue
Definition: vpColor.h:185