123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
- <title>Custom Polygon</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>
- #include <list><br>
- namespace gtl = boost::polygon;<br>
- using namespace boost::polygon::operators;<br><br>
- //first lets turn our polygon usage code into a generic<br>
- //function parameterized by polygon type<br>
- template <typename Polygon><br>
- void test_polygon() {<br>
- //lets construct a 10x10 rectangle shaped polygon<br>
- typedef typename gtl::polygon_traits<Polygon>::point_type Point;<br>
- Point pts[] = {gtl::construct<Point>(0, 0),<br>
- gtl::construct<Point>(10, 0),<br>
- gtl::construct<Point>(10, 10),<br>
- gtl::construct<Point>(0, 10) };<br>
- Polygon poly;<br>
- gtl::set_points(poly, pts, pts+4);<br>
- <br>
- //now lets see what we can do with this polygon<br>
- assert(gtl::area(poly) == 100.0f);<br>
- assert(gtl::contains(poly, gtl::construct<Point>(5, 5)));<br>
- assert(!gtl::contains(poly, gtl::construct<Point>(15, 5)));<br>
- gtl::rectangle_data<int> rect;<br>
- assert(gtl::extents(rect, poly)); //get bounding box of poly<br>
- assert(gtl::equivalence(rect, poly)); //hey, that's slick<br>
- assert(gtl::winding(poly) == gtl::COUNTERCLOCKWISE);<br>
- assert(gtl::perimeter(poly) == 40.0f);<br>
- <br>
- //add 5 to all coords of poly<br>
- gtl::convolve(poly, gtl::construct<Point>(5, 5));<br>
- //multiply all coords of poly by 2<br>
- gtl::scale_up(poly, 2);<br>
- gtl::set_points(rect, gtl::point_data<int>(10, 10),<br>
- gtl::point_data<int>(30, 30));<br>
- assert(gtl::equivalence(poly, rect));<br>
- }<br>
- <br>
- //Now lets declare our own polygon class<br>
- //Oops, we need a point class to support our polygon, lets borrow<br>
- //the CPoint example<br>
- struct CPoint {<br>
- int x;<br>
- int y;<br>
- };<br>
- <br>
- //we have to get CPoint working with boost polygon to make our polygon<br>
- //that uses CPoint working with boost polygon<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>
- //I'm lazy and use the stl everywhere to avoid writing my own classes<br>
- //my toy polygon is a std::list<CPoint><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>
- t.insert(t.end(), input_begin, input_end);<br>
- return t;<br>
- }<br>
- <br>
- };<br>
- } }<br>
- <br>
- //now there's nothing left to do but test that our polygon<br>
- //works with library interfaces<br>
- int main() {<br>
- test_polygon<CPolygon>(); //woot!<br>
- return 0;<br>
- }<br>
- </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 © 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>
|