Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r71417 - trunk/libs/geometry/doc/src/examples/core
From: barend.gehrels_at_[hidden]
Date: 2011-04-22 07:03:32


Author: barendgehrels
Date: 2011-04-22 07:03:31 EDT (Fri, 22 Apr 2011)
New Revision: 71417
URL: http://svn.boost.org/trac/boost/changeset/71417

Log:
Added / updated core examples
Added:
   trunk/libs/geometry/doc/src/examples/core/rings.cpp (contents, props changed)
Text files modified:
   trunk/libs/geometry/doc/src/examples/core/Jamfile.v2 | 1
   trunk/libs/geometry/doc/src/examples/core/get_box.cpp | 2
   trunk/libs/geometry/doc/src/examples/core/tag.cpp | 97 +++++++++++++++++++++++++++++++--------
   3 files changed, 79 insertions(+), 21 deletions(-)

Modified: trunk/libs/geometry/doc/src/examples/core/Jamfile.v2
==============================================================================
--- trunk/libs/geometry/doc/src/examples/core/Jamfile.v2 (original)
+++ trunk/libs/geometry/doc/src/examples/core/Jamfile.v2 2011-04-22 07:03:31 EDT (Fri, 22 Apr 2011)
@@ -26,5 +26,6 @@
 exe closure : closure.cpp ;
 
 exe point_type : point_type.cpp ;
+exe rings : rings.cpp ;
 exe tag : tag.cpp ;
 exe tag_cast : tag_cast.cpp ;

