gl_draw.hpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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.
  8. // Modifications copyright (c) 2019 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 <boost/mpl/assert.hpp>
  17. namespace boost { namespace geometry { namespace index { namespace detail {
  18. namespace utilities {
  19. namespace dispatch {
  20. template <typename Point, size_t Dimension>
  21. struct gl_draw_point
  22. {};
  23. template <typename Point>
  24. struct gl_draw_point<Point, 2>
  25. {
  26. static inline void apply(Point const& p, typename coordinate_type<Point>::type z)
  27. {
  28. typename coordinate_type<Point>::type const& x = geometry::get<0>(p);
  29. typename coordinate_type<Point>::type const& y = geometry::get<1>(p);
  30. /*glBegin(GL_POINT);
  31. glVertex3f(x, y, z);
  32. glEnd();*/
  33. glBegin(GL_QUADS);
  34. glVertex3f(x+1, y, z);
  35. glVertex3f(x, y+1, z);
  36. glVertex3f(x-1, y, z);
  37. glVertex3f(x, y-1, z);
  38. glEnd();
  39. }
  40. };
  41. template <typename Box, size_t Dimension>
  42. struct gl_draw_box
  43. {};
  44. template <typename Box>
  45. struct gl_draw_box<Box, 2>
  46. {
  47. static inline void apply(Box const& b, typename coordinate_type<Box>::type z)
  48. {
  49. glBegin(GL_LINE_LOOP);
  50. glVertex3f(geometry::get<min_corner, 0>(b), geometry::get<min_corner, 1>(b), z);
  51. glVertex3f(geometry::get<max_corner, 0>(b), geometry::get<min_corner, 1>(b), z);
  52. glVertex3f(geometry::get<max_corner, 0>(b), geometry::get<max_corner, 1>(b), z);
  53. glVertex3f(geometry::get<min_corner, 0>(b), geometry::get<max_corner, 1>(b), z);
  54. glEnd();
  55. }
  56. };
  57. template <typename Segment, size_t Dimension>
  58. struct gl_draw_segment
  59. {};
  60. template <typename Segment>
  61. struct gl_draw_segment<Segment, 2>
  62. {
  63. static inline void apply(Segment const& s, typename coordinate_type<Segment>::type z)
  64. {
  65. glBegin(GL_LINES);
  66. glVertex3f(geometry::get<0, 0>(s), geometry::get<0, 1>(s), z);
  67. glVertex3f(geometry::get<1, 0>(s), geometry::get<1, 1>(s), z);
  68. glEnd();
  69. }
  70. };
  71. template <typename Indexable, typename Tag>
  72. struct gl_draw_indexable
  73. {
  74. BOOST_MPL_ASSERT_MSG((false), NOT_IMPLEMENTED_FOR_THIS_TAG, (Tag));
  75. };
  76. template <typename Box>
  77. struct gl_draw_indexable<Box, box_tag>
  78. : gl_draw_box<Box, geometry::dimension<Box>::value>
  79. {};
  80. template <typename Point>
  81. struct gl_draw_indexable<Point, point_tag>
  82. : gl_draw_point<Point, geometry::dimension<Point>::value>
  83. {};
  84. template <typename Segment>
  85. struct gl_draw_indexable<Segment, segment_tag>
  86. : gl_draw_segment<Segment, geometry::dimension<Segment>::value>
  87. {};
  88. } // namespace dispatch
  89. template <typename Indexable> inline
  90. void gl_draw_indexable(Indexable const& i, typename coordinate_type<Indexable>::type z)
  91. {
  92. dispatch::gl_draw_indexable<
  93. Indexable,
  94. typename tag<Indexable>::type
  95. >::apply(i, z);
  96. }
  97. } // namespace utilities
  98. namespace rtree { namespace utilities {
  99. namespace visitors {
  100. template <typename MembersHolder>
  101. struct gl_draw
  102. : public MembersHolder::visitor_const
  103. {
  104. typedef typename MembersHolder::box_type box_type;
  105. typedef typename MembersHolder::translator_type translator_type;
  106. typedef typename MembersHolder::internal_node internal_node;
  107. typedef typename MembersHolder::leaf leaf;
  108. inline gl_draw(translator_type const& t,
  109. size_t level_first = 0,
  110. size_t level_last = (std::numeric_limits<size_t>::max)(),
  111. typename coordinate_type<box_type>::type z_coord_level_multiplier = 1
  112. )
  113. : tr(t)
  114. , level_f(level_first)
  115. , level_l(level_last)
  116. , z_mul(z_coord_level_multiplier)
  117. , level(0)
  118. {}
  119. inline void operator()(internal_node const& n)
  120. {
  121. typedef typename rtree::elements_type<internal_node>::type elements_type;
  122. elements_type const& elements = rtree::elements(n);
  123. if ( level_f <= level )
  124. {
  125. size_t level_rel = level - level_f;
  126. if ( level_rel == 0 )
  127. glColor3f(0.75f, 0.0f, 0.0f);
  128. else if ( level_rel == 1 )
  129. glColor3f(0.0f, 0.75f, 0.0f);
  130. else if ( level_rel == 2 )
  131. glColor3f(0.0f, 0.0f, 0.75f);
  132. else if ( level_rel == 3 )
  133. glColor3f(0.75f, 0.75f, 0.0f);
  134. else if ( level_rel == 4 )
  135. glColor3f(0.75f, 0.0f, 0.75f);
  136. else if ( level_rel == 5 )
  137. glColor3f(0.0f, 0.75f, 0.75f);
  138. else
  139. glColor3f(0.5f, 0.5f, 0.5f);
  140. for (typename elements_type::const_iterator it = elements.begin();
  141. it != elements.end(); ++it)
  142. {
  143. detail::utilities::gl_draw_indexable(it->first, level_rel * z_mul);
  144. }
  145. }
  146. size_t level_backup = level;
  147. ++level;
  148. if ( level < level_l )
  149. {
  150. for (typename elements_type::const_iterator it = elements.begin();
  151. it != elements.end(); ++it)
  152. {
  153. rtree::apply_visitor(*this, *it->second);
  154. }
  155. }
  156. level = level_backup;
  157. }
  158. inline void operator()(leaf const& n)
  159. {
  160. typedef typename rtree::elements_type<leaf>::type elements_type;
  161. elements_type const& elements = rtree::elements(n);
  162. if ( level_f <= level )
  163. {
  164. size_t level_rel = level - level_f;
  165. glColor3f(0.25f, 0.25f, 0.25f);
  166. for (typename elements_type::const_iterator it = elements.begin();
  167. it != elements.end(); ++it)
  168. {
  169. detail::utilities::gl_draw_indexable(tr(*it), level_rel * z_mul);
  170. }
  171. }
  172. }
  173. translator_type const& tr;
  174. size_t level_f;
  175. size_t level_l;
  176. typename coordinate_type<box_type>::type z_mul;
  177. size_t level;
  178. };
  179. } // namespace visitors
  180. template <typename Rtree> inline
  181. void gl_draw(Rtree const& tree,
  182. size_t level_first = 0,
  183. size_t level_last = (std::numeric_limits<size_t>::max)(),
  184. typename coordinate_type<
  185. typename Rtree::bounds_type
  186. >::type z_coord_level_multiplier = 1
  187. )
  188. {
  189. typedef utilities::view<Rtree> RTV;
  190. RTV rtv(tree);
  191. if ( !tree.empty() )
  192. {
  193. glColor3f(0.75f, 0.75f, 0.75f);
  194. detail::utilities::gl_draw_indexable(tree.bounds(), 0);
  195. }
  196. visitors::gl_draw<
  197. typename RTV::members_holder
  198. > gl_draw_v(rtv.translator(), level_first, level_last, z_coord_level_multiplier);
  199. rtv.apply_visitor(gl_draw_v);
  200. }
  201. }} // namespace rtree::utilities
  202. }}}} // namespace boost::geometry::index::detail
  203. #endif // BOOST_GEOMETRY_INDEX_DETAIL_RTREE_UTILITIES_GL_DRAW_HPP