1 // inputvalue.hxx - provide input to autopilot components
3 // Written by Torsten Dreyer
4 // Copyright (C) 2010 Torsten Dreyer - Torsten (at) t3r (dot) de
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.
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.
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.
21 #ifndef _INPUTVALUE_HXX
22 #define _INPUTVALUE_HXX 1
24 #include <simgear/structure/SGExpression.hxx>
26 namespace FGXMLAutopilot {
28 typedef SGSharedPtr<class InputValue> InputValue_ptr;
29 typedef SGSharedPtr<class PeriodicalValue> PeriodicalValue_ptr;
32 * @brief Model a periodical value like angular values
34 * Most common use for periodical values are angular values.
35 * If y = f(x) = f(x + n*period), this is a periodical function
37 class PeriodicalValue : public SGReferenced {
39 InputValue_ptr minPeriod; // The minimum value of the period
40 InputValue_ptr maxPeriod; // The maximum value of the period
42 PeriodicalValue( SGPropertyNode_ptr node );
43 double normalize( double value );
47 * @brief A input value for analog autopilot components
49 * Input values may be constants, property values, transformed with a scale
50 * and/or offset, clamped to min/max values, be periodical, bound to
51 * conditions or evaluated from expressions.
53 class InputValue : public SGReferenced {
55 double _value; // The value as a constant or initializer for the property
56 bool _abs; // return absolute value
57 SGPropertyNode_ptr _property; // The name of the property containing the value
58 InputValue_ptr _offset; // A fixed offset, defaults to zero
59 InputValue_ptr _scale; // A constant scaling factor defaults to one
60 InputValue_ptr _min; // A minimum clip defaults to no clipping
61 InputValue_ptr _max; // A maximum clip defaults to no clipping
62 PeriodicalValue_ptr _periodical; //
63 SGSharedPtr<const SGCondition> _condition;
64 SGSharedPtr<SGExpressiond> _expression; ///< expression to generate the value
67 InputValue( SGPropertyNode_ptr node = NULL, double value = 0.0, double offset = 0.0, double scale = 1.0 );
69 void parse( SGPropertyNode_ptr, double value = 0.0, double offset = 0.0, double scale = 1.0 );
71 /* get the value of this input, apply scale and offset and clipping */
72 double get_value() const;
74 /* set the input value after applying offset and scale */
75 void set_value( double value );
77 inline double get_scale() const {
78 return _scale == NULL ? 1.0 : _scale->get_value();
81 inline double get_offset() const {
82 return _offset == NULL ? 0.0 : _offset->get_value();
85 inline bool is_enabled() const {
86 return _condition == NULL ? true : _condition->test();
92 * @brief A chained list of InputValues
94 * Many compoments support InputValueLists as input. Each InputValue may be bound to
95 * a condition. This list supports the get_value() function to retrieve the value
96 * of the first InputValue in this list that has a condition evaluating to true.
98 class InputValueList : public std::vector<InputValue_ptr> {
100 InputValueList( double def = 0.0 ) : _def(def) { }
102 InputValue_ptr get_active() const {
103 for (const_iterator it = begin(); it != end(); ++it) {
104 if( (*it)->is_enabled() )
110 double get_value() const {
111 InputValue_ptr input = get_active();
112 return input == NULL ? _def : input->get_value();