4f5108d143
[SVN r83350]
161 lines
6.2 KiB
HTML
161 lines
6.2 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||
<html><head>
|
||
|
||
|
||
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252"><title>Custom Point</title></head><body>
|
||
|
||
<p><font face="Courier New">/*<br>
|
||
Copyright 2008 Intel Corporation<br>
|
||
<br>
|
||
Use, modification and distribution are subject to the Boost Software License,<br>
|
||
Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at<br>
|
||
http://www.boost.org/LICENSE_1_0.txt).<br>
|
||
*/<br>
|
||
#include <boost/polygon/polygon.hpp><br>
|
||
#include <cassert><br>
|
||
namespace gtl = boost::polygon;<br>
|
||
using namespace boost::polygon::operators;<br><br>
|
||
//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>
|
||
//constructing a gtl point<br>
|
||
int x = 10;<br>
|
||
int y = 20;<br>
|
||
//Point pt(x, y);<br>
|
||
Point pt = gtl::construct<Point>(x, y);<br>
|
||
assert(gtl::x(pt) == 10);<br>
|
||
assert(gtl::y(pt) == 20);<br>
|
||
<br>
|
||
//a quick primer in isotropic point access<br>
|
||
typedef gtl::orientation_2d O;<br>
|
||
using gtl::HORIZONTAL;<br>
|
||
using gtl::VERTICAL;<br>
|
||
O o = HORIZONTAL;<br>
|
||
assert(gtl::x(pt) == gtl::get(pt, o));<br>
|
||
<br>
|
||
o = o.get_perpendicular();<br>
|
||
assert(o == VERTICAL);<br>
|
||
assert(gtl::y(pt) == gtl::get(pt, o));<br>
|
||
<br>
|
||
gtl::set(pt, o, 30);<br>
|
||
assert(gtl::y(pt) == 30);<br>
|
||
<br>
|
||
//using some of the library functions<br>
|
||
//Point pt2(10, 30);<br>
|
||
Point pt2 = gtl::construct<Point>(10, 30);<br>
|
||
assert(gtl::equivalence(pt, pt2));<br>
|
||
<br>
|
||
gtl::transformation<int> tr(gtl::axis_transformation::SWAP_XY);<br>
|
||
gtl::transform(pt, tr);<br>
|
||
assert(gtl::equivalence(pt, gtl::construct<Point>(30, 10)));<br>
|
||
<br>
|
||
gtl::transformation<int> tr2 = tr.inverse();<br>
|
||
assert(tr == tr2); //SWAP_XY is its own inverse transform<br>
|
||
<br>
|
||
gtl::transform(pt, tr2);<br>
|
||
assert(gtl::equivalence(pt, pt2)); //the two points are equal again<br>
|
||
<br>
|
||
gtl::move(pt, o, 10); //move pt 10 units in y<br>
|
||
assert(gtl::euclidean_distance(pt, pt2) == 10.0f);<br>
|
||
<br>
|
||
gtl::move(pt, o.get_perpendicular(), 10); //move pt 10 units in x<br>
|
||
assert(gtl::manhattan_distance(pt, pt2) == 20);<br>}<br>
|
||
<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>
|
||
int x;<br>
|
||
int y;<br>};<br>
|
||
<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>
|
||
<br>
|
||
<br>//First we register it as a point with boost polygon<br>namespace boost {
|
||
namespace polygon {<br>
|
||
template <><br>
|
||
struct geometry_concept<CPoint> { typedef point_concept type; };<br> <br>
|
||
<br> //Then we specialize the gtl point traits for our point type<br>
|
||
template <><br>
|
||
struct point_traits<CPoint> {<br>
|
||
|
||
typedef int coordinate_type;<br>
|
||
<br>
|
||
|
||
static inline coordinate_type get(const CPoint& point, <br>
|
||
|
||
orientation_2d orient) {<br>
|
||
|
||
|
||
if(orient == HORIZONTAL)<br>
|
||
|
||
|
||
|
||
return point.x;<br>
|
||
|
||
|
||
return point.y;<br>
|
||
|
||
}<br>
|
||
};<br>
|
||
<br>
|
||
template <><br>
|
||
struct point_mutable_traits<CPoint> {<br>
|
||
typedef int coordinate_type;<br>
|
||
<br>
|
||
</font></p>
|
||
<p><font face="Courier New">
|
||
|
||
static inline void set(CPoint& point, orientation_2d orient, int value) {<br>
|
||
|
||
|
||
if(orient == HORIZONTAL)<br>
|
||
|
||
|
||
|
||
point.x = value;<br>
|
||
|
||
|
||
else<br>
|
||
|
||
|
||
point.y = value;<br>
|
||
|
||
}<br>
|
||
|
||
static inline CPoint construct(int x_value, int y_value) {<br>
|
||
|
||
|
||
CPoint retval;<br>
|
||
|
||
|
||
retval.x = x_value;<br>
|
||
|
||
|
||
retval.y = y_value; <br>
|
||
|
||
|
||
return retval;<br>
|
||
|
||
}<br>
|
||
};<br>} }<br>
|
||
<br>//Now lets see if the CPoint works with the library functions<br>int main() {<br>
|
||
test_point<CPoint>(); //yay! All your testing is done for you.<br>
|
||
return 0;<br>}<br>
|
||
<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>
|
||
</font></p>
|
||
|
||
|
||
|
||
<table class="docinfo" id="table1" frame="void" rules="none">
|
||
<colgroup>
|
||
<col class="docinfo-name"><col class="docinfo-content">
|
||
</colgroup>
|
||
<tbody valign="top">
|
||
<tr>
|
||
<th class="docinfo-name">Copyright:</th>
|
||
<td>Copyright <20> Intel Corporation 2008-2010.</td>
|
||
</tr>
|
||
<tr class="field">
|
||
<th class="docinfo-name">License:</th>
|
||
<td class="field-body">Distributed under the Boost Software License,
|
||
Version 1.0. (See accompanying file <tt class="literal">
|
||
<span class="pre">LICENSE_1_0.txt</span></tt> or copy at
|
||
<a class="reference" target="_top" href="http://www.boost.org/LICENSE_1_0.txt">
|
||
http://www.boost.org/LICENSE_1_0.txt</a>)</td>
|
||
</tr>
|
||
</tbody></table>
|
||
|
||
</body></html> |