]> git.mxchange.org Git - flightgear.git/blob - src/Main/util.cxx
Reduce amount of log output at level=debug.
[flightgear.git] / src / Main / util.cxx
1 // util.cxx - general-purpose utility functions.
2 // Copyright (C) 2002  Curtis L. Olson  - http://www.flightgear.org/~curt
3 //
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.
8 //
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.
13 //
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.
17 //
18 // $Id$
19
20 #ifdef HAVE_CONFIG_H
21 #  include <config.h>
22 #endif
23
24 #include <simgear/compiler.h>
25
26 #include <math.h>
27
28 #include <cstdlib>
29
30 #include <vector>
31
32 #include <simgear/debug/logstream.hxx>
33 #include <simgear/math/SGLimits.hxx>
34 #include <simgear/math/SGMisc.hxx>
35
36 #include "fg_io.hxx"
37 #include "fg_props.hxx"
38 #include "globals.hxx"
39 #include "util.hxx"
40
41 #ifdef OSG_LIBRARY_STATIC
42 #include "osgDB/Registry"
43 #endif
44
45 using std::vector;
46
47 // Originally written by Alex Perry.
48 double
49 fgGetLowPass (double current, double target, double timeratio)
50 {
51     if ( timeratio < 0.0 ) {
52         if ( timeratio < -1.0 ) {
53                                 // time went backwards; kill the filter
54                 current = target;
55         } else {
56                                 // ignore mildly negative time
57         }
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
64         current = target;
65     } else {
66                                 // Moderate time step; non linear response
67         double keep = exp(-timeratio);
68         current = current * keep + target * (1.0 - keep);
69     }
70
71     return current;
72 }
73
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)
78 {
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);
83
84     w = fgGetNode("/sim/paths/validate/write", true);
85     w->setAttribute(SGPropertyNode::READ, true);
86     w->setAttribute(SGPropertyNode::WRITE, true);
87
88     SGPropertyNode *prop = write ? w : r;
89     prop->setStringValue(str);
90     const char *result = prop->getStringValue();
91     return result[0] ? result : 0;
92 }
93
94 // end of util.cxx
95