]> git.mxchange.org Git - flightgear.git/blob - src/Environment/environment.cxx
bb5894443266da66e1de20f069c64e930c9885b7
[flightgear.git] / src / Environment / environment.cxx
1 // environment.cxx -- routines to model the natural environment
2 //
3 // Written by David Megginson, started February 2002.
4 //
5 // Copyright (C) 2002  David Megginson - david@megginson.com
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 // $Id$
22
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #ifdef HAVE_WINDOWS_H
29 #  include <windows.h>                     
30 #endif
31
32 #include <math.h>
33
34 #include <simgear/debug/logstream.hxx>
35
36 #include <Main/fg_props.hxx>
37 #include "environment.hxx"
38
39
40 FGEnvironment::FGEnvironment()
41   : visibility_m(32000),
42     temperature_sea_level_degc(20),
43     pressure_sea_level_inhg(28),
44     wind_from_heading_deg(0),
45     wind_speed_kt(0),
46     wind_from_north_fps(0),
47     wind_from_east_fps(0),
48     wind_from_down_fps(0)
49 {
50 }
51
52
53 FGEnvironment::~FGEnvironment()
54 {
55 }
56
57
58 double
59 FGEnvironment::get_visibility_m () const
60 {
61   return visibility_m;
62 }
63
64 double
65 FGEnvironment::get_temperature_sea_level_degc () const
66 {
67   return temperature_sea_level_degc;
68 }
69
70 double
71 FGEnvironment::get_pressure_sea_level_inhg () const
72 {
73   return pressure_sea_level_inhg;
74 }
75
76 double
77 FGEnvironment::get_wind_from_heading_deg () const
78 {
79   return wind_from_heading_deg;
80 }
81
82 double
83 FGEnvironment::get_wind_speed_kt () const
84 {
85   return wind_speed_kt;
86 }
87
88 double
89 FGEnvironment::get_wind_from_north_fps () const
90 {
91   return wind_from_north_fps;
92 }
93
94 double
95 FGEnvironment::get_wind_from_east_fps () const
96 {
97   return wind_from_east_fps;
98 }
99
100 double
101 FGEnvironment::get_wind_from_down_fps () const
102 {
103   return wind_from_down_fps;
104 }
105
106
107
108 void
109 FGEnvironment::set_visibility_m (double v)
110 {
111   visibility_m = v;
112 }
113
114 void
115 FGEnvironment::set_temperature_sea_level_degc (double t)
116 {
117   temperature_sea_level_degc = t;
118 }
119
120 void
121 FGEnvironment::set_pressure_sea_level_inhg (double p)
122 {
123   pressure_sea_level_inhg = p;
124 }
125
126 void
127 FGEnvironment::set_wind_from_heading_deg (double h)
128 {
129   wind_from_heading_deg = h;
130   _recalc_ne();
131 }
132
133 void
134 FGEnvironment::set_wind_speed_kt (double s)
135 {
136   wind_speed_kt = s;
137   _recalc_ne();
138 }
139
140 void
141 FGEnvironment::set_wind_from_north_fps (double n)
142 {
143   wind_from_north_fps = n;
144   _recalc_hdgspd();
145 }
146
147 void
148 FGEnvironment::set_wind_from_east_fps (double e)
149 {
150   wind_from_east_fps = e;
151   _recalc_hdgspd();
152 }
153
154 void
155 FGEnvironment::set_wind_from_down_fps (double d)
156 {
157   wind_from_down_fps = d;
158   _recalc_hdgspd();
159 }
160
161 void
162 FGEnvironment::_recalc_hdgspd ()
163 {
164   double angle_rad;
165
166   if (wind_from_east_fps == 0) {
167     angle_rad = (wind_from_north_fps >= 0 ? SGD_PI/2 : -SGD_PI/2);
168   } else {
169     angle_rad = atan(wind_from_north_fps/wind_from_east_fps);
170   }
171   wind_from_heading_deg = angle_rad * SGD_RADIANS_TO_DEGREES;
172   if (wind_from_east_fps >= 0)
173     wind_from_heading_deg = 90 - wind_from_heading_deg;
174   else
175     wind_from_heading_deg = 270 - wind_from_heading_deg;
176   if (angle_rad == 0)
177     wind_speed_kt = fabs(wind_from_east_fps
178                          * SG_METER_TO_NM * SG_FEET_TO_METER * 3600);
179   else
180     wind_speed_kt = (wind_from_north_fps / sin(angle_rad))
181       * SG_METER_TO_NM * SG_FEET_TO_METER * 3600;
182 }
183
184 void
185 FGEnvironment::_recalc_ne ()
186 {
187   double speed_fps =
188     wind_speed_kt * SG_NM_TO_METER * SG_METER_TO_FEET * (1.0/3600);
189
190   wind_from_north_fps = speed_fps *
191     cos(wind_from_heading_deg * SGD_DEGREES_TO_RADIANS);
192   wind_from_east_fps = speed_fps *
193     sin(wind_from_heading_deg * SGD_DEGREES_TO_RADIANS);
194 }
195
196 // end of environment.cxx
197