ViSP
 All Classes Functions Variables Enumerations Enumerator Friends Groups Pages
testPolygon.cpp
1 /****************************************************************************
2  *
3  * $Id: testPolygon.cpp 4056 2013-01-05 13:04:42Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2013 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 
71 void usage(const char *name, const char *badparam)
72 {
73  fprintf(stdout, "\n\
74 test the generic 2D polygons.\n\
75 \n\
76 SYNOPSIS\n\
77  %s [-c] [-d] [-h]\n \
78 ", name);
79 
80  fprintf(stdout, "\n\
81 OPTIONS: \n\
82  -c \n\
83  Disable mouse click.\n\
84 \n\
85  -d \n\
86  Turn off display.\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,
107  bool& opt_display, bool& opt_click)
108 {
109  const char *optarg;
110  int c;
111  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
112 
113  switch (c) {
114  case 'c': opt_click = false; break;
115  case 'd': opt_display = false; break;
116  case 'h': usage(argv[0], NULL); return false; break;
117 
118  default:
119  usage(argv[0], optarg); return false; break;
120  }
121  }
122 
123  if ((c == 1) || (c == -1)) {
124  // standalone param or error
125  usage(argv[0], NULL);
126  std::cerr << "ERROR: " << std::endl;
127  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
128  return false;
129  }
130 
131  return true;
132 }
133 
134 
135 
136 /* -------------------------------------------------------------------------- */
137 /* MAIN FUNCTION */
138 /* -------------------------------------------------------------------------- */
139 
140 int
141 main(int argc, const char** argv)
142 {
143  bool opt_display = true;
144  bool opt_click = true;
145  vpImage<unsigned char> I(480, 640, 255);
146 
147  // Read the command line options
148  if (getOptions(argc, argv, opt_display, opt_click) == false) {
149  return (-1);
150  }
151 
152  std::vector <vpImagePoint> vec1;
153  vec1.push_back(vpImagePoint(200, 200));
154  vec1.push_back(vpImagePoint(200, 400));
155  vec1.push_back(vpImagePoint(320, 400));
156  vec1.push_back(vpImagePoint(380, 300));
157  vec1.push_back(vpImagePoint(280, 280));
158  vpPolygon p1;
159  p1.buildFrom(vec1);
160 
161  std::vector <vpImagePoint> vec2;
162  vec2.push_back(vpImagePoint(20, 20));
163  vec2.push_back(vpImagePoint(100, 20));
164  vec2.push_back(vpImagePoint(100, 100));
165  vec2.push_back(vpImagePoint(20, 100));
166  vpPolygon p2(vec2);
167 
168 
169  std::vector <vpImagePoint> vec3;
170  vec2.push_back(vpImagePoint(250, 250));
171  vec2.push_back(vpImagePoint(150, 250));
172  vec2.push_back(vpImagePoint(250, 250));
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::displayCharString(I, vpImagePoint(10, 10), "Click to finish", vpColor::red);
210  vpDisplay::flush(I);
211 
212  if (opt_click)
214 
215 
217  vpDisplay::displayCharString(I, vpImagePoint(10, 10), "Left click to add a point", vpColor::red);
218  vpDisplay::displayCharString(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 
250 
double getTop() const
Definition: vpRect.h:169
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:133
Define the X11 console to display images.
Definition: vpDisplayX.h:152
void display(const vpImage< unsigned char > &I, const vpColor &color, unsigned int thickness=1)
Definition: vpPolygon.cpp:403
double getRight() const
Definition: vpRect.h:162
static const vpColor green
Definition: vpColor.h:170
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:1991
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:79
static const vpColor red
Definition: vpColor.h:167
static const vpColor orange
Definition: vpColor.h:177
bool isInside(const vpImagePoint &iP)
Definition: vpPolygon.cpp:247
Defines a generic 2D polygon.
Definition: vpPolygon.h:102
double getBottom() const
Definition: vpRect.h:98
vpRect getBoundingBox() const
Definition: vpPolygon.h:168
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:203
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
virtual void displayCharString(const vpImagePoint &ip, const char *text, const vpColor &color=vpColor::green)=0
void initClick(const vpImage< unsigned char > &I)
Definition: vpPolygon.cpp:160
Defines a rectangle in the plane.
Definition: vpRect.h:82
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:92
static const vpColor lightBlue
Definition: vpColor.h:172
double getLeft() const
Definition: vpRect.h:156
virtual void displayPoint(const vpImagePoint &ip, const vpColor &color)=0
void buildFrom(const std::vector< vpImagePoint > &corners)
Definition: vpPolygon.cpp:127
static const vpColor blue
Definition: vpColor.h:173