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 ((__cplusplus >= 201402L) || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201402L)))
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 ((__cplusplus >= 201103L) || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201103L)))
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 ((__cplusplus >= 201402L) || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201402L)))
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 ((__cplusplus >= 201103L) || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201103L)))
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()
124 : _corners(), _center(), _area(0.), _goodPoly(true), _bbox(), m_PnPolyConstants(), m_PnPolyMultiples()
126 if (corners.size() < 3) {
141 : _corners(), _center(), _area(0.), _goodPoly(true), _bbox(), m_PnPolyConstants(), m_PnPolyMultiples()
143 if (corners.size() < 3) {
155 : _corners(poly._corners), _center(poly._center), _area(poly._area), _goodPoly(poly._goodPoly), _bbox(poly._bbox),
156 m_PnPolyConstants(poly.m_PnPolyConstants), m_PnPolyMultiples(poly.m_PnPolyMultiples)
176 m_PnPolyConstants = poly.m_PnPolyConstants;
177 m_PnPolyMultiples = poly.m_PnPolyMultiples;
192 if (create_convex_hull) {
193 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC)
194 init(convexHull(corners));
215 if (create_convex_hull) {
216 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC)
217 init(convexHull(corners));
240 const bool create_convex_hull)
242 std::vector<vpImagePoint> ipCorners(corners.size());
243 unsigned int corners_size = 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);
293 std::vector<vpImagePoint> cornersClick;
299 cornersClick.push_back(ip);
324 precalcValuesPnPoly();
344 precalcValuesPnPoly();
367 double denominator = (di1 * dj2) - (dj1 * di2);
369 if (fabs(denominator) < std::numeric_limits<double>::epsilon()) {
373 double alpha = -(((ip1.
get_i() - ip3.
get_i()) * dj2) + (di2 * (ip3.
get_j() - ip1.
get_j()))) / denominator;
374 if ((alpha < 0) || (alpha >= 1)) {
378 double beta = -((di1 * (ip3.
get_j() - ip1.
get_j())) + (dj1 * (ip1.
get_i() - ip3.
get_i()))) / denominator;
379 if ((beta < 0) || (beta >= 1)) {
407 infPoint.
set_i(infPoint.
get_i() + (1000 * generator()));
408 infPoint.
set_j(infPoint.
get_j() + (1000 * generator()));
410 bool oddNbIntersections =
false;
411 unsigned int v_corners_size =
_corners.size();
412 for (
unsigned int i = 0; i < v_corners_size; ++i) {
415 bool intersection =
false;
424 intersection = testIntersectionSegments(ip1, ip2, ip, infPoint);
431 oddNbIntersections = !oddNbIntersections;
435 test = oddNbIntersections;
442 bool oddNodes =
false;
443 size_t v_corners_size =
_corners.size();
444 for (
size_t i = 0, j = (v_corners_size - 1); i < v_corners_size; ++i) {
447 oddNodes ^= (ip.
get_v() * m_PnPolyMultiples[i] + m_PnPolyConstants[i] < ip.
get_u());
461 void vpPolygon::precalcValuesPnPoly()
467 m_PnPolyConstants.resize(
_corners.size());
468 m_PnPolyMultiples.resize(
_corners.size());
470 size_t v_corners_size =
_corners.size();
471 for (
size_t i = 0, j = (v_corners_size - 1); i < v_corners_size; ++i) {
473 m_PnPolyConstants[i] =
_corners[i].get_u();
474 m_PnPolyMultiples[i] = 0.0;
477 m_PnPolyConstants[i] = (
_corners[i].get_u() -
505 unsigned int v_corners_size =
_corners.size();
506 for (
unsigned int i = 0; i < v_corners_size; ++i) {
507 unsigned int i_p_1 = (i + 1) %
_corners.size();
538 for (
unsigned int i = 0; i < (
_corners.size() - 1); ++i) {
546 unsigned int v_corners_size =
_corners.size();
547 for (
unsigned int i = 0; i < v_corners_size; ++i) {
548 unsigned int i_p_1 = (i + 1) %
_corners.size();
581 std::set<double> setI;
582 std::set<double> setJ;
583 unsigned int v_corners_size =
_corners.size();
584 for (
unsigned int i = 0; i < v_corners_size; ++i) {
589 std::set<double>::const_iterator iterI = setI.end();
590 std::set<double>::const_iterator iterJ = setJ.end();
612 const unsigned int N =
static_cast<unsigned int>(
_corners.size());
613 for (
unsigned int i = 0; i < N; ++i) {
641 return (
static_cast<unsigned int>(
_corners.size()));
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.