POV-Ray : Newsgroups : povray.general : Macros and arrays : Re: Macros and arrays Server Time
7 Aug 2024 17:29:44 EDT (-0400)
  Re: Macros and arrays  
From: ingo
Date: 13 Aug 2001 11:52:49
Message: <Xns90FCB5E4BBA97seed7@povray.org>
in news:3b77e774@news.povray.org Gail Shaw wrote:

> I generate a random number between 0 and 1 then read along the
> array checking
> to see if the random number is smaller than the array value. If it
> is I use the
> array index as an index into the object array and stop checking,
> otherwise I increment the
> array index.

If I understand it right what you do, this will not get you an even 
distribution.
 
 
> The problem I have is if one of the objects is created using a
> macro then the macro is evaluated
> when the object is assigned to the ObjectsArray and I'd like to
> find a way to only evaluate the
> macro when the object is actually created in the scene.

My solution, not very elegant.

---%<------%<---
#declare Stream=seed(7);

#macro PickObject(ObjArr, ProbArr, Stream)
   #local Len=dimension_size(ProbArr,1)-0.0000001;
   #local Loop=1;
   #while (Loop)
      #local Pos=int(rand(Stream)*Len); //range 0 to dimension_size-1
      //#debug concat("pos", str(Pos,0,-1),"\n")
      #local Prob=ProbArr[Pos];
      //#debug concat("prob",str(Prob,0,-1),"\n")
      #local Rnd=rand(Stream);
      //#debug concat("rnd",str(Rnd,0,-1),"\n")
      #if( Rnd <= Prob)
         #local Result=ObjArr[Pos];
         #local Loop=0;
      #end     
   #end
   (Result)
#end

#macro Sphere1()sphere{0,0.2 pigment{rgb 1}}#end
#macro Sphere2()sphere{0,0.3 pigment{rgb 1}}#end
#macro Sphere3()sphere{0,0.4 pigment{rgb 1}}#end
#macro Sphere4()sphere{0,0.5 pigment{rgb 1}}#end
#declare Sphere5=sphere{0,0.6 pigment{rgb 1}}

#declare ObjArr=array[5]{1,2,3,4,5}
#declare ProbArray=array[5]{0.1,0.3,0.4,0.6,0.2}

//while (...)
   #declare Result=PickObject(ObjArr, ProbArray, Stream);
   #switch(Result)
      #case(0)
         object{
            Sphere1()
      #break
      #case(1)
         object{
            Sphere2()
         }
      #break
      
      //etc
                    
   #end
//#end
camera {
   location <0,0,-4>
   look_at 0
}
light_source{
   <500,500,-500>
   rgb 1
}
---%<------%<---


Ingo

-- 
Photography: http://members.home.nl/ingoogni/
Pov-Ray    : http://members.home.nl/seed7/


Post a reply to this message

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