Visual Servoing Platform  version 3.6.1 under development (2024-05-16)
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
54 TEST_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  // Check if std:c++14 or higher
66 #if ((__cplusplus >= 201402L) || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201402L)))
67  for (const auto &poly_corner : poly.getCorners()) {
68  REQUIRE(std::find(cbegin(rect_corners), cend(rect_corners), poly_corner) != cend(rect_corners));
69  }
70 #else
71  for (const auto &poly_corner : poly.getCorners()) {
72  REQUIRE(std::find(begin(rect_corners), end(rect_corners), poly_corner) != end(rect_corners));
73  }
74 #endif
75  }
76 }
77 #endif
78 
79 int main(int argc, char *argv[])
80 {
81  Catch::Session session;
82  session.applyCommandLine(argc, argv);
83 
84  return session.run();
85 }
86 #else
87 // Fallback to classic tests
88 
89 bool testConvexHull()
90 {
91 #ifdef VISP_HAVE_OPENCV
92  const vpRect rect(0, 0, 200, 400);
93  std::vector<vpImagePoint> rect_corners;
94  rect_corners.push_back(rect.getTopLeft());
95  rect_corners.push_back(rect.getTopRight());
96  rect_corners.push_back(rect.getBottomRight());
97  rect_corners.push_back(rect.getBottomLeft());
98 
99  vpPolygon poly;
100  poly.buildFrom(rect_corners, true);
101 
102  // Check if std:c++14 or higher
103 #if ((__cplusplus >= 201402L) || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201402L)))
104  for (const auto &poly_corner : poly.getCorners()) {
105  if (std::find(cbegin(rect_corners), cend(rect_corners), poly_corner) == cend(rect_corners)) {
106  return false;
107  }
108  }
109 #else
110  for (const auto &poly_corner : poly.getCorners()) {
111  if (std::find(begin(rect_corners), end(rect_corners), poly_corner) == end(rect_corners)) {
112  return false;
113  }
114  }
115 #endif
116 
117 #endif
118 
119  return true;
120 }
121 
122 int main()
123 {
124  if (!testConvexHull()) {
125  return EXIT_FAILURE;
126  }
127 
128  return EXIT_SUCCESS;
129 }
130 #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)
Definition: vpPolygon.cpp:190
Defines a rectangle in the plane.
Definition: vpRect.h:76
vpImagePoint getTopLeft() const
Definition: vpRect.h:196