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;
67 #if ((__cplusplus >= 201402L) || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201402L)))
68 std::transform(cbegin(ips), cend(ips), std::back_inserter(cv_pts), [](
const vpImagePoint &ip) {
69 return cv::Point(
static_cast<int>(ip.
get_u()),
static_cast<int>(ip.
get_v()));
72 std::transform(begin(ips), end(ips), std::back_inserter(cv_pts), [](
const vpImagePoint &ip) {
73 return cv::Point(
static_cast<int>(ip.
get_u()),
static_cast<int>(ip.
get_v()));
78 std::vector<cv::Point> cv_conv_hull_corners;
79 cv::convexHull(cv_pts, cv_conv_hull_corners);
82 std::vector<vpImagePoint> conv_hull_corners;
84 #if ((__cplusplus >= 201402L) || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201402L)))
85 std::transform(cbegin(cv_conv_hull_corners), cend(cv_conv_hull_corners), std::back_inserter(conv_hull_corners),
86 [](
const cv::Point &pt) {
87 return vpImagePoint {
static_cast<double>(pt.y),
static_cast<double>(pt.x) };
90 std::transform(begin(cv_conv_hull_corners), end(cv_conv_hull_corners), std::back_inserter(conv_hull_corners),
91 [](
const cv::Point &pt) {
92 return vpImagePoint {
static_cast<double>(pt.y),
static_cast<double>(pt.x) };
96 return conv_hull_corners;
105 : _corners(), _center(), _area(0.), _goodPoly(true), _bbox(), m_PnPolyConstants(), m_PnPolyMultiples()
117 : _corners(), _center(), _area(0.), _goodPoly(true), _bbox(), m_PnPolyConstants(), m_PnPolyMultiples()
119 if (corners.size() < 3) {
134 : _corners(), _center(), _area(0.), _goodPoly(true), _bbox(), m_PnPolyConstants(), m_PnPolyMultiples()
136 if (corners.size() < 3) {
148 : _corners(poly._corners), _center(poly._center), _area(poly._area), _goodPoly(poly._goodPoly), _bbox(poly._bbox),
149 m_PnPolyConstants(poly.m_PnPolyConstants), m_PnPolyMultiples(poly.m_PnPolyMultiples)
169 m_PnPolyConstants = poly.m_PnPolyConstants;
170 m_PnPolyMultiples = poly.m_PnPolyMultiples;
185 if (create_convex_hull) {
186 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC)
187 init(convexHull(corners));
208 if (create_convex_hull) {
209 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC)
210 init(convexHull(corners));
234 const bool create_convex_hull)
236 std::vector<vpImagePoint> ipCorners(corners.size());
237 for (
unsigned int i = 0; i < corners.size(); ++i) {
240 buildFrom(ipCorners, create_convex_hull);
253 unsigned int thickness)
258 std::vector<vpImagePoint> cornersClick;
264 cornersClick.push_back(ip);
287 std::vector<vpImagePoint> cornersClick;
293 cornersClick.push_back(ip);
318 precalcValuesPnPoly();
338 precalcValuesPnPoly();
361 double denominator = di1 * dj2 - dj1 * di2;
363 if (fabs(denominator) < std::numeric_limits<double>::epsilon()) {
367 double alpha = -((ip1.
get_i() - ip3.
get_i()) * dj2 + di2 * (ip3.
get_j() - ip1.
get_j())) / denominator;
368 if (alpha < 0 || alpha >= 1) {
372 double beta = -(di1 * (ip3.
get_j() - ip1.
get_j()) + dj1 * (ip1.
get_i() - ip3.
get_i())) / denominator;
373 if (beta < 0 || beta >= 1) {
399 infPoint.
set_i(infPoint.
get_i() + 1000 * generator());
400 infPoint.
set_j(infPoint.
get_j() + 1000 * generator());
406 bool oddNbIntersections =
false;
407 for (
unsigned int i = 0; i <
_corners.size(); ++i) {
410 bool intersection =
false;
418 intersection = testIntersectionSegments(ip1, ip2, ip, infPoint);
425 oddNbIntersections = !oddNbIntersections;
429 test = oddNbIntersections;
435 bool oddNodes =
false;
439 oddNodes ^= (ip.
get_v() * m_PnPolyMultiples[i] + m_PnPolyConstants[i] < ip.
get_u());
452 void vpPolygon::precalcValuesPnPoly()
458 m_PnPolyConstants.resize(
_corners.size());
459 m_PnPolyMultiples.resize(
_corners.size());
463 m_PnPolyConstants[i] =
_corners[i].get_u();
464 m_PnPolyMultiples[i] = 0.0;
467 m_PnPolyConstants[i] =
_corners[i].get_u() -
495 for (
unsigned int i = 0; i <
_corners.size(); ++i) {
496 unsigned int i_p_1 = (i + 1) %
_corners.size();
527 for (
unsigned int i = 0; i<(
_corners.size()-1); ++i) {
535 for (
unsigned int i = 0; i <
_corners.size(); ++i) {
536 unsigned int i_p_1 = (i + 1) %
_corners.size();
569 std::set<double> setI;
570 std::set<double> setJ;
571 for (
unsigned int i = 0; i <
_corners.size(); ++i) {
576 std::set<double>::const_iterator iterI = setI.end();
577 std::set<double>::const_iterator iterJ = setJ.end();
599 const unsigned int N = (
unsigned int)
_corners.size();
600 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.