]> git.mxchange.org Git - flightgear.git/blob - src/ATC/ATCProjection.cxx
ignore resets for now because every z/Z key press would trigger a call to NOAA. We...
[flightgear.git] / src / ATC / ATCProjection.cxx
1 // ATCProjection.cxx - A convienience projection class for the ATC/AI system.
2 //
3 // Written by David Luff, started 2002.
4 //
5 // Copyright (C) 2002  David C Luff - david.luff@nottingham.ac.uk
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
21 #include "ATCProjection.hxx"
22 #include <math.h>
23 #include <simgear/constants.h>
24
25 #include <iostream>
26 SG_USING_STD(cout);
27
28 #define DCL_PI  3.1415926535f
29 //#define SG_PI  ((SGfloat) M_PI)
30 #define DCL_DEGREES_TO_RADIANS  (DCL_PI/180.0)
31 #define DCL_RADIANS_TO_DEGREES  (180.0/DCL_PI)
32
33 FGATCProjection::FGATCProjection() {
34     origin.setlat(0.0);
35     origin.setlon(0.0);
36     origin.setelev(0.0);
37     correction_factor = cos(origin.lat() * DCL_DEGREES_TO_RADIANS);
38 }
39
40 FGATCProjection::~FGATCProjection() {
41 }
42
43 void FGATCProjection::Init(Point3D centre) {
44     origin = centre;
45     correction_factor = cos(origin.lat() * DCL_DEGREES_TO_RADIANS);
46 }
47
48 Point3D FGATCProjection::ConvertToLocal(Point3D pt) {
49     double delta_lat = pt.lat() - origin.lat();
50     double delta_lon = pt.lon() - origin.lon();
51
52     double y = sin(delta_lat * DCL_DEGREES_TO_RADIANS) * SG_EQUATORIAL_RADIUS_M;
53     double x = sin(delta_lon * DCL_DEGREES_TO_RADIANS) * SG_EQUATORIAL_RADIUS_M * correction_factor;
54
55     return(Point3D(x,y,0.0));
56 }
57
58 Point3D FGATCProjection::ConvertFromLocal(Point3D pt) {
59         double delta_lat = asin(pt.y() / SG_EQUATORIAL_RADIUS_M) * DCL_RADIANS_TO_DEGREES;
60         double delta_lon = (asin(pt.x() / SG_EQUATORIAL_RADIUS_M) * DCL_RADIANS_TO_DEGREES) / correction_factor;
61         
62     return(Point3D(origin.lon()+delta_lon, origin.lat()+delta_lat, 0.0));
63 }
64
65 /**********************************************************************************/
66
67 FGATCAlignedProjection::FGATCAlignedProjection() {
68     origin.setlat(0.0);
69     origin.setlon(0.0);
70     origin.setelev(0.0);
71     correction_factor = cos(origin.lat() * DCL_DEGREES_TO_RADIANS);
72 }
73
74 FGATCAlignedProjection::~FGATCAlignedProjection() {
75 }
76
77 void FGATCAlignedProjection::Init(Point3D centre, double heading) {
78     origin = centre;
79     theta = heading * DCL_DEGREES_TO_RADIANS;
80     correction_factor = cos(origin.lat() * DCL_DEGREES_TO_RADIANS);
81 }
82
83 Point3D FGATCAlignedProjection::ConvertToLocal(Point3D pt) {
84     // convert from lat/lon to orthogonal
85     double delta_lat = pt.lat() - origin.lat();
86     double delta_lon = pt.lon() - origin.lon();
87     double y = sin(delta_lat * DCL_DEGREES_TO_RADIANS) * SG_EQUATORIAL_RADIUS_M;
88     double x = sin(delta_lon * DCL_DEGREES_TO_RADIANS) * SG_EQUATORIAL_RADIUS_M * correction_factor;
89
90     // Align
91     double xbar = x;
92     x = x*cos(theta) - y*sin(theta);
93     y = (xbar*sin(theta)) + (y*cos(theta));
94
95     return(Point3D(x,y,pt.elev()));
96 }
97
98 Point3D FGATCAlignedProjection::ConvertFromLocal(Point3D pt) {
99         //cout << "theta = " << theta << '\n';
100         //cout << "origin = " << origin << '\n';
101     // de-align
102     double thi = theta * -1.0;
103     double x = pt.x()*cos(thi) - pt.y()*sin(thi);
104     double y = (pt.x()*sin(thi)) + (pt.y()*cos(thi));
105
106     // convert from orthogonal to lat/lon
107     double delta_lat = asin(y / SG_EQUATORIAL_RADIUS_M) * DCL_RADIANS_TO_DEGREES;
108     double delta_lon = (asin(x / SG_EQUATORIAL_RADIUS_M) * DCL_RADIANS_TO_DEGREES) / correction_factor;
109
110     return(Point3D(origin.lon()+delta_lon, origin.lat()+delta_lat, pt.elev()));
111 }