Visual Servoing Platform  version 3.6.1 under development (2024-11-21)
testPolygon.cpp
/*
* ViSP, open source Visual Servoing Platform software.
* Copyright (C) 2005 - 2023 by Inria. All rights reserved.
*
* This software is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* See the file LICENSE.txt at the root directory of this source
* distribution for additional information about the GNU GPL.
*
* For using ViSP with software that can not be combined with the GNU
* GPL, please contact Inria about acquiring a ViSP Professional
* Edition License.
*
* See https://visp.inria.fr for more information.
*
* This software was developed at:
* Inria Rennes - Bretagne Atlantique
* Campus Universitaire de Beaulieu
* 35042 Rennes Cedex
* France
*
* If you have questions regarding the use of this file, please contact
* Inria at visp@inria.fr
*
* This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
* WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*
* Description:
* Example which test the polygon.
*/
#include <visp3/core/vpConfig.h>
#include <visp3/core/vpImagePoint.h>
#include <visp3/core/vpPolygon.h>
#include <visp3/io/vpParseArgv.h>
#include <visp3/core/vpDisplay.h>
#include <visp3/gui/vpDisplayGDI.h>
#include <visp3/gui/vpDisplayGTK.h>
#include <visp3/gui/vpDisplayX.h>
#include <math.h>
#include <iostream>
#include <string>
#include <vector>
#define GETOPTARGS "cdm:h"
void usage(const char *name, const char *badparam);
bool getOptions(int argc, const char **argv, bool &opt_display, bool &opt_click, int &method);
void usage(const char *name, const char *badparam)
{
fprintf(stdout, "\n\
test the generic 2D polygons.\n\
\n\
SYNOPSIS\n\
%s [-c] [-d] [-h]\n\
",
name);
fprintf(stdout, "\n\
OPTIONS: \n\
-c \n\
Disable mouse click.\n\
\n\
-d \n\
Turn off display.\n\
\n\
-m \n\
Point in polygon test method.\n\
\n\
-h\n\
Print the help.\n\n");
if (badparam) {
fprintf(stderr, "ERROR: \n");
fprintf(stderr, "\nBad parameter [%s]\n", badparam);
}
}
bool getOptions(int argc, const char **argv, bool &opt_display, bool &opt_click, int &method)
{
#ifdef ENABLE_VISP_NAMESPACE
using namespace VISP_NAMESPACE_NAME;
#endif
const char *optarg_;
int c;
while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
switch (c) {
case 'c':
opt_click = false;
break;
case 'd':
opt_display = false;
break;
case 'm':
method = atoi(optarg_);
break;
case 'h':
usage(argv[0], nullptr);
return false;
break;
default:
usage(argv[0], optarg_);
return false;
break;
}
}
if ((c == 1) || (c == -1)) {
// standalone param or error
usage(argv[0], nullptr);
std::cerr << "ERROR: " << std::endl;
std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
return false;
}
return true;
}
/* --------------------------------------------------------------------------
*/
/* MAIN FUNCTION */
/* --------------------------------------------------------------------------
*/
int main(int argc, const char **argv)
{
#ifdef ENABLE_VISP_NAMESPACE
using namespace VISP_NAMESPACE_NAME;
#endif
try {
bool opt_display = true;
bool opt_click = true;
vpImage<unsigned char> I(480, 640, 255);
// Read the command line options
if (getOptions(argc, argv, opt_display, opt_click, method) == false) {
return EXIT_FAILURE;
}
std::vector<vpImagePoint> vec1;
vec1.push_back(vpImagePoint(200, 200));
vec1.push_back(vpImagePoint(200, 400));
vec1.push_back(vpImagePoint(320, 400));
vec1.push_back(vpImagePoint(380, 300));
vec1.push_back(vpImagePoint(280, 280));
p1.buildFrom(vec1);
std::vector<vpImagePoint> vec2;
vec2.push_back(vpImagePoint(20, 20));
vec2.push_back(vpImagePoint(100, 20));
vec2.push_back(vpImagePoint(100, 100));
vec2.push_back(vpImagePoint(20, 100));
vpPolygon p2(vec2);
std::vector<vpImagePoint> vec3;
vpPolygon p3(vec3);
#if defined(VISP_HAVE_X11)
vpDisplayX display;
#elif defined(VISP_HAVE_GTK)
vpDisplayGTK display;
#elif defined(VISP_HAVE_GDI)
vpDisplayGDI display;
#else
opt_display = false;
#endif
std::cout << " Polygon 1 : " << std::endl;
std::cout << " area : " << p1.getArea() << std::endl;
std::cout << " center : " << p1.getCenter() << std::endl << std::endl;
std::cout << " Polygon 2 : " << std::endl;
std::cout << " area : " << p2.getArea() << std::endl;
std::cout << " center : " << p2.getCenter() << std::endl << std::endl;
std::cout << " Polygon 3 : " << std::endl;
std::cout << " area : " << p3.getArea() << std::endl;
std::cout << " center : " << p3.getCenter() << std::endl;
if (opt_display) {
#if (defined VISP_HAVE_X11) || (defined VISP_HAVE_GTK) || (defined VISP_HAVE_GDI)
display.init(I, 10, 10, "Test vpPolygon");
#endif
p2.display(I, vpColor::red, 1);
vpDisplay::displayCross(I, p2.getCenter(), 5, vpColor::red);
p3.display(I, vpColor::blue, 1);
vpDisplay::displayText(I, vpImagePoint(10, 10), "Click to finish", vpColor::red);
if (opt_click)
vpDisplay::displayText(I, vpImagePoint(10, 10), "Left click to add a point", vpColor::red);
vpDisplay::displayText(I, vpImagePoint(20, 10), "Right click to build the polygon", vpColor::red);
if (opt_click) {
p4.initClick(I);
std::cout << std::endl;
std::cout << " Polygon 4 : " << std::endl;
std::cout << " area : " << p4.getArea() << std::endl;
std::cout << " center : " << p4.getCenter() << std::endl;
std::cout << "Click to continue." << std::endl;
vpRect bbox = p4.getBoundingBox();
for (unsigned int i = (unsigned int)floor(bbox.getTop()); i < (unsigned int)ceil(bbox.getBottom()); ++i) {
for (unsigned int j = (unsigned int)floor(bbox.getLeft()); j < (unsigned int)ceil(bbox.getRight()); ++j) {
}
}
}
std::cout << "Click to continue." << std::endl;
for (unsigned int i = 0; i < I.getHeight(); ++i) {
for (unsigned int j = 0; j < I.getWidth(); ++j) {
}
}
}
std::cout << "Click to finish." << std::endl;
// Benchmark Point In Polygon test method
std::vector<vpImagePoint> corners = p4.getCorners();
std::cout << "Nb polygon corners=" << corners.size() << std::endl;
vpPolygon polygon_benchmark(corners);
vpImage<unsigned char> I_segmentIntersection(480, 640, 0);
vpImage<unsigned char> I_rayCasting(480, 640, 0);
double t_benchmark = vpTime::measureTimeMs();
for (unsigned int i = 0; i < I_segmentIntersection.getHeight(); i++) {
for (unsigned int j = 0; j < I_segmentIntersection.getWidth(); j++) {
if (polygon_benchmark.isInside(vpImagePoint(i, j), vpPolygon::PnPolySegmentIntersection)) {
I_segmentIntersection[i][j] = 255;
}
}
}
t_benchmark = vpTime::measureTimeMs() - t_benchmark;
std::cout << "PnPolySegmentIntersection: " << t_benchmark << " ms" << std::endl;
t_benchmark = vpTime::measureTimeMs();
for (unsigned int i = 0; i < I_rayCasting.getHeight(); i++) {
for (unsigned int j = 0; j < I_rayCasting.getWidth(); j++) {
if (polygon_benchmark.isInside(vpImagePoint(i, j), vpPolygon::PnPolyRayCasting)) {
I_rayCasting[i][j] = 255;
}
}
}
t_benchmark = vpTime::measureTimeMs() - t_benchmark;
std::cout << "PnPolyRayCasting: " << t_benchmark << " ms" << std::endl;
#if defined(VISP_HAVE_X11)
vpDisplayX display1, display2;
#elif defined(VISP_HAVE_GTK)
vpDisplayGTK display1, display2;
#elif defined(VISP_HAVE_GDI)
vpDisplayGDI display1, display2;
#endif
#if (defined VISP_HAVE_X11) || (defined VISP_HAVE_GTK) || (defined VISP_HAVE_GDI)
display1.init(I_segmentIntersection, 10, 10, "Segment Intersection test");
display2.init(I_rayCasting, (int)I_segmentIntersection.getWidth() + 10, 10, "Ray Casting test");
#endif
vpDisplay::display(I_segmentIntersection);
vpDisplay::display(I_rayCasting);
vpDisplay::displayText(I_rayCasting, 20, 20, "Click to quit.", vpColor::red);
vpDisplay::flush(I_segmentIntersection);
vpDisplay::flush(I_rayCasting);
vpDisplay::getClick(I_rayCasting);
}
}
return EXIT_SUCCESS;
}
catch (const vpException &e) {
std::cout << "Catch an exception: " << e << std::endl;
return EXIT_FAILURE;
}
}
static const vpColor red
Definition: vpColor.h:217
static const vpColor orange
Definition: vpColor.h:227
static const vpColor blue
Definition: vpColor.h:223
static const vpColor lightBlue
Definition: vpColor.h:222
static const vpColor green
Definition: vpColor.h:220
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:130
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:133
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="") VP_OVERRIDE
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
static void displayCross(const vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
static void close(vpImage< unsigned char > &I)
static void flush(const vpImage< unsigned char > &I)
static void displayPoint(const vpImage< unsigned char > &I, const vpImagePoint &ip, const vpColor &color, unsigned int thickness=1)
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
error that can be emitted by ViSP classes.
Definition: vpException.h:60
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:82
unsigned int getWidth() const
Definition: vpImage.h:242
unsigned int getHeight() const
Definition: vpImage.h:181
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:70
Defines a generic 2D polygon.
Definition: vpPolygon.h:103
const std::vector< vpImagePoint > & getCorners() const
Definition: vpPolygon.h:140
void display(const vpImage< unsigned char > &I, const vpColor &color, unsigned int thickness=1) const
Definition: vpPolygon.cpp:610
vpRect getBoundingBox() const
Definition: vpPolygon.h:164
vpPolygon & buildFrom(const std::vector< vpImagePoint > &corners, const bool &create_convex_hull=false)
Definition: vpPolygon.cpp:193
vpImagePoint getCenter() const
Definition: vpPolygon.h:156
double getArea() const
Definition: vpPolygon.h:148
PointInPolygonMethod
Definition: vpPolygon.h:106
@ PnPolyRayCasting
Definition: vpPolygon.h:108
@ PnPolySegmentIntersection
Definition: vpPolygon.h:107
void initClick(const vpImage< unsigned char > &I, unsigned int size=5, const vpColor &color=vpColor::red, unsigned int thickness=1)
Definition: vpPolygon.cpp:265
bool isInside(const vpImagePoint &iP, const PointInPolygonMethod &method=PnPolyRayCasting) const
Definition: vpPolygon.cpp:400
Defines a rectangle in the plane.
Definition: vpRect.h:79
double getLeft() const
Definition: vpRect.h:173
double getRight() const
Definition: vpRect.h:179
double getBottom() const
Definition: vpRect.h:97
double getTop() const
Definition: vpRect.h:192
VISP_EXPORT double measureTimeMs()