-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshared.h
203 lines (167 loc) · 5.89 KB
/
shared.h
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
/*****************************************************************************
* ____ ____ _ _ _ *
* /# /_\_ | _ \ (_) __| | (_) ___ _ __ *
* | |/o\o\ | | | | | | / _` | | | / _ \ | '__| *
* | \\_/_/ | |_| | | | | (_| | | | | __/ | | *
* / |_ | |____/ |_| \__,_| |_| \___| |_| *
* | ||\_ ~| *
* | ||| \/ *
* | ||| Project : KFreeFlight : a KDE4 GUI frontend for FlightGear *
* \// | *
* || | Developper : Didier FABERT <didier.fabert@gmail.com> *
* ||_ \ Date : 2009, April *
* \_| o| ,__, *
* \___/ Copyright (C) 2009 by didier fabert (oo)____ *
* ||||__ (__) )\ *
* (___)_) File : ||--|| * *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
*****************************************************************************/
#ifndef SHARED_H
#define SHARED_H
#include <stdio.h>
#include <stdlib.h>
#include "sgcoremag.h"
/**
* @author Didier Fabert <didier.fabert@gmail.com>
*/
/**
* Return the current date in julian calendar
* http://pagesperso-orange.fr/jean-paul.cornec/formule_jj.htm
*/
inline long getCurrentJulianDate()
{
long julianDate = 0;
int month, day, year;
time_t now = time( NULL );
struct tm *gmt;
gmt = gmtime( &now );
month = gmt->tm_mon + 1;
day = gmt->tm_mday;
year = gmt->tm_year + 1900;
if ( month <= 2 )
{
year = year - 1;
month = month + 12;
}
int c = year / 100;
int b = 2 - c + ( c / 4 );
double t = gmt->tm_mday + ( gmt->tm_hour / 24 ) + ( gmt->tm_min / 1440 )
+ ( gmt->tm_sec / 86400 );
int x = ( 365.25 * ( year + 4716 ) );
int y = ( 30.6001 * ( month + 1 ) );
julianDate = ( x + y + t + b - 1524.5 );
return julianDate;
}
/**
* Calculate the magnetic variation for a place
* @param longitude : the place longitude in degrees
* @param latitude : the place latitude in degrees
* @param altitude : the place elevation in feets
* @return place magnetic variation in degrees
*/
inline double getMagneticOffset( double longitude, double latitude, double altitude )
{
double declinaison;
double field[6];
long jdate = getCurrentJulianDate();
longitude = M_PI * ( longitude ) / 180.0; //convert degrees to radians
latitude = M_PI * ( latitude ) / 180.0; //convert degrees to radians
altitude = ( altitude * 0.3048006 ) / 1000.0; //convert feets to kilometers
declinaison = calc_magvar( latitude, longitude, altitude, jdate, field );
declinaison = ( declinaison * 180.0 ) / M_PI; //convert radians to meters
return declinaison;
}
/**
* Calculate distance between two know coordinate places
* @param lon1 the first place longitude in degrees
* @param lat1 the first place latitude in degrees
* @param lon2 the second place longitude in degrees
* @param lat2 the second place latitude in degrees
* @return the distance in kilometers
*/
inline double calculateDistance( double lon1, double lat1, double lon2, double lat2 )
{
double earthRadius = 6371.0; // earth's mean radius in km
double dLat, dLong, a, c, d;
//convert angle in radians and calculate the delta
lon1 = M_PI * ( lon1 ) / 180;
lat1 = M_PI * ( lat1 ) / 180;
lon2 = M_PI * ( lon2 ) / 180;
lat2 = M_PI * ( lat2 ) / 180;
dLat = lat2 - lat1;
dLong = lon2 - lon1;
//apply the very very simple math formula :-)
//found at http://www.movable-type.co.uk/scripts/LatLong.html
a = ( sin( dLat / 2 ) * sin( dLat / 2 ) ) + ( cos( lat1 ) * cos( lat2 ) * sin( dLong / 2 ) * sin( dLong / 2 ) );
c = 2 * atan2( sqrt( a ), sqrt( 1 - a ) );
d = earthRadius * c;
return d;
}
inline double kmToUSMile( double val )
{
return ( val / 1.60934 );
}
inline double kmToNauticMile( double val )
{
return ( val / 1.832 );
}
inline double mToNauticMile( double val )
{
return ( val / 1609.34 );
}
inline double mToUSMile( double val )
{
return ( val / 1832.0 );
}
inline double usMileToKm( double val )
{
return ( val * 1.60934 );
}
inline double usMileToM( double val )
{
return ( val * 1609.34 );
}
inline double nauticMileToKm( double val )
{
return ( val * 1.832 );
}
inline double nauticMileToM( double val )
{
return ( val * 1832.0 );
}
inline double usMileToNauticMile( double val )
{
return ( ( val * 1.60934 ) / 1.832 );
}
inline double nauticMileToUSMile( double val )
{
return ( ( val * 1.832 ) / 1.60934 );
}
/// Formula found at http://csgnetwork.com/pressinmbcvt.html
inline double inHgTomBars( double val )
{
return ( val / 0.0295301);
}
/// Formula found at http://csgnetwork.com/pressinmbcvt.html
inline double mBarsToinHg( double val )
{
return ( val / 33.8637526);
}
inline double footToMeter( double val )
{
return ( val * 0.3048 );
}
inline double meterToFoot( double val )
{
return ( val / 0.3048 );
}
#endif // SHARED_H