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