]> git.proxmox.com Git - ceph.git/blame - 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
CommitLineData
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>
8Copyright 2008 Intel Corporation<br>
9<br>
10Use, modification and distribution are subject to the Boost Software License,<br>
11Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at<br>
12http://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>
16namespace gtl = boost::polygon;<br>
17using 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;
20int x = 10;<br>&nbsp; &nbsp;
21int y = 20;<br>&nbsp; &nbsp;
22//Point pt(x, y);<br>&nbsp; &nbsp;
23Point pt = gtl::construct&lt;Point&gt;(x, y);<br>&nbsp; &nbsp;
24assert(gtl::x(pt) == 10);<br>&nbsp; &nbsp;
25assert(gtl::y(pt) == 20);<br>&nbsp; &nbsp;
26<br>&nbsp; &nbsp;
27//a quick primer in isotropic point access<br>&nbsp; &nbsp;
28typedef gtl::orientation_2d O;<br>&nbsp; &nbsp;
29using gtl::HORIZONTAL;<br>&nbsp; &nbsp;
30using gtl::VERTICAL;<br>&nbsp; &nbsp;
31O o = HORIZONTAL;<br>&nbsp; &nbsp;
32assert(gtl::x(pt) == gtl::get(pt, o));<br>&nbsp; &nbsp;
33<br>&nbsp; &nbsp;
34o = o.get_perpendicular();<br>&nbsp; &nbsp;
35assert(o == VERTICAL);<br>&nbsp; &nbsp;
36assert(gtl::y(pt) == gtl::get(pt, o));<br>&nbsp; &nbsp;
37<br>&nbsp; &nbsp;
38gtl::set(pt, o, 30);<br>&nbsp; &nbsp;
39assert(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;
43Point pt2 = gtl::construct&lt;Point&gt;(10, 30);<br>&nbsp; &nbsp;
44assert(gtl::equivalence(pt, pt2));<br>&nbsp; &nbsp;
45<br>&nbsp; &nbsp;
46gtl::transformation&lt;int&gt; tr(gtl::axis_transformation::SWAP_XY);<br>&nbsp; &nbsp;
47gtl::transform(pt, tr);<br>&nbsp; &nbsp;
48assert(gtl::equivalence(pt, gtl::construct&lt;Point&gt;(30, 10)));<br>&nbsp; &nbsp;
49<br>&nbsp; &nbsp;
50gtl::transformation&lt;int&gt; tr2 = tr.inverse();<br>&nbsp; &nbsp;
51assert(tr == tr2); //SWAP_XY is its own inverse transform<br>&nbsp; &nbsp;
52<br>&nbsp; &nbsp;
53gtl::transform(pt, tr2);<br>&nbsp; &nbsp;
54assert(gtl::equivalence(pt, pt2)); //the two points are equal again<br>&nbsp; &nbsp;
55<br>&nbsp; &nbsp;
56gtl::move(pt, o, 10); //move pt 10 units in y<br>&nbsp; &nbsp;
57assert(gtl::euclidean_distance(pt, pt2) == 10.0f);<br>&nbsp; &nbsp;
58<br>&nbsp; &nbsp;
59gtl::move(pt, o.get_perpendicular(), 10); //move pt 10 units in x<br>&nbsp; &nbsp;
60assert(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;
62int x;<br>&nbsp; &nbsp;
63int 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 {
67namespace polygon {<br>&nbsp;&nbsp;&nbsp;
68template &lt;&gt;<br>&nbsp; &nbsp;
69struct 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;
71template &lt;&gt;<br>&nbsp; &nbsp;
72struct point_traits&lt;CPoint&gt; {<br>&nbsp; &nbsp;
73&nbsp; &nbsp;
74typedef int coordinate_type;<br>&nbsp; &nbsp;
75<br>&nbsp; &nbsp;
76&nbsp; &nbsp;
77static inline coordinate_type get(const CPoint&amp; point, <br>&nbsp; &nbsp;
78&nbsp; &nbsp;
79orientation_2d orient) {<br>&nbsp; &nbsp;
80&nbsp; &nbsp;
81&nbsp; &nbsp;
82if(orient == HORIZONTAL)<br>&nbsp; &nbsp;
83&nbsp; &nbsp;
84&nbsp; &nbsp;
85&nbsp; &nbsp;
86return point.x;<br>&nbsp; &nbsp;
87&nbsp; &nbsp;
88&nbsp; &nbsp;
89return point.y;<br>&nbsp; &nbsp;
90&nbsp; &nbsp;
91}<br>&nbsp; &nbsp;
92};<br>&nbsp; &nbsp;
93<br>&nbsp; &nbsp;
94template &lt;&gt;<br>&nbsp; &nbsp;
95struct 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;
101static inline void set(CPoint&amp; point, orientation_2d orient, int value) {<br>&nbsp; &nbsp;
102&nbsp; &nbsp;
103&nbsp; &nbsp;
104if(orient == HORIZONTAL)<br>&nbsp; &nbsp;
105&nbsp; &nbsp;
106&nbsp; &nbsp;
107&nbsp; &nbsp;
108point.x = value;<br>&nbsp; &nbsp;
109&nbsp; &nbsp;
110&nbsp; &nbsp;
111else<br>&nbsp; &nbsp;
112&nbsp; &nbsp;
113&nbsp; &nbsp;
114point.y = value;<br>&nbsp; &nbsp;
115&nbsp; &nbsp;
116}<br>&nbsp; &nbsp;
117&nbsp; &nbsp;
118static inline CPoint construct(int x_value, int y_value) {<br>&nbsp; &nbsp;
119&nbsp; &nbsp;
120&nbsp; &nbsp;
121CPoint retval;<br>&nbsp; &nbsp;
122&nbsp; &nbsp;
123&nbsp; &nbsp;
124retval.x = x_value;<br>&nbsp; &nbsp;
125&nbsp; &nbsp;
126&nbsp; &nbsp;
127retval.y = y_value; <br>&nbsp; &nbsp;
128&nbsp; &nbsp;
129&nbsp; &nbsp;
130return 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;
135test_point&lt;CPoint&gt;(); //yay! All your testing is done for you.<br>&nbsp; &nbsp;
136return 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