test_buffer_svg_per_turn.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Unit Test Helper
  3. // Copyright (c) 2010-2019 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. #ifndef BOOST_GEOMETRY_TEST_BUFFER_SVG_PER_TURN_HPP
  8. #define BOOST_GEOMETRY_TEST_BUFFER_SVG_PER_TURN_HPP
  9. #include <fstream>
  10. #include <vector>
  11. #include "test_buffer_svg.hpp"
  12. #include <boost/ptr_container/ptr_vector.hpp>
  13. template <typename Point>
  14. class save_turns_visitor
  15. {
  16. public :
  17. typedef std::vector<std::pair<Point, int> > vector_type;
  18. template <typename Turns>
  19. inline void get_turns(Turns const& turns)
  20. {
  21. for (typename boost::range_iterator<Turns const>::type it =
  22. boost::begin(turns); it != boost::end(turns); ++it)
  23. {
  24. m_points.push_back(std::make_pair(it->point, it->turn_index));
  25. }
  26. }
  27. template <typename PieceCollection>
  28. inline void apply(PieceCollection const& collection, int phase)
  29. {
  30. if (phase == 0)
  31. {
  32. get_turns(collection.m_turns);
  33. }
  34. }
  35. vector_type const& get_points() { return m_points; }
  36. private :
  37. vector_type m_points;
  38. };
  39. template <typename Point>
  40. class mapper_visitor
  41. {
  42. public :
  43. mapper_visitor(std::string const& complete, int index, Point const& point)
  44. : m_filename(get_filename(complete, index))
  45. , m_svg(m_filename.c_str())
  46. , m_mapper(m_svg, 1000, 800)
  47. , m_visitor(m_mapper)
  48. , m_buffer_mapper(m_filename)
  49. {
  50. box_type box;
  51. double half_size = 75.0; // specific for multi_point buffer
  52. bg::set<bg::min_corner, 0>(box, bg::get<0>(point) - half_size);
  53. bg::set<bg::min_corner, 1>(box, bg::get<1>(point) - half_size);
  54. bg::set<bg::max_corner, 0>(box, bg::get<0>(point) + half_size);
  55. bg::set<bg::max_corner, 1>(box, bg::get<1>(point) + half_size);
  56. m_mapper.add(box);
  57. m_visitor.set_alternate_box(box);
  58. m_buffer_mapper.set_alternate_box(box);
  59. }
  60. // It is used in a ptr vector
  61. virtual ~mapper_visitor()
  62. {
  63. }
  64. template <typename PieceCollection>
  65. inline void apply(PieceCollection const& collection, int phase)
  66. {
  67. m_visitor.apply(collection, phase);
  68. }
  69. template <typename Geometry, typename GeometryBuffer>
  70. void map_input_output(Geometry const& geometry,
  71. GeometryBuffer const& buffered, bool negative)
  72. {
  73. m_buffer_mapper.map_input_output(m_mapper, geometry, buffered, negative);
  74. }
  75. private :
  76. std::string get_filename(std::string const& complete, int index)
  77. {
  78. std::ostringstream filename;
  79. filename << "buffer_per_turn_" << complete << "_" << index << ".svg";
  80. return filename.str();
  81. }
  82. typedef bg::svg_mapper<Point> mapper_type;
  83. typedef bg::model::box<Point> box_type;
  84. std::string m_filename;
  85. std::ofstream m_svg;
  86. mapper_type m_mapper;
  87. svg_visitor<mapper_type, box_type> m_visitor;
  88. buffer_svg_mapper<Point> m_buffer_mapper;
  89. };
  90. template <typename Point>
  91. class per_turn_visitor
  92. {
  93. // Both fstreams and svg mappers are non-copyable,
  94. // therefore we need to use dynamic memory
  95. typedef boost::ptr_vector<mapper_visitor<Point> > container_type;
  96. container_type mappers;
  97. public :
  98. typedef std::pair<Point, int> pair_type;
  99. typedef std::vector<pair_type> vector_type;
  100. per_turn_visitor(std::string const& complete_caseid,
  101. vector_type const& points)
  102. {
  103. namespace bg = boost::geometry;
  104. if (points.size() > 100u)
  105. {
  106. // Defensive check. Too much intersections. Don't create anything
  107. return;
  108. }
  109. BOOST_FOREACH(pair_type const& p, points)
  110. {
  111. mappers.push_back(new mapper_visitor<Point>(complete_caseid, p.second, p.first));
  112. }
  113. }
  114. template <typename PieceCollection>
  115. inline void apply(PieceCollection const& collection, int phase)
  116. {
  117. for(typename container_type::iterator it = mappers.begin();
  118. it != mappers.end(); ++it)
  119. {
  120. it->apply(collection, phase);
  121. }
  122. }
  123. template <typename Geometry, typename GeometryBuffer>
  124. void map_input_output(Geometry const& geometry,
  125. GeometryBuffer const& buffered, bool negative)
  126. {
  127. for(typename container_type::iterator it = mappers.begin();
  128. it != mappers.end(); ++it)
  129. {
  130. it->map_input_output(geometry, buffered, negative);
  131. }
  132. }
  133. };
  134. #endif // BOOST_GEOMETRY_TEST_BUFFER_SVG_PER_TURN_HPP