Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
geometry/example/c08_custom_non_std_example.cpp at develop · boostorg/geometry · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
boostorg
geometry
Repository navigation
Code
Issues
146
(146)
Pull requests
16
(16)
Discussions
Actions
Projects
Wiki
Security and quality
Insights
More
items
Files
Expand file tree
develop
Breadcrumbs
geometry
/
example
/
c08_custom_non_std_example.cpp
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
324 lines (255 loc) · 8.27 KB
develop
Breadcrumbs
geometry
/
example
/
c08_custom_non_std_example.cpp
Copy path
Top
File metadata and controls
Code
Blame
324 lines (255 loc) · 8.27 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
//
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 polygon example
#
include
<
iostream
>
#
include
<
type_traits
>
#
include
<
boost/assert.hpp
>
#
include
<
boost/iterator/iterator_adaptor.hpp
>
#
include
<
boost/iterator/iterator_categories.hpp
>
#
include
<
boost/iterator/iterator_facade.hpp
>
#
include
<
boost/geometry/geometry.hpp
>
#
include
<
boost/geometry/geometries/register/point.hpp
>
#
include
<
boost/geometry/geometries/register/ring.hpp
>
#
include
<
boost/geometry/util/add_const_if_c.hpp
>
//
Sample point, having x/y
struct
my_point
{
my_point
(
double
a =
0
,
double
b =
0
)
: x(a), y(b)
{}
double
x,y;
};
//
Sample polygon, having legacy methods
//
(similar to e.g. COM objects)
class
my_polygon
{
std::vector<my_point> points;
public :
void
add_point
(my_point
const
& p) { points.
push_back
(p); }
//
Const access
my_point
const
&
get_point
(std::
size_t
i)
const
{
BOOST_ASSERT
(i < points.
size
());
return
points[i];
}
//
Mutable access
my_point &
get_point
(std::
size_t
i)
{
BOOST_ASSERT
(i < points.
size
());
return
points[i];
}
int
point_count
()
const
{
return
points.
size
(); }
void
erase_all
() { points.
clear
(); }
inline
void
set_size
(
int
n) { points.
resize
(n); }
};
//
----------------------------------------------------------------------------
//
Adaption: implement iterator and range-extension, and register with Boost.Geometry
//
1) implement iterator (const and non-const versions)
template
<
typename
MyPolygon>
struct
custom_iterator
:
public
boost
::iterator_facade
<
custom_iterator<MyPolygon>,
my_point,
boost::random_access_traversal_tag,
typename
boost::mpl::if_
<
std::is_const<MyPolygon>,
my_point
const
,
my_point
>::type&
>
{
//
Constructor for begin()
explicit
custom_iterator
(MyPolygon& polygon)
: m_polygon(&polygon)
, m_index(
0
)
{}
//
Constructor for end()
explicit
custom_iterator
(
bool
, MyPolygon& polygon)
: m_polygon(&polygon)
, m_index(polygon.point_count())
{}
//
Default constructor
explicit
custom_iterator
()
: m_polygon(
NULL
)
, m_index(-
1
)
{}
typedef
typename
boost::mpl::if_
<
std::is_const<MyPolygon>,
my_point
const
,
my_point
>::type my_point_type;
private:
friend
class
boost
::iterator_core_access;
typedef
boost::iterator_facade
<
custom_iterator<MyPolygon>,
my_point,
boost::random_access_traversal_tag,
my_point_type&
> facade;
MyPolygon* m_polygon;
int
m_index;
bool
equal
(custom_iterator
const
& other)
const
{
return
this
->
m_index
== other.
m_index
;
}
typename
facade::difference_type
distance_to
(custom_iterator
const
& other)
const
{
return
other.
m_index
-
this
->
m_index
;
}
void
advance
(
typename
facade::difference_type n)
{
m_index += n;
if
(m_polygon !=
NULL
&& (m_index >= m_polygon->
point_count
()
|| m_index <
0
)
)
{
m_index = m_polygon->
point_count
();
}
}
void
increment
()
{
advance
(
1
);
}
void
decrement
()
{
advance
(-
1
);
}
//
const and non-const dereference of this iterator
my_point_type&
dereference
()
const
{
return
m_polygon->
get_point
(m_index);
}
};
//
2) Implement Boost.Range const functionality
//
using method 2, "provide free-standing functions and specialize metafunctions"
//
2a) meta-functions
namespace
boost
{
template
<>
struct
range_mutable_iterator
<my_polygon>
{
typedef
custom_iterator<my_polygon> type;
};
template
<>
struct
range_const_iterator
<my_polygon>
{
typedef
custom_iterator<my_polygon
const
> type;
};
//
RangeEx
template
<>
struct
range_size
<my_polygon>
{
typedef
std::
size_t
type;
};
}
//
namespace 'boost'
//
2b) free-standing function for Boost.Range ADP
inline
custom_iterator<my_polygon>
range_begin
(my_polygon& polygon)
{
return
custom_iterator<my_polygon>(polygon);
}
inline
custom_iterator<my_polygon
const
>
range_begin
(my_polygon
const
& polygon)
{
return
custom_iterator<my_polygon
const
>(polygon);
}
inline
custom_iterator<my_polygon>
range_end
(my_polygon& polygon)
{
return
custom_iterator<my_polygon>(
true
, polygon);
}
inline
custom_iterator<my_polygon
const
>
range_end
(my_polygon
const
& polygon)
{
return
custom_iterator<my_polygon
const
>(
true
, polygon);
}
//
3) optional, for writable geometries only, implement push_back/resize/clear
namespace
boost
{
namespace
geometry
{
namespace
traits
{
template
<>
struct
push_back
<my_polygon>
{
static
inline
void
apply
(my_polygon& polygon, my_point
const
& point)
{
polygon.
add_point
(point);
}
};
template
<>
struct
resize
<my_polygon>
{
static
inline
void
apply
(my_polygon& polygon, std::
size_t
new_size)
{
polygon.
set_size
(new_size);
}
};
template
<>
struct
clear
<my_polygon>
{
static
inline
void
apply
(my_polygon& polygon)
{
polygon.
erase_all
();
}
};
}}}
//
4) register with Boost.Geometry
BOOST_GEOMETRY_REGISTER_POINT_2D
(my_point,
double
, cs::cartesian, x, y)
BOOST_GEOMETRY_REGISTER_RING
(my_polygon)
//
end adaption
//
----------------------------------------------------------------------------
void
walk_using_iterator
(my_polygon
const
& polygon)
{
for
(custom_iterator<my_polygon
const
> it = custom_iterator<my_polygon
const
>(polygon);
it != custom_iterator<my_polygon
const
>(
true
, polygon);
++it)
{
std::cout <<
boost::geometry::dsv
(*it) << std::endl;
}
std::cout << std::endl;
}
void
walk_using_range
(my_polygon
const
& polygon)
{
for
(boost::range_iterator<my_polygon
const
>::type it
=
boost::begin
(polygon);
it !=
boost::end
(polygon);
++it)
{
std::cout <<
boost::geometry::dsv
(*it) << std::endl;
}
std::cout << std::endl;
}
int
main
()
{
my_polygon container1;
//
Create (as an example) a regular polygon
const
int
n =
5
;
const
double
d = (
360
/ n) * boost::geometry::math::d2r<
double
>();
double
a =
0
;
for
(
int
i =
0
; i < n +
1
; i++, a += d)
{
container1.
add_point
(
my_point
(
sin
(a),
cos
(a)));
}
std::cout <<
"
Walk using Boost.Iterator derivative
"
<< std::endl;
walk_using_iterator
(container1);
std::cout <<
"
Walk using Boost.Range extension
"
<< std::endl << std::endl;
walk_using_range
(container1);
std::cout <<
"
Use it by Boost.Geometry
"
<< std::endl;
std::cout <<
"
Area:
"
<<
boost::geometry::area
(container1) << std::endl;
//
Container 2 will be modified by Boost.Geometry. Add all points but the last one.
my_polygon container2;
for
(
int
i =
0
; i < n; i++)
{
//
Use here the Boost.Geometry internal way of inserting (but the my_polygon way of getting)
boost::geometry::traits::push_back<my_polygon>::
apply
(container2, container1.
get_point
(i));
}
std::cout <<
"
Second container is not closed:
"
<< std::endl;
walk_using_range
(container2);
//
Correct (= close it)
boost::geometry::correct
(container2);
std::cout <<
"
Now it is closed:
"
<< std::endl;
walk_using_range
(container2);
std::cout <<
"
Area:
"
<<
boost::geometry::area
(container2) << std::endl;
//
Use things from std:: using Boost.Range
std::reverse
(
boost::begin
(container2),
boost::end
(container2));
std::cout <<
"
Area reversed:
"
<<
boost::geometry::area
(container2) << std::endl;
return
0
;
}
You can’t perform that action at this time.