1 // util.cxx - general-purpose utility functions.
2 // Copyright (C) 2002 Curtis L. Olson - http://www.flightgear.org/~curt
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License as
6 // published by the Free Software Foundation; either version 2 of the
7 // License, or (at your option) any later version.
9 // This program is distributed in the hope that it will be useful, but
10 // WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include <simgear/compiler.h>
30 #include <simgear/debug/logstream.hxx>
33 #include "fg_props.hxx"
34 #include "globals.hxx"
37 #ifdef OSG_LIBRARY_STATIC
38 #include "osgDB/Registry"
42 fgDefaultWeatherValue (const char * propname, double value)
46 SGPropertyNode * branch = fgGetNode("/environment/config/boundary", true);
47 vector<SGPropertyNode_ptr> entries = branch->getChildren("entry");
48 for (i = 0; i < entries.size(); i++) {
49 entries[i]->setDoubleValue(propname, value);
52 branch = fgGetNode("/environment/config/aloft", true);
53 entries = branch->getChildren("entry");
54 for (i = 0; i < entries.size(); i++) {
55 entries[i]->setDoubleValue(propname, value);
61 fgSetupWind (double min_hdg, double max_hdg, double speed, double gust)
63 // Initialize to a reasonable state
64 fgDefaultWeatherValue("wind-from-heading-deg", min_hdg);
65 fgDefaultWeatherValue("wind-speed-kt", speed);
67 SG_LOG(SG_GENERAL, SG_INFO, "WIND: " << min_hdg << '@' <<
68 speed << " knots" << endl);
70 // Now, add some variety to the layers
75 fgSetDouble("/environment/config/boundary/entry[1]/wind-from-heading-deg",
77 fgSetDouble("/environment/config/boundary/entry[1]/wind-speed-kt",
84 fgSetDouble("/environment/config/aloft/entry[0]/wind-from-heading-deg",
86 fgSetDouble("/environment/config/aloft/entry[0]/wind-speed-kt",
93 fgSetDouble("/environment/config/aloft/entry[1]/wind-from-heading-deg",
95 fgSetDouble("/environment/config/aloft/entry[1]/wind-speed-kt",
102 fgSetDouble("/environment/config/aloft/entry[2]/wind-from-heading-deg",
104 fgSetDouble("/environment/config/aloft/entry[2]/wind-speed-kt",
112 #ifdef OSG_LIBRARY_STATIC
113 osgDB::Registry::instance( true);
116 SG_LOG(SG_GENERAL, SG_INFO, "Exiting FlightGear with status " << status);
121 // Originally written by Alex Perry.
123 fgGetLowPass (double current, double target, double timeratio)
125 if ( timeratio < 0.0 ) {
126 if ( timeratio < -1.0 ) {
127 // time went backwards; kill the filter
130 // ignore mildly negative time
132 } else if ( timeratio < 0.2 ) {
133 // Normal mode of operation; fast
134 // approximation to exp(-timeratio)
135 current = current * (1.0 - timeratio) + target * timeratio;
136 } else if ( timeratio > 5.0 ) {
137 // Huge time step; assume filter has settled
140 // Moderate time step; non linear response
141 double keep = exp(-timeratio);
142 current = current * keep + target * (1.0 - keep);
150 fgUnescape (const char *s)
162 } else if (*s == 'n') {
164 } else if (*s == 'r') {
166 } else if (*s == 't') {
168 } else if (*s == 'v') {
170 } else if (*s == 'f') {
172 } else if (*s == 'a') {
174 } else if (*s == 'b') {
176 } else if (*s == 'x') {
180 for (int i = 0; i < 2 && isxdigit(*s); i++, s++)
181 v = v * 16 + (isdigit(*s) ? *s - '0' : 10 + tolower(*s) - 'a');
185 } else if (*s >= '0' && *s <= '7') {
187 for (int i = 0; i < 3 && *s >= '0' && *s <= '7'; i++, s++)
188 v = v * 8 + *s - '0';
201 const char *fgValidatePath (const char *str, bool write)
203 static SGPropertyNode_ptr r, w;
205 r = fgGetNode("/sim/paths/validate/read", true);
206 r->setAttribute(SGPropertyNode::READ, true);
207 r->setAttribute(SGPropertyNode::WRITE, true);
209 w = fgGetNode("/sim/paths/validate/write", true);
210 w->setAttribute(SGPropertyNode::READ, true);
211 w->setAttribute(SGPropertyNode::WRITE, true);
213 SGPropertyNode *prop = write ? w : r;
214 prop->setStringValue(str);
215 const char *result = prop->getStringValue();
216 return result[0] ? result : 0;