summaryrefslogtreecommitdiff
path: root/libs/geometry/test/algorithms/distance_linear_linear.cpp
blob: 9c329e429c8ff8319b1480fb61f1a93dfc2f580f (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
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
// Boost.Geometry (aka GGL, Generic Geometry Library)
// Unit Test

// Copyright (c) 2014, Oracle and/or its affiliates.

// Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle

// Licensed under the Boost Software License version 1.0.
// http://www.boost.org/users/license.html

#include <iostream>

#ifndef BOOST_TEST_MODULE
#define BOOST_TEST_MODULE test_distance_linear_linear
#endif

#include <boost/test/included/unit_test.hpp>

#include "test_distance_common.hpp"


typedef bg::model::point<double,2,bg::cs::cartesian>  point_type;
typedef bg::model::segment<point_type>                segment_type;
typedef bg::model::linestring<point_type>             linestring_type;
typedef bg::model::multi_linestring<linestring_type>  multi_linestring_type;

namespace services = bg::strategy::distance::services;
typedef bg::default_distance_result<point_type>::type return_type;

typedef bg::strategy::distance::projected_point<> point_segment_strategy;

//===========================================================================

template <typename Strategy>
void test_distance_segment_segment(Strategy const& strategy)
{
#ifdef BOOST_GEOMETRY_TEST_DEBUG
    std::cout << std::endl;
    std::cout << "segment/segment distance tests" << std::endl;
#endif
    typedef test_distance_of_geometries<segment_type, segment_type> tester;

    tester::apply("segment(0 0,10 0)",
                  "segment(4 2,4 0.5)",
                  return_type(0.5), return_type(0.25), strategy);

    tester::apply("segment(0 0,10 0)",
                  "segment(4 2,4 -0.5)",
                  return_type(0), return_type(0), strategy);

    tester::apply("segment(0 0,10 0)",
                  "segment(4 2,0 0)",
                  return_type(0), return_type(0), strategy);

    tester::apply("segment(0 0,10 0)",
                  "segment(-2 3,1 2)",
                  return_type(2), return_type(4), strategy);
}

//===========================================================================

template <typename Strategy>
void test_distance_segment_linestring(Strategy const& strategy)
{
#ifdef BOOST_GEOMETRY_TEST_DEBUG
    std::cout << std::endl;
    std::cout << "segment/linestring distance tests" << std::endl;
#endif
    typedef test_distance_of_geometries<segment_type, linestring_type> tester;

    tester::apply("segment(-1 -1,-2 -2)",
                  "linestring(2 1,1 2,4 0)",
                  sqrt(12.5), 12.5, strategy);

    tester::apply("segment(1 1,2 2)",
                  "linestring(2 1,1 2,4 0)",
                  0, 0, strategy);
}

//===========================================================================

template <typename Strategy>
void test_distance_linestring_linestring(Strategy const& strategy)
{
#ifdef BOOST_GEOMETRY_TEST_DEBUG
    std::cout << std::endl;
    std::cout << "linestring/linestring distance tests" << std::endl;
#endif
    typedef test_distance_of_geometries
        <
            linestring_type, linestring_type
        > tester;

    // It is not obvious that linestrings with only one point are valid
    tester::apply("linestring(1 1)", "linestring(2 1)",
                  1, 1, strategy);

    tester::apply("linestring(1 1,3 1)", "linestring(2 1)",
                  0, 0, strategy);

    tester::apply("linestring(1 1)", "linestring(0 0,-2 0,2 -2,2 0)",
                  sqrt(2.0), 2, strategy);

    tester::apply("linestring(1 1,1 1)", "linestring(2 1,2 1)",
                  1, 1, strategy);

    tester::apply("linestring(1 1,1 1,1 1)", "linestring(2 1,2 1,2 1,2 1)",
                  1, 1, strategy);

    tester::apply("linestring(1 1,3 1)", "linestring(2 1,2 1)",
                  0, 0, strategy);

    tester::apply("linestring(1 1,1 1)", "linestring(0 0,-2 0,2 -2,2 0)",
                  sqrt(2.0), 2, strategy);

    tester::apply("linestring(1 1,3 1)", "linestring(2 1, 4 1)",
                  0, 0, strategy);

    tester::apply("linestring(1 1,2 2,3 3)", "linestring(2 1,1 2,4 0)",
                  0, 0, strategy);

    tester::apply("linestring(1 1,2 2,3 3)", "linestring(1 0,2 -1,4 0)",
                  1, 1, strategy);

    tester::apply("linestring(1 1,2 2,3 3)",
                  "linestring(1 -10,2 0,2.1 -10,4 0)",
                  sqrt(2.0), 2, strategy);

    tester::apply("linestring(1 1,2 2,3 3)",
                  "linestring(1 -10,2 1.9,2.1 -10,4 0)",
                  sqrt(0.005), 0.005, strategy);

    tester::apply("linestring(1 1,1 2)", "linestring(0 0,-2 0,2 -2,2 0)",
                  sqrt(2.0), 2, strategy);
}

//===========================================================================

template <typename Strategy>
void test_distance_segment_multilinestring(Strategy const& strategy)
{
#ifdef BOOST_GEOMETRY_TEST_DEBUG
    std::cout << std::endl;
    std::cout << "segment/multilinestring distance tests" << std::endl;
#endif
    typedef test_distance_of_geometries
        <
            segment_type, multi_linestring_type
        > tester;

    tester::apply("segment(-1 -1,-2 -2)",
                  "multilinestring((2 1,1 2),(4 0,4 10))",
                  sqrt(12.5), 12.5, strategy);

    tester::apply("segment(1 1,2 2)",
                  "multilinestring((2 1,1 2),(4 0,4 10))",
                  0, 0, strategy);
}

//===========================================================================

template <typename Strategy>
void test_distance_linestring_multilinestring(Strategy const& strategy)
{
#ifdef BOOST_GEOMETRY_TEST_DEBUG
    std::cout << std::endl;
    std::cout << "linestring/multilinestring distance tests" << std::endl;
#endif

    typedef test_distance_of_geometries
        <
            linestring_type, multi_linestring_type
        > tester;

    tester::apply("linestring(1 1,2 2,3 3)",
                  "multilinestring((2 1,1 2,4 0),(1 -10,2 1.9,2.1 -10,4 0))",
                  0, 0, strategy, true);

    tester::apply("linestring(1 1,2 2,3 3)",
                  "multilinestring((1 -10,2 0,2.1 -10,4 0),(1 -10,2 1.9,2.1 -10,4 0))",
                  sqrt(0.005), 0.005, strategy, true);
}

//===========================================================================

template <typename Strategy>
void test_distance_multilinestring_multilinestring(Strategy const& strategy)
{
#ifdef BOOST_GEOMETRY_TEST_DEBUG
    std::cout << std::endl;
    std::cout << "multilinestring/multilinestring distance tests" << std::endl;
#endif

    typedef test_distance_of_geometries
        <
            multi_linestring_type, multi_linestring_type
        > tester;

    tester::apply("multilinestring((0 0,0 1,1 1),(10 0,11 1,12 2))",
                  "multilinestring((0.5 0.5,0.75 0.75),(11 0,11 7))",
                  0, 0, strategy);

    tester::apply("multilinestring((0 0,0 1,1 1),(10 0,11 1,12 2))",
                  "multilinestring((0.5 0.5,0.75 0.75),(11 0,11 0.9))",
                  sqrt(0.005), 0.005, strategy);

    tester::apply("multilinestring((0 0,0 1,1 1),(10 0,11 1,12 2))",
                  "multilinestring((0.5 0.5,0.75 0.75),(11.1 0,11.1 0.9))",
                  sqrt(0.02), 0.02, strategy);
}

//===========================================================================

template <typename Point, typename Strategy>
void test_more_empty_input_linear_linear(Strategy const& strategy)
{
#ifdef BOOST_GEOMETRY_TEST_DEBUG
    std::cout << std::endl;
    std::cout << "testing on empty inputs... " << std::flush;
#endif
    bg::model::linestring<Point> line_empty;
    bg::model::multi_linestring<bg::model::linestring<Point> > multiline_empty;

    bg::model::linestring<Point> line =
        from_wkt<bg::model::linestring<Point> >("linestring(0 0,1 1)");

    // 1st geometry is empty
    test_empty_input(line_empty, line, strategy);
    test_empty_input(multiline_empty, line, strategy);

    // 2nd geometry is empty
    test_empty_input(line, line_empty, strategy);
    test_empty_input(line, multiline_empty, strategy);

    // both geometries are empty
    test_empty_input(line_empty, line_empty, strategy);
    test_empty_input(line_empty, multiline_empty, strategy);
    test_empty_input(multiline_empty, multiline_empty, strategy);

#ifdef BOOST_GEOMETRY_TEST_DEBUG
    std::cout << "done!" << std::endl;
#endif
}

//===========================================================================

BOOST_AUTO_TEST_CASE( test_all_segment_segment )
{
    test_distance_segment_segment(point_segment_strategy());
}

BOOST_AUTO_TEST_CASE( test_all_segment_linestring )
{
    test_distance_segment_linestring(point_segment_strategy());
}

BOOST_AUTO_TEST_CASE( test_all_linestring_linestring )
{
    test_distance_linestring_linestring(point_segment_strategy());
}

BOOST_AUTO_TEST_CASE( test_all_segment_multilinestring )
{
    test_distance_segment_multilinestring(point_segment_strategy());
}

BOOST_AUTO_TEST_CASE( test_all_linestring_multilinestring )
{
    test_distance_linestring_multilinestring(point_segment_strategy());
}

BOOST_AUTO_TEST_CASE( test_all_multilinestring_multilinestring )
{
    test_distance_multilinestring_multilinestring(point_segment_strategy());
}

BOOST_AUTO_TEST_CASE( test_all_empty_input_linear_linear )
{
    test_more_empty_input_linear_linear<point_type>(point_segment_strategy());
}