Visual Servoing Platform version 3.6.0
Loading...
Searching...
No Matches
testPolygon2.cpp
1/****************************************************************************
2 *
3 * ViSP, open source Visual Servoing Platform software.
4 * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
5 *
6 * This software is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 * See the file LICENSE.txt at the root directory of this source
11 * distribution for additional information about the GNU GPL.
12 *
13 * For using ViSP with software that can not be combined with the GNU
14 * GPL, please contact Inria about acquiring a ViSP Professional
15 * Edition License.
16 *
17 * See https://visp.inria.fr for more information.
18 *
19 * This software was developed at:
20 * Inria Rennes - Bretagne Atlantique
21 * Campus Universitaire de Beaulieu
22 * 35042 Rennes Cedex
23 * France
24 *
25 * If you have questions regarding the use of this file, please contact
26 * Inria at visp@inria.fr
27 *
28 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30 *
31 * Description:
32 * Test vpPolygon class.
33 *
34 * Authors:
35 * Julien Dufour
36 *
37*****************************************************************************/
43#include <visp3/core/vpPolygon.h>
44
45// Core
46#include <visp3/core/vpConfig.h>
47#include <visp3/core/vpRect.h>
48
49#ifdef VISP_HAVE_CATCH2
50#define CATCH_CONFIG_RUNNER
51#include <catch.hpp>
52
53#ifdef VISP_HAVE_OPENCV
54TEST_CASE("Check OpenCV-bsed convex hull")
55{
56 SECTION("From vpRect")
57 {
58 const vpRect rect{0, 0, 200, 400};
59 const std::vector<vpImagePoint> rect_corners{rect.getTopLeft(), rect.getTopRight(), rect.getBottomRight(),
60 rect.getBottomLeft()};
61
62 vpPolygon poly{};
63 poly.buildFrom(rect_corners, true);
64
65#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_14)
66 for (const auto &poly_corner : poly.getCorners()) {
67 REQUIRE(std::find(cbegin(rect_corners), cend(rect_corners), poly_corner) != cend(rect_corners));
68 }
69#elif (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
70 for (const auto &poly_corner : poly.getCorners()) {
71 REQUIRE(std::find(begin(rect_corners), end(rect_corners), poly_corner) != end(rect_corners));
72 }
73#else
74 for (std::vector<vpImagePoint>::const_iterator it = poly.getCorners().begin(); it != poly.getCorners().end();
75 ++it) {
76 REQUIRE(std::find(rect_corners.begin(), rect_corners.end(), *it) != rect_corners.end());
77 }
78#endif
79 }
80}
81#endif
82
83int main(int argc, char *argv[])
84{
85 Catch::Session session;
86 session.applyCommandLine(argc, argv);
87
88 return session.run();
89}
90#else
91// Fallback to classic tests
92
93bool testConvexHull()
94{
95#ifdef VISP_HAVE_OPENCV
96 const vpRect rect(0, 0, 200, 400);
97 std::vector<vpImagePoint> rect_corners;
98 rect_corners.push_back(rect.getTopLeft());
99 rect_corners.push_back(rect.getTopRight());
100 rect_corners.push_back(rect.getBottomRight());
101 rect_corners.push_back(rect.getBottomLeft());
102
103 vpPolygon poly;
104 poly.buildFrom(rect_corners, true);
105
106#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_14)
107 for (const auto &poly_corner : poly.getCorners()) {
108 if (std::find(cbegin(rect_corners), cend(rect_corners), poly_corner) == cend(rect_corners)) {
109 return false;
110 }
111 }
112#elif (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
113 for (const auto &poly_corner : poly.getCorners()) {
114 if (std::find(begin(rect_corners), end(rect_corners), poly_corner) == end(rect_corners)) {
115 return false;
116 }
117 }
118#else
119 for (std::vector<vpImagePoint>::const_iterator it = poly.getCorners().begin(); it != poly.getCorners().end(); ++it) {
120 if (std::find(rect_corners.begin(), rect_corners.end(), *it) == rect_corners.end()) {
121 return false;
122 }
123 }
124#endif
125
126#endif
127
128 return true;
129}
130
131int main()
132{
133 if (! testConvexHull()) {
134 return EXIT_FAILURE;
135 }
136
137 return EXIT_SUCCESS;
138}
139#endif
Defines a generic 2D polygon.
Definition vpPolygon.h:97
const std::vector< vpImagePoint > & getCorners() const
Definition vpPolygon.h:147
void buildFrom(const std::vector< vpImagePoint > &corners, const bool create_convex_hull=false)
Defines a rectangle in the plane.
Definition vpRect.h:76
vpImagePoint getTopLeft() const
Definition vpRect.h:196