Visual Servoing Platform  version 3.2.0 under development (2019-01-22)
testPolygon.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  * Example which test the polygon.
33  *
34  * Author:
35  * Romain Tallonneau
36  *
37  *****************************************************************************/
38 #include <visp3/core/vpConfig.h>
39 #include <visp3/core/vpImagePoint.h>
40 #include <visp3/core/vpPolygon.h>
41 #include <visp3/io/vpParseArgv.h>
42 
43 #include <visp3/core/vpDisplay.h>
44 #include <visp3/gui/vpDisplayGDI.h>
45 #include <visp3/gui/vpDisplayGTK.h>
46 #include <visp3/gui/vpDisplayX.h>
47 
48 #include <math.h>
49 
50 #include <iostream>
51 #include <string>
52 #include <vector>
53 
55 #define GETOPTARGS "cdm:h"
56 
57 void usage(const char *name, const char *badparam);
58 bool getOptions(int argc, const char **argv, bool &opt_display, bool &opt_click, int &method);
59 
68 void usage(const char *name, const char *badparam)
69 {
70  fprintf(stdout, "\n\
71 test the generic 2D polygons.\n\
72 \n\
73 SYNOPSIS\n\
74  %s [-c] [-d] [-h]\n \
75 ", name);
76 
77  fprintf(stdout, "\n\
78 OPTIONS: \n\
79  -c \n\
80  Disable mouse click.\n\
81 \n\
82  -d \n\
83  Turn off display.\n\
84 \n\
85  -m \n\
86  Point in polygon test method.\n\
87 \n\
88  -h\n\
89  Print the help.\n\n");
90 
91  if (badparam) {
92  fprintf(stderr, "ERROR: \n");
93  fprintf(stderr, "\nBad parameter [%s]\n", badparam);
94  }
95 }
96 
106 bool getOptions(int argc, const char **argv, bool &opt_display, bool &opt_click, int &method)
107 {
108  const char *optarg_;
109  int c;
110  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
111 
112  switch (c) {
113  case 'c':
114  opt_click = false;
115  break;
116  case 'd':
117  opt_display = false;
118  break;
119  case 'm':
120  method = atoi(optarg_);
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 /* --------------------------------------------------------------------------
146  */
147 /* MAIN FUNCTION */
148 /* --------------------------------------------------------------------------
149  */
150 
151 int main(int argc, const char **argv)
152 {
153  try {
154  bool opt_display = true;
155  bool opt_click = true;
157  vpImage<unsigned char> I(480, 640, 255);
158 
159  // Read the command line options
160  if (getOptions(argc, argv, opt_display, opt_click, method) == false) {
161  return (-1);
162  }
163 
164  std::vector<vpImagePoint> vec1;
165  vec1.push_back(vpImagePoint(200, 200));
166  vec1.push_back(vpImagePoint(200, 400));
167  vec1.push_back(vpImagePoint(320, 400));
168  vec1.push_back(vpImagePoint(380, 300));
169  vec1.push_back(vpImagePoint(280, 280));
170  vpPolygon p1;
171  p1.buildFrom(vec1);
172 
173  std::vector<vpImagePoint> vec2;
174  vec2.push_back(vpImagePoint(20, 20));
175  vec2.push_back(vpImagePoint(100, 20));
176  vec2.push_back(vpImagePoint(100, 100));
177  vec2.push_back(vpImagePoint(20, 100));
178  vpPolygon p2(vec2);
179 
180  std::vector<vpImagePoint> vec3;
181  vpPolygon p3(vec3);
182 
183 #if defined VISP_HAVE_X11
184  vpDisplayX display;
185 #elif defined VISP_HAVE_GTK
186  vpDisplayGTK display;
187 #elif defined VISP_HAVE_GDI
188  vpDisplayGDI display;
189 #else
190  opt_display = false;
191 #endif
192 
193  std::cout << " Polygon 1 : " << std::endl;
194  std::cout << " area : " << p1.getArea() << std::endl;
195  std::cout << " center : " << p1.getCenter() << std::endl << std::endl;
196 
197  std::cout << " Polygon 2 : " << std::endl;
198  std::cout << " area : " << p2.getArea() << std::endl;
199  std::cout << " center : " << p2.getCenter() << std::endl << std::endl;
200 
201  std::cout << " Polygon 3 : " << std::endl;
202  std::cout << " area : " << p3.getArea() << std::endl;
203  std::cout << " center : " << p3.getCenter() << std::endl;
204 
205  if (opt_display) {
206 #if (defined VISP_HAVE_X11) || (defined VISP_HAVE_GTK) || (defined VISP_HAVE_GDI)
207  display.init(I, 10, 10, "Test vpPolygon");
208 #endif
210  p1.display(I, vpColor::green, 1);
212  p2.display(I, vpColor::red, 1);
213  vpDisplay::displayCross(I, p2.getCenter(), 5, vpColor::red);
214  p3.display(I, vpColor::blue, 1);
215  vpDisplay::displayCross(I, p3.getCenter(), 5, vpColor::lightBlue);
216  vpDisplay::displayText(I, vpImagePoint(10, 10), "Click to finish", vpColor::red);
217  vpDisplay::flush(I);
218 
219  if (opt_click)
221 
223  vpDisplay::displayText(I, vpImagePoint(10, 10), "Left click to add a point", vpColor::red);
224  vpDisplay::displayText(I, vpImagePoint(20, 10), "Right click to build the polygon", vpColor::red);
225  vpDisplay::flush(I);
226  if (opt_click) {
227  vpPolygon p4;
228  p4.initClick(I);
229  p4.display(I, vpColor::green, 1);
230  std::cout << std::endl;
231  std::cout << " Polygon 4 : " << std::endl;
232  std::cout << " area : " << p4.getArea() << std::endl;
233  std::cout << " center : " << p4.getCenter() << std::endl;
234  std::cout << "Click to continue." << std::endl;
235  vpDisplay::flush(I);
237 
238  vpRect bbox = p4.getBoundingBox();
239  for (unsigned int i = (unsigned int)floor(bbox.getTop()); i < (unsigned int)ceil(bbox.getBottom()); ++i) {
240  for (unsigned int j = (unsigned int)floor(bbox.getLeft()); j < (unsigned int)ceil(bbox.getRight()); ++j) {
241  if (p4.isInside(vpImagePoint(i, j), (vpPolygon::PointInPolygonMethod)method)) {
243  }
244  }
245  }
246  vpDisplay::flush(I);
247 
248  std::cout << "Click to continue." << std::endl;
250  for (unsigned int i = 0; i < I.getHeight(); ++i) {
251  for (unsigned int j = 0; j < I.getWidth(); ++j) {
254  }
255  }
256  }
257  vpDisplay::flush(I);
258 
259  std::cout << "Click to finish." << std::endl;
260 
262  vpDisplay::close(I);
263 
264  // Benchmark Point In Polygon test method
265  std::vector<vpImagePoint> corners = p4.getCorners();
266  std::cout << "Nb polygon corners=" << corners.size() << std::endl;
267 
268  vpPolygon polygon_benchmark(corners);
269  vpImage<unsigned char> I_segmentIntersection(480, 640, 0);
270  vpImage<unsigned char> I_rayCasting(480, 640, 0);
271 
272  double t_benchmark = vpTime::measureTimeMs();
273  for (unsigned int i = 0; i < I_segmentIntersection.getHeight(); i++) {
274  for (unsigned int j = 0; j < I_segmentIntersection.getWidth(); j++) {
275  if (polygon_benchmark.isInside(vpImagePoint(i, j), vpPolygon::PnPolySegmentIntersection)) {
276  I_segmentIntersection[i][j] = 255;
277  }
278  }
279  }
280  t_benchmark = vpTime::measureTimeMs() - t_benchmark;
281  std::cout << "PnPolySegmentIntersection: " << t_benchmark << " ms" << std::endl;
282 
283  t_benchmark = vpTime::measureTimeMs();
284  for (unsigned int i = 0; i < I_rayCasting.getHeight(); i++) {
285  for (unsigned int j = 0; j < I_rayCasting.getWidth(); j++) {
286  if (polygon_benchmark.isInside(vpImagePoint(i, j), vpPolygon::PnPolyRayCasting)) {
287  I_rayCasting[i][j] = 255;
288  }
289  }
290  }
291  t_benchmark = vpTime::measureTimeMs() - t_benchmark;
292  std::cout << "PnPolyRayCasting: " << t_benchmark << " ms" << std::endl;
293 
294 #if defined VISP_HAVE_X11
295  vpDisplayX display1, display2;
296 #elif defined VISP_HAVE_GTK
297  vpDisplayGTK display1, display2;
298 #elif defined VISP_HAVE_GDI
299  vpDisplayGDI display1, display2;
300 #endif
301 
302 #if (defined VISP_HAVE_X11) || (defined VISP_HAVE_GTK) || (defined VISP_HAVE_GDI)
303  display1.init(I_segmentIntersection, 10, 10, "Segment Intersection test");
304  display2.init(I_rayCasting, (int)I_segmentIntersection.getWidth() + 10, 10, "Ray Casting test");
305 #endif
306 
307  vpDisplay::display(I_segmentIntersection);
308  vpDisplay::display(I_rayCasting);
309  vpDisplay::displayText(I_rayCasting, 20, 20, "Click to quit.", vpColor::red);
310  vpDisplay::flush(I_segmentIntersection);
311  vpDisplay::flush(I_rayCasting);
312 
313  vpDisplay::getClick(I_rayCasting);
314  }
315  }
316 
317  return 0;
318  } catch (const vpException &e) {
319  std::cout << "Catch an exception: " << e << std::endl;
320  return 1;
321  }
322 }
double getTop() const
Definition: vpRect.h:175
const std::vector< vpImagePoint > & getCorners() const
Definition: vpPolygon.h:153
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void close(vpImage< unsigned char > &I)
unsigned int getWidth() const
Definition: vpImage.h:239
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
error that can be emited by ViSP classes.
Definition: vpException.h:71
static void displayPoint(const vpImage< unsigned char > &I, const vpImagePoint &ip, const vpColor &color, unsigned int thickness=1)
double getRight() const
Definition: vpRect.h:162
static const vpColor green
Definition: vpColor.h:183
static void flush(const vpImage< unsigned char > &I)
VISP_EXPORT double measureTimeMs()
Definition: vpTime.cpp:88
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
static const vpColor red
Definition: vpColor.h:180
static const vpColor orange
Definition: vpColor.h:190
bool isInside(const vpImagePoint &iP, const PointInPolygonMethod &method=PnPolyRayCasting) const
Definition: vpPolygon.cpp:317
Defines a generic 2D polygon.
Definition: vpPolygon.h:103
double getBottom() const
Definition: vpRect.h:94
vpRect getBoundingBox() const
Definition: vpPolygon.h:177
PointInPolygonMethod
Definition: vpPolygon.h:120
static void display(const vpImage< unsigned char > &I)
void initClick(const vpImage< unsigned char > &I, unsigned int size=5, const vpColor &color=vpColor::red, unsigned int thickness=1)
Definition: vpPolygon.cpp:181
double getArea() const
Definition: vpPolygon.h:161
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:138
vpImagePoint getCenter() const
Definition: vpPolygon.h:169
static void displayCross(const vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
unsigned int getHeight() const
Definition: vpImage.h:178
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 lightBlue
Definition: vpColor.h:185
double getLeft() const
Definition: vpRect.h:156
void buildFrom(const std::vector< vpImagePoint > &corners)
Definition: vpPolygon.cpp:139
static const vpColor blue
Definition: vpColor.h:186
void display(const vpImage< unsigned char > &I, const vpColor &color, unsigned int thickness=1) const
Definition: vpPolygon.cpp:523