POV-Ray : Newsgroups : povray.general : macros returning values Server Time
18 Nov 2024 09:12:30 EST (-0500)
  macros returning values (Message 1 to 10 of 10)  
From: Ernst Fritsch
Subject: macros returning values
Date: 6 Sep 2002 12:05:21
Message: <web.3d78d1281394686cdf77d9a70@news.povray.org>


I sometimes use macros to do some calculations and in the end I get a
value (float, vector, whatever). POV 3.1 had the need to put a ";" at the
end of every #local or #declare directive with floats or vectors but there
it just produced a warning and since I ran POV with warning console off I
never thought much about it any more.
Now it seems to be a hard error and I stumbled over a funny behaviour.
Parsing
of a scene stopped because I had to put a ";" at the end of a directive that
looked like this:

#local value = macro()

I tried

#local value = macro();



function but I guess you know what I mean).

Now I have a problem when I need the same macro in something like this:

translate <macro(), value, value>



Now I can put variable declarations into those parts as a workaround but
what I would like to know is:
Is it a bug or a feature?


Post a reply to this message

From: Christoph Hormann
Subject: Re: macros returning values
Date: 6 Sep 2002 12:18:17
Message: <3D78D549.AE541171@gmx.de>
Ernst Fritsch wrote:
> 
> [...]
> Parsing
> of a scene stopped because I had to put a ";" at the end of a directive that
> looked like this:
> 
> #local value = macro()
> 
> I tried
> 
> #local value = macro();
> 


> function but I guess you know what I mean).

You will have to post the code you are using, this is not a general
problem, things like:

#macro XX(test) 
  test
#end 

#declare YY=XX(0); 
#declare ZZ=<XX(0), 0, 0>; 

work perfectly.

Christoph

-- 
POV-Ray tutorials, IsoWood include,                 
TransSkin and more: http://www.tu-bs.de/~y0013390/  
Last updated 13 Aug. 2002 _____./\/^>_*_<^\/\.______


Post a reply to this message

From: Warp
Subject: Re: macros returning values
Date: 6 Sep 2002 13:08:11
Message: <3d78e0fb@news.povray.org>
You are "returning" the value in the non-recommendable way.
  The correct way of "returning" a value is to put it in a #local identifier
and then put that identifier alone at the very end of the macro.
  For example something like this:

#macro Whatever()
  #if(something)
    do_something
    #local result = whatever;
  #else
    do_something_else
    #local result = something_else;
  #end
  result
#end

  In some few cases this is not strictly necessary, though. For example:

#macro Min(A, B)
  (A<B ? A : B)
#end

-- 
#macro M(A,N,D,L)plane{-z,-9pigment{mandel L*9translate N color_map{[0rgb x]
[1rgb 9]}scale<D,D*3D>*1e3}rotate y*A*8}#end M(-3<1.206434.28623>70,7)M(
-1<.7438.1795>1,20)M(1<.77595.13699>30,20)M(3<.75923.07145>80,99)// - Warp -


Post a reply to this message

From: Shay
Subject: Re: macros returning values
Date: 6 Sep 2002 13:47:37
Message: <3d78ea39@news.povray.org>
Ernst Fritsch <ern### [at] citywebde> wrote in message
news:web.3d78d1281394686cdf77d9a70@news.povray.org...

Not sure this helps you, but my preferred way of doing it is this:

#macro MakeFive (VARIABLE)       // macro to make a variable = 5;
  #local VARIABLE = 5;           // set var referenced by VARIABLE = to 5
#end                             // end #macro MakeFive (VARIABLE)

#local Value = 0;                // name variable
MakeFive (Value);                // assign value to named variable

This uses a few more lines, but allows for a lot more freedom. Of course
this won't work inside of a conditional statement, but there are other
advantages, like being able to assign an entire array to a variable.

 -Shay


Post a reply to this message

