POV-Ray : Newsgroups : povray.general : Trouble parsing recursive macro Server Time
28 Nov 2024 05:45:40 EST (-0500)
  Trouble parsing recursive macro (Message 1 to 10 of 10)  
From: barton
Subject: Trouble parsing recursive macro
Date: 28 Jul 2007 13:30:02
Message: <web.46ab7a3b6b420bc8e071738a0@news.povray.org>
I'm creating a fairly simple recursive macro which creates a 10x10x10 cube,
and based on the level of recursion, breaks down each sub cube into
10x10x10 cubes, which may be in turn broken down into 10x10x10 cubes...

[take a look at
http://loudsl01-253-100-25.iglou.com/~tiger/pov_images/cube1.png]

Here's the source:

// by Barton Chittenden, based on PYRAMID2.POV by Chris Young

// Define the macro.  Parameters are:
//   X:  position of lower/right/forward block
//   Y:  position of lower/right/forward block
//   Z:  position of lower/right/forward block
//   E:  Length of edge of cube
//   L:  level of recursion

#macro Cube(X,Y,Z,E,L)

  #local Side = E * 0.80;
  #if ( L = 0)
        box { <X,Y,Z>, <X+Side,Y+Side,Z+Side>
                //pigment{color <R,G,B> }
        }
  #end // if recusion is false

  #if (L > 0)
    # local New_L = L - 1;
    # local New_E = Side / 10;
    # local i = 0;
    # local j = 0;
    # local k = 0;

    #while(i < 10)
        #while(j < 10)
                #while(k < 10)
                        Cube(
                                X+i*New_E,
                                Y+j*New_E,
                                Z+k*New_E,
                                New_E, New_L
                                )
                        #declare k = k+1;
                #end // Xhile k
                #declare k = 0;
                #declare j = j+1;
        #end // while j
        #declare j = 0;
        #declare i = i+1;
    #end // while i
  #end // if recursion is true
#end // macro Cube


union {
  Cube(-cube_radius,0,-15,10,2)

  pigment { color rgb <1,0,0> }
}

light_source { <2,20,-10> color rgb <1,1,1> }
light_source { <2,10,-20> color rgb <1,1,1> }

background { color rgb <0, 0, 0> }

// Cube is 10 units wide... sub cubes

camera {
        right x
        location <0,3*cube_radius,-25>
        look_at  <0,0,-3.5>
}

The trouble that I'm having is that I'm running out of memory because there
are numerous tokens created for each sub cube. If I use

Cube(-cube_radius,0,-15,10,1)

I'm fine; it creates 1002 objects (one for each sub cube, then two lights).

the render shows:

  0:00:00 Parsing
  0:00:00 Creating bounding slabs
  0:00:00 Creating vista buffer
  0:00:00 Creating light buffers 209K tokens

Indicating roughtly 200 tokens/sub cube

If I increase the recursion level to 2:

Cube(-cube_radius,0,-15,10,2)

I try to render, and it bottoms out at

0:02:12 Parsing 207628K tokens

Now most of the sub cubes are hidden, but because there is some space
between the sub cubes, there are areas where you can see all the way
through the cube. I'm trying to figure out a way of not parsing the sub
cubes that I can't see so that I don't swamp the memory, yet I want to make
sure that the parts of the
structure which are visible actually show correctly.

I'm not stuck on recursion, although that seems to be the simplest way of
generating a structure of this type.

Any thoughts?


Post a reply to this message

From: Christian Froeschlin
Subject: Re: Trouble parsing recursive macro
Date: 28 Jul 2007 18:10:53
Message: <46abbeed$1@news.povray.org>
barton wrote:

> I'm not stuck on recursion, although that seems to be the simplest way of
> generating a structure of this type.

Well, your branch factor is a whopping 10^3 = 1000 so obviously you
are not going to achieve a great recursion depth with this approach.
Are you stuck on the 10? Make the division count a parameter and
play with it, e.g., a 5x5x5 cube would be a lot friendlier.

The recursive approach itself is not the problem, just that
the object you are trying to build has a lot of components.

As there are gaps on all levels, almost every box could contribute
something to the output image. However, you might get away with
skipping all inner cubes (i.e., only recurse if one of i,j,k is
exactly 0 or 10), effectively reducing the branch factor to
10x10x10 - 8x8x8 = 488.


Post a reply to this message

From: Christian Froeschlin
Subject: Re: Trouble parsing recursive macro
Date: 28 Jul 2007 18:21:30
Message: <46abc16a$1@news.povray.org>
> The recursive approach itself is not the problem, just that
> the object you are trying to build has a lot of components.

Well, actually it could be possible to render the million boxes
you'd get at recursion depth 2, so you could try to get over the
parse problem using an iterative approach. Still I'd recommend
to reduce the number of objects. You might even try to fake the
last recursion level by using some sort of checker-like texture
with transparency.


Post a reply to this message

From: barton
Subject: Re: Trouble parsing recursive macro
Date: 28 Jul 2007 19:25:01
Message: <web.46abcf0a53db5e6e071738a0@news.povray.org>
Christian Froeschlin <chr### [at] chrfrde> wrote:
> barton wrote:
>
> > I'm not stuck on recursion, although that seems to be the simplest way of
> > generating a structure of this type.
>
> Well, your branch factor is a whopping 10^3 = 1000 so obviously you
> are not going to achieve a great recursion depth with this approach.
> Are you stuck on the 10? Make the division count a parameter and
> play with it, e.g., a 5x5x5 cube would be a lot friendlier.

Yeah... the point of the exercise it to illustrate how big a billion is ;-)

