CGA functions

Functions are used to encapsulate evaluations which are used several times in the rules. Unlike rules, functions are typed (i.e. they return a value) and do not change the current shape. Functions can be parameterized, conditional, and stochastic.

Example

getHeight(area) =
   case area > 1000 : 300
   case area > 500 :
      20% : 200
      50% : 150
      else : 100
   else : rand(20,50)

The getHeight function takes one float parameter (area), and returns a height depending on the parameter. If area is larger than 1000, 300 is returned. If area is larger than 500 (but smaller than, or equal to, 1000), the return value is either 200, 150 or 100 with probabilities 0.2, 0.5 and 0.3. If area is smaller than or equal to 500, a random value between 20 and 50 is returned. A rule which uses getHeight might look like this:

Lot --> extrude(getHeight(geometry.area)) Envelope

Note:

In contrast to attributes, functions are evaluated in every call. This means a function like below:

height = rand(30, 50)

makes sense only for dedicated purposes because it returns a different value every time it is used.

Const Functions

Functions can be made constant with the const keyword. Const functions behave the same as attrs, the only difference is that const functions are internal to the rule file and can not be mapped in the Inspector.


In this topic