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