1 <!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN">
5 <meta http-equiv=
"Content-Type" content=
"text/html; charset=windows-1252"><title>Custom Point
</title></head><body>
7 <p><font face=
"Courier New">/*
<br>
8 Copyright
2008 Intel Corporation
<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>
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>
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>
34 o = o.get_perpendicular();
<br>
35 assert(o == VERTICAL);
<br>
36 assert(gtl::y(pt) == gtl::get(pt, o));
<br>
38 gtl::set(pt, o,
30);
<br>
39 assert(gtl::y(pt) ==
30);
<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>
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>
50 gtl::transformation
<int
> tr2 = tr.inverse();
<br>
51 assert(tr == tr2); //SWAP_XY is its own inverse transform
<br>
53 gtl::transform(pt, tr2);
<br>
54 assert(gtl::equivalence(pt, pt2)); //the two points are equal again
<br>
56 gtl::move(pt, o,
10); //move pt
10 units in y
<br>
57 assert(gtl::euclidean_distance(pt, pt2) ==
10.0f);
<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>
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>
74 typedef int coordinate_type;
<br>
77 static inline coordinate_type get(const CPoint
& point,
<br>
79 orientation_2d orient) {
<br>
82 if(orient == HORIZONTAL)
<br>
86 return point.x;
<br>
89 return point.y;
<br>
94 template
<><br>
95 struct point_mutable_traits
<CPoint
> {
<br>
96 typedef int coordinate_type;
<br>
99 <p><font face=
"Courier New">
101 static inline void set(CPoint
& point, orientation_2d orient, int value) {
<br>
104 if(orient == HORIZONTAL)
<br>
108 point.x = value;
<br>
111 else
<br>
114 point.y = value;
<br>
118 static inline CPoint construct(int x_value, int y_value) {
<br>
121 CPoint retval;
<br>
124 retval.x = x_value;
<br>
127 retval.y = y_value;
<br>
130 return retval;
<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>
142 <table class=
"docinfo" id=
"table1" frame=
"void" rules=
"none">
144 <col class=
"docinfo-name"><col class=
"docinfo-content">
148 <th class=
"docinfo-name">Copyright:
</th>
149 <td>Copyright © Intel Corporation
2008-
2010.
</td>
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>