POV-Ray : Newsgroups : povray.general : Re: Difference Small objects and Glass Cube Server Time
2 Aug 2024 20:19:37 EDT (-0400)
  Re: Difference Small objects and Glass Cube (Message 6 to 15 of 15)  
<<< Previous 5 Messages Goto Initial 10 Messages
From: Warp
Subject: Re: Difference Small objects and Glass Cube
Date: 14 Sep 2004 11:28:29
Message: <41470e1d@news.povray.org>
Thorsten Froehlich <tho### [at] trfde> wrote:
> >   I didn't know user-defined functions supported recursion or iteration...

> You can view "sum" and "prod" as iterations...

  Perhaps, but they are very limited.

  For example, I can't think of any way of making a mandelbrot function
using those.
  If the functions supported true recursion it would be rather easy:

#declare MandIter =
  function(n, Re, Im, Zr, Zi)
  { select(n>50 | Zr*Zr+Zi*Zi > 2, 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) }

  (Now you could do for example "pigment { function { Mandel(x,y) } ... }".)

-- 
#macro N(D)#if(D>99)cylinder{M()#local D=div(D,104);M().5,2pigment{rgb M()}}
N(D)#end#end#macro M()<mod(D,13)-6mod(div(D,13)8)-3,10>#end blob{
N(11117333955)N(4254934330)N(3900569407)N(7382340)N(3358)N(970)}//  - Warp -


Post a reply to this message

From: Slime
Subject: Re: Difference Small objects and Glass Cube
Date: 15 Sep 2004 20:00:11
Message: <4148d78b$1@news.povray.org>
>   I didn't know user-defined functions supported recursion or iteration...

Recursion I'm pretty sure they do support directly, although you need a base
case which has to kick in before 1024 recursions:

plane {
 -z,0
 pigment {
  #local myfunc = function(x) {select(x,0,x+myfunc(x-1))}
  function {myfunc(x)}
 }
 finish {ambient 1 diffuse 0}
}

camera {location -z*10 look_at 0}


Iteration isn't supported directly except with the sum/prod functions as
Thorsten said. What I tend to do when I need iteration is code it with SDL,
which results in a huge function and is only feasible in some cases
(excluding cases where some variable needs to be kept track of and its value
responded to in future iterations). For instance, this function (from my UV
Oven macro) does an O(log(n)) search through an SDL array in order to call
the function in the n'th index:

 #macro generateCallFaceColor(low, high)
  #if (low = high)
   facecolor[low](u,v, colorindex)
  #else
   #local mid = floor((low + high)*0.5);
   select(
    n - mid - 0.5,
    generateCallFaceColor(low, mid),
    generateCallFaceColor(mid+1, high)
   )
  #end
 #end
 #local callfacecolor = function(n, u, v, colorindex) {
  select(n,
   0.5, // if n < 0, use grey
   generateCallFaceColor(0, numfaces-1)
  )
 }

The resulting function doesn't actually iterate, but it behaves as though it
does.

 - Slime
 [ http://www.slimeland.com/ ]


Post a reply to this message

From: Slime
Subject: Re: Difference Small objects and Glass Cube
Date: 15 Sep 2004 20:00:17
Message: <4148d791$1@news.povray.org>
>   If the functions supported true recursion it would be rather easy:
>
> #declare MandIter =
>   function(n, Re, Im, Zr, Zi)
>   { select(n>50 | Zr*Zr+Zi*Zi > 2, 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) }
>
>   (Now you could do for example "pigment { function { Mandel(x,y) }
... }".)

And you can! Looks good. =)

(I find it impressive that you could do that from memory without actually
checking if it worked... though I think the > 2 should be > 4)

 - Slime
 [ http://www.slimeland.com/ ]


Post a reply to this message

From: Slime
Subject: Re: Difference Small objects and Glass Cube
Date: 15 Sep 2004 23:36:49
Message: <41490a51$1@news.povray.org>
> Recursion I'm pretty sure they do support directly, although you need a
base
> case which has to kick in before 1024 recursions:


By the way, I think I'll take this opportunity to say that if POV-Ray's
functions implemented tail recursion as iteration internally, that would
make them a lot more powerful (by removing the limit on recursion depth,
letting us effectively use it for iteration). Feature request! =)

