POV-Ray : Newsgroups : povray.advanced-users : Using arrays in functions Server Time
23 Dec 2024 08:12:14 EST (-0500)
  Using arrays in functions (Message 1 to 5 of 5)  
From: Trevor G Quayle
Subject: Using arrays in functions
Date: 16 Mar 2011 12:10:01
Message: <web.4d80e040f18413e381c811d20@news.povray.org>
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


Post a reply to this message

From: Edouard
Subject: Re: Using arrays in functions
Date: 16 Mar 2011 17:00:00
Message: <web.4d81249bb19991fcdaada170@news.povray.org>
"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.


Post a reply to this message

From: Trevor G Quayle
Subject: Re: Using arrays in functions
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

From: clipka
Subject: Re: Using arrays in functions
Date: 17 Mar 2011 05:49:59
Message: <4d81d947$1@news.povray.org>
Am 16.03.2011 17:07, schrieb Trevor G Quayle:
> 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)]}

Note that inside a function declaration, any and all variables (except 
for references to the function parameters) are evaluated at parse time; 
this is also true for array variables. But at parse time the function 
parameters x and y are unknown for obvious reasons.

BTW, evaluation of the variables is delegated to the general SDL parser, 
not the specialized function syntax parser; therefore in your case x and 
y are interpreted as the respective axis vectors rather than the 
function parameters.


Post a reply to this message

From: Trevor G Quayle
Subject: Re: Using arrays in functions
Date: 17 Mar 2011 09:45:01
Message: <web.4d820f56b19991f81c811d20@news.povray.org>
clipka <ano### [at] anonymousorg> wrote:
> Am 16.03.2011 17:07, schrieb Trevor G Quayle:
> > 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)]}
>
> Note that inside a function declaration, any and all variables (except
> for references to the function parameters) are evaluated at parse time;
> this is also true for array variables. But at parse time the function
> parameters x and y are unknown for obvious reasons.
>
> BTW, evaluation of the variables is delegated to the general SDL parser,
> not the specialized function syntax parser; therefore in your case x and
> y are interpreted as the respective axis vectors rather than the
> function parameters.

I figured as much and was hoping for a direct workaround that I was missing.
Regardless, I have found an alternate solution that suits my current
requirements.


-tgq


Post a reply to this message

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