Visual Servoing Platform  version 3.5.1 under development (2023-03-29)
vpPolygon.h
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2022 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  * Defines a generic 2D polygon.
33  *
34  * Author:
35  * Amaury Dame
36  * Nicolas Melchior
37  * Romain Tallonneau
38  * Fabien Spindler
39  * Julien Dufour
40  *
41  *****************************************************************************/
42 
43 #ifndef vpPolygon_h
44 #define vpPolygon_h
45 
46 #include <list>
47 #include <vector>
48 
49 #include <visp3/core/vpCameraParameters.h>
50 #include <visp3/core/vpColor.h>
51 #include <visp3/core/vpImage.h>
52 #include <visp3/core/vpImagePoint.h>
53 #include <visp3/core/vpPoint.h>
54 #include <visp3/core/vpRect.h>
55 
105 class VISP_EXPORT vpPolygon
106 {
107 protected:
109  std::vector<vpImagePoint> _corners;
114  double _area;
117  bool _goodPoly;
120 
121 public:
124  PnPolyRayCasting
126  };
127 
128  vpPolygon();
129  explicit vpPolygon(const std::vector<vpImagePoint> &corners);
130  explicit vpPolygon(const std::list<vpImagePoint> &corners);
131  vpPolygon(const vpPolygon &poly);
132  virtual ~vpPolygon();
133 
134  vpPolygon &operator=(const vpPolygon &poly);
135 
136  void buildFrom(const std::vector<vpImagePoint> &corners, const bool create_convex_hull = false);
137  void buildFrom(const std::list<vpImagePoint> &corners, const bool create_convex_hull = false);
138  void buildFrom(const std::vector<vpPoint> &corners, const vpCameraParameters &cam,
139  const bool create_convex_hull = false);
140 
141  unsigned int getSize() const;
142  void initClick(const vpImage<unsigned char> &I, unsigned int size = 5, const vpColor &color = vpColor::red,
143  unsigned int thickness = 1);
144  void initClick(const vpImage<vpRGBa> &I, unsigned int size = 5, const vpColor &color = vpColor::red,
145  unsigned int thickness = 1);
146 
147  bool isInside(const vpImagePoint &iP, const PointInPolygonMethod &method = PnPolyRayCasting) const;
148 
149  void display(const vpImage<unsigned char> &I, const vpColor &color, unsigned int thickness = 1) const;
150 
156  const std::vector<vpImagePoint> &getCorners() const { return _corners; }
157 
164  inline double getArea() const { return this->_area; }
165 
172  inline vpImagePoint getCenter() const { return this->_center; }
173 
180  inline vpRect getBoundingBox() const { return _bbox; }
181 
182 protected:
183  void init(const std::vector<vpImagePoint> &corners);
184  void init(const std::list<vpImagePoint> &corners);
185  void updateArea();
186  void updateCenter();
187  void updateBoundingBox();
188 
189 private:
190  bool testIntersectionSegments(const vpImagePoint &ip1, const vpImagePoint &ip2, const vpImagePoint &ip3,
191  const vpImagePoint &ip4) const;
192  void precalcValuesPnPoly();
193 
194  std::vector<double> m_PnPolyConstants;
195  std::vector<double> m_PnPolyMultiples;
196 
197  //###################
198  // Static Functions
199  //###################
200 
201 public:
202  static bool isInside(const std::vector<vpImagePoint> &roi, const double &i, const double &j,
203  const PointInPolygonMethod &method = PnPolyRayCasting);
204 };
205 
206 #endif
Generic class defining intrinsic camera parameters.
Class to define RGB colors available for display functionnalities.
Definition: vpColor.h:158
static const vpColor red
Definition: vpColor.h:217
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:89
Defines a generic 2D polygon.
Definition: vpPolygon.h:106
const std::vector< vpImagePoint > & getCorners() const
Definition: vpPolygon.h:156
bool _goodPoly
Definition: vpPolygon.h:117
vpRect getBoundingBox() const
Definition: vpPolygon.h:180
vpImagePoint getCenter() const
Definition: vpPolygon.h:172
vpImagePoint _center
Definition: vpPolygon.h:112
double getArea() const
Definition: vpPolygon.h:164
vpRect _bbox
Bounding box containing the polygon.
Definition: vpPolygon.h:119
PointInPolygonMethod
Definition: vpPolygon.h:122
@ PnPolySegmentIntersection
Definition: vpPolygon.h:123
std::vector< vpImagePoint > _corners
Collection of image points containing the corners.
Definition: vpPolygon.h:109
double _area
Area of the polygon.
Definition: vpPolygon.h:114
Defines a rectangle in the plane.
Definition: vpRect.h:80