41 #include <visp3/core/vpDisplay.h>
42 #include <visp3/core/vpException.h>
43 #include <visp3/core/vpMeterPixelConversion.h>
44 #include <visp3/core/vpPolygon.h>
45 #include <visp3/core/vpUniRand.h>
48 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC)
50 #include <opencv2/imgproc/imgproc.hpp>
57 template <
typename IpContainer> std::vector<vpImagePoint> convexHull(
const IpContainer &ips)
59 if (ips.size() == 0) {
61 "Convex Hull can not be computed as the input does not contain any image point.");
65 std::vector<cv::Point> cv_pts;
66 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_14)
67 std::transform(cbegin(ips), cend(ips), std::back_inserter(cv_pts), [](
const vpImagePoint &ip) {
68 return cv::Point(
static_cast<int>(ip.
get_u()),
static_cast<int>(ip.
get_v()));
70 #elif (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
71 std::transform(begin(ips), end(ips), std::back_inserter(cv_pts), [](
const vpImagePoint &ip) {
72 return cv::Point(
static_cast<int>(ip.
get_u()),
static_cast<int>(ip.
get_v()));
75 for (
typename IpContainer::const_iterator it = ips.begin(); it != ips.end(); ++it) {
76 cv_pts.push_back(cv::Point(
static_cast<int>(it->get_u()),
static_cast<int>(it->get_v())));
81 std::vector<cv::Point> cv_conv_hull_corners;
82 cv::convexHull(cv_pts, cv_conv_hull_corners);
85 std::vector<vpImagePoint> conv_hull_corners;
86 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_14)
87 std::transform(cbegin(cv_conv_hull_corners), cend(cv_conv_hull_corners), std::back_inserter(conv_hull_corners),
88 [](
const cv::Point &pt) {
89 return vpImagePoint{
static_cast<double>(pt.y),
static_cast<double>(pt.x)};
91 #elif (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
92 std::transform(begin(cv_conv_hull_corners), end(cv_conv_hull_corners), std::back_inserter(conv_hull_corners),
93 [](
const cv::Point &pt) {
94 return vpImagePoint{
static_cast<double>(pt.y),
static_cast<double>(pt.x)};
97 for (std::vector<cv::Point>::const_iterator it = cv_conv_hull_corners.begin(); it != cv_conv_hull_corners.end();
99 conv_hull_corners.push_back(
vpImagePoint(
static_cast<double>(it->y),
static_cast<double>(it->x)));
103 return conv_hull_corners;
112 : _corners(), _center(), _area(0.), _goodPoly(true), _bbox(), m_PnPolyConstants(), m_PnPolyMultiples()
125 : _corners(), _center(), _area(0.), _goodPoly(true), _bbox(), m_PnPolyConstants(), m_PnPolyMultiples()
127 if (corners.size() < 3) {
142 : _corners(), _center(), _area(0.), _goodPoly(true), _bbox(), m_PnPolyConstants(), m_PnPolyMultiples()
144 if (corners.size() < 3) {
156 : _corners(poly._corners), _center(poly._center), _area(poly._area), _goodPoly(poly._goodPoly), _bbox(poly._bbox),
157 m_PnPolyConstants(poly.m_PnPolyConstants), m_PnPolyMultiples(poly.m_PnPolyMultiples)
178 m_PnPolyConstants = poly.m_PnPolyConstants;
179 m_PnPolyMultiples = poly.m_PnPolyMultiples;
194 if (create_convex_hull) {
195 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC)
196 init(convexHull(corners));
216 if (create_convex_hull) {
217 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC)
218 init(convexHull(corners));
241 const bool create_convex_hull)
243 std::vector<vpImagePoint> ipCorners(corners.size());
244 for (
unsigned int i = 0; i < corners.size(); ++i) {
247 buildFrom(ipCorners, create_convex_hull);
260 unsigned int thickness)
265 std::vector<vpImagePoint> cornersClick;
271 cornersClick.push_back(ip);
294 std::vector<vpImagePoint> cornersClick;
300 cornersClick.push_back(ip);
325 precalcValuesPnPoly();
345 precalcValuesPnPoly();
368 double denominator = di1 * dj2 - dj1 * di2;
370 if (fabs(denominator) < std::numeric_limits<double>::epsilon()) {
374 double alpha = -((ip1.
get_i() - ip3.
get_i()) * dj2 + di2 * (ip3.
get_j() - ip1.
get_j())) / denominator;
375 if (alpha < 0 || alpha >= 1) {
379 double beta = -(di1 * (ip3.
get_j() - ip1.
get_j()) + dj1 * (ip1.
get_i() - ip3.
get_i())) / denominator;
380 if (beta < 0 || beta >= 1) {
406 infPoint.
set_i(infPoint.
get_i() + 1000 * generator());
407 infPoint.
set_j(infPoint.
get_j() + 1000 * generator());
413 bool oddNbIntersections =
false;
414 for (
unsigned int i = 0; i <
_corners.size(); ++i) {
417 bool intersection =
false;
425 intersection = testIntersectionSegments(ip1, ip2, ip, infPoint);
431 oddNbIntersections = !oddNbIntersections;
435 test = oddNbIntersections;
441 bool oddNodes =
false;
445 oddNodes ^= (ip.
get_v() * m_PnPolyMultiples[i] + m_PnPolyConstants[i] < ip.
get_u());
458 void vpPolygon::precalcValuesPnPoly()
464 m_PnPolyConstants.resize(
_corners.size());
465 m_PnPolyMultiples.resize(
_corners.size());
469 m_PnPolyConstants[i] =
_corners[i].get_u();
470 m_PnPolyMultiples[i] = 0.0;
472 m_PnPolyConstants[i] =
_corners[i].get_u() -
500 for (
unsigned int i = 0; i <
_corners.size(); ++i) {
501 unsigned int i_p_1 = (i + 1) %
_corners.size();
532 for(
unsigned int i=0; i<(
_corners.size()-1); ++i){
540 for (
unsigned int i = 0; i <
_corners.size(); ++i) {
541 unsigned int i_p_1 = (i + 1) %
_corners.size();
573 std::set<double> setI;
574 std::set<double> setJ;
575 for (
unsigned int i = 0; i <
_corners.size(); ++i) {
580 std::set<double>::const_iterator iterI = setI.end();
581 std::set<double>::const_iterator iterJ = setJ.end();
603 const unsigned int N = (
unsigned int)
_corners.size();
604 for (
unsigned int i = 0; i < N; ++i) {
Generic class defining intrinsic camera parameters.
Class to define RGB colors available for display functionalities.
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void displayLine(const vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1, bool segment=true)
static void displayCross(const vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
static void flush(const vpImage< unsigned char > &I)
error that can be emitted by ViSP classes.
@ badValue
Used to indicate that a value is not in the allowed range.
@ notImplementedError
Not implemented.
@ divideByZeroError
Division by zero.
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
static bool equal(double x, double y, double threshold=0.001)
static void convertPoint(const vpCameraParameters &cam, const double &x, const double &y, double &u, double &v)
Defines a generic 2D polygon.
void display(const vpImage< unsigned char > &I, const vpColor &color, unsigned int thickness=1) const
void init(const std::vector< vpImagePoint > &corners)
vpRect _bbox
Bounding box containing the polygon.
@ PnPolySegmentIntersection
void buildFrom(const std::vector< vpImagePoint > &corners, const bool create_convex_hull=false)
unsigned int getSize() const
std::vector< vpImagePoint > _corners
Collection of image points containing the corners.
vpPolygon & operator=(const vpPolygon &poly)
double _area
Area of the polygon.
void initClick(const vpImage< unsigned char > &I, unsigned int size=5, const vpColor &color=vpColor::red, unsigned int thickness=1)
bool isInside(const vpImagePoint &iP, const PointInPolygonMethod &method=PnPolyRayCasting) const
void setBottomRight(const vpImagePoint &bottomRight)
void setTopLeft(const vpImagePoint &topLeft)
Class for generating random numbers with uniform probability density.