Visual Servoing Platform version 3.6.0
Loading...
Searching...
No Matches
testDisplayPolygonLines.cpp
1/****************************************************************************
2 *
3 * ViSP, open source Visual Servoing Platform software.
4 * Copyright (C) 2005 - 2023 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 https://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
58void usage(const char *name, const char *badparam);
59bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display);
60
68void usage(const char *name, const char *badparam)
69{
70 fprintf(stdout, "\n\
71Display polygon lines.\n\
72\n\
73SYNOPSIS\n\
74 %s [-c] [-d] [-h]\n",
75 name);
76
77 fprintf(stdout, "\n\
78OPTIONS: Default\n\
79 -c\n\
80 Disable the mouse click. Useful to automate the \n\
81 execution of this program without human intervention.\n\
82\n\
83 -d \n\
84 Disable the image display. This can be useful \n\
85 for automatic tests. \n\
86\n\
87 -h\n\
88 Print the help.\n\n");
89
90 if (badparam) {
91 fprintf(stderr, "ERROR: \n");
92 fprintf(stderr, "\nBad parameter [%s]\n", badparam);
93 }
94}
95
110bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display)
111{
112 const char *optarg_;
113 int c;
114
115 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
116 switch (c) {
117 case 'c':
118 click_allowed = false;
119 break;
120 case 'd':
121 display = false;
122 break;
123 case 'h':
124 usage(argv[0], NULL);
125 return false;
126 break;
127
128 default:
129 usage(argv[0], optarg_);
130 return false;
131 break;
132 }
133 }
134
135 if ((c == 1) || (c == -1)) {
136 // standalone param or error
137 usage(argv[0], NULL);
138 std::cerr << "ERROR: " << std::endl;
139 std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
140 return false;
141 }
142
143 return true;
144}
145
146int main(int argc, const char **argv)
147{
148#ifdef VISP_HAVE_DISPLAY
149 bool opt_click_allowed = true;
150 bool opt_display = true;
151
152 // Read the command line options
153 if (getOptions(argc, argv, opt_click_allowed, opt_display) == false) {
154 return EXIT_FAILURE;
155 }
156
157 if (opt_display && opt_click_allowed) {
158 vpImage<unsigned char> I(480, 640, 127);
159 vpImage<vpRGBa> I_color(480, 640);
160
161#if defined(VISP_HAVE_X11)
162 vpDisplayX d, d2;
163#elif defined(VISP_HAVE_GTK)
164 vpDisplayGTK d, d2;
165#elif defined(VISP_HAVE_GDI)
166 vpDisplayGDI d, d2;
167#elif defined(VISP_HAVE_D3D9)
168 vpDisplayD3D d, d2;
169#elif defined(HAVE_OPENCV_HIGHGUI)
170 vpDisplayOpenCV d, d2;
171#endif
172 d.init(I, 0, 0, "Grayscale image");
173
175 vpDisplay::displayText(I, 20, 20, "Left click to draw a polygon, right click when it is finished.", vpColor::red);
177
178 vpPolygon polygon;
179 polygon.initClick(I);
180
182 vpDisplay::displayText(I, 20, 20, "Shape is not closed. Click to display dashed lines.", vpColor::red);
183 vpDisplay::displayLine(I, polygon.getCorners(), false, vpColor::red, 2);
186
188 vpDisplay::displayText(I, 20, 20, "Shape is closed. Click to draw on color image.", vpColor::red);
189 vpDisplay::displayDotLine(I, polygon.getCorners(), true, vpColor::red, 2);
192
193 d2.init(I_color, I.getWidth(), 0, "Color image");
194 // Create colormap
195 for (unsigned int i = 0; i < I_color.getHeight(); i++) {
196 double hue = i / (double)I_color.getHeight(), saturation = 1.0, value = 1.0;
197 unsigned char rgb[3];
198 vpImageConvert::HSVToRGB(&hue, &saturation, &value, rgb, 1);
199
200 for (unsigned int j = 0; j < I_color.getWidth(); j++) {
201 I_color[i][j].R = rgb[0];
202 I_color[i][j].G = rgb[1];
203 I_color[i][j].B = rgb[2];
204 }
205 }
206
207 vpDisplay::display(I_color);
208 vpDisplay::displayText(I_color, 20, 20, "Left click to draw a polygon, right click when it is finished.",
210 vpDisplay::flush(I_color);
211
212 polygon.initClick(I_color);
213
214 vpDisplay::display(I_color);
215 vpDisplay::displayText(I_color, 20, 20, "Shape is closed. Click to display dashed lines.", vpColor::black);
216 vpDisplay::displayLine(I_color, polygon.getCorners(), true, vpColor::red, 2);
217 vpDisplay::flush(I_color);
218 vpDisplay::getClick(I_color);
219
220 vpDisplay::display(I_color);
221 vpDisplay::displayText(I_color, 20, 20, "Shape is not closed. Click to quit.", vpColor::black);
222 vpDisplay::displayDotLine(I_color, polygon.getCorners(), false, vpColor::red, 2);
223 vpDisplay::flush(I_color);
224 vpDisplay::getClick(I_color);
225 }
226#else
227 (void)argc;
228 (void)argv;
229#endif
230 return EXIT_SUCCESS;
231}
static const vpColor red
Definition vpColor.h:211
static const vpColor black
Definition vpColor.h:205
Display for windows using Direct3D 3rd party. Thus to enable this class Direct3D should be installed....
Display for windows using GDI (available on any windows 32 platform).
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition vpDisplayX.h:132
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 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)
Definition of the vpImage class member functions.
Definition vpImage.h:135
unsigned int getWidth() const
Definition vpImage.h:242
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Defines a generic 2D polygon.
Definition vpPolygon.h:97
const std::vector< vpImagePoint > & getCorners() const
Definition vpPolygon.h:147
void initClick(const vpImage< unsigned char > &I, unsigned int size=5, const vpColor &color=vpColor::red, unsigned int thickness=1)