1 // Boost.Polygon library voronoi_advanced_tutorial.cpp file
3 // Copyright Andrii Sydorchuk 2010-2012.
4 // Distributed under the Boost Software License, Version 1.0.
5 // (See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
8 // See http://www.boost.org for updates, documentation, and revision history.
15 // This will work properly only with GCC compiler.
17 typedef long double fpt80;
19 // Random generators and distributions.
20 #include <boost/random/mersenne_twister.hpp>
21 #include <boost/random/uniform_int_distribution.hpp>
22 #include <boost/timer/timer.hpp>
24 #include <boost/polygon/voronoi.hpp>
25 using namespace boost::polygon;
27 struct my_ulp_comparison {
34 Result operator()(fpt80 a, fpt80 b, unsigned int maxUlps) const {
38 Result res = operator()(b, a, maxUlps);
39 if (res == EQUAL) return res;
40 return (res == LESS) ? MORE : LESS;
42 ieee854_long_double lhs, rhs;
45 if (lhs.ieee.negative ^ rhs.ieee.negative)
46 return lhs.ieee.negative ? LESS : MORE;
47 boost::uint64_t le = lhs.ieee.exponent; le =
48 (le << 32) + lhs.ieee.mantissa0;
49 boost::uint64_t re = rhs.ieee.exponent; re =
50 (re << 32) + rhs.ieee.mantissa0;
51 if (lhs.ieee.negative) {
54 le = (le == re) ? 0 : 1;
55 le = (le << 32) + lhs.ieee.mantissa1;
56 re = rhs.ieee.mantissa1;
57 return (re + maxUlps < le) ? LESS : EQUAL;
61 le = lhs.ieee.mantissa0;
62 re = (le == re) ? 0 : 1;
63 re = (re << 32) + rhs.ieee.mantissa1;
64 return (le + maxUlps < re) ? LESS : EQUAL;
69 struct my_fpt_converter {
71 fpt80 operator()(const T& that) const {
72 return static_cast<fpt80>(that);
75 template <std::size_t N>
76 fpt80 operator()(const typename detail::extended_int<N> &that) const {
78 for (std::size_t i = 1; i <= (std::min)((std::size_t)3, that.size()); ++i) {
80 result *= static_cast<fpt80>(0x100000000ULL);
81 result += that.chunks()[that.size() - i];
83 return (that.count() < 0) ? -result : result;
87 // Voronoi diagram traits.
88 struct my_voronoi_diagram_traits {
89 typedef fpt80 coordinate_type;
90 typedef voronoi_cell<coordinate_type> cell_type;
91 typedef voronoi_vertex<coordinate_type> vertex_type;
92 typedef voronoi_edge<coordinate_type> edge_type;
96 bool operator()(const vertex_type &v1, const vertex_type &v2) const {
97 return (ulp_cmp(v1.x(), v2.x(), ULPS) == my_ulp_comparison::EQUAL &&
98 ulp_cmp(v1.y(), v2.y(), ULPS) == my_ulp_comparison::EQUAL);
101 my_ulp_comparison ulp_cmp;
102 } vertex_equality_predicate_type;
105 // Voronoi ctype traits for 48-bit signed integer input coordinates.
106 struct my_voronoi_ctype_traits {
107 typedef boost::int64_t int_type;
108 typedef detail::extended_int<3> int_x2_type;
109 typedef detail::extended_int<3> uint_x2_type;
110 typedef detail::extended_int<128> big_int_type;
111 typedef fpt80 fpt_type;
112 typedef fpt80 efpt_type;
113 typedef my_ulp_comparison ulp_cmp_type;
114 typedef my_fpt_converter to_fpt_converter_type;
115 typedef my_fpt_converter to_efpt_converter_type;
118 const unsigned int GENERATED_POINTS = 100;
119 const boost::int64_t MAX = 0x1000000000000LL;
122 boost::mt19937_64 gen(std::time(0));
123 boost::random::uniform_int_distribution<boost::int64_t> distr(-MAX, MAX-1);
124 voronoi_builder<boost::int64_t, my_voronoi_ctype_traits> vb;
125 for (size_t i = 0; i < GENERATED_POINTS; ++i) {
126 boost::int64_t x = distr(gen);
127 boost::int64_t y = distr(gen);
128 vb.insert_point(x, y);
131 printf("Constructing Voronoi diagram of %d points...\n", GENERATED_POINTS);
132 boost::timer::cpu_timer t;
133 voronoi_diagram<fpt80, my_voronoi_diagram_traits> vd;
136 boost::timer::cpu_times times = t.elapsed();
137 std::string ftime = boost::timer::format(times, 5, "%w");
139 printf("Construction done in: %s seconds.\n", ftime.c_str());
140 printf("Resulting Voronoi graph has the following stats:\n");
141 printf("Number of Voronoi cells: %lu.\n", vd.num_cells());
142 printf("Number of Voronoi vertices: %lu.\n", vd.num_vertices());
143 printf("Number of Voronoi edges: %lu.\n", vd.num_edges());