File: geometry_envelope_test.cpp

package info (click to toggle)
mapnik 3.0.12%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 17,084 kB
  • ctags: 18,454
  • sloc: cpp: 142,516; python: 1,416; sh: 769; makefile: 170; xml: 140; lisp: 13
file content (152 lines) | stat: -rw-r--r-- 4,798 bytes parent folder | download | duplicates (4)
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
#include "catch.hpp"

#include <mapnik/geometry.hpp>
#include <mapnik/geometry_envelope.hpp>

namespace {

template <typename T>
void envelope_test()
{
    using namespace mapnik::geometry;
    using coord_type = T;

    {
        geometry<coord_type> geom(point<coord_type>(1,2));
        mapnik::box2d<coord_type> bbox = mapnik::geometry::envelope(geom);
        REQUIRE( bbox.minx() == 1 );
        REQUIRE( bbox.miny() == 2 );
        REQUIRE( bbox.maxx() == 1 );
        REQUIRE( bbox.maxy() == 2 );
    }
    {
        // Test empty geom
        geometry<coord_type> geom = mapnik::geometry::geometry_empty();
        mapnik::box2d<coord_type> bbox = mapnik::geometry::envelope(geom);
        REQUIRE_FALSE( bbox.valid() );
    }
    {
        line_string<coord_type> line;
        line.add_coord(0,0);
        line.add_coord(1,1);
        line.add_coord(2,2);
        geometry<coord_type> geom(line);
        mapnik::box2d<coord_type> bbox = mapnik::geometry::envelope(geom);
        REQUIRE( bbox.minx() == 0 );
        REQUIRE( bbox.miny() == 0 );
        REQUIRE( bbox.maxx() == 2 );
        REQUIRE( bbox.maxy() == 2 );
    }
    {
        line_string<coord_type> line;
        line.add_coord(0,0);
        line.add_coord(1,1);
        line.add_coord(2,2);
        line_string<coord_type> line2;
        line2.add_coord(0,0);
        line2.add_coord(-1,-1);
        line2.add_coord(-2,-2);
        multi_line_string<coord_type> multi_line;
        multi_line.emplace_back(std::move(line));
        multi_line.emplace_back(std::move(line2));
        geometry<coord_type> geom(multi_line);
        mapnik::box2d<coord_type> bbox = mapnik::geometry::envelope(geom);
        REQUIRE( bbox.minx() == -2 );
        REQUIRE( bbox.miny() == -2 );
        REQUIRE( bbox.maxx() == 2 );
        REQUIRE( bbox.maxy() == 2 );
    }
    {
        polygon<coord_type> poly;
        linear_ring<coord_type> ring;
        ring.add_coord(0,0);
        ring.add_coord(-10,0);
        ring.add_coord(-10,10);
        ring.add_coord(0,10);
        ring.add_coord(0,0);
        poly.set_exterior_ring(std::move(ring));
        geometry<coord_type> geom(poly);
        mapnik::box2d<coord_type> bbox = mapnik::geometry::envelope(geom);
        REQUIRE( bbox.minx() == -10 );
        REQUIRE( bbox.miny() == 0 );
        REQUIRE( bbox.maxx() == 0 );
        REQUIRE( bbox.maxy() == 10 );

        multi_polygon<coord_type> mp;
        mp.push_back(poly);
        geometry<coord_type> geom_mp(mp);
        bbox = mapnik::geometry::envelope(geom_mp);
        REQUIRE( bbox.minx() == -10 );
        REQUIRE( bbox.miny() == 0 );
        REQUIRE( bbox.maxx() == 0 );
        REQUIRE( bbox.maxy() == 10 );

        geometry_collection<coord_type> gc;
        bbox = mapnik::geometry::envelope(gc);
        REQUIRE_FALSE( bbox.valid() );
        gc.push_back(geom_mp);
        bbox = mapnik::geometry::envelope(gc);
        REQUIRE( bbox.minx() == -10 );
        REQUIRE( bbox.miny() == 0 );
        REQUIRE( bbox.maxx() == 0 );
        REQUIRE( bbox.maxy() == 10 );
        gc.emplace_back(point<coord_type>(-50,-50));
        bbox = mapnik::geometry::envelope(gc);
        REQUIRE( bbox.minx() == -50 );
        REQUIRE( bbox.miny() == -50 );
        REQUIRE( bbox.maxx() == 0 );
        REQUIRE( bbox.maxy() == 10 );
    }

    {
        // polygon with hole
        polygon<coord_type> poly;
        linear_ring<coord_type> ring;
        ring.add_coord(0,0);
        ring.add_coord(-10,0);
        ring.add_coord(-10,10);
        ring.add_coord(0,10);
        ring.add_coord(0,0);
        poly.set_exterior_ring(std::move(ring));
        linear_ring<coord_type> hole;
        hole.add_coord(-7,7);
        hole.add_coord(-7,3);
        hole.add_coord(-3,3);
        hole.add_coord(-3,7);
        hole.add_coord(-7,7);
        poly.add_hole(std::move(hole));
        geometry<coord_type> geom(poly);
        mapnik::box2d<coord_type> bbox = mapnik::geometry::envelope(poly);
        REQUIRE( bbox.minx() == -10 );
        REQUIRE( bbox.miny() == 0 );
        REQUIRE( bbox.maxx() == 0 );
        REQUIRE( bbox.maxy() == 10 );
        // add another hole inside the first hole
        // which should be considered a hit
        linear_ring<coord_type> fill;
        fill.add_coord(-6,4);
        fill.add_coord(-6,6);
        fill.add_coord(-4,6);
        fill.add_coord(-4,4);
        fill.add_coord(-6,4);
        poly.add_hole(std::move(fill));
        bbox = mapnik::geometry::envelope(poly);
        REQUIRE( bbox.minx() == -10 );
        REQUIRE( bbox.miny() == 0 );
        REQUIRE( bbox.maxx() == 0 );
        REQUIRE( bbox.maxy() == 10 );
    }
}

}

TEST_CASE("geometry ops - envelope") {

SECTION("envelope_test")
{
    envelope_test<int>();
    envelope_test<double>();
    envelope_test<float>();
}

}