POV-Ray : Newsgroups : povray.general : declare a variable in a function Server Time
22 Dec 2024 13:56:54 EST (-0500)
  declare a variable in a function (Message 1 to 10 of 11)  
Goto Latest 10 Messages Next 1 Messages >>>
From: ingo
Subject: declare a variable in a function
Date: 2 Apr 2021 12:49:19
Message: <XnsAD00BF763472Bseed7@news.povray.org>
i.i.r.c. there is/was a way to declare a variable in a function. But how? 
I don't remember and can't find it in the remnants of my archive.

Ingo


Post a reply to this message

From: William F Pokorny
Subject: Re: declare a variable in a function
Date: 2 Apr 2021 13:26:31
Message: <606753c7$1@news.povray.org>
On 4/2/21 12:49 PM, ingo wrote:
> i.i.r.c. there is/was a way to declare a variable in a function. But how?
> I don't remember and can't find it in the remnants of my archive.
> 
> Ingo
> 

I'm not aware of any such feature. You can set up calculations on the 
function call and the calls can be recursive.

It's therefore possible to use select to use one part of a written 
function to set some values plus a usePartTwo flag, say, which would 
then be used to call the same function again. The flag causing the 
second - non-reursive - part of the function to run with the calculated 
values/vars being passed in on the recursive call.

Bill P.


Post a reply to this message

From: Tor Olav Kristensen
Subject: Re: declare a variable in a function
Date: 2 Apr 2021 13:40:00
Message: <web.606755c0db16780d6f19eb189db30a9@news.povray.org>
ingo <ing### [at] tagpovrayorg> wrote:
> i.i.r.c. there is/was a way to declare a variable in a function. But how?
> I don't remember and can't find it in the remnants of my archive.

You can not have variables that are local to the function. (And you can not have
any declarations of variables inside the function that are executed when you
call the function.) If a function is created within a macro, it can access
variables that are local to the macro. But be aware, because the scope of
function names are problematic and not well documented in POV-Ray.

Towards the end of this thread I explained how one can get around the problem
with missing local variables in functions:

Newsgroup: povray.text.scene-files
Date: 2003-11-19 16:55:01
Subject: My favourite isosurface (see p.b.i. for image)
Author: Alex Kluchikov
http://news.povray.org/povray.text.scene-files/thread/
%3Cweb.3fbbe555cd7b5c811235fd70%40news.povray.org%3E/

For those that have not yet tried to program in any functional languages, it may
be an interesting exercise to program this way.

--
Tor Olav
http://subcube.com
https://github.com/t-o-k


Post a reply to this message

From: Tor Olav Kristensen
Subject: Re: declare a variable in a function
Date: 2 Apr 2021 15:05:00
Message: <web.60676a37db16780d6f19eb189db30a9@news.povray.org>
"Tor Olav Kristensen" <tor### [at] TOBEREMOVEDgmailcom> wrote:
> ingo <ing### [at] tagpovrayorg> wrote:
> > i.i.r.c. there is/was a way to declare a variable in a function. But how?
> > I don't remember and can't find it in the remnants of my archive.
>
> You can not have variables that are local to the function. (And you can not have
> any declarations of variables inside the function that are executed when you
> call the function.) If a function is created within a macro, it can access
> variables that are local to the macro. But be aware, because the scope of
> function names are problematic and not well documented in POV-Ray.
>...

At the end of the thread below you can find an example with an array of
functions in a macro. In that example a similar technique is used. Each
function, starting with the last one, calls the one before it until the
convergence criterion has been reached or the first function has been reached.

Newsgroup: povray.advanced-users
Date: 2003-05-02 09:53:29
Subject: Use of povray functions to make fractals other than "famous" Mandelbrot
with same scalability?
Author: Greg M  Johnson
http://news.povray.org/povray.advanced-users/thread/%3Ccjameshuff-CBB1E4.15064403052003%40netplex.aussie.org%3E/

As Hans Mikelson mentioned in that thread; This is "fake" recursion.
I'm not sure if real recursive functions are now safe to use in POV-Ray.
IIRC they were at some point not safe to use and could cause POV-Ray to crash.

--
Tor Olav
http://subcube.com
https://github.com/t-o-k


Post a reply to this message

From: ingo
Subject: Re: declare a variable in a function
Date: 2 Apr 2021 17:13:22
Message: <XnsAD00EC3ADF180seed7@news.povray.org>
in news:606753c7$1@news.povray.org William F Pokorny wrote:

> I'm not aware of any such feature

Bill, Tor Olav,

Usefull suggestions, as allways. Thanks! But I'm just not sure either one 
of the ways is what I'm thinkin' of. I.i.r.c It was something Thorsten 
mentioned / showed when functions where introduced. 
Anyway I'll study your suggestions. For now I "fixed" it by calling 
another function. Works fine, no hurries.

/*:Ramp Oscilator, goes from 0 to 1 over 1 Frequency period

Not for direct sound use because of DC.
*/
#declare Ramp = function(Freq, Amp, Tick, SRate){
  Amp * mod(Tick, 1/(Freq/SRate)) * (Freq/SRate)
}


/*:Triangle wave Oscilator*/
#declare TriangleOsc = function(Freq, Amp, Tick, SRate){
  (select (
    Ramp(Freq, 1, Tick, SRate) - 0.25,
    Ramp(Freq, 1, Tick, SRate) * 4,
    select(
      Ramp(Freq, 1, Tick, SRate) - 0.75,
      2 - Ramp(Freq, 1, Tick, SRate) * 4,
      Ramp(Freq, 1, Tick, SRate) * 4 - 4
    )
  )) * Amp
}


Cheers,

Ingo


Post a reply to this message

From: Tor Olav Kristensen
Subject: Re: declare a variable in a function
Date: 2 Apr 2021 19:05:00
Message: <web.6067a28edb16780d6f19eb189db30a9@news.povray.org>
ingo <ing### [at] tagpovrayorg> wrote:
> in news:606753c7$1@news.povray.org William F Pokorny wrote:
>
> > I'm not aware of any such feature
>
> Bill, Tor Olav,
>
> Usefull suggestions, as allways. Thanks! But I'm just not sure either one
> of the ways is what I'm thinkin' of. I.i.r.c It was something Thorsten
> mentioned / showed when functions where introduced.
> Anyway I'll study your suggestions. For now I "fixed" it by calling
> another function. Works fine, no hurries.
>
> /*:Ramp Oscilator, goes from 0 to 1 over 1 Frequency period
>
> Not for direct sound use because of DC.
> */
>...

Ingo

I'm sorry, but I have no recollection of such a feature.

In your TriangleOsc function the same Ramp value is calculated 5 times.
If you want more efficient functions then I suggest something like this:

#declare TempFn =
    function(Ramp) {
        select(
            Ramp - 0.25,
            Ramp,
            select(
                Ramp - 0.75,
                0.5 - Ramp,
                Ramp - 1
            )
        )
    }
;

#declare TriangleOscFn =
    function(Freq, Amp, Tick, SRate) {
        4*Amp*TempFn(Freq/SRate*mod(Tick, SRate/Freq))
    }
;

--
Tor Olav
http://subcube.com
https://github.com/t-o-k


Post a reply to this message

From: ingo
Subject: Re: declare a variable in a function
Date: 3 Apr 2021 02:11:08
Message: <XnsAD01534407C0Dseed7@news.povray.org>
in news:web.6067a28edb16780d6f19eb189db30a9@news.povray.org Tor Olav 
Kristensen wrote:

> Ramp value is calculated 5 times

That's indeed what bothered me. Thanks Tor Olav,

Ingo


Post a reply to this message

From: ingo
Subject: Re: declare a variable in a function
Date: 3 Apr 2021 02:47:21
Message: <XnsAD015969220ADseed7@news.povray.org>
in news:web.6067a28edb16780d6f19eb189db30a9@news.povray.org Tor Olav 
Kristensen wrote:

> I'm sorry, but I have no recollection of such a feature.

Maybe it was this "abuse" I was thinking of:


#declare Ramp = function(Freq, Tick, SRate){
  mod(Tick, SRate/Freq) * (Freq/SRate)
} 

#declare TriOsc = function(Freq, Amp, Tick, SRate) {
  #local TriSelect = function(R) {
    select(
      R - 0.25,
      R,
      select(
        R - 0.75,
        0.5 - R,
        R - 1
      )
    )
  };
  4*Amp*TriSelect(Ramp(Freq,Tick,SRate))
};

It adds nothing but keeping the functions together in one block,

Ingo


Post a reply to this message

From: Tor Olav Kristensen
Subject: Re: declare a variable in a function
Date: 4 Apr 2021 19:45:00
Message: <web.606a4ed5db16780d6f19eb189db30a9@news.povray.org>
ingo <ing### [at] tagpovrayorg> wrote:
> in news:web.6067a28edb16780d6f19eb189db30a9@news.povray.org Tor Olav
> Kristensen wrote:
>
> > I'm sorry, but I have no recollection of such a feature.
>
> Maybe it was this "abuse" I was thinking of:
>
>
> #declare Ramp = function(Freq, Tick, SRate){
>   mod(Tick, SRate/Freq) * (Freq/SRate)
> }
>
> #declare TriOsc = function(Freq, Amp, Tick, SRate) {
>   #local TriSelect = function(R) {
>     select(
>       R - 0.25,
>       R,
>       select(
>         R - 0.75,
>         0.5 - R,
>         R - 1
>       )
>     )
>   };
>   4*Amp*TriSelect(Ramp(Freq,Tick,SRate))
> };
>
> It adds nothing but keeping the functions together in one block,

Well, that code added more than nothing for me:
It added a moment of confusion.

I would not recommend writing it like that. The most important reason is that it
could lead new POV-Ray users to believe that the variable that holds the
TriSelect function is local to the TriOsc function.

My understanding is that it is only local to the file that holds this code.

--
Tor Olav
http://subcube.com
https://github.com/t-o-k


Post a reply to this message

From: ingo
Subject: Re: declare a variable in a function
Date: 5 Apr 2021 14:03:21
Message: <XnsAD03CC03DBAC4seed7@news.povray.org>
in news:web.606a4ed5db16780d6f19eb189db30a9@news.povray.org Tor Olav 
Kristensen wrote:

> It added a moment of confusion.

You're right. It does,

Ingo


Post a reply to this message

Goto Latest 10 Messages Next 1 Messages >>>

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