]> git.mxchange.org Git - flightgear.git/blob - src/Autopilot/inputvalue.hxx
AircraftModel hacking for package support.
[flightgear.git] / src / Autopilot / inputvalue.hxx
1 // inputvalue.hxx - provide input to autopilot components
2 //
3 // Written by Torsten Dreyer
4 // Copyright (C) 2010  Torsten Dreyer - Torsten (at) t3r (dot) de
5 //
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License as
8 // published by the Free Software Foundation; either version 2 of the
9 // License, or (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful, but
12 // WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19 //
20
21 #ifndef _INPUTVALUE_HXX
22 #define _INPUTVALUE_HXX 1
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28
29 #include <simgear/structure/SGExpression.hxx>
30
31 namespace FGXMLAutopilot {
32
33 typedef SGSharedPtr<class InputValue> InputValue_ptr;
34 typedef SGSharedPtr<class PeriodicalValue> PeriodicalValue_ptr;
35
36 /**
37  * @brief Model a periodical value like angular values
38  *
39  * Most common use for periodical values are angular values.
40  * If y = f(x) = f(x + n*period), this is a periodical function
41  */
42 class PeriodicalValue : public SGReferenced {
43 private:
44      InputValue_ptr minPeriod; // The minimum value of the period
45      InputValue_ptr maxPeriod; // The maximum value of the period
46 public:
47      PeriodicalValue( SGPropertyNode& prop_root,
48                       SGPropertyNode& cfg );
49      double normalize( double value ) const;
50      double normalizeSymmetric( double value ) const;
51 };
52
53 /**
54  * @brief A input value for analog autopilot components
55  *
56  * Input values may be constants, property values, transformed with a scale
57  * and/or offset, clamped to min/max values, be periodical, bound to 
58  * conditions or evaluated from expressions.
59  */
60 class InputValue : public SGReferenced {
61 private:
62      double             _value;    // The value as a constant or initializer for the property
63      bool               _abs;      // return absolute value
64      SGPropertyNode_ptr _property; // The name of the property containing the value
65      InputValue_ptr _offset;   // A fixed offset, defaults to zero
66      InputValue_ptr _scale;    // A constant scaling factor defaults to one
67      InputValue_ptr _min;      // A minimum clip defaults to no clipping
68      InputValue_ptr _max;      // A maximum clip defaults to no clipping
69      PeriodicalValue_ptr  _periodical; //
70      SGSharedPtr<const SGCondition> _condition;
71      SGSharedPtr<SGExpressiond> _expression;  ///< expression to generate the value
72      
73 public:
74     InputValue( SGPropertyNode& prop_root,
75                 SGPropertyNode& node,
76                 double value = 0.0,
77                 double offset = 0.0,
78                 double scale = 1.0 );
79
80     /**
81      *
82      * @param prop_root Root node for all properties with relative path
83      * @param cfg       Configuration node
84      * @param value     Default initial value
85      * @param offset    Default initial offset
86      * @param scale     Default initial scale
87      */
88     void parse( SGPropertyNode& prop_root,
89                 SGPropertyNode& cfg,
90                 double value = 0.0,
91                 double offset = 0.0,
92                 double scale = 1.0 );
93
94     /* get the value of this input, apply scale and offset and clipping */
95     double get_value() const;
96
97     /* set the input value after applying offset and scale */
98     void set_value( double value );
99
100     inline double get_scale() const {
101       return _scale == NULL ? 1.0 : _scale->get_value();
102     }
103
104     inline double get_offset() const {
105       return _offset == NULL ? 0.0 : _offset->get_value();
106     }
107
108     inline bool is_enabled() const {
109       return _condition == NULL ? true : _condition->test();
110     }
111
112 };
113
114 /**
115  * @brief A chained list of InputValues
116  *
117  * Many compoments support InputValueLists as input. Each InputValue may be bound to
118  * a condition. This list supports the get_value() function to retrieve the value
119  * of the first InputValue in this list that has a condition evaluating to true.
120  */
121 class InputValueList : public std::vector<InputValue_ptr> {
122   public:
123     InputValueList( double def = 0.0 ) : _def(def) { }
124
125     InputValue_ptr get_active() const {
126       for (const_iterator it = begin(); it != end(); ++it) {
127         if( (*it)->is_enabled() )
128           return *it;
129       }
130       return NULL;
131     }
132
133     double get_value() const {
134       InputValue_ptr input = get_active();
135       return input == NULL ? _def : input->get_value();
136     }
137   private:
138
139     double _def;
140
141 };
142
143 }
144
145 #endif