|
|
> When defining the function for an isosurface I would lke to be able
> to use #while loops, for example, to compute a series to some specified
> precision.
Unfortunately, you can't really have loops (although there is a summation
function which can help in some cases) or variables, but some things can be
faked if necessary, especially with recursion.
Here are some handy tricks:
The functional equivalent of precomputing a variable for multiple later uses
is this:
#declare func1 = function(x,y,z, precomputed){...}
#declare func2 = function(x,y,z) {func1(x,y,z, computeSomething(x,y,z))}
An if/else type of thing can be done with select():
select(
x-3, // "if x is less than 3", or more precisely "if x minus 3 is less
than zero"
x, // then
-x // else
)
And this can be used with recursion, which can be used to replace a while
loop. For instance, Warp once wrote this code to create a mandel fractal
(from "Re: Difference Small objects and Glass Cube" on Tuesday, September
14, 2004 11:28 AM in povray.general):
#declare MandIter =
function(n, Re, Im, Zr, Zi)
{ select(n>50 | Zr*Zr+Zi*Zi > 4, 0,
MandIter(n+1, Re, Im, Zr*Zr-Zi*Zi+Re, 2*Zr*Zi+Im), n/50)
}
#declare Mandel = function(Re, Im) { MandIter(0, Re, Im, Re, Im) }
> Direct email replies welcome.
I think it's best to reply via the newsgroup so the community benefits from
the knowledge passed on.
- Slime
[ http://www.slimeland.com/ ]
Post a reply to this message
|
|
|
|
Slime <fak### [at] emailaddress> wrote:
> Unfortunately, you can't really have loops
You say this and then later you give an example of a looping function
(using recursion)... :P
It's true that user-defined functions don't have a loop structure per se
(like 'for', 'while', etc), but usually you can use recursion to achieve
the same thing (even though the lack of assignable variables makes it a
bit cumbersome sometimes).
--
- Warp
Post a reply to this message
|
|
|
|
Paul Bourke wrote:
> Is it possible to use more interesting user defined float functions
> in an isosurface?
>
> When defining the function for an isosurface I would lke to be able
> to use #while loops, for example, to compute a series to some specified
> precision.
In addition to the technique Slime outlined you can of course use #while
etc. in functions to generate functions (whithout actual render time
loops of course). In some cases this can be an efficient way to
generate complicated functions (if your loop parameters do not depend on
the function parameters).
My waves include file:
http://www.tu-bs.de/%7Ey0013390/pov/water/water_inc.html
gives an example for this.
Christoph
--
POV-Ray tutorials, include files, Landscape of the week:
http://www.tu-bs.de/~y0013390/ (Last updated 24 Jul. 2005)
MegaPOV with mechanics simulation: http://megapov.inetart.net/
Post a reply to this message
|
|
|
|
Paul Bourke wrote:
> Is it possible to use more interesting user defined float functions
> in an isosurface?
>
> When defining the function for an isosurface I would lke to be able
> to use #while loops, for example, to compute a series to some specified
> precision.
>
> Direct email replies welcome.
In POV-Ray 3.6 (or was it only 3.6.1, I don't remember for sure, I tested
with 3.7 beta), real recursive functions work as an experimental feature.
You still get a possible error reported, but possible errors are non-fatal,
so the parser does continue. Thus, this should work:
#declare f = function(n) { select(n - 2.01, 1, f(n - 1) + f(n - 2)) }
#declare n = 1;
#while(n < 20)
#debug str(f(n), 0, 0)
#debug "\n"
#declare n = n + 1;
#end
As far as declaring variables inside functions is concerned (as others have
mentioned), sum and prod "declare" an iteration variable, and thus with sum
or prod one can get a _few_ local variables (they are truly local and not
computed more than once) when using them together with a suitable final
value for the iteration.
Thorsten
Post a reply to this message
|
|