File: intersection.cpp

package info (click to toggle)
boost1.90 1.90.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 593,120 kB
  • sloc: cpp: 4,190,908; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,774; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (214 lines) | stat: -rw-r--r-- 8,140 bytes parent folder | download | duplicates (5)
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
// Boost.Geometry
// Unit Test

// Copyright (c) 2025 Adam Wulkiewicz, Lodz, Poland.

// Copyright (c) 2016-2019 Oracle and/or its affiliates.

// Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
// Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle

// 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)

#include "test_formula.hpp"
#include "intersection_cases.hpp"

#include <boost/geometry/formulas/andoyer_inverse.hpp>
#include <boost/geometry/formulas/geographic.hpp>
#include <boost/geometry/formulas/gnomonic_intersection.hpp>
#include <boost/geometry/formulas/sjoberg_intersection.hpp>
#include <boost/geometry/formulas/thomas_direct.hpp>
#include <boost/geometry/formulas/thomas_inverse.hpp>
#include <boost/geometry/formulas/vincenty_direct.hpp>
#include <boost/geometry/formulas/vincenty_inverse.hpp>

#include <boost/geometry/strategies/geographic/parameters.hpp>

#include <boost/geometry/srs/spheroid.hpp>

void check_result(expected_result const& result, expected_result const& expected,
                  expected_result const& reference, double reference_error,
                  bool check_reference_only)
{
    //BOOST_CHECK_MESSAGE((false), "(" << result.lon << " " << result.lat << ") vs (" << expected.lon << " " << expected.lat << ")");
    check_one(result.lon, expected.lon, reference.lon, reference_error, false, check_reference_only);
    check_one(result.lat, expected.lat, reference.lat, reference_error, false, check_reference_only);
}

void test_formulas(expected_results const& results, bool check_reference_only)
{
    // reference result
    if (results.sjoberg_vincenty.lon == ND)
    {
        return;
    }

    double const d2r = bg::math::d2r<double>();
    double const r2d = bg::math::r2d<double>();

    double lona1r = results.p1.lon * d2r;
    double lata1r = results.p1.lat * d2r;
    double lona2r = results.p2.lon * d2r;
    double lata2r = results.p2.lat * d2r;
    double lonb1r = results.q1.lon * d2r;
    double latb1r = results.q1.lat * d2r;
    double lonb2r = results.q2.lon * d2r;
    double latb2r = results.q2.lat * d2r;

    expected_result result;

    // WGS84
    bg::srs::spheroid<double> spheroid(6378137.0, 6356752.3142451793);

    if (results.gnomonic_vincenty.lon != ND)
    {
        bg::formula::gnomonic_intersection<double, bg::formula::vincenty_inverse, bg::formula::vincenty_direct>
            ::apply(lona1r, lata1r, lona2r, lata2r, lonb1r, latb1r, lonb2r, latb2r, result.lon, result.lat, spheroid);
        result.lon *= r2d;
        result.lat *= r2d;
        check_result(result, results.gnomonic_vincenty, results.sjoberg_vincenty, 0.00000001, check_reference_only);
    }

    if (results.gnomonic_thomas.lon != ND)
    {
        bg::formula::gnomonic_intersection<double, bg::strategy::thomas::inverse, bg::strategy::thomas::direct>
                ::apply(lona1r, lata1r, lona2r, lata2r, lonb1r, latb1r, lonb2r, latb2r, result.lon, result.lat, spheroid);
        result.lon *= r2d;
        result.lat *= r2d;
        check_result(result, results.gnomonic_thomas, results.sjoberg_vincenty, 0.0000001, check_reference_only);
    }

    if (results.sjoberg_vincenty.lon != ND)
    {
        bg::formula::sjoberg_intersection<double, bg::formula::vincenty_inverse, 4>
            ::apply(lona1r, lata1r, lona2r, lata2r, lonb1r, latb1r, lonb2r, latb2r, result.lon, result.lat, spheroid);
        result.lon *= r2d;
        result.lat *= r2d;
        check_result(result, results.sjoberg_vincenty, results.sjoberg_vincenty, 0.00000001, check_reference_only);
    }

    if (results.sjoberg_thomas.lon != ND)
    {
        bg::formula::sjoberg_intersection<double, bg::formula::thomas_inverse, 2>
            ::apply(lona1r, lata1r, lona2r, lata2r, lonb1r, latb1r, lonb2r, latb2r, result.lon, result.lat, spheroid);
        result.lon *= r2d;
        result.lat *= r2d;
        check_result(result, results.sjoberg_thomas, results.sjoberg_vincenty, 0.0000001, check_reference_only);
    }

    if (results.sjoberg_andoyer.lon != ND)
    {
        bg::formula::sjoberg_intersection<double, bg::formula::andoyer_inverse, 1>
            ::apply(lona1r, lata1r, lona2r, lata2r, lonb1r, latb1r, lonb2r, latb2r, result.lon, result.lat, spheroid);
        result.lon *= r2d;
        result.lat *= r2d;
        check_result(result, results.sjoberg_andoyer, results.sjoberg_vincenty, 0.0001, check_reference_only);
    }

    if (results.great_elliptic.lon != ND)
    {
        typedef bg::model::point<double, 2, bg::cs::geographic<bg::degree> > point_geo;
        typedef bg::model::point<double, 3, bg::cs::cartesian> point_3d;
        point_geo a1(results.p1.lon, results.p1.lat);
        point_geo a2(results.p2.lon, results.p2.lat);
        point_geo b1(results.q1.lon, results.q1.lat);
        point_geo b2(results.q2.lon, results.q2.lat);
        point_3d a1v = bg::formula::geo_to_cart3d<point_3d>(a1, spheroid);
        point_3d a2v = bg::formula::geo_to_cart3d<point_3d>(a2, spheroid);
        point_3d b1v = bg::formula::geo_to_cart3d<point_3d>(b1, spheroid);
        point_3d b2v = bg::formula::geo_to_cart3d<point_3d>(b2, spheroid);
        point_3d resv(0, 0, 0);
        point_geo res(0, 0);
        bg::formula::great_elliptic_intersection(a1v, a2v, b1v, b2v, resv, spheroid);
        res = bg::formula::cart3d_to_geo<point_geo>(resv, spheroid);
        result.lon = bg::get<0>(res);
        result.lat = bg::get<1>(res);
        check_result(result, results.great_elliptic, results.sjoberg_vincenty, 0.01, check_reference_only);
    }
}

