Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
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
39
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/vpDisplayFactory.h>
47#include <visp3/io/vpParseArgv.h>
48
49// List of allowed command line options
50#define GETOPTARGS "cdh"
51
52#ifdef ENABLE_VISP_NAMESPACE
53using namespace VISP_NAMESPACE_NAME;
54#endif
55
56void usage(const char *name, const char *badparam);
57bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display);
58
66void usage(const char *name, const char *badparam)
67{
68 fprintf(stdout, "\n\
69Display polygon lines.\n\
70\n\
71SYNOPSIS\n\
72 %s [-c] [-d] [-h]\n",
73 name);
74
75 fprintf(stdout, "\n\
76OPTIONS: Default\n\
77 -c\n\
78 Disable the mouse click. Useful to automate the \n\
79 execution of this program without human intervention.\n\
80\n\
81 -d \n\
82 Disable the image display. This can be useful \n\
83 for automatic tests. \n\
84\n\
85 -h\n\
86 Print the help.\n\n");
87
88 if (badparam) {
89 fprintf(stderr, "ERROR: \n");
90 fprintf(stderr, "\nBad parameter [%s]\n", badparam);
91 }
92}
93
108bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display)
109{
110 const char *optarg_;
111 int c;
112
113 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
114 switch (c) {
115 case 'c':
116 click_allowed = false;
117 break;
118 case 'd':
119 display = false;
120 break;
121 case 'h':
122 usage(argv[0], nullptr);
123 return false;
124
125 default:
126 usage(argv[0], optarg_);
127 return false;
128 }
129 }
130
131 if ((c == 1) || (c == -1)) {
132 // standalone param or error
133 usage(argv[0], nullptr);
134 std::cerr << "ERROR: " << std::endl;
135 std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
136 return false;
137 }
138
139 return true;
140}
141
142int main(int argc, const char **argv)
143{
144#ifdef VISP_HAVE_DISPLAY
145 bool opt_click_allowed = true;
146 bool opt_display = true;
147
148 // Read the command line options
149 if (getOptions(argc, argv, opt_click_allowed, opt_display) == false) {
150 return EXIT_FAILURE;
151 }
152
153 if (opt_display && opt_click_allowed) {
154 vpImage<unsigned char> I(480, 640, 127);
155 vpImage<vpRGBa> I_color(480, 640);
156
157#if(VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
158 std::shared_ptr<vpDisplay> d = vpDisplayFactory::createDisplay();
159 std::shared_ptr<vpDisplay> d2 = vpDisplayFactory::createDisplay();
160#else
163#endif
164 d->init(I, 0, 0, "Grayscale image");
165
167 vpDisplay::displayText(I, 20, 20, "Left click to draw a polygon, right click when it is finished.", vpColor::red);
169
170 vpPolygon polygon;
171 polygon.initClick(I);
172
174 vpDisplay::displayText(I, 20, 20, "Shape is not closed. Click to display dashed lines.", vpColor::red);
175 vpDisplay::displayLine(I, polygon.getCorners(), false, vpColor::red, 2);
178
180 vpDisplay::displayText(I, 20, 20, "Shape is closed. Click to draw on color image.", vpColor::red);
181 vpDisplay::displayDotLine(I, polygon.getCorners(), true, vpColor::red, 2);
184
185 d2->init(I_color, I.getWidth(), 0, "Color image");
186 // Create colormap
187 for (unsigned int i = 0; i < I_color.getHeight(); i++) {
188 double hue = i / static_cast<double>(I_color.getHeight()), saturation = 1.0, value = 1.0;
189 unsigned char rgb[3];
190 vpImageConvert::HSVToRGB(&hue, &saturation, &value, rgb, 1);
191
192 for (unsigned int j = 0; j < I_color.getWidth(); j++) {
193 I_color[i][j].R = rgb[0];
194 I_color[i][j].G = rgb[1];
195 I_color[i][j].B = rgb[2];
196 }
197 }
198
199 vpDisplay::display(I_color);
200 vpDisplay::displayText(I_color, 20, 20, "Left click to draw a polygon, right click when it is finished.",
202 vpDisplay::flush(I_color);
203
204 polygon.initClick(I_color);
205
206 vpDisplay::display(I_color);
207 vpDisplay::displayText(I_color, 20, 20, "Shape is closed. Click to display dashed lines.", vpColor::black);
208 vpDisplay::displayLine(I_color, polygon.getCorners(), true, vpColor::red, 2);
209 vpDisplay::flush(I_color);
210 vpDisplay::getClick(I_color);
211
212 vpDisplay::display(I_color);
213 vpDisplay::displayText(I_color, 20, 20, "Shape is not closed. Click to quit.", vpColor::black);
214 vpDisplay::displayDotLine(I_color, polygon.getCorners(), false, vpColor::red, 2);
215 vpDisplay::flush(I_color);
216 vpDisplay::getClick(I_color);
217#if(VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
218 delete d;
219 delete d2;
220#endif
221 }
222#else
223 (void)argc;
224 (void)argv;
225#endif
226 return EXIT_SUCCESS;
227}
static const vpColor red
Definition vpColor.h:198
static const vpColor black
Definition vpColor.h:192
Class that defines generic functionalities for display.
Definition vpDisplay.h:171
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:131
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
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)
std::shared_ptr< vpDisplay > createDisplay()
Return a smart pointer vpDisplay specialization if a GUI library is available or nullptr otherwise.
vpDisplay * allocateDisplay()
Return a newly allocated vpDisplay specialization if a GUI library is available or nullptr otherwise.