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);
74 // Write out path to validation node and read it back in. A Nasal
75 // listener is supposed to replace the path with a validated version
76 // or an empty string otherwise.
77 const char *fgValidatePath (const char *str, bool write)
79 SGPropertyNode_ptr r, w;
80 r = fgGetNode("/sim/paths/validate/read", true);
81 r->setAttribute(SGPropertyNode::READ, true);
82 r->setAttribute(SGPropertyNode::WRITE, true);
84 w = fgGetNode("/sim/paths/validate/write", true);
85 w->setAttribute(SGPropertyNode::READ, true);
86 w->setAttribute(SGPropertyNode::WRITE, true);
88 SGPropertyNode *prop = write ? w : r;
89 prop->setStringValue(str);
90 const char *result = prop->getStringValue();
91 return result[0] ? result : 0;
94 //------------------------------------------------------------------------------
95 std::string fgValidatePath(const std::string& path, bool write)
97 const char* validate_path = fgValidatePath(path.c_str(), write);
98 return std::string(validate_path ? validate_path : "");