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.
24 #include <simgear/compiler.h>
32 #include <simgear/debug/logstream.hxx>
33 #include <simgear/math/SGLimits.hxx>
34 #include <simgear/math/SGMisc.hxx>
37 #include "fg_props.hxx"
38 #include "globals.hxx"
41 #ifdef OSG_LIBRARY_STATIC
42 #include "osgDB/Registry"
47 // Originally written by Alex Perry.
49 fgGetLowPass (double current, double target, double timeratio)
51 if ( timeratio < 0.0 ) {
52 if ( timeratio < -1.0 ) {
53 // time went backwards; kill the filter
56 // ignore mildly negative time
58 } else if ( timeratio < 0.2 ) {
59 // Normal mode of operation; fast
60 // approximation to exp(-timeratio)
61 current = current * (1.0 - timeratio) + target * timeratio;
62 } else if ( timeratio > 5.0 ) {
63 // Huge time step; assume filter has settled
66 // Moderate time step; non linear response
67 double keep = exp(-timeratio);
68 current = current * keep + target * (1.0 - keep);
76 fgUnescape (const char *s)
88 } else if (*s == 'n') {
90 } else if (*s == 'r') {
92 } else if (*s == 't') {
94 } else if (*s == 'v') {
96 } else if (*s == 'f') {
98 } else if (*s == 'a') {
100 } else if (*s == 'b') {
102 } else if (*s == 'x') {
106 for (int i = 0; i < 2 && isxdigit(*s); i++, s++)
107 v = v * 16 + (isdigit(*s) ? *s - '0' : 10 + tolower(*s) - 'a');
111 } else if (*s >= '0' && *s <= '7') {
113 for (int i = 0; i < 3 && *s >= '0' && *s <= '7'; i++, s++)
114 v = v * 8 + *s - '0';
127 // Write out path to validation node and read it back in. A Nasal
128 // listener is supposed to replace the path with a validated version
129 // or an empty string otherwise.
130 const char *fgValidatePath (const char *str, bool write)
132 static SGPropertyNode_ptr r, w;
134 r = fgGetNode("/sim/paths/validate/read", true);
135 r->setAttribute(SGPropertyNode::READ, true);
136 r->setAttribute(SGPropertyNode::WRITE, true);
138 w = fgGetNode("/sim/paths/validate/write", true);
139 w->setAttribute(SGPropertyNode::READ, true);
140 w->setAttribute(SGPropertyNode::WRITE, true);
142 SGPropertyNode *prop = write ? w : r;
143 prop->setStringValue(str);
144 const char *result = prop->getStringValue();
145 return result[0] ? result : 0;