void test_4_input_combinations(expected_results const& results, bool check_reference_only)
{
    test_formulas(results, check_reference_only);

#ifdef BOOST_GEOMETRY_TEST_GEO_INTERSECTION_TEST_SIMILAR
    {
        expected_results results_alt = results;
        std::swap(results_alt.p1, results_alt.p2);
        test_formulas(results_alt, true);
    }
    {
        expected_results results_alt = results;
        std::swap(results_alt.q1, results_alt.q2);
        test_formulas(results_alt, true);
    }
    {
        expected_results results_alt = results;
        std::swap(results_alt.p1, results_alt.p2);
        std::swap(results_alt.q1, results_alt.q2);
        test_formulas(results_alt, true);
    }
#endif
}

void test_all(expected_results const& results)
{
    test_4_input_combinations(results, false);

#ifdef BOOST_GEOMETRY_TEST_GEO_INTERSECTION_TEST_SIMILAR
    expected_results results_alt = results;
    results_alt.p1.lat *= -1;
    results_alt.p2.lat *= -1;
    results_alt.q1.lat *= -1;
    results_alt.q2.lat *= -1;
    results_alt.gnomonic_vincenty.lat *= -1;
    results_alt.gnomonic_thomas.lat *= -1;
    results_alt.sjoberg_vincenty.lat *= -1;
    results_alt.sjoberg_thomas.lat *= -1;
    results_alt.sjoberg_andoyer.lat *= -1;
    results_alt.great_elliptic.lat *= -1;
    test_4_input_combinations(results_alt, true);
#endif
}

void test_bugs()
{
    // https://github.com/boostorg/geometry/issues/612
    {
        double lon, lat;
        bg::formula::sjoberg_intersection<double, bg::formula::andoyer_inverse, 1>
            ::apply(-0.0872665, -0.0872665, -0.0872665, 0.0872665,
                    0.0, 1.57e-07, -0.392699, 1.57e-07,
                    lon, lat, bg::srs::spheroid<double>());
        check_one("issue 612", lon, -0.087266500535674751);
        check_one("issue 612", lat, 1.5892499139622920e-07);
    }
}

void test_special_cases()
{
    {
        double lon, lat;
        bg::formula::sjoberg_intersection<double, bg::formula::andoyer_inverse, 1>
            ::apply(-0.01, 0.0, 0.01, 0.0,
                    0.0, -0.01, 0.0, 0.01,
                    lon, lat, bg::srs::spheroid<double>());
        check_one("geodesic on equator", lon, 0.0);
        check_one("geodesic on equator", lat, 0.0);
    }
}

int test_main(int, char*[])
{
    for (size_t i = 0; i < expected_size; ++i)
    {
        test_all(expected[i]);
    }

    test_bugs();
    test_special_cases();

    return 0;
}