custom_linestring.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Unit Test
  3. // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
  4. // Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
  5. // Copyright (c) 2009-2012 Mateusz Loskot, London, UK.
  6. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  7. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  8. // Use, modification and distribution is subject to the Boost Software License,
  9. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  10. // http://www.boost.org/LICENSE_1_0.txt)
  11. #include <deque>
  12. #include <vector>
  13. #include <geometry_test_common.hpp>
  14. #include <boost/geometry/algorithms/append.hpp>
  15. #include <boost/geometry/algorithms/clear.hpp>
  16. #include <boost/geometry/algorithms/make.hpp>
  17. #include <boost/geometry/core/access.hpp>
  18. #include <boost/geometry/geometries/concepts/linestring_concept.hpp>
  19. #include <boost/geometry/geometries/geometries.hpp>
  20. #include <boost/geometry/geometries/register/linestring.hpp>
  21. #include <boost/geometry/geometries/adapted/c_array.hpp>
  22. #include <boost/geometry/geometries/adapted/boost_tuple.hpp>
  23. #include <test_common/test_point.hpp>
  24. BOOST_GEOMETRY_REGISTER_C_ARRAY_CS(cs::cartesian)
  25. BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian)
  26. BOOST_GEOMETRY_REGISTER_LINESTRING_TEMPLATED(std::vector)
  27. BOOST_GEOMETRY_REGISTER_LINESTRING_TEMPLATED(std::deque)
  28. //#define TEST_FAIL_CLEAR
  29. //#define TEST_FAIL_APPEND
  30. // ----------------------------------------------------------------------------
  31. // First custom linestring, requires ONLY one traits: to register itself as a linestring
  32. template <typename P>
  33. struct custom_linestring1 : std::vector<P> {};
  34. namespace boost { namespace geometry { namespace traits {
  35. template <typename P>
  36. struct tag< custom_linestring1<P> > { typedef linestring_tag type; };
  37. }}} // namespace bg::traits
  38. // ----------------------------------------------------------------------------
  39. // Second custom linestring, decides to implement all edit operations itself
  40. // by specializing the "use_std" traits to false.
  41. // It should therefore implement the traits:: clear / append_point
  42. template <typename P>
  43. struct custom_linestring2 : std::deque<P> // std::pair<typename std::vector<P>::const_iterator, typename std::vector<P>::const_iterator>
  44. {
  45. };
  46. namespace boost { namespace geometry { namespace traits {
  47. template <typename P>
  48. struct tag< custom_linestring2<P> > { typedef linestring_tag type; };
  49. #if ! defined(TEST_FAIL_CLEAR)
  50. template <typename P>
  51. struct clear< custom_linestring2<P> >
  52. {
  53. // does not use std::vector<P>.clear() but something else.
  54. static inline void apply(custom_linestring2<P>& ls) { ls.resize(0); }
  55. };
  56. #endif
  57. }}} // namespace bg::traits
  58. // ----------------------------------------------------------------------------
  59. template <typename G>
  60. void test_linestring()
  61. {
  62. BOOST_CONCEPT_ASSERT( (bg::concepts::Linestring<G>) );
  63. BOOST_CONCEPT_ASSERT( (bg::concepts::ConstLinestring<G>) );
  64. G geometry;
  65. typedef typename bg::point_type<G>::type P;
  66. bg::clear(geometry);
  67. BOOST_CHECK_EQUAL(boost::size(geometry), 0u);
  68. bg::append(geometry, bg::make_zero<P>());
  69. BOOST_CHECK_EQUAL(boost::size(geometry), 1u);
  70. //std::cout << geometry << std::endl;
  71. bg::clear(geometry);
  72. BOOST_CHECK_EQUAL(boost::size(geometry), 0u);
  73. //P p = boost::range::front(geometry);
  74. }
  75. template <typename P>
  76. void test_all()
  77. {
  78. test_linestring<bg::model::linestring<P> >();
  79. test_linestring<bg::model::linestring<P, std::vector> >();
  80. test_linestring<bg::model::linestring<P, std::deque> >();
  81. test_linestring<custom_linestring1<P> >();
  82. test_linestring<custom_linestring2<P> >();
  83. test_linestring<std::vector<P> >();
  84. test_linestring<std::deque<P> >();
  85. //test_linestring<std::list<P> >();
  86. }
  87. int test_main(int, char* [])
  88. {
  89. test_all<test::test_point>();
  90. test_all<boost::tuple<float, float> >();
  91. test_all<bg::model::point<int, 2, bg::cs::cartesian> >();
  92. test_all<bg::model::point<float, 2, bg::cs::cartesian> >();
  93. test_all<bg::model::point<double, 2, bg::cs::cartesian> >();
  94. return 0;
  95. }