]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
2 | <html><head> | |
3 | ||
4 | ||
5 | <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"><title>Custom Point</title></head><body> | |
6 | ||
7 | <p><font face="Courier New">/*<br> | |
8 | Copyright 2008 Intel Corporation<br> | |
9 | <br> | |
10 | Use, modification and distribution are subject to the Boost Software License,<br> | |
11 | Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at<br> | |
12 | http://www.boost.org/LICENSE_1_0.txt).<br> | |
13 | */<br> | |
14 | #include <boost/polygon/polygon.hpp><br> | |
15 | #include <cassert><br> | |
16 | namespace gtl = boost::polygon;<br> | |
17 | using namespace boost::polygon::operators;<br><br> | |
18 | //lets make the body of main from point_usage.cpp<br>//a generic function parameterized by point type<br>template <typename Point><br>void test_point() {<br> | |
19 | //constructing a gtl point<br> | |
20 | int x = 10;<br> | |
21 | int y = 20;<br> | |
22 | //Point pt(x, y);<br> | |
23 | Point pt = gtl::construct<Point>(x, y);<br> | |
24 | assert(gtl::x(pt) == 10);<br> | |
25 | assert(gtl::y(pt) == 20);<br> | |
26 | <br> | |
27 | //a quick primer in isotropic point access<br> | |
28 | typedef gtl::orientation_2d O;<br> | |
29 | using gtl::HORIZONTAL;<br> | |
30 | using gtl::VERTICAL;<br> | |
31 | O o = HORIZONTAL;<br> | |
32 | assert(gtl::x(pt) == gtl::get(pt, o));<br> | |
33 | <br> | |
34 | o = o.get_perpendicular();<br> | |
35 | assert(o == VERTICAL);<br> | |
36 | assert(gtl::y(pt) == gtl::get(pt, o));<br> | |
37 | <br> | |
38 | gtl::set(pt, o, 30);<br> | |
39 | assert(gtl::y(pt) == 30);<br> | |
40 | <br> | |
41 | //using some of the library functions<br> | |
42 | //Point pt2(10, 30);<br> | |
43 | Point pt2 = gtl::construct<Point>(10, 30);<br> | |
44 | assert(gtl::equivalence(pt, pt2));<br> | |
45 | <br> | |
46 | gtl::transformation<int> tr(gtl::axis_transformation::SWAP_XY);<br> | |
47 | gtl::transform(pt, tr);<br> | |
48 | assert(gtl::equivalence(pt, gtl::construct<Point>(30, 10)));<br> | |
49 | <br> | |
50 | gtl::transformation<int> tr2 = tr.inverse();<br> | |
51 | assert(tr == tr2); //SWAP_XY is its own inverse transform<br> | |
52 | <br> | |
53 | gtl::transform(pt, tr2);<br> | |
54 | assert(gtl::equivalence(pt, pt2)); //the two points are equal again<br> | |
55 | <br> | |
56 | gtl::move(pt, o, 10); //move pt 10 units in y<br> | |
57 | assert(gtl::euclidean_distance(pt, pt2) == 10.0f);<br> | |
58 | <br> | |
59 | gtl::move(pt, o.get_perpendicular(), 10); //move pt 10 units in x<br> | |
60 | assert(gtl::manhattan_distance(pt, pt2) == 20);<br>}<br> | |
61 | <br>//Now lets declare our own point type<br>//Bjarne says that if a class doesn't maintain an<br>//invariant just use a struct.<br>struct CPoint {<br> | |
62 | int x;<br> | |
63 | int y;<br>};<br> | |
64 | <br>//There, nice a simple...but wait, it doesn't do anything<br>//how do we use it to do all the things a point needs to do?<br> | |
65 | <br> | |
66 | <br>//First we register it as a point with boost polygon<br>namespace boost { | |
67 | namespace polygon {<br> | |
68 | template <><br> | |
69 | struct geometry_concept<CPoint> { typedef point_concept type; };<br> <br> | |
70 | <br> //Then we specialize the gtl point traits for our point type<br> | |
71 | template <><br> | |
72 | struct point_traits<CPoint> {<br> | |
73 | | |
74 | typedef int coordinate_type;<br> | |
75 | <br> | |
76 | | |
77 | static inline coordinate_type get(const CPoint& point, <br> | |
78 | | |
79 | orientation_2d orient) {<br> | |
80 | | |
81 | | |
82 | if(orient == HORIZONTAL)<br> | |
83 | | |
84 | | |
85 | | |
86 | return point.x;<br> | |
87 | | |
88 | | |
89 | return point.y;<br> | |
90 | | |
91 | }<br> | |
92 | };<br> | |
93 | <br> | |
94 | template <><br> | |
95 | struct point_mutable_traits<CPoint> {<br> | |
96 | typedef int coordinate_type;<br> | |
97 | <br> | |
98 | </font></p> | |
99 | <p><font face="Courier New"> | |
100 | | |
101 | static inline void set(CPoint& point, orientation_2d orient, int value) {<br> | |
102 | | |
103 | | |
104 | if(orient == HORIZONTAL)<br> | |
105 | | |
106 | | |
107 | | |
108 | point.x = value;<br> | |
109 | | |
110 | | |
111 | else<br> | |
112 | | |
113 | | |
114 | point.y = value;<br> | |
115 | | |
116 | }<br> | |
117 | | |
118 | static inline CPoint construct(int x_value, int y_value) {<br> | |
119 | | |
120 | | |
121 | CPoint retval;<br> | |
122 | | |
123 | | |
124 | retval.x = x_value;<br> | |
125 | | |
126 | | |
127 | retval.y = y_value; <br> | |
128 | | |
129 | | |
130 | return retval;<br> | |
131 | | |
132 | }<br> | |
133 | };<br>} }<br> | |
134 | <br>//Now lets see if the CPoint works with the library functions<br>int main() {<br> | |
135 | test_point<CPoint>(); //yay! All your testing is done for you.<br> | |
136 | return 0;<br>}<br> | |
137 | <br>//Now you know how to map a user type to the library point concept<br>//and how to write a generic function parameterized by point type<br>//using the library interfaces to access it.<br> | |
138 | </font></p> | |
139 | ||
140 | ||
141 | ||
142 | <table class="docinfo" id="table1" frame="void" rules="none"> | |
143 | <colgroup> | |
144 | <col class="docinfo-name"><col class="docinfo-content"> | |
145 | </colgroup> | |
146 | <tbody valign="top"> | |
147 | <tr> | |
148 | <th class="docinfo-name">Copyright:</th> | |
149 |