]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/polygon/doc/gtl_custom_point.htm
bump version to 12.2.2-pve1
[ceph.git] / ceph / src / boost / libs / polygon / doc / gtl_custom_point.htm
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 &lt;boost/polygon/polygon.hpp&gt;<br>
15 #include &lt;cassert&gt;<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 &lt;typename Point&gt;<br>void test_point() {<br>&nbsp;
19 &nbsp; //constructing a gtl point<br>&nbsp; &nbsp;
20 int x = 10;<br>&nbsp; &nbsp;
21 int y = 20;<br>&nbsp; &nbsp;
22 //Point pt(x, y);<br>&nbsp; &nbsp;
23 Point pt = gtl::construct&lt;Point&gt;(x, y);<br>&nbsp; &nbsp;
24 assert(gtl::x(pt) == 10);<br>&nbsp; &nbsp;
25 assert(gtl::y(pt) == 20);<br>&nbsp; &nbsp;
26 <br>&nbsp; &nbsp;
27 //a quick primer in isotropic point access<br>&nbsp; &nbsp;
28 typedef gtl::orientation_2d O;<br>&nbsp; &nbsp;
29 using gtl::HORIZONTAL;<br>&nbsp; &nbsp;
30 using gtl::VERTICAL;<br>&nbsp; &nbsp;
31 O o = HORIZONTAL;<br>&nbsp; &nbsp;
32 assert(gtl::x(pt) == gtl::get(pt, o));<br>&nbsp; &nbsp;
33 <br>&nbsp; &nbsp;
34 o = o.get_perpendicular();<br>&nbsp; &nbsp;
35 assert(o == VERTICAL);<br>&nbsp; &nbsp;
36 assert(gtl::y(pt) == gtl::get(pt, o));<br>&nbsp; &nbsp;
37 <br>&nbsp; &nbsp;
38 gtl::set(pt, o, 30);<br>&nbsp; &nbsp;
39 assert(gtl::y(pt) == 30);<br>&nbsp; &nbsp;
40 <br>&nbsp; &nbsp;
41 //using some of the library functions<br>&nbsp; &nbsp;
42 //Point pt2(10, 30);<br>&nbsp; &nbsp;
43 Point pt2 = gtl::construct&lt;Point&gt;(10, 30);<br>&nbsp; &nbsp;
44 assert(gtl::equivalence(pt, pt2));<br>&nbsp; &nbsp;
45 <br>&nbsp; &nbsp;
46 gtl::transformation&lt;int&gt; tr(gtl::axis_transformation::SWAP_XY);<br>&nbsp; &nbsp;
47 gtl::transform(pt, tr);<br>&nbsp; &nbsp;
48 assert(gtl::equivalence(pt, gtl::construct&lt;Point&gt;(30, 10)));<br>&nbsp; &nbsp;
49 <br>&nbsp; &nbsp;
50 gtl::transformation&lt;int&gt; tr2 = tr.inverse();<br>&nbsp; &nbsp;
51 assert(tr == tr2); //SWAP_XY is its own inverse transform<br>&nbsp; &nbsp;
52 <br>&nbsp; &nbsp;
53 gtl::transform(pt, tr2);<br>&nbsp; &nbsp;
54 assert(gtl::equivalence(pt, pt2)); //the two points are equal again<br>&nbsp; &nbsp;
55 <br>&nbsp; &nbsp;
56 gtl::move(pt, o, 10); //move pt 10 units in y<br>&nbsp; &nbsp;
57 assert(gtl::euclidean_distance(pt, pt2) == 10.0f);<br>&nbsp; &nbsp;
58 <br>&nbsp; &nbsp;
59 gtl::move(pt, o.get_perpendicular(), 10); //move pt 10 units in x<br>&nbsp; &nbsp;
60 assert(gtl::manhattan_distance(pt, pt2) == 20);<br>}<br>&nbsp; &nbsp;
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>&nbsp; &nbsp;
62 int x;<br>&nbsp; &nbsp;
63 int y;<br>};<br>&nbsp; &nbsp;
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>&nbsp; &nbsp;
65 <br>&nbsp; &nbsp;
66 <br>//First we register it as a point with boost polygon<br>namespace boost {
67 namespace polygon {<br>&nbsp;&nbsp;&nbsp;
68 template &lt;&gt;<br>&nbsp; &nbsp;
69 struct geometry_concept&lt;CPoint&gt; { typedef point_concept type; };<br>&nbsp;<br>&nbsp; &nbsp;
70 <br>&nbsp;&nbsp;&nbsp; //Then we specialize the gtl point traits for our point type<br>&nbsp; &nbsp;
71 template &lt;&gt;<br>&nbsp; &nbsp;
72 struct point_traits&lt;CPoint&gt; {<br>&nbsp; &nbsp;
73 &nbsp; &nbsp;
74 typedef int coordinate_type;<br>&nbsp; &nbsp;
75 <br>&nbsp; &nbsp;
76 &nbsp; &nbsp;
77 static inline coordinate_type get(const CPoint&amp; point, <br>&nbsp; &nbsp;
78 &nbsp; &nbsp;
79 orientation_2d orient) {<br>&nbsp; &nbsp;
80 &nbsp; &nbsp;
81 &nbsp; &nbsp;
82 if(orient == HORIZONTAL)<br>&nbsp; &nbsp;
83 &nbsp; &nbsp;
84 &nbsp; &nbsp;
85 &nbsp; &nbsp;
86 return point.x;<br>&nbsp; &nbsp;
87 &nbsp; &nbsp;
88 &nbsp; &nbsp;
89 return point.y;<br>&nbsp; &nbsp;
90 &nbsp; &nbsp;
91 }<br>&nbsp; &nbsp;
92 };<br>&nbsp; &nbsp;
93 <br>&nbsp; &nbsp;
94 template &lt;&gt;<br>&nbsp; &nbsp;
95 struct point_mutable_traits&lt;CPoint&gt; {<br>
96 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; typedef int coordinate_type;<br>
97 <br>
98 </font></p>
99 <p><font face="Courier New">&nbsp; &nbsp;
100 &nbsp; &nbsp;
101 static inline void set(CPoint&amp; point, orientation_2d orient, int value) {<br>&nbsp; &nbsp;
102 &nbsp; &nbsp;
103 &nbsp; &nbsp;
104 if(orient == HORIZONTAL)<br>&nbsp; &nbsp;
105 &nbsp; &nbsp;
106 &nbsp; &nbsp;
107 &nbsp; &nbsp;
108 point.x = value;<br>&nbsp; &nbsp;
109 &nbsp; &nbsp;
110 &nbsp; &nbsp;
111 else<br>&nbsp; &nbsp;
112 &nbsp; &nbsp;
113 &nbsp; &nbsp;
114 point.y = value;<br>&nbsp; &nbsp;
115 &nbsp; &nbsp;
116 }<br>&nbsp; &nbsp;
117 &nbsp; &nbsp;
118 static inline CPoint construct(int x_value, int y_value) {<br>&nbsp; &nbsp;
119 &nbsp; &nbsp;
120 &nbsp; &nbsp;
121 CPoint retval;<br>&nbsp; &nbsp;
122 &nbsp; &nbsp;
123 &nbsp; &nbsp;
124 retval.x = x_value;<br>&nbsp; &nbsp;
125 &nbsp; &nbsp;
126 &nbsp; &nbsp;
127 retval.y = y_value; <br>&nbsp; &nbsp;
128 &nbsp; &nbsp;
129 &nbsp; &nbsp;
130 return retval;<br>&nbsp; &nbsp;
131 &nbsp; &nbsp;
132 }<br>&nbsp; &nbsp;
133 };<br>} }<br>&nbsp; &nbsp;
134 <br>//Now lets see if the CPoint works with the library functions<br>int main() {<br>&nbsp; &nbsp;
135 test_point&lt;CPoint&gt;(); //yay! All your testing is done for you.<br>&nbsp; &nbsp;
136 return 0;<br>}<br>&nbsp; &nbsp;
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>&nbsp; &nbsp;
138 &nbsp;</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 <td>Copyright © Intel Corporation 2008-2010.</td>
150 </tr>
151 <tr class="field">
152 <th class="docinfo-name">License:</th>
153 <td class="field-body">Distributed under the Boost Software License,
154 Version 1.0. (See accompanying file <tt class="literal">
155 <span class="pre">LICENSE_1_0.txt</span></tt> or copy at
156 <a class="reference" target="_top" href="http://www.boost.org/LICENSE_1_0.txt">
157 http://www.boost.org/LICENSE_1_0.txt</a>)</td>
158 </tr>
159 </tbody></table>
160
161 </body></html>