1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5 Date started: August 25 2004
7 ------------- Copyright (C) 2001 Jon S. Berndt (jon@jsbsim.org) -------------
9 This program is free software; you can redistribute it and/or modify it under
10 the terms of the GNU Lesser General Public License as published by the Free Software
11 Foundation; either version 2 of the License, or (at your option) any later
14 This program is distributed in the hope that it will be useful, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
19 You should have received a copy of the GNU Lesser General Public License along with
20 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21 Place - Suite 330, Boston, MA 02111-1307, USA.
23 Further information about the GNU Lesser General Public License can also be found on
24 the world wide web at http://www.gnu.org.
26 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
28 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
33 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
39 #include "FGParameter.h"
41 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
43 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
45 #define ID_FUNCTION "$Id$"
47 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
49 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
53 class FGPropertyManager;
56 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
58 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
60 /** Represents a mathematical function.
61 The FGFunction class is a powerful and versatile resource that allows
62 algebraic functions to be defined in a JSBSim configuration file. It is
63 similar in concept to MathML (Mathematical Markup Language, www.w3.org/Math/),
64 but simpler and more terse.
65 A function definition consists of an operation, a value, a table, or a property
66 (which evaluates to a value). The currently supported operations are:
68 - difference (takes n args)
69 - product (takes n args)
70 - quotient (takes 2 args)
80 - atan2 (takes 2 args)
86 - random (Gaussian random number)
89 An operation is defined in the configuration file as in the following example:
93 <value> 3.14159 </value>
94 <property> velocities/qbar </property>
96 <value> 0.125 </value>
97 <property> metrics/wingarea </property>
102 A full function definition, such as is used in the aerodynamics section of a
103 configuration file includes the function element, and other elements. It should
104 be noted that there can be only one non-optional (non-documentation) element -
105 that is, one operation element - in the top-level function definition.
106 Multiple value and/or property elements cannot be immediate child
107 members of the function element. Almost always, the first operation within the
108 function element will be a product or sum. For example:
111 <function name="aero/coefficient/Clr">
112 <description>Roll moment due to yaw rate</description>
114 <property>aero/qbar-area</property>
115 <property>metrics/bw-ft</property>
116 <property>aero/bi2vel</property>
117 <property>velocities/r-aero-rad_sec</property>
119 <independentVar>aero/alpha-rad</independentVar>
129 The "lowest level" in a function is always a value or a property, which cannot
130 itself contain another element. As shown, operations can contain values,
131 properties, tables, or other operations. In the first above example, the sum
132 element contains all three. What is evaluated is written algebraically as:
134 @code 3.14159 + qbar + (0.125 * wingarea) @endcode
136 Some operations can take only a single argument. That argument, however, can be
137 an operation (such as sum) which can contain other items. The point to keep in
138 mind is that it evaluates to a single value - which is just what the trigonometric
139 functions require (except atan2, which takes two arguments).
144 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
145 DECLARATION: FGFunction
146 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
148 // Todo: Does this class need a copy constructor, like FGLGear?
150 class FGFunction : public FGParameter
155 When this constructor is called, the XML element pointed to in memory by the
156 element argument is traversed. If other FGParameter-derived objects (values,
157 functions, properties, or tables) are encountered, this instance of the
158 FGFunction object will store a pointer to the found object and pass the relevant
159 Element pointer to the constructor for the new object. In other words, each
160 FGFunction object maintains a list of "child" FGParameter-derived objects which
161 in turn may each contain its own list, and so on. At runtime, each object
162 evaluates its child parameters, which each may have its own child parameters to
164 @param PropertyManager a pointer to the property manager instance.
165 @param element a pointer to the Element object containing the function definition.
166 @param prefix an optional prefix to prepend to the name given to the property
167 that represents this function (if given).
169 FGFunction(FGPropertyManager* PropertyManager, Element* element, const std::string& prefix="");
171 virtual ~FGFunction();
173 /** Retrieves the value of the function object.
174 @return the total value of the function. */
175 double GetValue(void) const;
177 /** The value that the function evaluates to, as a string.
178 @return the value of the function as a string. */
179 std::string GetValueAsString(void) const;
181 /// Retrieves the name of the function.
182 std::string GetName(void) const {return Name;}
184 /** Specifies whether to cache the value of the function, so it is calculated only
186 If shouldCache is true, then the value of the function is calculated, and
187 a flag is set so further calculations done this frame will use the cached value.
188 In order to turn off caching, cacheValue must be called with a false argument.
189 @param shouldCache specifies whether the function should cache the computed value. */
190 void cacheValue(bool shouldCache);
193 std::vector <FGParameter*> Parameters;
194 FGPropertyManager* const PropertyManager;
197 std::string description_string;
198 std::string property_string;
199 std::string value_string;
200 std::string table_string;
201 std::string p_string;
202 std::string v_string;
203 std::string t_string;
204 std::string function_string;
205 std::string sum_string;
206 std::string difference_string;
207 std::string product_string;
208 std::string quotient_string;
209 std::string pow_string;
210 std::string exp_string;
211 std::string abs_string;
212 std::string sin_string;
213 std::string cos_string;
214 std::string tan_string;
215 std::string asin_string;
216 std::string acos_string;
217 std::string atan_string;
218 std::string atan2_string;
219 std::string min_string;
220 std::string max_string;
221 std::string avg_string;
222 std::string fraction_string;
223 std::string mod_string;
224 std::string random_string;
225 std::string integer_string;
227 enum functionType {eTopLevel=0, eProduct, eDifference, eSum, eQuotient, ePow,
228 eExp, eAbs, eSin, eCos, eTan, eASin, eACos, eATan, eATan2,
229 eMin, eMax, eAvg, eFrac, eInteger, eMod, eRandom} Type;
232 void Debug(int from);
235 } // namespace JSBSim