is_convex.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Unit Test
  3. // Copyright (c) 2015 Barend Gehrels, Amsterdam, the Netherlands.
  4. // Use, modification and distribution is subject to the Boost Software License,
  5. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. #include <cstddef>
  8. #include <string>
  9. #include <boost/geometry/algorithms/is_convex.hpp>
  10. #include <geometry_test_common.hpp>
  11. #include <boost/geometry/algorithms/correct.hpp>
  12. #include <boost/geometry/io/wkt/wkt.hpp>
  13. #include <boost/geometry/strategies/strategies.hpp>
  14. #include <boost/geometry/geometries/point_xy.hpp>
  15. #include <boost/geometry/geometries/ring.hpp>
  16. template <typename Geometry>
  17. void test_one(std::string const& case_id, std::string const& wkt, bool expected)
  18. {
  19. Geometry geometry;
  20. bg::read_wkt(wkt, geometry);
  21. bg::correct(geometry);
  22. bool detected = bg::is_convex(geometry);
  23. BOOST_CHECK_MESSAGE(detected == expected,
  24. "Not as expected, case: " << case_id
  25. << " / expected: " << expected
  26. << " / detected: " << detected);
  27. }
  28. template <typename P>
  29. void test_all()
  30. {
  31. // rectangular, with concavity
  32. std::string const concave1 = "polygon((1 1, 1 4, 3 4, 3 3, 4 3, 4 4, 5 4, 5 1, 1 1))";
  33. std::string const triangle = "polygon((1 1, 1 4, 5 1, 1 1))";
  34. test_one<bg::model::ring<P> >("triangle", triangle, true);
  35. test_one<bg::model::ring<P> >("concave1", concave1, false);
  36. test_one<bg::model::ring<P, false, false> >("triangle", triangle, true);
  37. test_one<bg::model::ring<P, false, false> >("concave1", concave1, false);
  38. test_one<bg::model::box<P> >("box", "box(0 0,2 2)", true);
  39. }
  40. int test_main(int, char* [])
  41. {
  42. test_all<bg::model::d2::point_xy<int> >();
  43. test_all<bg::model::d2::point_xy<double> >();
  44. return 0;
  45. }