Expressions: Difference between revisions

From FlightGear wiki
Jump to navigation Jump to search
(FPE checks)
No edit summary
 
(21 intermediate revisions by 9 users not shown)
Line 1: Line 1:
{{WIP}}
{{Stub}}
== General ==
{{-}}
Expressions are a feature of the SimGear library and provide a nice way of implementing complex math formulas using XML syntax.
'''Expressions''' (or '''SGExpressions''') are a feature of the SimGear library and provide a nice way of implementing complex math formulas using XML syntax.
They are supported in many systems within the FlightGear code.
They are supported in many systems within the FlightGear code.
'''Beware:''' Expressions do not check if your math creates floating point exceptions, like division by zero, square root of a negative number etc.
 
{{caution|Expressions do not check if your math creates floating point exceptions (like division by zero conditions, taking the square root of a negative number, etc.). This can cause undefined behavior and may result in NaNs or even Cascading NaNs.}}


== Usage ==
== Usage ==
Expressions are supported in
Expressions are supported in
* [[Autopilot_Configuration_Reference#Expressions|Autopilot configuration files]]
* [[Autopilot Configuration Reference#Expressions|Autopilot configuration files]]
* The particle system configuration files
* Particle system configuration files
* Animations (translate, rotate, scale, range, blend)
* Animations (translate, rotate, scale, range, blend)
* The shader technique
* The shader technique
* [[Conditions]]


== Sample Expressions ==
== Sample Expressions ==
This is a sample expression for c = sqrt(a*a + b^2)
This is a sample expression for <code>c = sqrt(a*a + b^2)</code>. Children/arguments are parsed in the order they appear in in the file (or the order in which they are set via property methods).
&lt;expression>
<syntaxhighlight lang="xml">
  &lt;sqrt>
<expression>
    &lt;sum>
  <sqrt>
      &lt;product>
    <sum>
        &lt;property>/value/a</property>
      <product>
        &lt;property>/value/a</property>
        <property>/value/a</property>
      &lt;/product>
        <property>/value/a</property>
      &lt;pow>
      </product>
        &lt;property>/value/b</property>
      <pow>
        &lt;value>2</value>
        <property>/value/b</property>
      &lt;/pow>
        <value>2</value>
    &lt;/sum>
      </pow>
  &lt;/sqrt>
    </sum>
&lt;/expression>
  </sqrt>
</expression>
</syntaxhighlight>
 


== Supported elements ==
== Supported elements ==
&lt;acos&gt;
<syntaxhighlight lang="xml">
&lt;asin&gt;
<abs> <!-- also: fabs -->
&lt;atan&gt;
<acos>
&lt;ceil&gt;
<asin>
&lt;cos&gt;
<atan>
&lt;cosh&gt;
<atan2>
&lt;exp&gt;
<ceil>
&lt;floor&gt;
<clip> <!-- <clipMin> <clipMax> -->
&lt;log&gt;
<cos>
&lt;log10&gt;
<cosh>
&lt;rad2deg&gt;
<difference> <!-- also: dif -->
&lt;sin&gt;
<div>
&lt;sinh&gt;
<exp>
&lt;sqrt&gt;
<floor>
&lt;tan&gt;
<log>
&lt;tanh&gt;
<log10>
&lt;atan2&gt;
<max>
&lt;div&gt;
<min>
&lt;mod&gt;
<mod>
&lt;pow&gt;
<pow>
&lt;value&gt;
<product> <!-- also: prod -->
&lt;property&gt;
<property> <!-- Unlike elsewhere, 'prop' does not work in expressions. -->
&lt;abs&gt; (same as fabs)
<rad2deg>
&lt;sqr&gt;
<deg2rad>
&lt;clip&gt;
<sin>
&lt;div&gt;
<sinh>
&lt;mod&gt;
<sqr>
&lt;sum&gt;
<sqrt>
&lt;difference&gt; (same as dif)
<sum>
&lt;product&gt; (same as prod)
<table> <!-- <entry><ind> value </ind><dep> value </dep></entry> -->
&lt;min&gt;
<tan>
&lt;max&gt;
<tanh>
&lt;table&gt;
<value>
</syntaxhighlight>
 
=== Table ===
The table expression uses the following syntax, similarly to the <interpolation> element:<syntaxhighlight lang="xml">
<table>
<!-- put an input element here, this can be <property> or any other operation
<entry>
<ind>0.0</ind> <!-- the value of the input (independent value) -->
<dep>0.0</dep> <!-- the value of the output (dependent value) -->
</entry>
        ... <!-- you can put as many entries as you want -->
</table>
</syntaxhighlight>
 
==== Example ====
The table:
{| class="wikitable"
|0.0
|5.0
|-
|0.5
|7.5
|-
|0.9
| -5.0
|-
|1.0
|25.0
|}
translates to the following code:<syntaxhighlight lang="xml">
<table>
<!-- put an input element here, this can be a <property> or any other operation -->
<entry>
<ind>0.0</ind>
<dep>5.0</dep>
</entry>
<entry>
<ind>0.5</ind>
<dep>7.5</dep>
</entry>
<entry>
<ind>0.9</ind>
<dep>-5.0</dep>
</entry>
<entry>
<ind>1.0</ind>
<dep>25.0</dep>
</entry>
</table>
</syntaxhighlight>
 
===Clip===
An expression to limit e.g. rotation:
<syntaxhighlight lang="xml">
  <expression>
    <clip>
      <clipMin>-45</clipMin>
      <clipMax>45</clipMax>
      <property>orientation/pitch-deg</property>
    </clip>
  </expression>
</syntaxhighlight>
 
== Hints and tips ==
=== Rounding ===
While there is no element for rounding, this workaround can be used for that:
<syntaxhighlight lang="xml">
<expression>
  <floor>
    <sum>
      <property>your/property/here</property>
      <value>0.5</value>
    </sum>
  </floor>
</expression>
</syntaxhighlight>
 
== Related content ==
=== Wiki articles ===
* [[Conditions]]
* [[PropertyList XML files]]
 
=== Source code ===
* {{simgear file|simgear/structure/SGExpression.hxx}}
* {{simgear file|simgear/structure/SGExpression.cxx}}
 
[[Category:XML]]

Latest revision as of 04:59, 18 January 2024

This article is a stub. You can help the wiki by expanding it.


Expressions (or SGExpressions) are a feature of the SimGear library and provide a nice way of implementing complex math formulas using XML syntax. They are supported in many systems within the FlightGear code.

Caution  Expressions do not check if your math creates floating point exceptions (like division by zero conditions, taking the square root of a negative number, etc.). This can cause undefined behavior and may result in NaNs or even Cascading NaNs.

Usage

Expressions are supported in

Sample Expressions

This is a sample expression for c = sqrt(a*a + b^2). Children/arguments are parsed in the order they appear in in the file (or the order in which they are set via property methods).

<expression>
  <sqrt>
    <sum>
      <product>
        <property>/value/a</property>
        <property>/value/a</property>
      </product>
      <pow>
        <property>/value/b</property>
        <value>2</value>
      </pow>
    </sum>
  </sqrt>
</expression>


Supported elements

<abs> <!-- also: fabs -->
<acos>
<asin>
<atan>
<atan2>
<ceil>
<clip> <!-- <clipMin> <clipMax> -->
<cos>
<cosh>
<difference> <!-- also: dif -->
<div>
<exp>
<floor>
<log>
<log10>
<max>
<min>
<mod>
<pow>
<product> <!-- also: prod -->
<property> <!-- Unlike elsewhere, 'prop' does not work in expressions. -->
<rad2deg>
<deg2rad>
<sin>
<sinh>
<sqr>
<sqrt>
<sum>
<table> <!-- <entry><ind> value </ind><dep> value </dep></entry> -->
<tan>
<tanh>
<value>

Table

The table expression uses the following syntax, similarly to the <interpolation> element:

<table>
	<!-- put an input element here, this can be <property> or any other operation
	<entry>
		<ind>0.0</ind> <!-- the value of the input (independent value) -->
		<dep>0.0</dep> <!-- the value of the output (dependent value) -->
	</entry>
        ... <!-- you can put as many entries as you want -->
</table>

Example

The table:

0.0 5.0
0.5 7.5
0.9 -5.0
1.0 25.0

translates to the following code:

<table>
	<!-- put an input element here, this can be a <property> or any other operation -->
	<entry>
		<ind>0.0</ind>
		<dep>5.0</dep>
	</entry>
	<entry>
		<ind>0.5</ind>
		<dep>7.5</dep>
	</entry>
	<entry>
		<ind>0.9</ind>
		<dep>-5.0</dep>
	</entry>
	<entry>
		<ind>1.0</ind>
		<dep>25.0</dep>
	</entry>
</table>

Clip

An expression to limit e.g. rotation:

  <expression>
    <clip>
      <clipMin>-45</clipMin>
      <clipMax>45</clipMax>
      <property>orientation/pitch-deg</property>
    </clip>
  </expression>

Hints and tips

Rounding

While there is no element for rounding, this workaround can be used for that:

<expression>
  <floor>
    <sum>
      <property>your/property/here</property>
      <value>0.5</value>
    </sum>
  </floor>
</expression>

Related content

Wiki articles

Source code