gl_draw.hpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. // Boost.Geometry Index
  2. //
  3. // R-tree OpenGL drawing visitor implementation
  4. //
  5. // Copyright (c) 2011-2013 Adam Wulkiewicz, Lodz, Poland.
  6. //
  7. // This file was modified by Oracle on 2019-2021.
  8. // Modifications copyright (c) 2019-2021 Oracle and/or its affiliates.
  9. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  10. //
  11. // Use, modification and distribution is subject to the Boost Software License,
  12. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  13. // http://www.boost.org/LICENSE_1_0.txt)
  14. #ifndef BOOST_GEOMETRY_INDEX_DETAIL_RTREE_UTILITIES_GL_DRAW_HPP
  15. #define BOOST_GEOMETRY_INDEX_DETAIL_RTREE_UTILITIES_GL_DRAW_HPP
  16. #include <limits>
  17. #include <boost/geometry/core/access.hpp>
  18. #include <boost/geometry/core/coordinate_dimension.hpp>
  19. #include <boost/geometry/core/coordinate_type.hpp>
  20. #include <boost/geometry/core/static_assert.hpp>
  21. #include <boost/geometry/core/tag.hpp>
  22. #include <boost/geometry/core/tags.hpp>
  23. #include <boost/geometry/index/detail/rtree/node/node_elements.hpp>
  24. namespace boost { namespace geometry { namespace index { namespace detail {
  25. namespace utilities {
  26. namespace dispatch {
  27. template <typename Point, size_t Dimension>
  28. struct gl_draw_point
  29. {};
  30. template <typename Point>
  31. struct gl_draw_point<Point, 2>
  32. {
  33. static inline void apply(Point const& p, typename coordinate_type<Point>::type z)
  34. {
  35. typename coordinate_type<Point>::type const& x = geometry::get<0>(p);
  36. typename coordinate_type<Point>::type const& y = geometry::get<1>(p);
  37. /*glBegin(GL_POINT);
  38. glVertex3f(x, y, z);
  39. glEnd();*/
  40. glBegin(GL_QUADS);
  41. glVertex3f(x+1, y, z);
  42. glVertex3f(x, y+1, z);
  43. glVertex3f(x-1, y, z);
  44. glVertex3f(x, y-1, z);
  45. glEnd();
  46. }
  47. };
  48. template <typename Box, size_t Dimension>
  49. struct gl_draw_box
  50. {};
  51. template <typename Box>
  52. struct gl_draw_box<Box, 2>
  53. {
  54. static inline void apply(Box const& b, typename coordinate_type<Box>::type z)
  55. {
  56. glBegin(GL_LINE_LOOP);
  57. glVertex3f(geometry::get<min_corner, 0>(b), geometry::get<min_corner, 1>(b), z);
  58. glVertex3f(geometry::get<max_corner, 0>(b), geometry::get<min_corner, 1>(b), z);
  59. glVertex3f(geometry::get<max_corner, 0>(b), geometry::get<max_corner, 1>(b), z);
  60. glVertex3f(geometry::get<min_corner, 0>(b), geometry::get<max_corner, 1>(b), z);
  61. glEnd();
  62. }
  63. };
  64. template <typename Segment, size_t Dimension>
  65. struct gl_draw_segment
  66. {};
  67. template <typename Segment>
  68. struct gl_draw_segment<Segment, 2>
  69. {
  70. static inline void apply(Segment const& s, typename coordinate_type<Segment>::type z)
  71. {
  72. glBegin(GL_LINES);
  73. glVertex3f(geometry::get<0, 0>(s), geometry::get<0, 1>(s), z);
  74. glVertex3f(geometry::get<1, 0>(s), geometry::get<1, 1>(s), z);
  75. glEnd();
  76. }
  77. };
  78. template <typename Indexable, typename Tag>
  79. struct gl_draw_indexable
  80. {
  81. BOOST_GEOMETRY_STATIC_ASSERT_FALSE(
  82. "Not implemented for this Indexable type.",
  83. Indexable, Tag);
  84. };
  85. template <typename Box>
  86. struct gl_draw_indexable<Box, box_tag>
  87. : gl_draw_box<Box, geometry::dimension<Box>::value>
  88. {};
  89. template <typename Point>
  90. struct gl_draw_indexable<Point, point_tag>
  91. : gl_draw_point<Point, geometry::dimension<Point>::value>
  92. {};
  93. template <typename Segment>
  94. struct gl_draw_indexable<Segment, segment_tag>
  95. : gl_draw_segment<Segment, geometry::dimension<Segment>::value>
  96. {};
  97. } // namespace dispatch
  98. template <typename Indexable> inline
  99. void gl_draw_indexable(Indexable const& i, typename coordinate_type<Indexable>::type z)
  100. {
  101. dispatch::gl_draw_indexable<
  102. Indexable,
  103. typename tag<Indexable>::type
  104. >::apply(i, z);
  105. }
  106. } // namespace utilities
  107. namespace rtree { namespace utilities {
  108. namespace visitors {
  109. template <typename MembersHolder>
  110. struct gl_draw
  111. : public MembersHolder::visitor_const
  112. {
  113. typedef typename MembersHolder::box_type box_type;
  114. typedef typename MembersHolder::translator_type translator_type;
  115. typedef typename MembersHolder::internal_node internal_node;
  116. typedef typename MembersHolder::leaf leaf;
  117. inline gl_draw(translator_type const& t,
  118. size_t level_first = 0,
  119. size_t level_last = (std::numeric_limits<size_t>::max)(),
  120. typename coordinate_type<box_type>::type z_coord_level_multiplier = 1
  121. )
  122. : tr(t)
  123. , level_f(level_first)
  124. , level_l(level_last)
  125. , z_mul(z_coord_level_multiplier)
  126. , level(0)
  127. {}
  128. inline void operator()(internal_node const& n)
  129. {
  130. typedef typename rtree::elements_type<internal_node>::type elements_type;
  131. elements_type const& elements = rtree::elements(n);
  132. if ( level_f <= level )
  133. {
  134. size_t level_rel = level - level_f;
  135. if ( level_rel == 0 )
  136. glColor3f(0.75f, 0.0f, 0.0f);
  137. else if ( level_rel == 1 )
  138. glColor3f(0.0f, 0.75f, 0.0f);
  139. else if ( level_rel == 2 )
  140. glColor3f(0.0f, 0.0f, 0.75f);
  141. else if ( level_rel == 3 )
  142. glColor3f(0.75f, 0.75f, 0.0f);
  143. else if ( level_rel == 4 )
  144. glColor3f(0.75f, 0.0f, 0.75f);
  145. else if ( level_rel == 5 )
  146. glColor3f(0.0f, 0.75f, 0.75f);
  147. else
  148. glColor3f(0.5f, 0.5f, 0.5f);
  149. for (typename elements_type::const_iterator it = elements.begin();
  150. it != elements.end(); ++it)
  151. {
  152. detail::utilities::gl_draw_indexable(it->first, level_rel * z_mul);
  153. }
  154. }
  155. size_t level_backup = level;
  156. ++level;
  157. if ( level < level_l )
  158. {
  159. for (typename elements_type::const_iterator it = elements.begin();
  160. it != elements.end(); ++it)
  161. {
  162. rtree::apply_visitor(*this, *it->second);
  163. }
  164. }
  165. level = level_backup;
  166. }
  167. inline void operator()(leaf const& n)
  168. {
  169. typedef typename rtree::elements_type<leaf>::type elements_type;
  170. elements_type const& elements = rtree::elements(n);
  171. if ( level_f <= level )
  172. {
  173. size_t level_rel = level - level_f;
  174. glColor3f(0.25f, 0.25f, 0.25f);
  175. for (typename elements_type::const_iterator it = elements.begin();
  176. it != elements.end(); ++it)
  177. {
  178. detail::utilities::gl_draw_indexable(tr(*it), level_rel * z_mul);
  179. }
  180. }
  181. }
  182. translator_type const& tr;
  183. size_t level_f;
  184. size_t level_l;
  185. typename coordinate_type<box_type>::type z_mul;
  186. size_t level;
  187. };
  188. } // namespace visitors
  189. template <typename Rtree> inline
  190. void gl_draw(Rtree const& tree,
  191. size_t level_first = 0,
  192. size_t level_last = (std::numeric_limits<size_t>::max)(),
  193. typename coordinate_type<
  194. typename Rtree::bounds_type
  195. >::type z_coord_level_multiplier = 1
  196. )
  197. {
  198. typedef utilities::view<Rtree> RTV;
  199. RTV rtv(tree);
  200. if ( !tree.empty() )
  201. {
  202. glColor3f(0.75f, 0.75f, 0.75f);
  203. detail::utilities::gl_draw_indexable(tree.bounds(), 0);
  204. }
  205. visitors::gl_draw<
  206. typename RTV::members_holder
  207. > gl_draw_v(rtv.translator(), level_first, level_last, z_coord_level_multiplier);
  208. rtv.apply_visitor(gl_draw_v);
  209. }
  210. }} // namespace rtree::utilities
  211. }}}} // namespace boost::geometry::index::detail
  212. #endif // BOOST_GEOMETRY_INDEX_DETAIL_RTREE_UTILITIES_GL_DRAW_HPP