(That feature plus support for data types other than numbers (which I'm sure
is easier said than done) might actually make POV functions nearly as
powerful as scheme or lisp.)

 - Slime
 [ http://www.slimeland.com/ ]


Post a reply to this message

From: Tor Olav Kristensen
Subject: Re: Difference Small objects and Glass Cube
Date: 16 Sep 2004 02:57:26
Message: <41493956$1@news.povray.org>
Slime wrote:

>>  If the functions supported true recursion it would be rather easy:
>>
>>#declare MandIter =
>>  function(n, Re, Im, Zr, Zi)
>>  { select(n>50 | Zr*Zr+Zi*Zi > 2, 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) }
>>
>>  (Now you could do for example "pigment { function { Mandel(x,y) }
> 
> ... }".)
> 
> And you can! Looks good. =)
...

Woaw ! That's nice. AFAIR from the pre-beta test period for v3.5,
it should not be possible for users to define recursive functions.

But obviously this has changed since then. (Unintentionally ?)


#version 3.6;

#include "colors.inc"

#declare IterFn =
   function(N, Re, Im, Zr, Zi) {
     select(
       N > 50 | Zr*Zr + Zi*Zi > 4,
       0,
       IterFn(N + 1, Re, Im, Zr*Zr - Zi*Zi + Re, 2*Zr*Zi + Im),
       N/50
     )
   }

#declare MandelFn = function(Re, Im) { IterFn(0, Re, Im, Re, Im) }

plane {
   -z, -2.5
   pigment {
     function { MandelFn(x, y) }
     color_map {
       [ 0.1 color Black   ]
       [ 0.3 color Cyan    ]
       [ 0.5 color Magenta ]
       [ 0.7 color Yellow  ]
       [ 0.9 color White   ]
     }
   }
   finish { ambient color White }
   translate 0.7*x
}

-- 
Tor Olav
http://subcube.net
http://subcube.com


Post a reply to this message

From: Tor Olav Kristensen
Subject: Re: Difference Small objects and Glass Cube
Date: 16 Sep 2004 03:10:20
Message: <41493c5c$1@news.povray.org>
Tor Olav Kristensen wrote:

> Slime wrote:
> 
>>>  If the functions supported true recursion it would be rather easy:
>>>
>>> #declare MandIter =
>>>  function(n, Re, Im, Zr, Zi)
>>>  { select(n>50 | Zr*Zr+Zi*Zi > 2, 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) }
>>>
>>>  (Now you could do for example "pigment { function { Mandel(x,y) }
>>
>>
>> ... }".)
>>
>> And you can! Looks good. =)
> 
> ...
> 
> Woaw ! That's nice. AFAIR from the pre-beta test period for v3.5,
> it should not be possible for users to define recursive functions.
> 
> But obviously this has changed since then. (Unintentionally ?)


Here's what happened when I tried the same with v3.5:

   Persistence of Vision Ray Tracer(tm) for Windows.
   ...
     This is version 3.5.icl.win32.
   ...
   File: E:\Raytracinger\RecursiveFunctionTest.pov  Line: 14
       )

     } <----ERROR

   Parse Error: Recursive function calls are not allowed!


   Returned from renderer with error status



This time I also checked the output when rendering with v3.6:

   Persistence of Vision Raytracer(tm) for Windows.
   ...
     This is version 3.6.1.icl8.win32.
   ...
   File: E:\Raytracinger\RecursiveFunctionTest.pov  Line: 14
   Possible Parse Error: Recursive function calls are not allowed!



- So it should still not be possible.

-- 
Tor Olav
http://subcube.net
http://subcube.com


Post a reply to this message

From: Slime
Subject: Re: Difference Small objects and Glass Cube
Date: 16 Sep 2004 09:09:09
Message: <41499075$1@news.povray.org>
>    Possible Parse Error: Recursive function calls are not allowed!

