POV-Ray : Newsgroups : povray.advanced-users : Local variables in a function Server Time
24 Nov 2024 14:33:46 EST (-0500)
  Local variables in a function (Message 1 to 10 of 10)  
From: bugman
Subject: Local variables in a function
Date: 3 Jul 2006 05:10:00
Message: <web.44a8de03ac5178027acd794a0@news.povray.org>
I know how to create local variables in a macro, but after extensive
searching, I still have not found any way to create local variables in a
function. Is this even possible?

Just for a crazy example, suppose I had something like this:
#declare f=function(x) {sum(i,1,1000,pow(sin(x),i))}

Then it would be much faster if I could make a local variable, say,
something like this:
#declare f=function(x) {sinx=sin(x); sum(i,1,1000,pow(sinx,i))}


Post a reply to this message

From: POVMAN
Subject: Re: Local variables in a function
Date: 3 Jul 2006 07:00:41
Message: <44a8f8d9@news.povray.org>
I dont believe it is possibl old chum, it's a math only thing.... as opposed 
to a programatic thing.

-- 
#####-----#####-----#####
POV Tips and Hints at ...
http://povman.blogspot.com/


Post a reply to this message

From: Mike Williams
Subject: Re: Local variables in a function
Date: 3 Jul 2006 07:38:14
Message: <CnbTmDA7DQqEFwYX@econym.demon.co.uk>
Wasn't it bugman who wrote:
>I know how to create local variables in a macro, but after extensive
>searching, I still have not found any way to create local variables in a
>function. Is this even possible?
>
>Just for a crazy example, suppose I had something like this:
>#declare f=function(x) {sum(i,1,1000,pow(sin(x),i))}
>
>Then it would be much faster if I could make a local variable, say,
>something like this:
>#declare f=function(x) {sinx=sin(x); sum(i,1,1000,pow(sinx,i))}

Within a function, you can't set the value of a variable

If you're using this particular function to produce results for
individual values of x, rather than using it for an isosurface or
pigment where x varies beyond the reach of the SDL, then you could pass
the variable as a parameter:

#declare f=function(sinx) {sum(i,1,10000000,pow(sinx,i))}  
#debug str( f(sin(0.1)),-1,-1 )

However, it turns out to only be 13% faster than writing 

#declare f=function(x) {sum(i,1,10000000,pow(sin(x),i))}
#debug str(f(0.1),-1,-1)

It looks like evaluating sin(x) is incredibly efficient in POV
functions.

-- 
Mike Williams
Gentleman of Leisure


Post a reply to this message

From: Warp
Subject: Re: Local variables in a function
Date: 3 Jul 2006 08:59:43
Message: <44a914bf$1@news.povray.org>
bugman wrote:
> I know how to create local variables in a macro, but after extensive
> searching, I still have not found any way to create local variables in a
> function. Is this even possible?

   Unfortunately the syntax of functions (which is completely
distinct from the syntax of the SDL) is quite limited compared
to the SDL. Using variables is one of the things it does not
(yet) support.


Post a reply to this message

From: bugman
Subject: Re: Local variables in a function
Date: 4 Jul 2006 00:40:01
Message: <web.44a9f04215c4a01e7acd794a0@news.povray.org>
Wow, thanks for the prompt replies!


Post a reply to this message

From: bugman
Subject: Re: Local variables in a function
Date: 16 Jul 2006 05:15:01
Message: <web.44ba026515c4a01e7acd794a0@news.povray.org>
OK, I have a slightly different question. Is there any way to extract an
array element in a function? For example:

#declare xlist=array[3] {5.5,2.1,7.8};
#declare getx=function(i) {xlist[int(i-1)]};


Post a reply to this message

From: Mike Williams
Subject: Re: Local variables in a function
Date: 16 Jul 2006 07:46:48
Message: <X1O2yCA9WiuEFw7D@econym.demon.co.uk>
Wasn't it bugman who wrote:
>OK, I have a slightly different question. Is there any way to extract an
>array element in a function? For example:
>
>#declare xlist=array[3] {5.5,2.1,7.8};
>#declare getx=function(i) {xlist[int(i-1)]};

The nearest I've managed to get is this:

http://www.econym.demon.co.uk/isotut/arrays.htm

You can use the individual elements of arrays, but you can't index an
array from inside a function.

-- 
Mike Williams
Gentleman of Leisure


Post a reply to this message

From: Tim Attwood
Subject: Re: Local variables in a function
Date: 17 Jul 2006 07:22:20
Message: <44bb72ec@news.povray.org>
If you are really set on feeding raw data into functions,
you can use an image map pigment function, and edit
an image to contain your data in one of the color channels.

#declare TooFun = function {
   pigment {
     image_map {
        tga "fun_map.tga"
     }
   }
};
#declare TooFunX = function(x) {
   TooFun(x,0,0).x
};


Post a reply to this message

From: bugman
Subject: Re: Local variables in a function
Date: 19 Jul 2006 06:15:00
Message: <web.44be05fe15c4a01e7acd794a0@news.povray.org>
Thanks for the clever ideas.

Here is the fruit of my labor:
http://bugman123.com/Physics/Magnets.m1v

My approach was to automatically write lengthy functions in include files.


Post a reply to this message

From: bugman
Subject: Re: Local variables in a function
Date: 16 Feb 2007 23:05:01
Message: <web.45d67ec115c4a01eec3783420@news.povray.org>
"bugman" <nomail@nomail> wrote:
> Thanks for the clever ideas.
>
> Here is the fruit of my labor:
> http://bugman123.com/Physics/Magnets.m1v
>
> My approach was to automatically write lengthy functions in include files.

Here is another one:
http://bugman123.com/Physics/Motor.m1v


Post a reply to this message

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