POV-Ray : Newsgroups : povray.newusers : Recursive #includes? Server Time
1 Nov 2024 15:27:49 EDT (-0400)
  Recursive #includes? (Message 1 to 3 of 3)  
From: John
Subject: Recursive #includes?
Date: 12 May 2005 02:00:05
Message: <web.4282f02eb4d902fd56675f080@news.povray.org>
Hi.  I have been studying some POV code lately and I am trying to figure out
how this works.  In my mind it would be an endless loop but it works.  Here
is a snippet of an older POV file from the IRTC site.  It's code to produce
a 3D Snowflake by Andreas Kahler.

The main file has this code to reference an #include file....

#declare TotalLevel = 4;

// the 3D snowflake
#render "nadding 3D snowflake...n"

#declare Tetra = intersection {
   plane {-y,1}
   plane {-z,1 rotate <19.47,    0, 0>}
   plane {-z,1 rotate <19.47, -120, 0>}
   plane {-z,1 rotate <19.47,  120, 0>}
   bounded_by { sphere { <0,0,0>, 3.0 }}
}

#declare level=TotalLevel - 1
union {
  object {
    Tetra
    rotate 180*x
    pigment {color rgb <0.1,0,0.7>}
    finish { ambient 0.1 diffuse 0.5 brilliance 1 phong 0.6 phong_size 60 }
  }
  #include "snowflak.inc"
  pigment {color rgb <0.9,0,0.1>}
  finish { ambient 0.1 diffuse 0.6 brilliance 1 phong 0.6 phong_size 60
reflection 0.1}
  scale 0.1
  rotate <90,30,0>
  translate <-0.7,0.9,0.7>
}

And the #include file "snowflak.inc"

#if (level > 0)
  #declare level = level - 1;

  object { Tetra }
  union {
    #include "snowflak.inc"
    scale 0.5
    translate -1.5*y
  }
  union {
    #include "snowflak.inc"
    scale 0.5
    translate -1.5*y
    rotate <0,180,0>
    rotate <109.47,0,0>
  }
  union {
    #include "snowflak.inc"
    scale 0.5
    translate -1.5*y
    rotate <0,180,0>
    rotate <109.47,120,0>
  }
  union {
    #include "snowflak.inc"
    scale 0.5
    translate -1.5*y
    rotate <0,180,0>
    rotate <109.47,-120,0>
  }

  #declare level = level + 1;

#else
  object { Tetra }
  object { Tetra }
#end


If anyone could tell me how this works I'd be grateful.

Thanks.
John


Post a reply to this message

From: Mike Williams
Subject: Re: Recursive #includes?
Date: 12 May 2005 02:34:50
Message: <lovqlCAAkvgCFwF9@econym.demon.co.uk>
Wasn't it John who wrote:
>Hi.  I have been studying some POV code lately and I am trying to figure out
>how this works.  In my mind it would be an endless loop but it works.

The more conventional way to do recursive programming is to use macros
and pass the level variable as a parameter. In that way each copy of the
macro has its own copy of the level variable, and the loop terminates
when the level variable becomes zero.

In this code, there's a single global variable called "level".

When the include file is called the first time, the level is 3. It calls
four copies of itself with level 2. After those copies, and all the
lower copies that they call, have all finished it sets the level back to
3 and terminates.

Let's consider one of the level 2 copies. It calls four copies with
level 1. After those copies, and all the lower copies that they call,
have all finished it sets the level back to 2 and terminates. It has to
set the variable back to 2, because it's a global variable. It can't
exit with the changed value because that would completely change what
happens in the parent copy.

Similarly, the level 1 copies call four level 0 copies and set the
global variable back to 1 just before they exit.

The level 0 copies don't change the level, they just create two
identical copies of "Tetra". (The second copy is a kluge to avoid the
"must have at least 2 objects in csg" warning).

-- 
Mike Williams
Gentleman of Leisure


Post a reply to this message

From: John
Subject: Re: Recursive #includes?
Date: 13 May 2005 01:20:01
Message: <web.428438c325f256e2839200fa0@news.povray.org>
Thanks for the info.  You have been a great help to me.  I don't really know
a lot about POV scripting but I am getting it a little bit at a time.

John


Post a reply to this message

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