summaryrefslogtreecommitdiff
path: root/libs/geometry/test/robustness/convex_hull/random_multi_points.cpp
blob: a0d3efb540bbc10af38d029082fa821f7c1469b3 (plain)
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
// Boost.Geometry (aka GGL, Generic Geometry Library)
// Robustness Test - convex_hull

// Copyright (c) 2012 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)

#define BOOST_GEOMETRY_REPORT_OVERLAY_ERROR
#define BOOST_GEOMETRY_NO_BOOST_TEST

#include <sstream>
#include <fstream>

#include <boost/program_options.hpp>
#include <boost/random/linear_congruential.hpp>
#include <boost/random/uniform_int.hpp>
#include <boost/random/uniform_real.hpp>
#include <boost/random/variate_generator.hpp>
#include <boost/timer.hpp>

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/geometries.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/multi/geometries/multi_geometries.hpp>
#include <boost/geometry/io/svg/svg_mapper.hpp>

struct settings_type
{
    bool svg;
    bool wkt;

    settings_type()
        : svg(false)
        , wkt(false)
    {}
};

namespace bg = boost::geometry;

template <typename Geometry1, typename Geometry2>
void create_svg(std::string const& filename, Geometry1 const& points, Geometry2 const& hull)
{
    typedef typename boost::geometry::point_type<Geometry1>::type point_type;

    boost::geometry::model::box<point_type> box;
    bg::envelope(hull, box);
    bg::buffer(box, box, 1.0);

    std::ofstream svg(filename.c_str());
    boost::geometry::svg_mapper<point_type> mapper(svg, 800, 800);
    mapper.add(box);

    mapper.map(hull, "opacity:0.8;fill:none;stroke:rgb(255,0,255);stroke-width:4;stroke-dasharray:1,7;stroke-linecap:round");
    mapper.map(points, "fill-opacity:0.5;fill:rgb(0,0,255);", 5);
}


template <typename MultiPoint, typename Generator>
inline void make_multi_point(MultiPoint& mp, Generator& generator, int pcount)
{
    typedef typename bg::point_type<MultiPoint>::type point_type;
    typedef typename bg::coordinate_type<MultiPoint>::type coordinate_type;

    for(int i = 0; i < pcount; i++)
    {
        coordinate_type x, y;
        x = generator();
        y = generator();

        point_type p;
        bg::set<0>(p, x);
        bg::set<1>(p, y);

        mp.push_back(p);
    }
}

template <typename MultiPoint, typename Polygon>
bool check_hull(MultiPoint const& mp, Polygon const& poly)
{
    for(typename boost::range_iterator<MultiPoint const>::type it = boost::begin(mp);
        it != boost::end(mp);
        ++it)
    {
        if (! bg::covered_by(*it, poly))
        {
            return false;
        }
    }
    return true;
}


template <typename MultiPoint, typename Generator>
void test_random_multi_points(MultiPoint& result, int& index,
            Generator& generator,
            int pcount, settings_type const& settings)
{
    typedef typename bg::point_type<MultiPoint>::type point_type;

    MultiPoint mp;
    bg::model::polygon<point_type> hull;

    make_multi_point(mp, generator, pcount);
    bg::convex_hull(mp, hull);
    // Check if each point lies in the hull
    bool correct = check_hull(mp, hull);
    if (! correct)
    {
        std::cout << "ERROR! " << std::endl
            << bg::wkt(mp) << std::endl
            << bg::wkt(hull) << std::endl
            << std::endl;
            ;
    }

    if (settings.svg || ! correct)
    {
        std::ostringstream out;
        out << "random_mp_" << index++ << "_" << pcount << ".svg";
        create_svg(out.str(), mp, hull);
    }
    if (settings.wkt)
    {
        std::cout 
            << "input: " << bg::wkt(mp) << std::endl
            << "output: " << bg::wkt(hull) << std::endl
            << std::endl;
            ;
    }
}


template <typename T>
void test_all(int seed, int count, int field_size, int pcount, settings_type const& settings)
{
    boost::timer t;

    typedef boost::minstd_rand base_generator_type;

    base_generator_type generator(seed);

    boost::uniform_int<> random_coordinate(0, field_size - 1);
    boost::variate_generator<base_generator_type&, boost::uniform_int<> >
        coordinate_generator(generator, random_coordinate);

    typedef bg::model::multi_point
        <
            bg::model::d2::point_xy<T>
        > mp;

    int index = 0;
    for(int i = 0; i < count; i++)
    {
        mp p;
        test_random_multi_points<mp>(p, index, coordinate_generator, pcount, settings);
    }
    std::cout
        << "points: " << index
        << " type: " << typeid(T).name()
        << " time: " << t.elapsed()  << std::endl;
}

int main(int argc, char** argv)
{
    try
    {
        namespace po = boost::program_options;
        po::options_description description("=== random_multi_points ===\nAllowed options");

        std::string type = "double";
        int count = 1;
        int seed = static_cast<unsigned int>(std::time(0));
        int pcount = 3;
        int field_size = 10;
        settings_type settings;

        description.add_options()
            ("help", "Help message")
            ("seed", po::value<int>(&seed), "Initialization seed for random generator")
            ("count", po::value<int>(&count)->default_value(1), "Number of tests")
            ("number", po::value<int>(&pcount)->default_value(30), "Number of points")
            ("size", po::value<int>(&field_size)->default_value(10), "Size of the field")
            ("type", po::value<std::string>(&type)->default_value("double"), "Type (int,float,double)")
            ("wkt", po::value<bool>(&settings.wkt)->default_value(false), "Create a WKT of the inputs, for all tests")
            ("svg", po::value<bool>(&settings.svg)->default_value(false), "Create a SVG for all tests")
        ;

        po::variables_map varmap;
        po::store(po::parse_command_line(argc, argv, description), varmap);
        po::notify(varmap);

        if (varmap.count("help"))
        {
            std::cout << description << std::endl;
            return 1;
        }

        if (type == "float")
        {
            test_all<float>(seed, count, field_size, pcount, settings);
        }
        else if (type == "double")
        {
            test_all<double>(seed, count, field_size, pcount, settings);
        }
        else if (type == "int")
        {
            test_all<int>(seed, count, field_size, pcount, settings);
        }

    }
    catch(std::exception const& e)
    {
        std::cout << "Exception " << e.what() << std::endl;
    }
    catch(...)
    {
        std::cout << "Other exception" << std::endl;
    }

    return 0;
}