POV-Ray : Newsgroups : povray.newusers : Recursive #includes? : Re: Recursive #includes? Server Time
29 Jul 2024 18:17:26 EDT (-0400)
  Re: Recursive #includes?  
From: Mike Williams
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

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