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.
20 #ifdef HAVE_CONFIG_H
\r
21 # include <config.h>
\r
24 #include <simgear/compiler.h>
33 #include <simgear/debug/logstream.hxx>
34 #include <simgear/math/SGLimits.hxx>
35 #include <simgear/math/SGMisc.hxx>
38 #include "fg_props.hxx"
39 #include "globals.hxx"
42 #ifdef OSG_LIBRARY_STATIC
43 #include "osgDB/Registry"
46 // Originally written by Alex Perry.
48 fgGetLowPass (double current, double target, double timeratio)
50 if ( timeratio < 0.0 ) {
51 if ( timeratio < -1.0 ) {
52 // time went backwards; kill the filter
55 // ignore mildly negative time
57 } else if ( timeratio < 0.2 ) {
58 // Normal mode of operation; fast
59 // approximation to exp(-timeratio)
60 current = current * (1.0 - timeratio) + target * timeratio;
61 } else if ( timeratio > 5.0 ) {
62 // Huge time step; assume filter has settled
65 // Moderate time step; non linear response
66 double keep = exp(-timeratio);
67 current = current * keep + target * (1.0 - keep);
75 fgUnescape (const char *s)
87 } else if (*s == 'n') {
89 } else if (*s == 'r') {
91 } else if (*s == 't') {
93 } else if (*s == 'v') {
95 } else if (*s == 'f') {
97 } else if (*s == 'a') {
99 } else if (*s == 'b') {
101 } else if (*s == 'x') {
105 for (int i = 0; i < 2 && isxdigit(*s); i++, s++)
106 v = v * 16 + (isdigit(*s) ? *s - '0' : 10 + tolower(*s) - 'a');
110 } else if (*s >= '0' && *s <= '7') {
112 for (int i = 0; i < 3 && *s >= '0' && *s <= '7'; i++, s++)
113 v = v * 8 + *s - '0';
126 // Write out path to validation node and read it back in. A Nasal
127 // listener is supposed to replace the path with a validated version
128 // or an empty string otherwise.
129 const char *fgValidatePath (const char *str, bool write)
131 static SGPropertyNode_ptr r, w;
133 r = fgGetNode("/sim/paths/validate/read", true);
134 r->setAttribute(SGPropertyNode::READ, true);
135 r->setAttribute(SGPropertyNode::WRITE, true);
137 w = fgGetNode("/sim/paths/validate/write", true);
138 w->setAttribute(SGPropertyNode::READ, true);
139 w->setAttribute(SGPropertyNode::WRITE, true);
141 SGPropertyNode *prop = write ? w : r;
142 prop->setStringValue(str);
143 const char *result = prop->getStringValue();
144 return result[0] ? result : 0;