]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/polygon/example/gtl_custom_polygon.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / polygon / example / gtl_custom_polygon.cpp
1 /*
2 Copyright 2008 Intel Corporation
3
4 Use, modification and distribution are 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 */
8 #include <boost/polygon/polygon.hpp>
9 #include <cassert>
10 #include <list>
11 namespace gtl = boost::polygon;
12 using namespace boost::polygon::operators;
13
14 //first lets turn our polygon usage code into a generic
15 //function parameterized by polygon type
16 template <typename Polygon>
17 void test_polygon() {
18 //lets construct a 10x10 rectangle shaped polygon
19 typedef typename gtl::polygon_traits<Polygon>::point_type Point;
20 Point pts[] = {gtl::construct<Point>(0, 0),
21 gtl::construct<Point>(10, 0),
22 gtl::construct<Point>(10, 10),
23 gtl::construct<Point>(0, 10) };
24 Polygon poly;
25 gtl::set_points(poly, pts, pts+4);
26
27 //now lets see what we can do with this polygon
28 assert(gtl::area(poly) == 100.0f);
29 assert(gtl::contains(poly, gtl::construct<Point>(5, 5)));
30 assert(!gtl::contains(poly, gtl::construct<Point>(15, 5)));
31 gtl::rectangle_data<int> rect;
32 assert(gtl::extents(rect, poly)); //get bounding box of poly
33 assert(gtl::equivalence(rect, poly)); //hey, that's slick
34 assert(gtl::winding(poly) == gtl::COUNTERCLOCKWISE);
35 assert(gtl::perimeter(poly) == 40.0f);
36
37 //add 5 to all coords of poly
38 gtl::convolve(poly, gtl::construct<Point>(5, 5));
39 //multiply all coords of poly by 2
40 gtl::scale_up(poly, 2);
41 gtl::set_points(rect, gtl::point_data<int>(10, 10),
42 gtl::point_data<int>(30, 30));
43 assert(gtl::equivalence(poly, rect));
44 }
45
46 //Now lets declare our own polygon class
47 //Oops, we need a point class to support our polygon, lets borrow
48 //the CPoint example
49 struct CPoint {
50 int x;
51 int y;
52 };
53
54 //we have to get CPoint working with boost polygon to make our polygon
55 //that uses CPoint working with boost polygon
56 namespace boost { namespace polygon {
57 template <>
58 struct geometry_concept<CPoint> { typedef point_concept type; };
59 template <>
60 struct point_traits<CPoint> {
61 typedef int coordinate_type;
62
63 static inline coordinate_type get(const CPoint& point,
64 orientation_2d orient) {
65 if(orient == HORIZONTAL)
66 return point.x;
67 return point.y;
68 }
69 };
70
71 template <>
72 struct point_mutable_traits<CPoint> {
73 typedef int coordinate_type;
74
75 static inline void set(CPoint& point, orientation_2d orient, int value) {
76 if(orient == HORIZONTAL)
77 point.x = value;
78 else
79 point.y = value;
80 }
81 static inline CPoint construct(int x_value, int y_value) {
82 CPoint retval;
83 retval.x = x_value;
84 retval.y = y_value;
85 return retval;
86 }
87 };
88 } }
89
90 //I'm lazy and use the stl everywhere to avoid writing my own classes
91 //my toy polygon is a std::list<CPoint>
92 typedef std::list<CPoint> CPolygon;
93
94 //we need to specialize our polygon concept mapping in boost polygon
95 namespace boost { namespace polygon {
96 //first register CPolygon as a polygon_concept type
97 template <>
98 struct geometry_concept<CPolygon>{ typedef polygon_concept type; };
99
100 template <>
101 struct polygon_traits<CPolygon> {
102 typedef int coordinate_type;
103 typedef CPolygon::const_iterator iterator_type;
104 typedef CPoint point_type;
105
106 // Get the begin iterator
107 static inline iterator_type begin_points(const CPolygon& t) {
108 return t.begin();
109 }
110
111 // Get the end iterator
112 static inline iterator_type end_points(const CPolygon& t) {
113 return t.end();
114 }
115
116 // Get the number of sides of the polygon
117 static inline std::size_t size(const CPolygon& t) {
118 return t.size();
119 }
120
121 // Get the winding direction of the polygon
122 static inline winding_direction winding(const CPolygon& t) {
123 return unknown_winding;
124 }
125 };
126
127 template <>
128 struct polygon_mutable_traits<CPolygon> {
129 //expects stl style iterators
130 template <typename iT>
131 static inline CPolygon& set_points(CPolygon& t,
132 iT input_begin, iT input_end) {
133 t.clear();
134 t.insert(t.end(), input_begin, input_end);
135 return t;
136 }
137
138 };
139 } }
140
141 //now there's nothing left to do but test that our polygon
142 //works with library interfaces
143 int main() {
144 test_polygon<CPolygon>(); //woot!
145 return 0;
146 }