Marble

MarbleMath.h
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 //
3 // SPDX-FileCopyrightText: 2007 Torsten Rahn <[email protected]>
4 // SPDX-FileCopyrightText: 2007 Inge Wallin <[email protected]>
5 //
6 
7 #ifndef MARBLE_MARBLEMATH_H
8 #define MARBLE_MARBLEMATH_H
9 
10 #include <QtGlobal>
11 
12 #include <cmath>
13 
14 
15 namespace
16 {
17  const qreal a1 = 1.0/6.0;
18  const qreal a2 = 1.0/24.0;
19  const qreal a3 = 61.0/5040;
20  const qreal a4 = 277.0/72576.0;
21  const qreal a5 = 50521.0/39916800.0;
22  const qreal a6 = 41581.0/95800320.0;
23  const qreal a7 = 199360981.0/1307674368000.0;
24  const qreal a8 = 228135437.0/4184557977600.0;
25  const qreal a9 = 2404879675441.0/121645100408832000.0;
26  const qreal a10 = 14814847529501.0/2043637686868377600.0;
27  const qreal a11 = 69348874393137901.0/25852016738884976640000.0;
28  const qreal a12 = 238685140977801337.0/238634000666630553600000.0;
29  const qreal a13 = 4087072509293123892361.0/10888869450418352160768000000.0;
30  const qreal a14 = 454540704683713199807.0/3209350995912777478963200000.0;
31  const qreal a15 = 441543893249023104553682821.0/8222838654177922817725562880000000.0;
32  const qreal a16 = 2088463430347521052196056349.0/102156677868375135241390522368000000.0;
33 }
34 
35 namespace Marble
36 {
37 
38 /**
39  * @brief This method calculates the shortest distance between two points on a sphere.
40  * @brief See: https://en.wikipedia.org/wiki/Great-circle_distance
41  * @param lon1 longitude of first point in radians
42  * @param lat1 latitude of first point in radians
43  * @param lon2 longitude of second point in radians
44  * @param lat2 latitude of second point in radians
45  */
46 inline qreal distanceSphere( qreal lon1, qreal lat1, qreal lon2, qreal lat2 ) {
47 
48  qreal h1 = sin( 0.5 * ( lat2 - lat1 ) );
49  qreal h2 = sin( 0.5 * ( lon2 - lon1 ) );
50  qreal d = h1 * h1 + cos( lat1 ) * cos( lat2 ) * h2 * h2;
51 
52  return 2.0 * atan2( sqrt( d ), sqrt( 1.0 - d ) );
53 }
54 
55 
56 /**
57  * @brief This method roughly calculates the shortest distance between two points on a sphere.
58  * @brief It's probably faster than distanceSphere(...) but for 7 significant digits only has
59  * @brief an accuracy of about 1 arcmin.
60  * @brief See: https://en.wikipedia.org/wiki/Great-circle_distance
61  */
62 inline qreal distanceSphereApprox( qreal lon1, qreal lat1, qreal lon2, qreal lat2 ) {
63  return acos( sin( lat1 ) * sin( lat2 ) + cos( lat1 ) * cos( lat2 ) * cos( lon1 - lon2 ) );
64 }
65 
66 
67 /**
68  * @brief This method is a fast Mac Laurin power series approximation of the
69  * @brief inverse Gudermannian. The inverse Gudermannian gives the vertical
70  * @brief position y in the Mercator projection in terms of the latitude.
71  * @brief See: https://en.wikipedia.org/wiki/Mercator_projection
72  */
73 inline qreal gdInv( qreal x ) {
74  const qreal x2 = x * x;
75  return x
76  + x * x2 * ( a1
77  + x2 * ( a2 + x2 * ( a3 + x2 * ( a4 + x2 * ( a5
78  + x2 * ( a6 + x2 * ( a7 + x2 * ( a8 + x2 * ( a9
79  + x2 * ( a10 + x2 * ( a11 + x2 * ( a12 + x2 * ( a13
80  + x2 * ( a14 + x2 * ( a15 + x2 * ( a16 ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) );
81 }
82 
83 }
84 
85 /**
86  * @brief This method is a fast Mac Laurin power series approximation of the
87  * Gudermannian. The Gudermannian gives the latitude
88  * in the Mercator projection in terms of the vertical position y.
89  * See: https://en.wikipedia.org/wiki/Mercator_projection
90  */
91 inline qreal gd( qreal x ) {
92 
93  /*
94  const qreal x2 = x * x;
95  return x
96  - x * x2 * ( a1
97  - x2 * ( a2 - x2 * ( a3 - x2 * ( a4 - x2 * ( a5
98  - x2 * ( a6 - x2 * ( a7 - x2 * ( a8 - x2 * ( a9
99  - x2 * ( a10 - x2 * ( a11 - x2 * ( a12 - x2 * ( a13
100  - x2 * ( a14 - x2 * ( a15 - x2 * ( a16 ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) );
101  */
102 
103  return atan ( sinh ( x ) );
104 }
105 
106 #endif
qreal gdInv(qreal x)
This method is a fast Mac Laurin power series approximation of the.
Definition: MarbleMath.h:73
qreal distanceSphere(qreal lon1, qreal lat1, qreal lon2, qreal lat2)
This method calculates the shortest distance between two points on a sphere.
Definition: MarbleMath.h:46
Binds a QML item to a specific geodetic location in screen coordinates.
qreal distanceSphereApprox(qreal lon1, qreal lat1, qreal lon2, qreal lat2)
This method roughly calculates the shortest distance between two points on a sphere.
Definition: MarbleMath.h:62
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Tue Sep 26 2023 03:51:17 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.