]> git.mxchange.org Git - flightgear.git/blob - src/Environment/presets.cxx
Merge branch 'merge-requests/1555' into next
[flightgear.git] / src / Environment / presets.cxx
1 // presets.cxx -- Wrap environment presets
2 //
3 // Written by Torsten Dreyer, January 2011
4 //
5 // Copyright (C) 2010  Torsten Dreyer Torsten(at)t3r(dot)de
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
22 #ifdef HAVE_CONFIG_H
23 #  include <config.h>
24 #endif
25 #include "presets.hxx"
26
27 #include <cmath>
28 #include <simgear/math/SGMisc.hxx>
29 #include <Main/fg_props.hxx>
30
31 namespace Environment {
32 namespace Presets {
33
34 PresetBase::PresetBase( const char * overrideNodePath )
35     : _overrideNodePath( overrideNodePath ) 
36 {
37 }
38
39 void PresetBase::setOverride( bool value ) 
40
41     /*
42     Don't initialize node in constructor because the class is used as a singleton
43     and created as a static variable in the initialization sequence when globals()
44     is not yet initialized and returns null.
45     */
46     if( _overrideNode == NULL ) 
47         _overrideNode = fgGetNode( _overrideNodePath.c_str(), true );
48     _overrideNode->setBoolValue( value ); 
49 }
50
51
52 Wind::Wind() : 
53     PresetBase("/environment/config/presets/wind-override")
54 {
55 }
56
57
58 void Wind::preset( double min_hdg, double max_hdg, double speed_kt, double gust_kt )
59 {
60     // see: PresetBase::setOverride()
61
62     //TODO: handle variable wind and gusts
63     if( _fromNorthNode == NULL )
64         _fromNorthNode = fgGetNode("/environment/config/presets/wind-from-north-fps", true );
65     
66     if( _fromEastNode == NULL )
67         _fromEastNode = fgGetNode("/environment/config/presets/wind-from-east-fps", true );
68
69     double avgHeading_rad = 
70       SGMiscd::normalizeAngle2(
71         (SGMiscd::normalizeAngle(min_hdg*SG_DEGREES_TO_RADIANS) + 
72          SGMiscd::normalizeAngle(max_hdg*SG_DEGREES_TO_RADIANS))/2);
73
74     double speed_fps = speed_kt * SG_NM_TO_METER * SG_METER_TO_FEET / 3600.0;
75     _fromNorthNode->setDoubleValue( speed_fps * cos(avgHeading_rad) );
76     _fromEastNode->setDoubleValue( speed_fps * sin(avgHeading_rad) );
77     setOverride( true );
78 }
79
80 Visibility::Visibility() : 
81     PresetBase("/environment/config/presets/visibility-m-override")
82 {
83 }
84
85 void Visibility::preset( double visibility_m )
86 {
87     // see: PresetBase::setOverride()
88     if( _visibilityNode == NULL )
89         _visibilityNode = fgGetNode("/environment/config/presets/visibility-m", true );
90
91     _visibilityNode->setDoubleValue(visibility_m );
92     setOverride( true );
93 }
94
95 Turbulence::Turbulence() : 
96     PresetBase("/environment/config/presets/turbulence-magnitude-norm-override")
97 {
98 }
99
100
101 void Turbulence::preset(double magnitude_norm)
102 {
103     // see: PresetBase::setOverride()
104     if( _magnitudeNode == NULL )
105         _magnitudeNode = fgGetNode("/environment/config/presets/turbulence-magnitude-norm", true );
106
107     _magnitudeNode->setDoubleValue( magnitude_norm );
108     setOverride( true );
109 }
110
111 Ceiling::Ceiling() : 
112     PresetBase("/environment/config/presets/ceiling-override")
113 {
114 }
115
116
117 void Ceiling::preset( double elevation, double thickness )
118 {
119     // see: PresetBase::setOverride()
120     if( _elevationNode == NULL )
121         _elevationNode = fgGetNode("/environment/config/presets/ceiling-elevation-ft", true);
122
123     if( _thicknessNode == NULL )
124         _thicknessNode = fgGetNode("/environment/config/presets/ceiling-elevation-ft", true);
125
126     _elevationNode->setDoubleValue( elevation );
127     _thicknessNode->setDoubleValue( thickness );
128     setOverride( true );
129 }
130
131 } // namespace Presets
132 } // namespace Environment
133