ViSP
testPolygon.cpp
1 /****************************************************************************
2  *
3  * $Id: testPolygon.cpp 5004 2014-11-24 08:24:18Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Example which test the polygon.
36  *
37  * Author:
38  * Romain Tallonneau
39  *
40  *****************************************************************************/
41 
42 
43 #include <visp/vpConfig.h>
44 #include <visp/vpPolygon.h>
45 #include <visp/vpParseArgv.h>
46 #include <visp/vpImagePoint.h>
47 
48 #include <visp/vpDisplay.h>
49 #include <visp/vpDisplayX.h>
50 #include <visp/vpDisplayGTK.h>
51 #include <visp/vpDisplayGDI.h>
52 
53 
54 #include <math.h>
55 
56 #include <iostream>
57 #include <string>
58 #include <vector>
59 
61 #define GETOPTARGS "cdh"
62 
63 void usage(const char *name, const char *badparam);
64 bool getOptions(int argc, const char **argv, bool& opt_display, bool& opt_click);
65 
74 void usage(const char *name, const char *badparam)
75 {
76  fprintf(stdout, "\n\
77 test the generic 2D polygons.\n\
78 \n\
79 SYNOPSIS\n\
80  %s [-c] [-d] [-h]\n \
81 ", name);
82 
83  fprintf(stdout, "\n\
84 OPTIONS: \n\
85  -c \n\
86  Disable mouse click.\n\
87 \n\
88  -d \n\
89  Turn off display.\n\
90 \n\
91  -h\n\
92  Print the help.\n\n");
93 
94  if (badparam) {
95  fprintf(stderr, "ERROR: \n" );
96  fprintf(stderr, "\nBad parameter [%s]\n", badparam);
97  }
98 }
99 
109 bool getOptions(int argc, const char **argv, bool& opt_display, bool& opt_click)
110 {
111  const char *optarg_;
112  int c;
113  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
114 
115  switch (c) {
116  case 'c': opt_click = false; break;
117  case 'd': opt_display = false; break;
118  case 'h': usage(argv[0], NULL); return false; break;
119 
120  default:
121  usage(argv[0], optarg_); return false; break;
122  }
123  }
124 
125  if ((c == 1) || (c == -1)) {
126  // standalone param or error
127  usage(argv[0], NULL);
128  std::cerr << "ERROR: " << std::endl;
129  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
130  return false;
131  }
132 
133  return true;
134 }
135 
136 
137 
138 /* -------------------------------------------------------------------------- */
139 /* MAIN FUNCTION */
140 /* -------------------------------------------------------------------------- */
141 
142 int
143 main(int argc, const char** argv)
144 {
145  try {
146  bool opt_display = true;
147  bool opt_click = true;
148  vpImage<unsigned char> I(480, 640, 255);
149 
150  // Read the command line options
151  if (getOptions(argc, argv, opt_display, opt_click) == false) {
152  return (-1);
153  }
154 
155  std::vector <vpImagePoint> vec1;
156  vec1.push_back(vpImagePoint(200, 200));
157  vec1.push_back(vpImagePoint(200, 400));
158  vec1.push_back(vpImagePoint(320, 400));
159  vec1.push_back(vpImagePoint(380, 300));
160  vec1.push_back(vpImagePoint(280, 280));
161  vpPolygon p1;
162  p1.buildFrom(vec1);
163 
164  std::vector <vpImagePoint> vec2;
165  vec2.push_back(vpImagePoint(20, 20));
166  vec2.push_back(vpImagePoint(100, 20));
167  vec2.push_back(vpImagePoint(100, 100));
168  vec2.push_back(vpImagePoint(20, 100));
169  vpPolygon p2(vec2);
170 
171 
172  std::vector <vpImagePoint> vec3;
173  vpPolygon p3(vec3);
174 
175 #if defined VISP_HAVE_X11
176  vpDisplayX display;
177 #elif defined VISP_HAVE_GTK
178  vpDisplayGTK display;
179 #elif defined VISP_HAVE_GDI
180  vpDisplayGDI display;
181 #else
182  opt_display = false;
183 #endif
184 
185  std::cout << " Polygon 1 : " << std::endl;
186  std::cout << " area : " << p1.getArea() << std::endl;
187  std::cout << " center : " << p1.getCenter() << std::endl << std::endl;
188 
189  std::cout << " Polygon 2 : " << std::endl;
190  std::cout << " area : " << p2.getArea() << std::endl;
191  std::cout << " center : " << p2.getCenter() << std::endl << std::endl;
192 
193  std::cout << " Polygon 3 : " << std::endl;
194  std::cout << " area : " << p3.getArea() << std::endl;
195  std::cout << " center : " << p3.getCenter() << std::endl;
196 
197 
198  if(opt_display){
199 #if (defined VISP_HAVE_X11) || (defined VISP_HAVE_GTK) || (defined VISP_HAVE_GDI)
200  display.init(I, 10, 10, "Test vpPolygon");
201 #endif
203  p1.display(I, vpColor::green, 1);
205  p2.display(I, vpColor::red, 1);
206  vpDisplay::displayCross(I, p2.getCenter(), 5, vpColor::red);
207  p3.display(I, vpColor::blue, 1);
208  vpDisplay::displayCross(I, p3.getCenter(), 5, vpColor::lightBlue);
209  vpDisplay::displayText(I, vpImagePoint(10, 10), "Click to finish", vpColor::red);
210  vpDisplay::flush(I);
211 
212  if (opt_click)
214 
215 
217  vpDisplay::displayText(I, vpImagePoint(10, 10), "Left click to add a point", vpColor::red);
218  vpDisplay::displayText(I, vpImagePoint(20, 10), "Right click to build the polygon", vpColor::red);
219  vpDisplay::flush(I);
220  if (opt_click) {
221  vpPolygon p4;
222  p4.initClick(I);
223  p4.display(I, vpColor::green, 1);
224  std::cout << std::endl;
225  std::cout << " Polygon 4 : " << std::endl;
226  std::cout << " area : " << p4.getArea() << std::endl;
227  std::cout << " center : " << p4.getCenter() << std::endl;
228  std::cout << "Click to continue." << std::endl;
229  vpDisplay::flush(I);
231 
232  vpRect bbox = p4.getBoundingBox();
233  for(unsigned int i= (unsigned int)floor(bbox.getTop()); i<(unsigned int)ceil(bbox.getBottom()); ++i){
234  for(unsigned int j=(unsigned int)floor(bbox.getLeft()); j<(unsigned int)ceil(bbox.getRight()); ++j){
235  if(p4.isInside(vpImagePoint(i, j))){
237  }
238  }
239  }
240  vpDisplay::flush(I);
241  std::cout << "Click to finish." << std::endl;
242 
244  }
245  }
246 
247  return 0;
248  }
249  catch(vpException e) {
250  std::cout << "Catch an exception: " << e << std::endl;
251  return 1;
252  }
253 }
254 
255 
double getTop() const
Definition: vpRect.h:180
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:132
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
Definition: vpDisplay.cpp:887
Define the X11 console to display images.
Definition: vpDisplayX.h:152
error that can be emited by ViSP classes.
Definition: vpException.h:76
void display(const vpImage< unsigned char > &I, const vpColor &color, unsigned int thickness=1)
Definition: vpPolygon.cpp:406
double getRight() const
Definition: vpRect.h:167
static const vpColor green
Definition: vpColor.h:170
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:2232
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:80
static const vpColor red
Definition: vpColor.h:167
static const vpColor orange
Definition: vpColor.h:177
bool isInside(const vpImagePoint &iP)
Definition: vpPolygon.cpp:250
Defines a generic 2D polygon.
Definition: vpPolygon.h:102
double getBottom() const
Definition: vpRect.h:103
vpRect getBoundingBox() const
Definition: vpPolygon.h:168
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:210
virtual void displayCross(const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)=0
double getArea() const
Definition: vpPolygon.h:148
The vpDisplayGTK allows to display image using the GTK+ library version 1.2.
Definition: vpDisplayGTK.h:145
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
vpImagePoint getCenter() const
Definition: vpPolygon.h:158
void initClick(const vpImage< unsigned char > &I)
Definition: vpPolygon.cpp:163
Defines a rectangle in the plane.
Definition: vpRect.h:85
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:93
static const vpColor lightBlue
Definition: vpColor.h:172
double getLeft() const
Definition: vpRect.h:161
virtual void displayPoint(const vpImagePoint &ip, const vpColor &color)=0
void buildFrom(const std::vector< vpImagePoint > &corners)
Definition: vpPolygon.cpp:130
static const vpColor blue
Definition: vpColor.h:173