>
> The recursive approach itself is not the problem, just that
> the object you are trying to build has a lot of components.
>
> As there are gaps on all levels, almost every box could contribute
> something to the output image. However, you might get away with
> skipping all inner cubes (i.e., only recurse if one of i,j,k is
> exactly 0 or 10), effectively reducing the branch factor to
> 10x10x10 - 8x8x8 = 488.

I'm definitely willing to give this a try... stupid question:

how do an 'or' in SDL? I would like to do something like this:

#if( i = 0 or j = 0 or k = 0 or i = 9 or j = 9 or k = 9)
  Cube(
    X+i*New_E,
    Y+j*New_E,
    Z+k*New_E,
    New_E, New_L
    )
#end


Post a reply to this message

From: barton
Subject: Re: Trouble parsing recursive macro
Date: 28 Jul 2007 21:00:01
Message: <web.46abe59553db5e6e071738a0@news.povray.org>
Ok; I figured it out...

check out http://loudsl01-253-100-25.iglou.com/~tiger/pov_images/cube2.png
for the results...

here's the code:

#macro Cube(X,Y,Z,E,L)

  #local Side = E * 0.80;
  #if ( L = 0)
        box { <X,Y,Z>, <X+Side,Y+Side,Z+Side>
                //pigment{color <R,G,B> }
        }
  #end // if recusion is false

  #if (L > 0)
    # local New_L = L - 1;
    # local New_E = Side / 10;
    # local i = 0;
    # local j = 0;
    # local k = 0;

    #while(i < 10)
        #while(j < 10)
                #while(k < 10)
                        #if( i = 0 | j = 0 | k = 0 | i = 9 | j = 9 | k = 9)
                                Cube(
                                        X+i*New_E,
                                        Y+j*New_E,
                                        Z+k*New_E,
                                        New_E, New_L
                                        )
                        #else
                                Cube(
                                        X+i*New_E,
                                        Y+j*New_E,
                                        Z+k*New_E,
                                        New_E, New_L - 1
                                        )
                        #end
                        #declare k = k+1;
                #end // Xhile k
                #declare k = 0;
                #declare j = j+1;
        #end // while j
        #declare j = 0;
        #declare i = i+1;
    #end // while i
  #end // if recursion is true
#end // macro Cube

#declare cube_radius = 3.9;

union {
  Cube(-cube_radius,0,-15,10,2)

  pigment { color rgb <1,0,0> }
}

light_source { <2,20,-10> color rgb <1,1,1> }
light_source { <2,10,-20> color rgb <1,1,1> }

background { color rgb <0, 0, 0> }

// Cube is 10 units wide... sub cubes

camera {
        right x
        location <0,3*cube_radius,-25>
        look_at  <0,0,-3.5>
}

What I did was to reduce the recursion level by 2 for the middle cubes ...
so the middle isn't empty, it's just filled with larger cubes that parse
more easily.


Post a reply to this message

From: Christian Froeschlin
Subject: Re: Trouble parsing recursive macro
Date: 29 Jul 2007 13:36:46
Message: <46acd02e$1@news.povray.org>
barton wrote:
> Ok; I figured it out...
> 
> check out http://loudsl01-253-100-25.iglou.com/~tiger/pov_images/cube2.png
> for the results...

well, now you have a million, good luck reaching for the billion ;)


Post a reply to this message

From: Mark Birch
Subject: Re: Trouble parsing recursive macro
Date: 29 Jul 2007 20:35:01
Message: <web.46ad312e53db5e64daddc090@news.povray.org>
As an alternative, create a mesh2 that has the 1st level of 10 x 10 x 10
cubes as a single object, and use copies of that.


Post a reply to this message

From: Mark Birch
Subject: Re: Trouble parsing recursive macro
Date: 29 Jul 2007 21:00:02
Message: <web.46ad376453db5e64daddc090@news.povray.org>
An easier way to cut out the interior cubes, would be to declare a smaller
dummy cube and perform an 'inside' test on that.  If your cubes would fall
inside the dummy object, then don't place them.


Post a reply to this message

From: Alain
Subject: Re: Trouble parsing recursive macro
Date: 30 Jul 2007 06:00:33
Message: <46adb6c1@news.povray.org>
Christian Froeschlin nous apporta ses lumieres en ce 2007/07/29 13:36:
> barton wrote:
>> Ok; I figured it out...
>>
>> check out 
>> http://loudsl01-253-100-25.iglou.com/~tiger/pov_images/cube2.png
>> for the results...
> 
> well, now you have a million, good luck reaching for the billion ;)
It should be easier or beter to fake the next step. Use 3 gradiants to place 
thin black lines, with the part between the lines transparent.

-- 
Alain
-------------------------------------------------
Zen Buddhism: Shit is, and is not.


Post a reply to this message

From: John VanSickle
Subject: Re: Trouble parsing recursive macro
Date: 30 Jul 2007 13:35:36
Message: <46ae2168@news.povray.org>
barton wrote:
> I'm creating a fairly simple recursive macro which creates a 10x10x10 cube,
> and based on the level of recursion, breaks down each sub cube into
> 10x10x10 cubes, which may be in turn broken down into 10x10x10 cubes...

You'll be lucky to get more than two levels of recursion before you have 
so many scene objects that your memory manager will scream bloody murder,

UNLESS

You are culling a large proportion of the obects from the scene.

Regards,
John


Post a reply to this message

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