POV-Ray : Newsgroups : povray.newusers : extracting x,y,z values in a macro Server Time
29 Jul 2024 20:28:24 EDT (-0400)
  extracting x,y,z values in a macro (Message 1 to 4 of 4)  
From: Max Ulbrich
Subject: extracting x,y,z values in a macro
Date: 22 Jan 2005 04:13:02
Message: <41f2191e$1@news.povray.org>
Hi,

I try to get a sum over an expression that contains data of an array. I 
try to use a macro for this purpose and later call this in the function 
part of the isosurface. In the part where I try to extract the x, y and 
z coordinates from the macro arguments something goes wrong. Although I 
do not get an error message, the x coordinate seems always to be 1.
It seems as if x gets evaluated before the call of the macro. Actually I 
would prefer to get this done just when the call happens. My guess is 
that macro isn't the right solution. Who can help me?
Thanks,
Max

#macro esum(posx,posy,posz)
   #local i=0;
   #local isum=0;
   #while (i<=arrcnt)
     #declare isum=isum+(1-1/(1+exp(-(sqrt(pow((posx.x-atmx[0])/vg,2)
                   +pow((posy.y-atmy[0])/vg,2)
                   +pow((posz.z-atmz[0])/vg,2))-v50/vg))));
     #declare i=i+1;
     #debug str(posx.x,1,0)
   #end
   #local imin=1-isum;
   imin
#end


isosurface {

   function { esum(x,y,z)  }
   contained_by {
     box { <26,-27,22>,<37,-34,27> }
   }
   threshold 0.5
   max_gradient 5
   pigment {Red}
  translate <-35,32,-24>
   scale 0.4

}


Post a reply to this message

From: Christoph Hormann
Subject: Re: extracting x,y,z values in a macro
Date: 22 Jan 2005 04:35:01
Message: <cst6go$5mj$1@chho.imagico.de>
Max Ulbrich wrote:
> Hi,
> 
> I try to get a sum over an expression that contains data of an array.

As you already have been told elsewhere you have to understand the 
difference between render time functions and parse time code.

The difference is explained in:

http://www.povray.org/documentation/view/3.6.1/231/#s02_02_01_06_02

Since neither the loop parameters nor the array indices depend on the 
function parameters you can probably do what you are trying to do with a 
macro but the parameters of the macros are parse time variables, not 
render time ones.

Examples for the use of macros for generating functions can be found in 
the IsoCSG library:

http://www.tu-bs.de/%7Ey0013390/pov/ic/index.html


Christoph

-- 
POV-Ray tutorials, include files, Sim-POV,
HCR-Edit and more: http://www.tu-bs.de/~y0013390/
Last updated 23 Sep. 2004 _____./\/^>_*_<^\/\.______


Post a reply to this message

From: Mike Williams
Subject: Re: extracting x,y,z values in a macro
Date: 22 Jan 2005 05:03:36
Message: <SVQ3aBAPTi8BFwdt@econym.demon.co.uk>
Wasn't it Max Ulbrich who wrote:
>Hi,
>
>I try to get a sum over an expression that contains data of an array. I 
>try to use a macro for this purpose and later call this in the function 
>part of the isosurface. In the part where I try to extract the x, y and 
>z coordinates from the macro arguments something goes wrong. Although I 
>do not get an error message, the x coordinate seems always to be 1.
>It seems as if x gets evaluated before the call of the macro. Actually I 
>would prefer to get this done just when the call happens. My guess is 
>that macro isn't the right solution. Who can help me?
>Thanks,
>Max

What's happening is that "x" "y" and "z" have two completely different
meanings in POV syntax. At run time (e.g. inside a function{}) they can
be function parameters. At parse time (e.g. inside a #macro) they are
shorthand for the vectors <1,0,0> <0,1,0> <0,0,1>.

You seem to have already noticed that inside the macros they are vectors
(because you pick out individual values from these vectors using the
syntax "posx.x") which gives us a big clue that "x" here is just
shorthand for "<1,0,0>".

We can get round this confusion by changing the names of the function
parameters, like this:

#declare F=function(A,B,C){esum(A,B,C) }
isosurface{ function{ F(x,y,z) }...
and also replacing "posx.x" by "posx" inside the macro, since we're now
attempting to pass float parameters instead of vectors.

Which then gives is the real POV error that's causing your problems

This part of the documentation applies:

  3.1.1.6.2  Functions and Macros

  You can use macros in functions, but the macros will be called only 
  once when the function is defined, not every time the function is 
  called. You cannot pass function variables to the macros. 

I.e. you can write 
  #declare F=function(A,B,C){A*B*C*esum(1,2,3)}
because the values of 1, 2 and 3 are available at parse time (when the
macro is evaluated) but you can't write
  #declare F=function(A,B,C){esum(A,B,C)}
because A, B and C are only available at run time

-- 
Mike Williams
Gentleman of Leisure


Post a reply to this message

From: Mike Williams
Subject: Re: extracting x,y,z values in a macro
Date: 22 Jan 2005 05:15:46
Message: <eVe06GA3ei8BFw8$@econym.demon.co.uk>
Wasn't it Max Ulbrich who wrote:
>Hi,
>
>I try to get a sum over an expression that contains data of an array. I 
>try to use a macro for this purpose and later call this in the function 
>part of the isosurface. In the part where I try to extract the x, y and 
>z coordinates from the macro arguments something goes wrong. Although I 
>do not get an error message, the x coordinate seems always to be 1.
>It seems as if x gets evaluated before the call of the macro. Actually I 
>would prefer to get this done just when the call happens. My guess is 
>that macro isn't the right solution. Who can help me?

I notice that this time you're not actually trying to index the arrays
at run time. You use atmx[0], which can be evaluated at parse time. So
in this case you can just write a loop inside your function definition
and it will all work.

It didn't produce anything very interesting when I ran it, but perhaps
that's because I didn't have very interesting values of vg, v50 or
atmx[0].

#declare F = function {
   #local i=0;
   #while (i<=arrcnt)
       +(1-1/(1+exp(-(sqrt(pow((x-atmx[0])/vg,2)
       +pow((y-atmy[0])/vg,2)
       +pow((z-atmz[0])/vg,2))-v50/vg))))
     #declare i=i+1;
   #end
}

isosurface {
   function{ 1 - F(x,y,z) }
   contained_by {
     box { <26,-27,22>,<37,-34,27> }
   }
   threshold 0.5
   max_gradient 5
   pigment {rgb x}
   translate <-35,32,-24>
   scale 0.4
}


Or, alternatively, instead of having a loop you can just multiply the
contained expression by arrcnt, since the expression evaluetes to the
same value for all values of "i".

-- 
Mike Williams
Gentleman of Leisure


Post a reply to this message

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