From: TinCanMan
Subject: Re: macros returning values
Date: 6 Sep 2002 13:53:41
Message: <3d78eba5$1@news.povray.org>
Depending on the complexity of what's going on you can also pass variables
like this:

#declare b=0;
#declare c=0;

#macro Makebca(a,b,c)
    #local b=a;
    #local c=a;
#end

Makebca(1,b,c)


Obviously this is a very stupid macro but it illustrates the concept, the
catch is you have to predefine the variables (ie, b & c)

-tgq


Post a reply to this message

From: Shay
Subject: Re: macros returning values
Date: 6 Sep 2002 13:59:46
Message: <3d78ed12$1@news.povray.org>
TinCanMan <Tin### [at] hotmailcom> wrote in message
news:3d78eba5$1@news.povray.org...
> Depending on the complexity of what's going on you can also pass variables
> like this:

Something about this post looks vaguely familiar, but I can't quite put my
finger on it. <g>

 -Shay


Post a reply to this message

From: Warp
Subject: Re: macros returning values
Date: 6 Sep 2002 19:25:24
Message: <3d793964@news.povray.org>
Shay <sah### [at] simcopartscom> wrote:
> This uses a few more lines, but allows for a lot more freedom. Of course
> this won't work inside of a conditional statement, but there are other
> advantages, like being able to assign an entire array to a variable.

  And with that style it's unusable in situations like:

#declare Value = 2*MyMacro(a)+5*MyMacro(b);

-- 
#macro M(A,N,D,L)plane{-z,-9pigment{mandel L*9translate N color_map{[0rgb x]
[1rgb 9]}scale<D,D*3D>*1e3}rotate y*A*8}#end M(-3<1.206434.28623>70,7)M(
-1<.7438.1795>1,20)M(1<.77595.13699>30,20)M(3<.75923.07145>80,99)// - Warp -


Post a reply to this message

From: TinCanMan
Subject: Re: macros returning values
Date: 6 Sep 2002 19:54:04
Message: <3d79401c$1@news.povray.org>
>   And with that style it's unusable in situations like:
>
> #declare Value = 2*MyMacro(a)+5*MyMacro(b);

Of course not, but it is useful in different situations.  For example in my
mobius macros, one of the macro returns position, direction and normal
vectors for a specific spot on a mobius surface, ie, it returns three
different values.  In this type of situation it is usuable and necessary.

-tgq


Post a reply to this message

From: Ernst Fritsch
Subject: Re: macros returning values
Date: 7 Sep 2002 04:50:03
Message: <web.3d79bcb87685eda0606fc760@news.povray.org>
Christoph Hormann wrote:
>
>You will have to post the code you are using, this is not a general
>problem, things like:
>
>#macro XX(test)
>  test
>#end
>
>#declare YY=XX(0);
>#declare ZZ=<XX(0), 0, 0>;
>
>work perfectly.
>
Well the code is kind of lengthy. I have to admit that I encountered it at
one point in my scenes and if it is not a general problem (to find that out

to sort out the exact circumstances before I bring it up again.

Thanks so far.


Post a reply to this message

From: Shay
Subject: Re: macros returning values
Date: 9 Sep 2002 11:26:16
Message: <3d7cbd98@news.povray.org>
TinCanMan <Tin### [at] hotmailcom> wrote in message
news:3d79401c$1@news.povray.org...

This also helps when I am developing code. I write a macro which performs
some operation to an array. I will then write the array to an include file
in the form:

#macro Name_Array [NAME]
  #local NAME = array[120843]
   {[array items]}
#end

This allows me to parse only subsequent operations to the array *without*
using any global #declares. It also allows me to remove several data items
from a macro which I might use to find a problem later. I can name any
values at all which I declare within my macro and write the information
outside of the macro when I am done. Then, when the macro is working as
intended, I can remove these items only from between the parenthesis in the
macro definition without having to crawl through the macro and remove
anything.

  -Shay


Post a reply to this message

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