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