Visual Servoing Platform  version 3.6.1 under development (2024-07-27)
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 
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 #ifdef VISP_BUILD_DEPRECATED_FUNCTIONS
121  VP_DEPRECATED void buildFrom(const std::vector<vpImagePoint> &corners, const bool create_convex_hull = false);
122  VP_DEPRECATED void buildFrom(const std::list<vpImagePoint> &corners, const bool create_convex_hull = false);
123  VP_DEPRECATED void buildFrom(const std::vector<vpPoint> &corners, const vpCameraParameters &cam,
124  const bool create_convex_hull = false);
125 #endif
126  vpPolygon &build(const std::vector<vpImagePoint> &corners, const bool &create_convex_hull = false);
127  vpPolygon &build(const std::list<vpImagePoint> &corners, const bool &create_convex_hull = false);
128  vpPolygon &build(const std::vector<vpPoint> &corners, const vpCameraParameters &cam,
129  const bool &create_convex_hull = false);
130 
131  unsigned int getSize() const;
132  void initClick(const vpImage<unsigned char> &I, unsigned int size = 5, const vpColor &color = vpColor::red,
133  unsigned int thickness = 1);
134  void initClick(const vpImage<vpRGBa> &I, unsigned int size = 5, const vpColor &color = vpColor::red,
135  unsigned int thickness = 1);
136 
137  bool isInside(const vpImagePoint &iP, const PointInPolygonMethod &method = PnPolyRayCasting) const;
138 
139  void display(const vpImage<unsigned char> &I, const vpColor &color, unsigned int thickness = 1) const;
140 
146  const std::vector<vpImagePoint> &getCorners() const { return _corners; }
147 
154  inline double getArea() const { return this->_area; }
155 
162  inline vpImagePoint getCenter() const { return this->_center; }
163 
170  inline vpRect getBoundingBox() const { return _bbox; }
171 
172  //###################
173  // Static Functions
174  //###################
175  static bool isInside(const std::vector<vpImagePoint> &roi, const double &i, const double &j,
176  const PointInPolygonMethod &method = PnPolyRayCasting);
177 
178 protected:
179  void init(const std::vector<vpImagePoint> &corners);
180  void init(const std::list<vpImagePoint> &corners);
181  void updateArea();
182  void updateCenter();
183  void updateBoundingBox();
184 
185 protected:
187  std::vector<vpImagePoint> _corners;
192  double _area;
195  bool _goodPoly;
198 
199 private:
200  bool testIntersectionSegments(const vpImagePoint &ip1, const vpImagePoint &ip2, const vpImagePoint &ip3,
201  const vpImagePoint &ip4) const;
202  void precalcValuesPnPoly();
203 
204  std::vector<double> m_PnPolyConstants;
205  std::vector<double> m_PnPolyMultiples;
206 };
207 END_VISP_NAMESPACE
208 #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:146
bool _goodPoly
Definition: vpPolygon.h:195
vpRect getBoundingBox() const
Definition: vpPolygon.h:170
vpImagePoint getCenter() const
Definition: vpPolygon.h:162
vpImagePoint _center
Definition: vpPolygon.h:190
double getArea() const
Definition: vpPolygon.h:154
vpRect _bbox
Bounding box containing the polygon.
Definition: vpPolygon.h:197
PointInPolygonMethod
Definition: vpPolygon.h:106
@ PnPolySegmentIntersection
Definition: vpPolygon.h:107
std::vector< vpImagePoint > _corners
Collection of image points containing the corners.
Definition: vpPolygon.h:187
double _area
Area of the polygon.
Definition: vpPolygon.h:192
Defines a rectangle in the plane.
Definition: vpRect.h:79