Modified: trunk/libs/geometry/doc/src/examples/core/get_box.cpp
==============================================================================
--- trunk/libs/geometry/doc/src/examples/core/get_box.cpp (original)
+++ trunk/libs/geometry/doc/src/examples/core/get_box.cpp 2011-04-22 07:03:31 EDT (Fri, 22 Apr 2011)
@@ -19,7 +19,7 @@
 {
     bg::model::box<bg::model::d2::point_xy<double> > box;
 
- bg::assign(box, 1, 3, 5, 6);
+ bg::assign_values(box, 1, 3, 5, 6);
 
     std::cout << "Box:"
         << " " << bg::get<bg::min_corner, 0>(box)

Added: trunk/libs/geometry/doc/src/examples/core/rings.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/geometry/doc/src/examples/core/rings.cpp 2011-04-22 07:03:31 EDT (Fri, 22 Apr 2011)
@@ -0,0 +1,74 @@
+// Boost.Geometry (aka GGL, Generic Geometry Library)
+// QuickBook Example
+
+// Copyright (c) 2011 Barend Gehrels, Amsterdam, the Netherlands.
+
+// Use, modification and distribution is subject to the Boost Software License,
+// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
+// http://www.boost.org/LICENSE_1_0.txt)
+
+//[rings
+/*`
+Shows how to access the exterior ring (one)
+and interior rings (zero or more) of a polygon.
+Also shows the related ring_type and interior_type.
+*/
+
+#include <iostream>
+#include <boost/geometry/geometry.hpp>
+
+
+int main()
+{
+ typedef boost::geometry::model::d2::point_xy<double> point;
+ typedef boost::geometry::model::polygon<point> polygon_type;
+
+ polygon_type poly;
+
+ typedef boost::geometry::ring_type<polygon_type>::type ring_type;
+ ring_type& ring = boost::geometry::exterior_ring(poly);
+
+ // For a ring of model::polygon, you can call "push_back".
+ // (internally, it is done using a traits::push_back class)
+ ring.push_back(point(0, 0));
+ ring.push_back(point(0, 5));
+ ring.push_back(point(5, 4));
+ ring.push_back(point(0, 0));
+
+ ring_type inner;
+ inner.push_back(point(1, 1));
+ inner.push_back(point(2, 1));
+ inner.push_back(point(2, 2));
+ inner.push_back(point(1, 1));
+
+ typedef boost::geometry::interior_type<polygon_type>::type int_type;
+ int_type& interiors = boost::geometry::interior_rings(poly);
+ interiors.push_back(inner);
+
+ std::cout << boost::geometry::dsv(poly) << std::endl;
+
+ // So int_type defines a collection of rings,
+ // which is a Boost.Range compatible range
+ // The type of an element of the collection is the very same ring type again.
+ // We show that.
+ typedef boost::range_value<int_type>::type int_ring_type;
+
+ std::cout
+ //<< std::autoboolstring
+ << boost::is_same<ring_type, int_ring_type>::value
+ << std::endl;
+
+ return 0;
+}
+
+//]
+
+//[rings_output
+/*`
+Output:
+[pre
+(((0, 0), (0, 5), (5, 4), (0, 0)), ((1, 1), (2, 1), (2, 2), (1, 1)))
+true
+]
+*/
+//]

Modified: trunk/libs/geometry/doc/src/examples/core/tag.cpp
==============================================================================
--- trunk/libs/geometry/doc/src/examples/core/tag.cpp (original)
+++ trunk/libs/geometry/doc/src/examples/core/tag.cpp 2011-04-22 07:03:31 EDT (Fri, 22 Apr 2011)
@@ -8,41 +8,98 @@
 // http://www.boost.org/LICENSE_1_0.txt)
 
 //[tag
-//` Examine the tag of some geometry types
+//` Shows how tag dispatching essentially works in Boost.Geometry
 
 #include <iostream>
-#include <typeinfo>
 #include <boost/geometry/geometry.hpp>
+#include <boost/geometry/geometries/adapted/tuple_cartesian.hpp>
+
+#include <boost/assign.hpp>
+
+template <typename Tag> struct dispatch {};
+
+// Specialization for points
+template <> struct dispatch<boost::geometry::point_tag>
+{
+ template <typename Point>
+ static inline void apply(Point const& p)
+ {
+ // Use the Boost.Geometry free function "get"
+ // working on all supported point types
+ std::cout << "Hello POINT "
+ << boost::geometry::get<0>(p) << ", "
+ << boost::geometry::get<1>(p)
+ << std::endl;
+ }
+};
+
+// Specialization for polygons
+template <> struct dispatch<boost::geometry::polygon_tag>
+{
+ template <typename Polygon>
+ static inline void apply(Polygon const& p)
+ {
+ // Use the Boost.Geometry manipulator "dsv"
+ // working on all supported geometries
+ std::cout << "Hello POLYGON, you look like: "
+ << boost::geometry::dsv(p)
+ << std::endl;
+ }
+};
+
+// Specialization for multipolygons
+template <> struct dispatch<boost::geometry::multi_polygon_tag>
+{
+ template <typename MultiPolygon>
+ static inline void apply(MultiPolygon const& m)
+ {
+ // Use the Boost.Range free function "size" because all
+ // multigeometries comply to Boost.Range
+ std::cout << "Hello MULTIPOLYGON, you contain: "
+ << boost::size(m) << " polygon(s)"
+ << std::endl;
+ }
+};
+
+template <typename Geometry>
+inline void hello(Geometry const& geometry)
+{
+ // Call the meta-function "tag" to dispatch, and call method (here "apply")
+ dispatch
+ <
+ typename boost::geometry::tag<Geometry>::type
+ >::apply(geometry);
+}
 
 int main()
 {
- typedef boost::geometry::model::d2::point_xy<double> point_type;
- typedef boost::geometry::model::polygon<point_type> polygon_type;
- typedef boost::geometry::model::multi_polygon<polygon_type> mp_type;
-
- typedef boost::geometry::tag<point_type>::type tag1;
- typedef boost::geometry::tag<polygon_type>::type tag2;
- typedef boost::geometry::tag<mp_type>::type tag3;
-
- std::cout
- << "tag 1: " << typeid(tag1).name() << std::endl
- << "tag 2: " << typeid(tag2).name() << std::endl
- << "tag 3: " << typeid(tag3).name() << std::endl
- ;
+ // Define polygon type (here: based on a Boost.Tuple)
+ typedef boost::geometry::model::polygon<boost::tuple<int, int> > polygon_type;
+
+ // Declare and fill a polygon and a multipolygon
+ polygon_type poly;
+ boost::geometry::exterior_ring(poly) = boost::assign::tuple_list_of(0, 0)(0, 10)(10, 5)(0, 0);
+
+ boost::geometry::model::multi_polygon<polygon_type> multi;
+ multi.push_back(poly);
+
+ // Call "hello" for point, polygon, multipolygon
+ hello(boost::make_tuple(2, 3));
+ hello(poly);
+ hello(multi);
 
     return 0;
 }
 
 //]
 
-
 //[tag_output
 /*`
-Output (in MSVC):
+Output:
 [pre
-tag 1: struct boost::geometry::point_tag
-tag 2: struct boost::geometry::polygon_tag
-tag 3: struct boost::geometry::multi_polygon_tag
+Hello POINT 2, 3
+Hello POLYGON, you look like: (((0, 0), (0, 10), (10, 5), (0, 0)))
+Hello MULTIPOLYGON, you contain: 1 polygon(s)
 ]
 */
 //]


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk