POV-Ray : Newsgroups : povray.general : Few tricks Server Time
23 Nov 2024 16:37:54 EST (-0500)
  Few tricks (Message 1 to 9 of 9)  
From: kurtz le pirate
Subject: Few tricks
Date: 26 Nov 2023 04:46:56
Message: <65631410$1@news.povray.org>
Hello,

In my current project, I'm doing a lot of calculations using macros and
functions. I need macros that return several values and a rounding
function. I'm sharing my solutions here (which may already be known to
the Grand Masters).



*** Tips 1 : Rounding ***

#declare pow10 = function(num_digits) {
  prod(i,1,num_digits,10)
  }
#declare fnRound = function (number, num_digits) {
  ceil(number*pow10(num_digits))/pow10(num_digits)
  }

#declare a = fnRound(pi,6); // round pi to 6 digits
#debug concat(str(a,0,9),"\n")

-> 3.141 593 000 (extra spaces added)

Waring : Be careful with the number of digits required, depending on the
precision of the calculations... especially with large numbers.




*** Tips 2 : Multiple return values from macro directly assigned to
variables ***


#declare MyMacro(p1, p2, p3, p4)
  ..
  ..
  array { Return1, Return2, Return3}
#end


Used in this way :
#declare { V1, V2, V3 } = MyMacro (a, b, c, d);



I hope this helps.
Critical comments welcome !
-- 
Kurtz le pirate
Compagnie de la Banquise


Post a reply to this message

From: jr
Subject: Re: Few tricks
Date: 26 Nov 2023 05:10:00
Message: <web.6563188bc0e3ff73f11225116cde94f1@news.povray.org>
hi,

kurtz le pirate <kur### [at] gmailcom> wrote:
> In my current project, ...
> *** Tips 2 : Multiple return values from macro directly assigned to
> variables ***
>
> #declare MyMacro(p1, p2, p3, p4)
>   ..
>   ..
>   array { Return1, Return2, Return3}
> #end
>
>
> Used in this way :
> #declare { V1, V2, V3 } = MyMacro (a, b, c, d);
>
> I hope this helps.
> Critical comments welcome !

puzzled rather than critical :-)  why an array ?  eg

#macro myMacro(p1,p2,p3,p4)
  ...
  (ret1, ret2, ret3)
#end

#declare (v,v2,v3) = myMacro(a,b,c,d);


too works.


regards, jr.


Post a reply to this message

From: Bald Eagle
Subject: Re: Few tricks
Date: 26 Nov 2023 09:10:00
Message: <web.65635113c0e3ff731f9dae3025979125@news.povray.org>
"jr" <cre### [at] gmailcom> wrote:

> puzzled rather than critical :-)  why an array ?  eg
>
> #macro myMacro(p1,p2,p3,p4)
>   ...
>   (ret1, ret2, ret3)
> #end
>
> #declare (v,v2,v3) = myMacro(a,b,c,d);
>
>
> too works.

I used to use arrays, and sometimes vectors for this type of thing before clipka
implemented the tuple-style assignment.

WFP has been uncovering some interesting issues related to the tuple-style
assignment, and may be responsible for many of my hairs being ripped out in
bewilderment and frustration.

With an array, you have a nice, compact method of storing any number of values,
especially when using a mixed array.

Also, depending upon how the rest of the code is written, one can simply
#declare NewArray = MacroGeneratedArray ();
and be done with it.

Probably the most useful thing I can think of, at the moment, is that one can
cycle incrementally through all of the array elements with an iterator, whereas
you can't do any such thing with the tuple-style assignment.

You can also copy the entire array with a single line of code.

As with anything, it all depends on what you're doing, how far ahead you're
thinking, and just individual coding style.

- BW


Post a reply to this message

From: kurtz le pirate
Subject: Re: Few tricks
Date: 27 Nov 2023 05:20:09
Message: <65646d59$1@news.povray.org>
On 26/11/2023 11:06, jr wrote:
> ...
> 
> puzzled rather than critical :-)  why an array ?  eg
> 


Hi.
Why array ? Because it's the only method I've found that works with any
type of data. I didn't know that POV could use lists (,,,,).
I couldn't find anything in the docs about array* and/or list(tuple).

It also depends on the number of values to be returned. For 3,4,5
values, the list is acceptable and a good choice. Beyond that, an array
is easier to use.



Thanks for your comment.



* : in the context of macro return values, of course ;)
-- 
Kurtz le pirate
Compagnie de la Banquise


Post a reply to this message

From: Bald Eagle
Subject: Re: Few tricks
Date: 27 Nov 2023 06:10:00
Message: <web.65647807c0e3ff731f9dae3025979125@news.povray.org>
kurtz le pirate <kur### [at] gmailcom> wrote:

> I couldn't find anything in the docs about array* and/or list(tuple).

https://wiki.povray.org/content/Reference:Declare_and_Local_Directives

https://news.povray.org/povray.news-submissions/thread/%3C558b4c09%241%40news.povray.org%3E/

- BW


Post a reply to this message

From: jr
Subject: Re: Few tricks
Date: 27 Nov 2023 06:45:00
Message: <web.6564801fc0e3ff73f11225116cde94f1@news.povray.org>
hi,

kurtz le pirate <kur### [at] gmailcom> wrote:
> ...
> Why array ? Because it's the only method I've found that works with any
> type of data. I didn't know that POV could use lists (,,,,).

I hadn't known one can split a returned array like that.  </grin>


(also @BE)
> It also depends on the number of values to be returned. For 3,4,5
> values, the list is acceptable and a good choice. Beyond that, an array
> is easier to use.

agree, good for "a few" return values only.


regards, jr.


Post a reply to this message

From: ingo
Subject: Re: Few tricks
Date: 27 Nov 2023 07:55:00
Message: <web.65649144c0e3ff7317bac71e8ffb8ce3@news.povray.org>
"jr" <cre### [at] gmailcom> wrote:
>
> I hadn't known one can split a returned array like that.  </grin>

The array unpacking works well and it looks clean. I mostly use it within
macro's. As soon I create multiple instances "enumerating a struct" works
better, but is less clean.

---%<------%<------%<--
// PoVRay 3.8 Scene File "goertzel.pov"
// author:  Ingo Janssen
// date:    2023-11-16
//
// Oscillator that uses 'inverse' Goertzel
//--------------------------------------------------------------------------

#version 3.7;

global_settings{ assumed_gamma 1.0 }
#default{ finish{ ambient 0.1 diffuse 0.9 }}

#declare TAU = 2 * pi;

/*Enumerate elements of Goertzel oscillator data structure*/
#declare _gsoMultiplier = 0;
#declare _gsoCurrent = 1;
#declare _gsoPrevious = 2;
#declare _gsoN = 3;


#macro initGSinOsc(Frequency, Phase, SampleRate)
  #local phaseIncrement = Frequency * TAU / SampleRate;
  #local gsoMultiplier = 2 * cos(phaseIncrement);
  #local gsoCurrent = sin(Phase);
  #local gsoPrevious = sin(Phase - phaseIncrement);
  #local gsoN = 0;
  #local GSO = array {gsoMultiplier, gsoCurrent, gsoPrevious, gsoN};
  GSO
#end

// if 3.7
//#macro initGSinOsc(Frequency, Phase, SampleRate)
//  #local GSO = array[4];
//  #local phaseIncrement = Frequency * TAU / SampleRate;
//  #local GSO[_gsoMultiplier] = 2 * cos(phaseIncrement);
//  #local GSO[_gsoCurrent] = sin(Phase);
//  #local GSO[_gsoPrevious] = sin(Phase - phaseIncrement);
//  #local GSO[_gsoN] = 0;
//  GSO
//#end

#macro Next(GSO)
  #local {gsoMultiplier, gsoCurrent, gsoPrevious, gsoN} = GSO;
  #if (gsoN > 0)
    #local d0 = gsoMultiplier * gsoCurrent - gsoPrevious;
    #local gsoPrevious = gsoCurrent;
    #local gsoCurrent = d0;
  #else
    #local gsoN = gsoN + 1;
  #end
  #local GSO = array {gsoMultiplier, gsoCurrent, gsoPrevious, gsoN};
#end

//if 3.7
//#macro Next(GSO)
//  #if (GSO[_gsoN] > 0)
//    #local d0 = GSO[_gsoMultiplier] * GSO[_gsoCurrent] - GSO[_gsoPrevious];
//    #local GSO[_gsoPrevious] = GSO[_gsoCurrent];
//    #local GSO[_gsoCurrent] = d0;
//  #else
//    #local GSO[_gsoN] = GSO[_gsoN] + 1;
//  #end
//#end



#declare Duration = 1; //sec
#declare SampleRate = 44100;
#declare Ticks = Duration * SampleRate;
#declare Frequency = 440;
#declare Phase = 0;

#declare T0 = datetime(now, "%S")
#declare GSinOsc = initGSinOsc(Frequency, Phase, SampleRate); // here the
unpacking is not usefull.
#declare Garray = array [Ticks];
#for (Tick, 0, Ticks - 1)
  Next(GSinOsc)
  #declare Garray = GSinOsc[_gsoCurrent];
#end
#declare T1 = datetime(now, "%S")

#declare T2 = datetime(now, "%S")
#declare Sarray = array[Ticks];
#declare phaseIncrement = Frequency * TAU / SampleRate;
#for (Tick, 0, Ticks - 1)
  #declare Sarray[Tick] = sin(Tick * phaseIncrement);
#end
#declare T3 = datetime(now, "%S")

#debug T0
#debug "\n"
#debug T1
#debug "\n"
#debug T2
#debug "\n"
#debug T3
#debug "\n"


---%<------%<------%<---


Post a reply to this message

From: kurtz le pirate
Subject: Re: Few tricks
Date: 27 Nov 2023 10:14:40
Message: <6564b260$1@news.povray.org>
On 27/11/2023 12:05, Bald Eagle wrote:
> kurtz le pirate <kur### [at] gmailcom> wrote:
> 
>> I couldn't find anything in the docs about array* and/or list(tuple).
> 
> https://wiki.povray.org/content/Reference:Declare_and_Local_Directives
> 
>
https://news.povray.org/povray.news-submissions/thread/%3C558b4c09%241%40news.povray.org%3E/
> 
> - BW
> 
*Unbelievable*
You know all the pages of the doc on your fingertips ?





-- 
Kurtz le pirate
Compagnie de la Banquise


Post a reply to this message

From: Bald Eagle
Subject: Re: Few tricks
Date: 27 Nov 2023 14:00:00
Message: <web.6564e71fc0e3ff731f9dae3025979125@news.povray.org>
kurtz le pirate <kur### [at] gmailcom> wrote:
> On 27/11/2023 12:05, Bald Eagle wrote:
> > kurtz le pirate <kur### [at] gmailcom> wrote:
> >
> >> I couldn't find anything in the docs about array* and/or list(tuple).
> >
> > https://wiki.povray.org/content/Reference:Declare_and_Local_Directives
> >
> >
https://news.povray.org/povray.news-submissions/thread/%3C558b4c09%241%40news.povray.org%3E/
> >
> > - BW
> >
> *Unbelievable*
> You know all the pages of the doc on your fingertips ?

lol, no - I just have a fairly good idea of where it might be, and have honed my
search skills.
That is, if the damned search feature actually gives me what I'm looking for.

90% of the time I can't even find my own posts on a topic.

- BW


Post a reply to this message

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