That's weird... and yet I use recursion all the time without problems. Never
noticed that error.

 - Slime
 [ http://www.slimeland.com/ ]


Post a reply to this message

From: Mike Andrews
Subject: Re: Difference Small objects and Glass Cube
Date: 16 Sep 2004 14:15:00
Message: <web.4149d7868f309b73c717c9af0@news.povray.org>
Hi Tor,

Tor Olav Kristensen <tor### [at] TOBEREMOVEDgmailcom> wrote:
> Woaw ! That's nice. AFAIR from the pre-beta test period for v3.5,
> it should not be possible for users to define recursive functions.
>
> But obviously this has changed since then. (Unintentionally ?)
>
>

I haven't seen this change in any of the change-logs, maybe it's an 'Easter
Egg' :-)

There's an nice variation I remembered from my days playing with Fractint,
so I went looking ...

[Mandelbrot code snipped]
>
> --
> Tor Olav
> http://subcube.net
> http://subcube.com

// start of code

#version 3.6;

#include "colors.inc"

// returns the 'potential' surface from 'Fractint'
#declare IterFn =
   function(N, Max, Bail, Slope, Re, Im, Zr, Zi) {
     select(
       N = Max | sqrt(Zr*Zr + Zi*Zi) > Bail,
       0,
       IterFn(N + 1, Max, Bail, Slope, Re, Im, Zr*Zr - Zi*Zi + Re, 2*Zr*Zi +
Im),
       min(1,select(N = Max, 0, pow(log(sqrt(Zr*Zr +
Zi*Zi))/pow(2,N),Slope), 0))
     )
   }

#declare MandelFn = function(Re, Im, Max, Bail, Slope) { IterFn(0, Max,
Bail, Slope, Re, Im, Re, Im) }

isosurface {
  function { y + MandelFn(x-0.7, z, 500, 2000, 0.6) }
  contained_by { box {-<3,1,3>,<3,0,3> } }
  max_gradient 6.5
  pigment {
    function { 1-MandelFn(x-0.7, z, 500, 2000, 0.3) }
    color_map {
      [ 0.0 color Black   ]
      [ 0.25 color Cyan    ]
      [ 0.5 color Magenta ]
      [ 0.75 color Yellow  ]
      [ 1.0 color White   ]
    }
  }
  rotate 10*z
  rotate -50*x
  translate 3*z
}

background { rgb <.5,.6,.7> }

light_source { <-2,4,0>*100 colour rgb 1 }

// end of code


Post a reply to this message

From: Warp
Subject: Re: Difference Small objects and Glass Cube
Date: 16 Sep 2004 19:52:09
Message: <414a2729@news.povray.org>
Slime <fak### [at] emailaddress> wrote:
> And you can! Looks good. =)

  Oh! Seems like 3.6 has more surprises than I expected. :-o

  During the beta-test of POV-Ray 3.5 I was working with Thorsten
testing the possibility of adding support for recursive calls in
functions, and they more or less worked ok, but due to some problems
which were difficult to solve back then it was decided that recursion
would not be supported (and explicitly disallowed).

  During the development of 3.6 I never heard of anything related
to this, so I assumed that no change had been done. However, seems
that something has been done (either purposely or inadvertedly, I don't
know which).

  Cool. This opens a whole new set of possibilities for functions.
(I just hope this was not just a mistake which will be "corrected"
in the next release... ;) )

> (I find it impressive that you could do that from memory without actually
> checking if it worked... though I think the > 2 should be > 4)

  I have written the mandelbrot set code so many times that I can do
it in my dreams... Except that this time I didn't remember to raise
the 2 to the square, so you are right: It should be 4, not 2. Perhaps
I'm getting old.

-- 
#macro N(D)#if(D>99)cylinder{M()#local D=div(D,104);M().5,2pigment{rgb M()}}
N(D)#end#end#macro M()<mod(D,13)-6mod(div(D,13)8)-3,10>#end blob{
N(11117333955)N(4254934330)N(3900569407)N(7382340)N(3358)N(970)}//  - Warp -


Post a reply to this message

From: Warp
Subject: Re: Difference Small objects and Glass Cube
Date: 16 Sep 2004 19:53:50
Message: <414a278e@news.povray.org>
Slime <fak### [at] emailaddress> wrote:
> >   I didn't know user-defined functions supported recursion or iteration...

> Recursion I'm pretty sure they do support directly

  The thing is that it was not supported in 3.5, and since I never heard
of any change regarding this, I assumed it was still unsupported.
  Well, you always learn something new. :)

-- 
#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

<<< Previous 5 Messages Goto Initial 10 Messages

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