gl_draw.hpp 7.2 KB

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