1 // Boost.Geometry (aka GGL, Generic Geometry Library)
2
3 // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
4 // Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
5 // Copyright (c) 2009-2012 Mateusz Loskot, London, UK.
6
7 // Use, modification and distribution is subject to the Boost Software License,
8 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
9 // http://www.boost.org/LICENSE_1_0.txt)
10 //
11 // Custom pointer-to-point example
12
13 #include <iostream>
14
15 #include <boost/foreach.hpp>
16
17 #include <boost/geometry.hpp>
18 #include <boost/geometry/geometries/geometries.hpp>
19 #include <boost/geometry/geometries/point_xy.hpp>
20 #include <boost/geometry/geometries/register/ring.hpp>
21
22 BOOST_GEOMETRY_REGISTER_RING_TEMPLATED(std::vector)
23
24
25 // Sample point, having x/y
26 struct my_point
27 {
my_pointmy_point28 my_point(double a = 0, double b = 0)
29 : x(a), y(b)
30 {}
31 double x,y;
32 };
33
34
35 namespace boost { namespace geometry { namespace traits {
36
37 template<> struct tag<my_point>
38 { typedef point_tag type; };
39
40 template<> struct coordinate_type<my_point>
41 { typedef double type; };
42
43 template<> struct coordinate_system<my_point>
44 { typedef cs::cartesian type; };
45
46 template<> struct dimension<my_point> : boost::mpl::int_<2> {};
47
48 template<>
49 struct access<my_point, 0>
50 {
getboost::geometry::traits::access51 static double get(my_point const& p)
52 {
53 return p.x;
54 }
55
setboost::geometry::traits::access56 static void set(my_point& p, double const& value)
57 {
58 p.x = value;
59 }
60 };
61
62 template<>
63 struct access<my_point, 1>
64 {
getboost::geometry::traits::access65 static double get(my_point const& p)
66 {
67 return p.y;
68 }
69
setboost::geometry::traits::access70 static void set(my_point& p, double const& value)
71 {
72 p.y = value;
73 }
74 };
75
76 }}} // namespace boost::geometry::traits
77
78
79
main()80 int main()
81 {
82 typedef std::vector<my_point*> ring_type;
83
84 ring_type a, b;
85
86 a.push_back(new my_point(0, 1));
87 a.push_back(new my_point(2, 5));
88 a.push_back(new my_point(5, 3));
89 a.push_back(new my_point(0, 1));
90
91 b.push_back(new my_point(3, 0));
92 b.push_back(new my_point(0, 3));
93 b.push_back(new my_point(4, 5));
94 b.push_back(new my_point(3, 0));
95
96 double aa = boost::geometry::area(a);
97 double ab = boost::geometry::area(b);
98
99 std::cout << "a: " << aa << std::endl;
100 std::cout << "b: " << ab << std::endl;
101
102 // This will NOT work because would need dynamicly allocating memory for point* in algorithms:
103 //std::vector<ring_type> unioned;
104 //boost::geometry::union<ring_type>(a, b, unioned);
105
106 // BEGIN TODO
107 // This compiles (and once worked) using pointers, but has to be fixed or deprecated
108 // The problem is now the cart_intersect/side where a temporary point is generated
109 //typedef boost::geometry::model::ring<boost::geometry::model::d2::point_xy<double> > ring_2d;
110 //std::vector<ring_2d> unioned;
111 //std::vector<ring_2d> intersected;
112
113 //boost::geometry::intersection(a, b, intersected);
114 //boost::geometry::union_(a, b, unioned);
115
116 //double ai = 0, au = 0;
117 //BOOST_FOREACH(ring_2d const& ring, intersected)
118 //{
119 // ai += boost::geometry::area(ring);
120 //}
121 //BOOST_FOREACH(ring_2d const& ring, unioned)
122 //{
123 // au += boost::geometry::area(ring);
124 //}
125
126 //std::cout << "a: " << aa << std::endl;
127 //std::cout << "b: " << ab << std::endl;
128 //std::cout << "a & b: " << ai << std::endl;
129 //std::cout << "a | b: " << au << std::endl;
130 //std::cout << "a + b - (a & b): " << (aa + ab - ai) << std::endl;
131 // END TODO
132
133
134 // free
135 BOOST_FOREACH(my_point* p, a)
136 {
137 delete p;
138 }
139
140 BOOST_FOREACH(my_point* p, b)
141 {
142 delete p;
143 }
144
145 return 0;
146 }
147