ViSP  3.0.0
testPolygon.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2015 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
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 http://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  * Example which test the polygon.
32  *
33  * Author:
34  * Romain Tallonneau
35  *
36  *****************************************************************************/
37 
38 
39 #include <visp3/core/vpConfig.h>
40 #include <visp3/core/vpPolygon.h>
41 #include <visp3/io/vpParseArgv.h>
42 #include <visp3/core/vpImagePoint.h>
43 
44 #include <visp3/core/vpDisplay.h>
45 #include <visp3/gui/vpDisplayX.h>
46 #include <visp3/gui/vpDisplayGTK.h>
47 #include <visp3/gui/vpDisplayGDI.h>
48 
49 
50 #include <math.h>
51 
52 #include <iostream>
53 #include <string>
54 #include <vector>
55 
57 #define GETOPTARGS "cdh"
58 
59 void usage(const char *name, const char *badparam);
60 bool getOptions(int argc, const char **argv, bool& opt_display, bool& opt_click);
61 
70 void usage(const char *name, const char *badparam)
71 {
72  fprintf(stdout, "\n\
73 test the generic 2D polygons.\n\
74 \n\
75 SYNOPSIS\n\
76  %s [-c] [-d] [-h]\n \
77 ", name);
78 
79  fprintf(stdout, "\n\
80 OPTIONS: \n\
81  -c \n\
82  Disable mouse click.\n\
83 \n\
84  -d \n\
85  Turn off display.\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 
105 bool getOptions(int argc, const char **argv, bool& opt_display, bool& opt_click)
106 {
107  const char *optarg_;
108  int c;
109  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
110 
111  switch (c) {
112  case 'c': opt_click = false; break;
113  case 'd': opt_display = false; break;
114  case 'h': usage(argv[0], NULL); return false; break;
115 
116  default:
117  usage(argv[0], optarg_); return false; break;
118  }
119  }
120 
121  if ((c == 1) || (c == -1)) {
122  // standalone param or error
123  usage(argv[0], NULL);
124  std::cerr << "ERROR: " << std::endl;
125  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
126  return false;
127  }
128 
129  return true;
130 }
131 
132 
133 
134 /* -------------------------------------------------------------------------- */
135 /* MAIN FUNCTION */
136 /* -------------------------------------------------------------------------- */
137 
138 int
139 main(int argc, const char** argv)
140 {
141  try {
142  bool opt_display = true;
143  bool opt_click = true;
144  vpImage<unsigned char> I(480, 640, 255);
145 
146  // Read the command line options
147  if (getOptions(argc, argv, opt_display, opt_click) == false) {
148  return (-1);
149  }
150 
151  std::vector <vpImagePoint> vec1;
152  vec1.push_back(vpImagePoint(200, 200));
153  vec1.push_back(vpImagePoint(200, 400));
154  vec1.push_back(vpImagePoint(320, 400));
155  vec1.push_back(vpImagePoint(380, 300));
156  vec1.push_back(vpImagePoint(280, 280));
157  vpPolygon p1;
158  p1.buildFrom(vec1);
159 
160  std::vector <vpImagePoint> vec2;
161  vec2.push_back(vpImagePoint(20, 20));
162  vec2.push_back(vpImagePoint(100, 20));
163  vec2.push_back(vpImagePoint(100, 100));
164  vec2.push_back(vpImagePoint(20, 100));
165  vpPolygon p2(vec2);
166 
167 
168  std::vector <vpImagePoint> vec3;
169  vpPolygon p3(vec3);
170 
171 #if defined VISP_HAVE_X11
172  vpDisplayX display;
173 #elif defined VISP_HAVE_GTK
174  vpDisplayGTK display;
175 #elif defined VISP_HAVE_GDI
176  vpDisplayGDI display;
177 #else
178  opt_display = false;
179 #endif
180 
181  std::cout << " Polygon 1 : " << std::endl;
182  std::cout << " area : " << p1.getArea() << std::endl;
183  std::cout << " center : " << p1.getCenter() << std::endl << std::endl;
184 
185  std::cout << " Polygon 2 : " << std::endl;
186  std::cout << " area : " << p2.getArea() << std::endl;
187  std::cout << " center : " << p2.getCenter() << std::endl << std::endl;
188 
189  std::cout << " Polygon 3 : " << std::endl;
190  std::cout << " area : " << p3.getArea() << std::endl;
191  std::cout << " center : " << p3.getCenter() << std::endl;
192 
193 
194  if(opt_display){
195 #if (defined VISP_HAVE_X11) || (defined VISP_HAVE_GTK) || (defined VISP_HAVE_GDI)
196  display.init(I, 10, 10, "Test vpPolygon");
197 #endif
199  p1.display(I, vpColor::green, 1);
201  p2.display(I, vpColor::red, 1);
202  vpDisplay::displayCross(I, p2.getCenter(), 5, vpColor::red);
203  p3.display(I, vpColor::blue, 1);
204  vpDisplay::displayCross(I, p3.getCenter(), 5, vpColor::lightBlue);
205  vpDisplay::displayText(I, vpImagePoint(10, 10), "Click to finish", vpColor::red);
206  vpDisplay::flush(I);
207 
208  if (opt_click)
210 
211 
213  vpDisplay::displayText(I, vpImagePoint(10, 10), "Left click to add a point", vpColor::red);
214  vpDisplay::displayText(I, vpImagePoint(20, 10), "Right click to build the polygon", vpColor::red);
215  vpDisplay::flush(I);
216  if (opt_click) {
217  vpPolygon p4;
218  p4.initClick(I);
219  p4.display(I, vpColor::green, 1);
220  std::cout << std::endl;
221  std::cout << " Polygon 4 : " << std::endl;
222  std::cout << " area : " << p4.getArea() << std::endl;
223  std::cout << " center : " << p4.getCenter() << std::endl;
224  std::cout << "Click to continue." << std::endl;
225  vpDisplay::flush(I);
227 
228  vpRect bbox = p4.getBoundingBox();
229  for(unsigned int i= (unsigned int)floor(bbox.getTop()); i<(unsigned int)ceil(bbox.getBottom()); ++i){
230  for(unsigned int j=(unsigned int)floor(bbox.getLeft()); j<(unsigned int)ceil(bbox.getRight()); ++j){
231  if(p4.isInside(vpImagePoint(i, j))){
233  }
234  }
235  }
236  vpDisplay::flush(I);
237  std::cout << "Click to finish." << std::endl;
238 
240  }
241  }
242 
243  return 0;
244  }
245  catch(vpException e) {
246  std::cout << "Catch an exception: " << e << std::endl;
247  return 1;
248  }
249 }
250 
251 
double getTop() const
Definition: vpRect.h:176
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
Definition: vpDisplay.cpp:888
Define the X11 console to display images.
Definition: vpDisplayX.h:148
error that can be emited by ViSP classes.
Definition: vpException.h:73
double getRight() const
Definition: vpRect.h:163
static const vpColor green
Definition: vpColor.h:166
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:2233
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:76
static const vpColor red
Definition: vpColor.h:163
static const vpColor orange
Definition: vpColor.h:173
bool isInside(const vpImagePoint &iP)
Definition: vpPolygon.cpp:246
Defines a generic 2D polygon.
Definition: vpPolygon.h:98
double getBottom() const
Definition: vpRect.h:99
vpRect getBoundingBox() const
Definition: vpPolygon.h:164
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:206
virtual void displayCross(const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)=0
double getArea() const
Definition: vpPolygon.h:144
The vpDisplayGTK allows to display image using the GTK+ library version 1.2.
Definition: vpDisplayGTK.h:141
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
vpImagePoint getCenter() const
Definition: vpPolygon.h:154
void initClick(const vpImage< unsigned char > &I)
Definition: vpPolygon.cpp:159
Defines a rectangle in the plane.
Definition: vpRect.h:81
virtual bool getClick(bool blocking=true)=0
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:168
double getLeft() const
Definition: vpRect.h:157
virtual void displayPoint(const vpImagePoint &ip, const vpColor &color)=0
void buildFrom(const std::vector< vpImagePoint > &corners)
Definition: vpPolygon.cpp:126
static const vpColor blue
Definition: vpColor.h:169
void display(const vpImage< unsigned char > &I, const vpColor &color, unsigned int thickness=1) const
Definition: vpPolygon.cpp:402