Visual Servoing Platform  version 3.2.0 under development (2019-01-22)
testDisplayPolygonLines.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 display polygon lines
33  *
34  *****************************************************************************/
35 
42 #include <cstdlib>
43 
44 #include <visp3/core/vpImage.h>
45 #include <visp3/core/vpImageConvert.h>
46 #include <visp3/core/vpPolygon.h>
47 #include <visp3/core/vpRect.h>
48 #include <visp3/gui/vpDisplayD3D.h>
49 #include <visp3/gui/vpDisplayGDI.h>
50 #include <visp3/gui/vpDisplayGTK.h>
51 #include <visp3/gui/vpDisplayOpenCV.h>
52 #include <visp3/gui/vpDisplayX.h>
53 #include <visp3/io/vpParseArgv.h>
54 
55 // List of allowed command line options
56 #define GETOPTARGS "cdh"
57 
58 void usage(const char *name, const char *badparam);
59 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display);
60 
68 void usage(const char *name, const char *badparam)
69 {
70  fprintf(stdout, "\n\
71 Display polygon lines.\n\
72 \n\
73 SYNOPSIS\n\
74  %s [-c] [-d] [-h]\n", name);
75 
76  fprintf(stdout, "\n\
77 OPTIONS: Default\n\
78  -c\n\
79  Disable the mouse click. Useful to automate the \n\
80  execution of this program without humain intervention.\n\
81 \n\
82  -d \n\
83  Disable the image display. This can be useful \n\
84  for automatic tests. \n\
85 \n\
86  -h\n\
87  Print the help.\n\n");
88 
89  if (badparam) {
90  fprintf(stderr, "ERROR: \n");
91  fprintf(stderr, "\nBad parameter [%s]\n", badparam);
92  }
93 }
94 
109 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display)
110 {
111  const char *optarg_;
112  int c;
113 
114  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
115  switch (c) {
116  case 'c':
117  click_allowed = false;
118  break;
119  case 'd':
120  display = false;
121  break;
122  case 'h':
123  usage(argv[0], NULL);
124  return false;
125  break;
126 
127  default:
128  usage(argv[0], optarg_);
129  return false;
130  break;
131  }
132  }
133 
134  if ((c == 1) || (c == -1)) {
135  // standalone param or error
136  usage(argv[0], NULL);
137  std::cerr << "ERROR: " << std::endl;
138  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
139  return false;
140  }
141 
142  return true;
143 }
144 
145 int main(int argc, const char **argv)
146 {
147 #ifdef VISP_HAVE_DISPLAY
148  bool opt_click_allowed = true;
149  bool opt_display = true;
150 
151  // Read the command line options
152  if (getOptions(argc, argv, opt_click_allowed, opt_display) == false) {
153  return EXIT_FAILURE;
154  }
155 
156  if (opt_display && opt_click_allowed) {
157  vpImage<unsigned char> I(480, 640, 127);
158  vpImage<vpRGBa> I_color(480, 640);
159 
160 #if defined(VISP_HAVE_X11)
161  vpDisplayX d, d2;
162 #elif defined(VISP_HAVE_GTK)
163  vpDisplayGTK d, d2;
164 #elif defined(VISP_HAVE_GDI)
165  vpDisplayGDI d, d2;
166 #elif defined(VISP_HAVE_D3D9)
167  vpDisplayD3D d, d2;
168 #elif defined(VISP_HAVE_OPENCV)
169  vpDisplayOpenCV d, d2;
170 #endif
171  d.init(I, 0, 0, "Grayscale image");
172 
174  vpDisplay::displayText(I, 20, 20, "Left click to draw a polygon, right click when it is finished.", vpColor::red);
175  vpDisplay::flush(I);
176 
177  vpPolygon polygon;
178  polygon.initClick(I);
179 
181  vpDisplay::displayText(I, 20, 20, "Shape is not closed. Click to display dashed lines.", vpColor::red);
182  vpDisplay::displayLine(I, polygon.getCorners(), false, vpColor::red, 2);
183  vpDisplay::flush(I);
185 
187  vpDisplay::displayText(I, 20, 20, "Shape is closed. Click to draw on color image.", vpColor::red);
188  vpDisplay::displayDotLine(I, polygon.getCorners(), true, vpColor::red, 2);
189  vpDisplay::flush(I);
191 
192  d2.init(I_color, I.getWidth(), 0, "Color image");
193  // Create colormap
194  for (unsigned int i = 0; i < I_color.getHeight(); i++) {
195  double hue = i / (double)I_color.getHeight(), saturation = 1.0, value = 1.0;
196  unsigned char rgb[3];
197  vpImageConvert::HSVToRGB(&hue, &saturation, &value, rgb, 1);
198 
199  for (unsigned int j = 0; j < I_color.getWidth(); j++) {
200  I_color[i][j].R = rgb[0];
201  I_color[i][j].G = rgb[1];
202  I_color[i][j].B = rgb[2];
203  }
204  }
205 
206  vpDisplay::display(I_color);
207  vpDisplay::displayText(I_color, 20, 20, "Left click to draw a polygon, right click when it is finished.",
209  vpDisplay::flush(I_color);
210 
211  polygon.initClick(I_color);
212 
213  vpDisplay::display(I_color);
214  vpDisplay::displayText(I_color, 20, 20, "Shape is closed. Click to display dashed lines.", vpColor::black);
215  vpDisplay::displayLine(I_color, polygon.getCorners(), true, vpColor::red, 2);
216  vpDisplay::flush(I_color);
217  vpDisplay::getClick(I_color);
218 
219  vpDisplay::display(I_color);
220  vpDisplay::displayText(I_color, 20, 20, "Shape is not closed. Click to quit.", vpColor::black);
221  vpDisplay::displayDotLine(I_color, polygon.getCorners(), false, vpColor::red, 2);
222  vpDisplay::flush(I_color);
223  vpDisplay::getClick(I_color);
224  }
225 #else
226  (void)argc;
227  (void)argv;
228 #endif
229  return EXIT_SUCCESS;
230 }
const std::vector< vpImagePoint > & getCorners() const
Definition: vpPolygon.h:153
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
unsigned int getWidth() const
Definition: vpImage.h:239
static const vpColor black
Definition: vpColor.h:174
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:129
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:151
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="")
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:180
Defines a generic 2D polygon.
Definition: vpPolygon.h:103
Display for windows using Direct3D 3rd party. Thus to enable this class Direct3D should be installed...
Definition: vpDisplayD3D.h:107
static void display(const vpImage< unsigned char > &I)
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
void initClick(const vpImage< unsigned char > &I, unsigned int size=5, const vpColor &color=vpColor::red, unsigned int thickness=1)
Definition: vpPolygon.cpp:181
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:138
static void displayLine(const vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1)
static void HSVToRGB(const double *hue, const double *saturation, const double *value, unsigned char *rgb, const unsigned int size)
static void displayDotLine(const vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1)