Using Arrays and Macros

© Mike Williams 2005

Up | Previous: Variable Parameters | Next: Steiner Surfaces | Next: Alphabetical Index


Using Arrays

You can't index an array inside an isosurface function.

You can use individual fixed elements of an array, like A[3] but you can't use the function variables to index the array like A[floor(x)].

If you've got a reasonably small number of elements in your array, you can use nested select() operations to pick out the individual elements of the array. In this example the array has eight elements.
 

#declare Index=function(i){
  select(i-4,
    select(i-2,
      select(i-1,A[0],A[1]),
      select(i-3,A[2],A[3])
    ),
    select(i-6,
      select(i-5,A[4],A[5]),
      select(i-7,A[6],A[7])
    )
  )
}

isosurface {
  function {F_cylinder(x,y,z,Index(x))}
This Index() function behaves rather like an array lookup, but it's going to be rather cumbersome to use this technique with large arrays.
You're not restricted to using float arrays. If you wish, you can create arrays of functions, like this:

#declare A[0] = function{f_sphere(x,y,z,1.0)}

And reference them like

function{A[0](x,y,z)}

Using Macros

You can't pass function variables to a macro.

You can call a macro from inside an isosurface function, but it gets evaluated once, at parse time.

However, it is possible to use macros when you approximate a parametric surface, because Ingo's "param.inc" includes the ability to work with macros instead of functions. You can't use Ingo's Parametric macro, because it only works with functions, you have to call the underlying Paramcalc macro directly, which is a bit trickier.

When using Paramcalc you have to name your macros (or functions) __Fx, __Fy, __Fz, rather than passing them as parameters to the macro. The remaining parameters of Paramcalc are the same as those for Parametric.

The code inside your macros has access to the #local variables within Ingo's include file. This means that you have to be careful to avoid variable name conflicts if you want the macro to work with variables that are declared in your own code. E.g. if you #declare variables in your own code called 'A', 'B', 'C', 'D', 'P', 'U' or 'V' then you can't use them within the passed macros because Paramcalc has #local variables with the same names.

You can't use variables u, v, x, y or z inside your macros, even as their formal parameters, because POV interprets them as shorthand for the unit vectors when parsing macros.

You can use arrays and vectors inside your macros.

Your macros can call functions and other macros.
 

#macro __Fx(U,V)
  My_Function(U) * cos(My_B*U) + My_Array[U]
#end

#include "param.inc"

object{
    Paramcalc(<0,0>,<2*pi,2*pi>,50,40,"")

 

Download a zip file containing the POV source files for all the images that appear on this page. (POV-ray 3.5 format only)

Up | Previous: Variable Parameters | Next: Steiner Surfaces | Next: Alphabetical Index