POV-Ray : Newsgroups : povray.advanced-users : Optimizing scenes with nested loops. Server Time
31 Oct 2024 19:29:00 EDT (-0400)
  Optimizing scenes with nested loops. (Message 1 to 3 of 3)  
From: Josh
Subject: Optimizing scenes with nested loops.
Date: 12 Aug 2005 23:45:01
Message: <web.42fd6be1d1dc40c7f6675d760@news.povray.org>
I am working on some timple functions to make my own grass (I want the
challenge of making my own, I know there are some out there already).  The
way I'm doing it right now gives me the results I want (it looks like I
want it to look), but the parsing process is very slow due to the nested
loops that I use.  I am using splines to calculate the path I want.  With
my spline, I simply draw triangles to make up the grass.  There is a loops
the goes up the spline, while drawing triangles.  This loop is contained in
another loop whose roll it is to create multiple blades.  Heck, here is the
source code:

#declare counter=0;
#declare num_blades=12000;

#declare s_point1=<0,0,0>;
#declare s_point2=<0,0,0>;
#declare s_step=.1;
#declare blade_width = .025;



#while( counter < num_blades)

#declare num1=((rand(R2)-.5)*30) ;//-11 ;

#declare num2=0;

#declare num3=(rand(R2)-.5)*30;//- 11  ;

#declare rot = rand(R2)*360;

#declare ctr = 0;


#declare sway=cosd(clock)*.025;

#declare s = spline {quadratic_spline  0, <0,0,0> .5, <0, .5,0> 1, <0,1,.5>
}



#while (ctr < 1)
  /*sphere {
    <s(ctr).x,s(ctr).y,s(ctr).z>,.02
    pigment { rgb <1-ctr,ctr,0> }     */



    #declare s_point1=s(ctr);
    #declare s_point2=s(ctr+s_step);


    triangle {<s_point1.x-blade_width,s_point1.y,s_point1.z>
<s_point1.x+blade_width,s_point1.y,s_point1.z>
<s_point1.x-blade_width,s_point2.y,s_point2.z> pigment {color rgb <0,ctr,0>
} finish {diffuse .5} translate <num1,0,num3>   rotate y*rot}
    triangle {<s_point1.x+blade_width,s_point1.y,s_point1.z>
<s_point1.x+blade_width,s_point2.y,s_point2.z>
<s_point1.x-blade_width,s_point2.y,s_point2.z> pigment {color rgb <0,ctr,0>
} finish {diffuse .5} translate <num1,0,num3>  rotate y*rot}


  //}




  #declare ctr = ctr + s_step;
#end


#declare counter = counter + 1;
#end







As already stated, the parsing step is very slow and takes a lot of ram
(once it hits swap, the time required goes through the roof.  Can someone
help me come up with a good optimization scheme?


Post a reply to this message

From: Jim Charter
Subject: Re: Optimizing scenes with nested loops.
Date: 13 Aug 2005 01:18:44
Message: <42fd82b4@news.povray.org>
Josh wrote:
> I am working on some timple functions to make my own grass (I want the
> challenge of making my own, I know there are some out there already).  The
> way I'm doing it right now gives me the results I want (it looks like I
> want it to look), but the parsing process is very slow due to the nested
> loops that I use.  I am using splines to calculate the path I want.  With
> my spline, I simply draw triangles to make up the grass.  There is a loops
> the goes up the spline, while drawing triangles.  This loop is contained in
> another loop whose roll it is to create multiple blades.  Heck, here is the
> source code:
> 

> 
> 
> 
> As already stated, the parsing step is very slow and takes a lot of ram
> (once it hits swap, the time required goes through the roof.  Can someone
> help me come up with a good optimization scheme?
> 

Yes, you want to avoid the swapping out.

What everyone does with this is to take advantage of "instancing" 
Basically you want to group some subset of your traingles in a mesh 
block with an assigned name.  Then you reuse this mesh object to build 
your larger structure.  In the case of grass or hair the best break 
point is usually at the "patch" level, so to speak. (ie meaning a 
"patch" of grass.)  Instancing individual blades is usually too small a 
number of triangles to be efficient so you put some number of blades 
into a patch then reuse the patch to produce a grass "lawn"

The last time I did this I believe I made patches of about 75 blades of 
grass, each blade with five triangles.  But that number can be quite 
flexble according to what you are trying to do and what kind of memory 
you have.  I also sometimes produce an array of grass patches each 
randomly different, then access the array randomly to get a more 
irregular look.


Post a reply to this message

From: Mike Williams
Subject: Re: Optimizing scenes with nested loops.
Date: 13 Aug 2005 01:50:19
Message: <mfea4CA4nY$CFwvC@econym.demon.co.uk>
Wasn't it Josh who wrote:
>I am working on some timple functions to make my own grass (I want the
>challenge of making my own, I know there are some out there already).  The
>way I'm doing it right now gives me the results I want (it looks like I
>want it to look), but the parsing process is very slow due to the nested
>loops that I use.  I am using splines to calculate the path I want.  With
>my spline, I simply draw triangles to make up the grass.  There is a loops
>the goes up the spline, while drawing triangles.  This loop is contained in
>another loop whose roll it is to create multiple blades.  Heck, here is the
>source code:

I notice that all your blades are actually identical within each frame
of the animation, so it's possible to have two separate loops, one that
#declares a blade as a union of triangles, and the other that places
lots of copies of it.

I then noticed that it's possible to create the blade as a mesh, instead
of a union, since it consists only of triangles.

On my old slow computer it now parses in 7 seconds, using 111 Mb peak
memory.

Here's the resulting code:


#version 3.6;
global_settings {assumed_gamma 1.0}
camera {location  <0,10,-30> look_at <0,0,0> angle 50}
background {rgb 1}
light_source {<-30, 100, -30> color rgb 1}
#include "math.inc"

#declare counter=0;
#declare num_blades=12000;

#declare s_point1=<0,0,0>;
#declare s_point2=<0,0,0>;
#declare s_step=.1;
#declare blade_width = .025;

#declare R2 = seed(1);

#declare sway=cosd(clock)*.025;
#declare s = spline {quadratic_spline  0, <0,0,0> .5, <0, .5,0> 1,
<0,1,.5>}

// Create a single grass blade as a mesh
#declare Blade = mesh {
  #declare ctr=0;
  #while (ctr < 1)
    #declare s_point1=s(ctr);
    #declare s_point2=s(ctr+s_step);
    #declare T = texture {
       pigment {color rgb <0,ctr,0>}
       finish {diffuse .5}
    }
    triangle {<s_point1.x-blade_width,s_point1.y,s_point1.z>
       <s_point1.x+blade_width,s_point1.y,s_point1.z>
       <s_point1.x-blade_width,s_point2.y,s_point2.z>
     texture {T}
    }
    triangle {<s_point1.x+blade_width,s_point1.y,s_point1.z>
        <s_point1.x+blade_width,s_point2.y,s_point2.z>
        <s_point1.x-blade_width,s_point2.y,s_point2.z>
     texture {T}
    }
    #declare ctr = ctr + s_step;
  #end
}

// place lots of blades
union {
  #while( counter < num_blades)
    #declare num1=((rand(R2)-.5)*30) ;//-11 ;
    #declare num2=0;
    #declare num3=(rand(R2)-.5)*30;//- 11  ;
    #declare rot = rand(R2)*360;

    object {Blade
      translate <num1,0,num3>
      rotate y*rot
    }
    #declare counter = counter + 1;
  #end
}

-- 
Mike Williams
Gentleman of Leisure


Post a reply to this message

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