Coverage Report

Created: 2026-01-09 07:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/PROJ/src/dist.cpp
Line
Count
Source
1
/******************************************************************************
2
 * Project:  PROJ
3
 * Purpose:  Distance computation
4
 *
5
 * Author:   Thomas Knudsen,  thokn@sdfe.dk,  2016-06-09/2016-11-06
6
 *
7
 ******************************************************************************
8
 * Copyright (c) 2016, 2017 Thomas Knudsen/SDFE
9
 *
10
 * Permission is hereby granted, free of charge, to any person obtaining a
11
 * copy of this software and associated documentation files (the "Software"),
12
 * to deal in the Software without restriction, including without limitation
13
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14
 * and/or sell copies of the Software, and to permit persons to whom the
15
 * Software is furnished to do so, subject to the following conditions:
16
 *
17
 * The above copyright notice and this permission notice shall be included
18
 * in all copies or substantial portions of the Software.
19
 *
20
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26
 * DEALINGS IN THE SOFTWARE.
27
 *****************************************************************************/
28
29
#define FROM_PROJ_CPP
30
31
#include "geodesic.h"
32
33
#include "proj.h"
34
#include "proj_internal.h"
35
#include <math.h>
36
37
#include "proj/internal/io_internal.hpp"
38
39
/* Geodesic distance (in meter) + fwd and rev azimuth between two points on the
40
 * ellipsoid */
41
0
PJ_COORD proj_geod(const PJ *P, PJ_COORD a, PJ_COORD b) {
42
0
    PJ_COORD c;
43
0
    if (!P->geod) {
44
0
        return proj_coord_error();
45
0
    }
46
    /* Note: the geodesic code takes arguments in degrees */
47
0
    geod_inverse(P->geod, PJ_TODEG(a.lpz.phi), PJ_TODEG(a.lpz.lam),
48
0
                 PJ_TODEG(b.lpz.phi), PJ_TODEG(b.lpz.lam), c.v, c.v + 1,
49
0
                 c.v + 2);
50
51
    // cppcheck-suppress uninitvar
52
0
    return c;
53
0
}
54
55
PJ_COORD proj_geod_direct(const PJ *P, PJ_COORD a, double azimuth,
56
0
                          double distance) {
57
0
    if (!P->geod) {
58
0
        return proj_coord_error();
59
0
    }
60
61
0
    double lat = 0, lon = 0, azi = 0;
62
0
    geod_direct(P->geod, PJ_TODEG(a.lpz.phi), PJ_TODEG(a.lpz.lam),
63
0
                PJ_TODEG(azimuth), distance, &lat, &lon, &azi);
64
65
0
    return proj_coord(PJ_TORAD(lon), PJ_TORAD(lat), PJ_TORAD(azi), 0);
66
0
}
67
68
/* Geodesic distance (in meter) between two points with angular 2D coordinates
69
 */
70
0
double proj_lp_dist(const PJ *P, PJ_COORD a, PJ_COORD b) {
71
0
    double s12, azi1, azi2;
72
    /* Note: the geodesic code takes arguments in degrees */
73
0
    if (!P->geod) {
74
0
        return HUGE_VAL;
75
0
    }
76
0
    geod_inverse(P->geod, PJ_TODEG(a.lpz.phi), PJ_TODEG(a.lpz.lam),
77
0
                 PJ_TODEG(b.lpz.phi), PJ_TODEG(b.lpz.lam), &s12, &azi1, &azi2);
78
0
    return s12;
79
0
}
80
81
/* The geodesic distance AND the vertical offset */
82
0
double proj_lpz_dist(const PJ *P, PJ_COORD a, PJ_COORD b) {
83
0
    if (HUGE_VAL == a.lpz.lam || HUGE_VAL == b.lpz.lam)
84
0
        return HUGE_VAL;
85
0
    return hypot(proj_lp_dist(P, a, b), a.lpz.z - b.lpz.z);
86
0
}
87
88
/* Euclidean distance between two points with linear 2D coordinates */
89
0
double proj_xy_dist(PJ_COORD a, PJ_COORD b) {
90
0
    return hypot(a.xy.x - b.xy.x, a.xy.y - b.xy.y);
91
0
}
92
93
/* Euclidean distance between two points with linear 3D coordinates */
94
0
double proj_xyz_dist(PJ_COORD a, PJ_COORD b) {
95
0
    return hypot(proj_xy_dist(a, b), a.xyz.z - b.xyz.z);
96
0
}