Visual Servoing Platform  version 3.6.1 under development (2024-12-03)
vpPolygon.h
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2024 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
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 https://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  * Defines a generic 2D polygon.
32  */
33 
34 #ifndef VP_POLYGON_H
35 #define VP_POLYGON_H
36 
37 #include <list>
38 #include <vector>
39 
40 #include <visp3/core/vpConfig.h>
41 #include <visp3/core/vpCameraParameters.h>
42 #include <visp3/core/vpColor.h>
43 #include <visp3/core/vpImage.h>
44 #include <visp3/core/vpImagePoint.h>
45 #include <visp3/core/vpPoint.h>
46 #include <visp3/core/vpRect.h>
47 
48 BEGIN_VISP_NAMESPACE
102 class VISP_EXPORT vpPolygon
103 {
104 public:
106  {
108  PnPolyRayCasting
110  };
111 
112  vpPolygon();
113  VP_EXPLICIT vpPolygon(const std::vector<vpImagePoint> &corners);
114  VP_EXPLICIT vpPolygon(const std::list<vpImagePoint> &corners);
115  vpPolygon(const vpPolygon &poly);
116  virtual ~vpPolygon();
117 
118  vpPolygon &operator=(const vpPolygon &poly);
119 
120  vpPolygon &buildFrom(const std::vector<vpImagePoint> &corners, const bool &create_convex_hull = false);
121  vpPolygon &buildFrom(const std::list<vpImagePoint> &corners, const bool &create_convex_hull = false);
122  vpPolygon &buildFrom(const std::vector<vpPoint> &corners, const vpCameraParameters &cam,
123  const bool &create_convex_hull = false);
124 
125  unsigned int getSize() const;
126  void initClick(const vpImage<unsigned char> &I, unsigned int size = 5, const vpColor &color = vpColor::red,
127  unsigned int thickness = 1);
128  void initClick(const vpImage<vpRGBa> &I, unsigned int size = 5, const vpColor &color = vpColor::red,
129  unsigned int thickness = 1);
130 
131  bool isInside(const vpImagePoint &iP, const PointInPolygonMethod &method = PnPolyRayCasting) const;
132 
133  void display(const vpImage<unsigned char> &I, const vpColor &color, unsigned int thickness = 1) const;
134 
140  const std::vector<vpImagePoint> &getCorners() const { return _corners; }
141 
148  inline double getArea() const { return this->_area; }
149 
156  inline vpImagePoint getCenter() const { return this->_center; }
157 
164  inline vpRect getBoundingBox() const { return _bbox; }
165 
166  //###################
167  // Static Functions
168  //###################
169  static bool isInside(const std::vector<vpImagePoint> &roi, const double &i, const double &j,
170  const PointInPolygonMethod &method = PnPolyRayCasting);
171 
172 protected:
173  void init(const std::vector<vpImagePoint> &corners);
174  void init(const std::list<vpImagePoint> &corners);
175  void updateArea();
176  void updateCenter();
177  void updateBoundingBox();
178 
179 protected:
181  std::vector<vpImagePoint> _corners;
186  double _area;
189  bool _goodPoly;
192 
193 private:
194  bool testIntersectionSegments(const vpImagePoint &ip1, const vpImagePoint &ip2, const vpImagePoint &ip3,
195  const vpImagePoint &ip4) const;
196  void precalcValuesPnPoly();
197 
198  std::vector<double> m_PnPolyConstants;
199  std::vector<double> m_PnPolyMultiples;
200 };
201 END_VISP_NAMESPACE
202 #endif
Generic class defining intrinsic camera parameters.
Class to define RGB colors available for display functionalities.
Definition: vpColor.h:157
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:82
Defines a generic 2D polygon.
Definition: vpPolygon.h:103
const std::vector< vpImagePoint > & getCorners() const
Definition: vpPolygon.h:140
bool _goodPoly
Definition: vpPolygon.h:189
vpRect getBoundingBox() const
Definition: vpPolygon.h:164
vpImagePoint getCenter() const
Definition: vpPolygon.h:156
vpImagePoint _center
Definition: vpPolygon.h:184
double getArea() const
Definition: vpPolygon.h:148
vpRect _bbox
Bounding box containing the polygon.
Definition: vpPolygon.h:191
PointInPolygonMethod
Definition: vpPolygon.h:106
@ PnPolySegmentIntersection
Definition: vpPolygon.h:107
std::vector< vpImagePoint > _corners
Collection of image points containing the corners.
Definition: vpPolygon.h:181
double _area
Area of the polygon.
Definition: vpPolygon.h:186
Defines a rectangle in the plane.
Definition: vpRect.h:79