]> git.mxchange.org Git - flightgear.git/blob - src/ATC/ATCProjection.cxx
The most important part is that it fixes possible
[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 FGATCProjection::FGATCProjection() {
26     _origin.setlat(0.0);
27     _origin.setlon(0.0);
28     _origin.setelev(0.0);
29     _correction_factor = cos(_origin.lat() * SG_DEGREES_TO_RADIANS);
30 }
31
32 FGATCProjection::FGATCProjection(const Point3D& centre) {
33     _origin = centre;
34     _correction_factor = cos(_origin.lat() * SG_DEGREES_TO_RADIANS);
35 }
36
37 FGATCProjection::~FGATCProjection() {
38 }
39
40 void FGATCProjection::Init(const Point3D& centre) {
41     _origin = centre;
42     _correction_factor = cos(_origin.lat() * SG_DEGREES_TO_RADIANS);
43 }
44
45 Point3D FGATCProjection::ConvertToLocal(const Point3D& pt) {
46     double delta_lat = pt.lat() - _origin.lat();
47     double delta_lon = pt.lon() - _origin.lon();
48
49     double y = sin(delta_lat * SG_DEGREES_TO_RADIANS) * SG_EQUATORIAL_RADIUS_M;
50     double x = sin(delta_lon * SG_DEGREES_TO_RADIANS) * SG_EQUATORIAL_RADIUS_M * _correction_factor;
51
52     return(Point3D(x,y,0.0));
53 }
54
55 Point3D FGATCProjection::ConvertFromLocal(const Point3D& pt) {
56     double delta_lat = asin(pt.y() / SG_EQUATORIAL_RADIUS_M) * SG_RADIANS_TO_DEGREES;
57     double delta_lon = (asin(pt.x() / SG_EQUATORIAL_RADIUS_M) * SG_RADIANS_TO_DEGREES) / _correction_factor;
58     
59     return(Point3D(_origin.lon()+delta_lon, _origin.lat()+delta_lat, 0.0));
60 }
61
62 /**********************************************************************************/
63
64 FGATCAlignedProjection::FGATCAlignedProjection() {
65     _origin.setlat(0.0);
66     _origin.setlon(0.0);
67     _origin.setelev(0.0);
68     _correction_factor = cos(_origin.lat() * SG_DEGREES_TO_RADIANS);
69 }
70
71 FGATCAlignedProjection::FGATCAlignedProjection(const Point3D& centre, double heading) {
72     _origin = centre;
73     _theta = heading * SG_DEGREES_TO_RADIANS;
74     _correction_factor = cos(_origin.lat() * SG_DEGREES_TO_RADIANS);
75 }
76
77 FGATCAlignedProjection::~FGATCAlignedProjection() {
78 }
79
80 void FGATCAlignedProjection::Init(const Point3D& centre, double heading) {
81     _origin = centre;
82     _theta = heading * SG_DEGREES_TO_RADIANS;
83     _correction_factor = cos(_origin.lat() * SG_DEGREES_TO_RADIANS);
84 }
85
86 Point3D FGATCAlignedProjection::ConvertToLocal(const Point3D& pt) {
87     // convert from lat/lon to orthogonal
88     double delta_lat = pt.lat() - _origin.lat();
89     double delta_lon = pt.lon() - _origin.lon();
90     double y = sin(delta_lat * SG_DEGREES_TO_RADIANS) * SG_EQUATORIAL_RADIUS_M;
91     double x = sin(delta_lon * SG_DEGREES_TO_RADIANS) * SG_EQUATORIAL_RADIUS_M * _correction_factor;
92
93     // Align
94     if(_theta != 0.0) {
95         double xbar = x;
96         x = x*cos(_theta) - y*sin(_theta);
97         y = (xbar*sin(_theta)) + (y*cos(_theta));
98     }
99
100     return(Point3D(x,y,pt.elev()));
101 }
102
103 Point3D FGATCAlignedProjection::ConvertFromLocal(const Point3D& pt) {
104     // de-align
105     double thi = _theta * -1.0;
106     double x = pt.x()*cos(thi) - pt.y()*sin(thi);
107     double y = (pt.x()*sin(thi)) + (pt.y()*cos(thi));
108
109     // convert from orthogonal to lat/lon
110     double delta_lat = asin(y / SG_EQUATORIAL_RADIUS_M) * SG_RADIANS_TO_DEGREES;
111     double delta_lon = (asin(x / SG_EQUATORIAL_RADIUS_M) * SG_RADIANS_TO_DEGREES) / _correction_factor;
112
113     return(Point3D(_origin.lon()+delta_lon, _origin.lat()+delta_lat, pt.elev()));
114 }