]> git.mxchange.org Git - flightgear.git/blob - src/ATC/ATCProjection.cxx
any wind < 1kt is "calm", not just 0.0
[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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20
21 #ifdef HAVE_CONFIG_H
22 #  include <config.h>
23 #endif
24
25 #include "ATCProjection.hxx"
26 #include <math.h>
27 #include <simgear/constants.h>
28
29 FGATCProjection::FGATCProjection() {
30     _origin.setlat(0.0);
31     _origin.setlon(0.0);
32     _origin.setelev(0.0);
33     _correction_factor = cos(_origin.lat() * SG_DEGREES_TO_RADIANS);
34 }
35
36 FGATCProjection::FGATCProjection(const Point3D& centre) {
37     _origin = centre;
38     _correction_factor = cos(_origin.lat() * SG_DEGREES_TO_RADIANS);
39 }
40
41 FGATCProjection::~FGATCProjection() {
42 }
43
44 void FGATCProjection::Init(const Point3D& centre) {
45     _origin = centre;
46     _correction_factor = cos(_origin.lat() * SG_DEGREES_TO_RADIANS);
47 }
48
49 Point3D FGATCProjection::ConvertToLocal(const Point3D& pt) {
50     double delta_lat = pt.lat() - _origin.lat();
51     double delta_lon = pt.lon() - _origin.lon();
52
53     double y = sin(delta_lat * SG_DEGREES_TO_RADIANS) * SG_EQUATORIAL_RADIUS_M;
54     double x = sin(delta_lon * SG_DEGREES_TO_RADIANS) * SG_EQUATORIAL_RADIUS_M * _correction_factor;
55
56     return(Point3D(x,y,0.0));
57 }
58
59 Point3D FGATCProjection::ConvertFromLocal(const Point3D& pt) {
60     double delta_lat = asin(pt.y() / SG_EQUATORIAL_RADIUS_M) * SG_RADIANS_TO_DEGREES;
61     double delta_lon = (asin(pt.x() / SG_EQUATORIAL_RADIUS_M) * SG_RADIANS_TO_DEGREES) / _correction_factor;
62     
63     return(Point3D(_origin.lon()+delta_lon, _origin.lat()+delta_lat, 0.0));
64 }
65
66 /**********************************************************************************/
67
68 FGATCAlignedProjection::FGATCAlignedProjection() {
69     _origin.setlat(0.0);
70     _origin.setlon(0.0);
71     _origin.setelev(0.0);
72     _correction_factor = cos(_origin.lat() * SG_DEGREES_TO_RADIANS);
73 }
74
75 FGATCAlignedProjection::FGATCAlignedProjection(const Point3D& centre, double heading) {
76     _origin = centre;
77     _theta = heading * SG_DEGREES_TO_RADIANS;
78     _correction_factor = cos(_origin.lat() * SG_DEGREES_TO_RADIANS);
79 }
80
81 FGATCAlignedProjection::~FGATCAlignedProjection() {
82 }
83
84 void FGATCAlignedProjection::Init(const Point3D& centre, double heading) {
85     _origin = centre;
86     _theta = heading * SG_DEGREES_TO_RADIANS;
87     _correction_factor = cos(_origin.lat() * SG_DEGREES_TO_RADIANS);
88 }
89
90 Point3D FGATCAlignedProjection::ConvertToLocal(const Point3D& pt) {
91     // convert from lat/lon to orthogonal
92     double delta_lat = pt.lat() - _origin.lat();
93     double delta_lon = pt.lon() - _origin.lon();
94     double y = sin(delta_lat * SG_DEGREES_TO_RADIANS) * SG_EQUATORIAL_RADIUS_M;
95     double x = sin(delta_lon * SG_DEGREES_TO_RADIANS) * SG_EQUATORIAL_RADIUS_M * _correction_factor;
96
97     // Align
98     if(_theta != 0.0) {
99         double xbar = x;
100         x = x*cos(_theta) - y*sin(_theta);
101         y = (xbar*sin(_theta)) + (y*cos(_theta));
102     }
103
104     return(Point3D(x,y,pt.elev()));
105 }
106
107 Point3D FGATCAlignedProjection::ConvertFromLocal(const Point3D& pt) {
108     // de-align
109     double thi = _theta * -1.0;
110     double x = pt.x()*cos(thi) - pt.y()*sin(thi);
111     double y = (pt.x()*sin(thi)) + (pt.y()*cos(thi));
112
113     // convert from orthogonal to lat/lon
114     double delta_lat = asin(y / SG_EQUATORIAL_RADIUS_M) * SG_RADIANS_TO_DEGREES;
115     double delta_lon = (asin(x / SG_EQUATORIAL_RADIUS_M) * SG_RADIANS_TO_DEGREES) / _correction_factor;
116
117     return(Point3D(_origin.lon()+delta_lon, _origin.lat()+delta_lat, pt.elev()));
118 }