gtl_custom_polygon_set.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. Copyright 2008 Intel Corporation
  3. Use, modification and distribution are subject to the Boost Software License,
  4. Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. http://www.boost.org/LICENSE_1_0.txt).
  6. */
  7. #include <boost/polygon/polygon.hpp>
  8. #include <list>
  9. #include <time.h>
  10. #include <cassert>
  11. #include <deque>
  12. #include <iostream>
  13. namespace gtl = boost::polygon;
  14. using namespace boost::polygon::operators;
  15. //once again we make our usage of the library generic
  16. //and parameterize it on the polygon set type
  17. template <typename PolygonSet>
  18. void test_polygon_set() {
  19. using namespace gtl;
  20. PolygonSet ps;
  21. ps += rectangle_data<int>(0, 0, 10, 10);
  22. PolygonSet ps2;
  23. ps2 += rectangle_data<int>(5, 5, 15, 15);
  24. PolygonSet ps3;
  25. assign(ps3, ps * ps2);
  26. PolygonSet ps4;
  27. ps4 += ps + ps2;
  28. assert(area(ps4) == area(ps) + area(ps2) - area(ps3));
  29. assert(equivalence((ps + ps2) - (ps * ps2), ps ^ ps2));
  30. rectangle_data<int> rect;
  31. assert(extents(rect, ps ^ ps2));
  32. assert(area(rect) == 225);
  33. assert(area(rect ^ (ps ^ ps2)) == area(rect) - area(ps ^ ps2));
  34. }
  35. //first thing is first, lets include all the code from previous examples
  36. //the CPoint example
  37. struct CPoint {
  38. int x;
  39. int y;
  40. };
  41. namespace boost { namespace polygon {
  42. template <>
  43. struct geometry_concept<CPoint> { typedef point_concept type; };
  44. template <>
  45. struct point_traits<CPoint> {
  46. typedef int coordinate_type;
  47. static inline coordinate_type get(const CPoint& point,
  48. orientation_2d orient) {
  49. if(orient == HORIZONTAL)
  50. return point.x;
  51. return point.y;
  52. }
  53. };
  54. template <>
  55. struct point_mutable_traits<CPoint> {
  56. typedef int coordinate_type;
  57. static inline void set(CPoint& point, orientation_2d orient, int value) {
  58. if(orient == HORIZONTAL)
  59. point.x = value;
  60. else
  61. point.y = value;
  62. }
  63. static inline CPoint construct(int x_value, int y_value) {
  64. CPoint retval;
  65. retval.x = x_value;
  66. retval.y = y_value;
  67. return retval;
  68. }
  69. };
  70. } }
  71. //the CPolygon example
  72. typedef std::list<CPoint> CPolygon;
  73. //we need to specialize our polygon concept mapping in boost polygon
  74. namespace boost { namespace polygon {
  75. //first register CPolygon as a polygon_concept type
  76. template <>
  77. struct geometry_concept<CPolygon>{ typedef polygon_concept type; };
  78. template <>
  79. struct polygon_traits<CPolygon> {
  80. typedef int coordinate_type;
  81. typedef CPolygon::const_iterator iterator_type;
  82. typedef CPoint point_type;
  83. // Get the begin iterator
  84. static inline iterator_type begin_points(const CPolygon& t) {
  85. return t.begin();
  86. }
  87. // Get the end iterator
  88. static inline iterator_type end_points(const CPolygon& t) {
  89. return t.end();
  90. }
  91. // Get the number of sides of the polygon
  92. static inline std::size_t size(const CPolygon& t) {
  93. return t.size();
  94. }
  95. // Get the winding direction of the polygon
  96. static inline winding_direction winding(const CPolygon& t) {
  97. return unknown_winding;
  98. }
  99. };
  100. template <>
  101. struct polygon_mutable_traits<CPolygon> {
  102. //expects stl style iterators
  103. template <typename iT>
  104. static inline CPolygon& set_points(CPolygon& t,
  105. iT input_begin, iT input_end) {
  106. t.clear();
  107. while(input_begin != input_end) {
  108. t.push_back(CPoint());
  109. gtl::assign(t.back(), *input_begin);
  110. ++input_begin;
  111. }
  112. return t;
  113. }
  114. };
  115. } }
  116. //OK, finally we get to declare our own polygon set type
  117. typedef std::deque<CPolygon> CPolygonSet;
  118. //deque isn't automatically a polygon set in the library
  119. //because it is a standard container there is a shortcut
  120. //for mapping it to polygon set concept, but I'll do it
  121. //the long way that you would use in the general case.
  122. namespace boost { namespace polygon {
  123. //first we register CPolygonSet as a polygon set
  124. template <>
  125. struct geometry_concept<CPolygonSet> { typedef polygon_set_concept type; };
  126. //next we map to the concept through traits
  127. template <>
  128. struct polygon_set_traits<CPolygonSet> {
  129. typedef int coordinate_type;
  130. typedef CPolygonSet::const_iterator iterator_type;
  131. typedef CPolygonSet operator_arg_type;
  132. static inline iterator_type begin(const CPolygonSet& polygon_set) {
  133. return polygon_set.begin();
  134. }
  135. static inline iterator_type end(const CPolygonSet& polygon_set) {
  136. return polygon_set.end();
  137. }
  138. //don't worry about these, just return false from them
  139. static inline bool clean(const CPolygonSet& polygon_set) { return false; }
  140. static inline bool sorted(const CPolygonSet& polygon_set) { return false; }
  141. };
  142. template <>
  143. struct polygon_set_mutable_traits<CPolygonSet> {
  144. template <typename input_iterator_type>
  145. static inline void set(CPolygonSet& polygon_set, input_iterator_type input_begin, input_iterator_type input_end) {
  146. polygon_set.clear();
  147. //this is kind of cheesy. I am copying the unknown input geometry
  148. //into my own polygon set and then calling get to populate the
  149. //deque
  150. polygon_set_data<int> ps;
  151. ps.insert(input_begin, input_end);
  152. ps.get(polygon_set);
  153. //if you had your own odd-ball polygon set you would probably have
  154. //to iterate through each polygon at this point and do something
  155. //extra
  156. }
  157. };
  158. } }
  159. int main() {
  160. long long c1 = clock();
  161. for(int i = 0; i < 1000; ++i)
  162. test_polygon_set<CPolygonSet>();
  163. long long c2 = clock();
  164. for(int i = 0; i < 1000; ++i)
  165. test_polygon_set<gtl::polygon_set_data<int> >();
  166. long long c3 = clock();
  167. long long diff1 = c2 - c1;
  168. long long diff2 = c3 - c2;
  169. if(diff1 > 0 && diff2)
  170. std::cout << "library polygon_set_data is " << float(diff1)/float(diff2) << "X faster than custom polygon set deque of CPolygon" << std::endl;
  171. else
  172. std::cout << "operation was too fast" << std::endl;
  173. return 0;
  174. }
  175. //Now you know how to map your own data type to polygon set concept
  176. //Now you also know how to make your application code that operates on geometry
  177. //data type agnostic from point through polygon set