252 lines
10 KiB
HTML
252 lines
10 KiB
HTML
<html>
|
||
|
||
<head>
|
||
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
|
||
<title>Custom Polygon Set</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 <list><br>
|
||
#include <time.h><br>
|
||
#include <cassert><br>
|
||
#include <deque><br>
|
||
#include <iostream><br>
|
||
namespace gtl = boost::polygon;<br>
|
||
using namespace boost::polygon::operators;<br><br>
|
||
//once again we make our usage of the library generic<br>
|
||
//and parameterize it on the polygon set type<br>
|
||
template <typename PolygonSet><br>
|
||
void test_polygon_set() {<br>
|
||
using namespace gtl; <br>
|
||
PolygonSet ps;<br>
|
||
ps += rectangle_data<int>(0, 0, 10, 10);<br>
|
||
PolygonSet ps2;<br>
|
||
ps2 += rectangle_data<int>(5, 5, 15, 15);<br>
|
||
PolygonSet ps3;<br>
|
||
assign(ps3, ps * ps2); <br>
|
||
PolygonSet ps4;<br>
|
||
ps4 += ps + ps2;<br>
|
||
assert(area(ps4) == area(ps) + area(ps2) - area(ps3));<br>
|
||
assert(equivalence((ps + ps2) - (ps * ps2), ps ^ ps2));<br>
|
||
rectangle_data<int> rect;<br>
|
||
assert(extents(rect, ps ^ ps2));<br>
|
||
assert(area(rect) == 225);<br>
|
||
assert(area(rect ^ (ps ^ ps2)) == area(rect) - area(ps ^ ps2)); <br>
|
||
}<br>
|
||
<br>
|
||
//first thing is first, lets include all the code from previous examples<br>
|
||
<br>
|
||
//the CPoint example<br>
|
||
struct CPoint {<br>
|
||
int x;<br>
|
||
int y;<br>
|
||
};<br>
|
||
<br>
|
||
namespace boost { namespace polygon {<br>
|
||
template <><br>
|
||
struct geometry_concept<CPoint> { typedef point_concept 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>
|
||
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>
|
||
//the CPolygon example<br>
|
||
typedef std::list<CPoint> CPolygon;<br>
|
||
<br>
|
||
//we need to specialize our polygon concept mapping in boost polygon<br>
|
||
namespace boost { namespace polygon {<br>
|
||
//first register CPolygon as a polygon_concept type<br>
|
||
template <><br>
|
||
struct geometry_concept<CPolygon>{ typedef polygon_concept type; };<br>
|
||
<br>
|
||
template <><br>
|
||
struct polygon_traits<CPolygon> {<br>
|
||
typedef int coordinate_type;<br>
|
||
typedef CPolygon::const_iterator iterator_type;<br>
|
||
typedef CPoint point_type;<br>
|
||
<br>
|
||
// Get the begin iterator<br>
|
||
static inline iterator_type begin_points(const CPolygon& t) {<br>
|
||
return t.begin();<br>
|
||
}<br>
|
||
<br>
|
||
// Get the end iterator<br>
|
||
static inline iterator_type end_points(const CPolygon& t) {<br>
|
||
return t.end();<br>
|
||
}<br>
|
||
<br>
|
||
// Get the number of sides of the polygon<br>
|
||
static inline std::size_t size(const CPolygon& t) {<br>
|
||
return t.size();<br>
|
||
}<br>
|
||
<br>
|
||
// Get the winding direction of the polygon<br>
|
||
static inline winding_direction winding(const CPolygon& t) {<br>
|
||
return unknown_winding;<br>
|
||
}<br>
|
||
};<br>
|
||
<br>
|
||
template <><br>
|
||
struct polygon_mutable_traits<CPolygon> {<br>
|
||
//expects stl style iterators<br>
|
||
template <typename iT><br>
|
||
static inline CPolygon& set_points(CPolygon& t, <br>
|
||
|
||
iT input_begin, iT input_end) {<br>
|
||
t.clear();<br>
|
||
while(input_begin != input_end) {<br>
|
||
t.push_back(CPoint());<br>
|
||
gtl::assign(t.back(), *input_begin);<br>
|
||
++input_begin;<br>
|
||
}<br>
|
||
return t;<br>
|
||
}<br>
|
||
<br>
|
||
};<br>
|
||
} }<br>
|
||
<br>
|
||
//OK, finally we get to declare our own polygon set type<br>
|
||
typedef std::deque<CPolygon> CPolygonSet;<br>
|
||
<br>
|
||
//deque isn't automatically a polygon set in the library<br>
|
||
//because it is a standard container there is a shortcut<br>
|
||
//for mapping it to polygon set concept, but I'll do it<br>
|
||
//the long way that you would use in the general case.<br>
|
||
namespace boost { namespace polygon {<br>
|
||
//first we register CPolygonSet as a polygon set<br>
|
||
template <><br>
|
||
struct geometry_concept<CPolygonSet> { typedef polygon_set_concept type;
|
||
};<br>
|
||
<br>
|
||
//next we map to the concept through traits<br>
|
||
template <><br>
|
||
struct polygon_set_traits<CPolygonSet> {<br>
|
||
typedef int coordinate_type;<br>
|
||
typedef CPolygonSet::const_iterator iterator_type;<br>
|
||
typedef CPolygonSet operator_arg_type;<br>
|
||
<br>
|
||
static inline iterator_type begin(const CPolygonSet&
|
||
polygon_set) {<br>
|
||
return polygon_set.begin();<br>
|
||
}<br>
|
||
<br>
|
||
static inline iterator_type end(const CPolygonSet&
|
||
polygon_set) {<br>
|
||
return polygon_set.end();<br>
|
||
}<br>
|
||
<br>
|
||
//don't worry about these, just return false from them<br>
|
||
static inline bool clean(const CPolygonSet& polygon_set) {
|
||
return false; }<br>
|
||
static inline bool sorted(const CPolygonSet& polygon_set) {
|
||
return false; }<br>
|
||
};<br>
|
||
<br>
|
||
template <><br>
|
||
struct polygon_set_mutable_traits<CPolygonSet> {<br>
|
||
template <typename input_iterator_type><br>
|
||
static inline void set(CPolygonSet& polygon_set,
|
||
input_iterator_type input_begin, input_iterator_type input_end) {<br>
|
||
polygon_set.clear();<br>
|
||
//this is kind of cheesy. I am copying the
|
||
unknown input geometry<br>
|
||
//into my own polygon set and then calling get to
|
||
populate the<br>
|
||
//deque<br>
|
||
polygon_set_data<int> ps;<br>
|
||
ps.insert(input_begin, input_end);<br>
|
||
ps.get(polygon_set);<br>
|
||
//if you had your own odd-ball polygon set you
|
||
would probably have<br>
|
||
//to iterate through each polygon at this point
|
||
and do something<br>
|
||
//extra<br>
|
||
}<br>
|
||
};<br>
|
||
} }<br>
|
||
<br>
|
||
int main() {<br>
|
||
long long c1 = clock();<br>
|
||
for(int i = 0; i < 1000; ++i) <br>
|
||
test_polygon_set<CPolygonSet>();<br>
|
||
long long c2 = clock();<br>
|
||
for(int i = 0; i < 1000; ++i) <br>
|
||
test_polygon_set<gtl::polygon_set_data<int> >();<br>
|
||
long long c3 = clock();<br>
|
||
long long diff1 = c2 - c1;<br>
|
||
long long diff2 = c3 - c2;<br>
|
||
if(diff1 > 0 && diff2)<br>
|
||
std::cout << "library polygon_set_data is " <<
|
||
float(diff1)/float(diff2) << "X faster than custom polygon set deque of CPolygon"
|
||
<< std::endl;<br>
|
||
else<br>
|
||
std::cout << "operation was too fast" << std::endl;<br>
|
||
return 0;<br>
|
||
}</font></p>
|
||
<p><font face="Courier New">//Now you know how to map your own data type to
|
||
polygon set concept<br>
|
||
//Now you also know how to make your application code that operates on geometry<br>
|
||
//data type agnostic from point through polygon set
|
||
</font></p>
|
||
|
||
|
||
<table class="docinfo" rules="none" frame="void" id="table1">
|
||
<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>
|
||
</table>
|
||
|
||
</body>
|
||
|
||
</html>
|