POV-Ray : Newsgroups : povray.advanced-users : Using arrays in functions : Re: Using arrays in functions Server Time
28 Sep 2024 06:09:10 EDT (-0400)
  Re: Using arrays in functions  
From: Trevor G Quayle
Date: 16 Mar 2011 17:20:01
Message: <web.4d81286cb19991f81c811d20@news.povray.org>
"Edouard" <pov### [at] edouardinfo> wrote:
> "Trevor G Quayle" <Tin### [at] hotmailcom> wrote:
> > I want to use an array inside a function, for example:
> >
> > function(x,y,z){MYARRAY[][]}
> >
> > I can hard code an array index, and it is fine:
> >
> > eg.  function(x,y,z){MYARRAY[2][3]}
> >
> > , however I can't create an array index from a function.  I get an error "Float
> > expected but vector or color expression found"
> >
> > e.g. function(x,y,z){MYARRAY[max(min(int(x),9),0)][max(min(int(y),9),0)]}
> >
> > parsing the function inside the array indices we return valid integer indices,
> > but it appears that POV is not parsing it this way: rather than precalculating
> > the function using the "x" and "y" values from the function, it is seeing them
> > as x and y vectors (<1,0,0>, <0,1,0>) which gives the error.  Changing to a
> > different reference from x,y,z eg.
> >
> > e.g. function(xx,yy,zz){MYARRAY[max(min(int(xx),9),0)][max(min(int(yy),9),0)]}
> >
> > results in an undeclared indetifier 'xx' error.
> >
> > I there some way I can work around this and force the function inside to be
> > calculated into my index value before the array tries to parse it?
> >
> > -tgq
>
> As far as I'm aware it simply can't be done. Functions and the SDL are basically
> two completely different things that are interpreted at different times.
>
> When I ran into this problem, I worked around it by stuffing my array values
> into a spline, then did a spline lookup inside my function. Perhaps you can get
> that trick to work for you too.
>
> Cheers,
> Edouard.

I figured it couldn't be done directly.  The only problem with a spline is that
in reality I can have a lot of entries (e.g. for a 400x400 array, 160,000), not
sure if there is a limit to the number of entries allowed in a spline.

I have found a solution for now: export my array to a df3 file and use the
density_file pattern to work my function.  Not the most elegant as I'd prefer to
be all internal, but it is working and the render speed-up I was looking for is
there, and for a bonus, I can see how the interpolation can help me smooth it
out.

-tgq


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.