POV-Ray : Newsgroups : povray.general : Slightly faster VRand_On/In_Sphere() Server Time
4 Nov 2024 19:15:26 EST (-0500)
  Slightly faster VRand_On/In_Sphere() (Message 1 to 10 of 30)  
Goto Latest 10 Messages Next 10 Messages >>>
From:
Subject: Slightly faster VRand_On/In_Sphere()
Date: 6 Nov 2003 01:56:34
Message: <3fa9f0a2$1@news.povray.org>
The following macro gives an uniform distribution within the sphere{0,1}
and is slightly faster (by 9%) than VRand_In_Sphere() from rand.inc:

#macro VRand_In_Sphere(Stream)
  #local Y = 2*rand(Stream)-1;
  vrotate ( (<sqrt(1-Y*Y),Y,0>*pow(rand(Stream),1/3)), 360*rand(Stream)*y )
#end


And this macro gives an uniform distribution on the surface of the
sphere{0,1}. It's slightly faster than VRand_On_Sphere() from rand.inc:

#macro VRand_On_Sphere(Stream)
  #local Y = 2*rand(Stream)-1;
  vrotate ( <sqrt(1-Y*Y), Y, 0>, 360*rand(Stream)*y )
#end


I suggest to replace the original rand.inc macros by these macros.

   Sputnik


Post a reply to this message

From: Slime
Subject: Re: Slightly faster VRand_On/In_Sphere()
Date: 6 Nov 2003 09:10:45
Message: <3faa5665$1@news.povray.org>
> <sqrt(1-Y*Y),Y,0>

I think that will cause points to be more dense towards the top and bottom
of the sphere, won't it?

It might be possible to correct it by running Y through some function like
acos() or something, but that would have to be researched.

 - Slime
 [ http://www.slimeland.com/ ]


Post a reply to this message

From: Warp
Subject: Re: Slightly faster VRand_On/In_Sphere()
Date: 6 Nov 2003 13:55:42
Message: <3faa992e@news.povray.org>
Can you mathematically prove that the distribution is even?

-- 
#macro M(A,N,D,L)plane{-z,-9pigment{mandel L*9translate N color_map{[0rgb x]
[1rgb 9]}scale<D,D*3D>*1e3}rotate y*A*8}#end M(-3<1.206434.28623>70,7)M(
-1<.7438.1795>1,20)M(1<.77595.13699>30,20)M(3<.75923.07145>80,99)// - Warp -


Post a reply to this message

From: Christopher James Huff
Subject: Re: Slightly faster VRand_On/In_Sphere()
Date: 6 Nov 2003 14:52:26
Message: <cjameshuff-4BAFBE.14520006112003@netplex.aussie.org>
In article <3fa9f0a2$1@news.povray.org>,


> I suggest to replace the original rand.inc macros by these macros.

Hmm, I haven't had time to go over the mathematics, but these look like 
they do give even results...

-- 
Christopher James Huff <cja### [at] earthlinknet>
http://home.earthlink.net/~cjameshuff/
POV-Ray TAG: chr### [at] tagpovrayorg
http://tag.povray.org/


Post a reply to this message

From: JC (Exether)
Subject: Re: Slightly faster VRand_On/In_Sphere()
Date: 7 Nov 2003 09:56:27
Message: <3fabb29b@news.povray.org>
Well, after understanding the message and going into its math a bit 
more, I came to following conclusions.

I think we can agree that if Frank's VRand_On_Sphere is uniform on the 
sphere then VRand_In_Sphere is uniform within the sphere.

So I considered VRand_On_Sphere which is a bit easier.
First remark I made is that the repartition is not obviously uniform.  :-)

What seem to happen here is that we have a combination of two 
non-uniform transformations that may lead to a uniform repartition.

<sqrt(1-Y*Y), Y, 0> is non uniform in terms of length of the circle, 
there will be more points in the middle of the circle and less at the 
top and bottom, but then the rotation is not uniform either as it will 
spread the points of the top and bottom on smaller surfaces.

The remaining question is : does the one exactly counter the other ?

I haven't answered this one yet, and I would really like to avoid doing 
it with my rusty math, isn't any math student around willing to do it ?? 
There's just a few arcos integrals and stuff like that implied I think.  :-)

JC


> The following macro gives an uniform distribution within the sphere{0,1}
> and is slightly faster (by 9%) than VRand_In_Sphere() from rand.inc:
> 
> #macro VRand_In_Sphere(Stream)
>   #local Y = 2*rand(Stream)-1;
>   vrotate ( (<sqrt(1-Y*Y),Y,0>*pow(rand(Stream),1/3)), 360*rand(Stream)*y )
> #end
> 
> 
> And this macro gives an uniform distribution on the surface of the
> sphere{0,1}. It's slightly faster than VRand_On_Sphere() from rand.inc:
> 
> #macro VRand_On_Sphere(Stream)
>   #local Y = 2*rand(Stream)-1;
>   vrotate ( <sqrt(1-Y*Y), Y, 0>, 360*rand(Stream)*y )
> #end
> 
> 
> I suggest to replace the original rand.inc macros by these macros.
> 
>    Sputnik
> 
>


Post a reply to this message

From: Slime
Subject: Re: Slightly faster VRand_On/In_Sphere()
Date: 7 Nov 2003 14:48:46
Message: <3fabf71e@news.povray.org>
> <sqrt(1-Y*Y), Y, 0> is non uniform in terms of length of the circle,
> there will be more points in the middle of the circle and less at the
> top and bottom, but then the rotation is not uniform either as it will
> spread the points of the top and bottom on smaller surfaces.
>
> The remaining question is : does the one exactly counter the other ?

Good point. It may very well do so. The <sqrt(1-Y*Y), Y, 0> distribution is
related to the cosine of the angle from the center of the circle to the
point <sqrt(1-Y*Y), Y, 0>. Also, the circumference of the circle which that
point is then randomly placed on is proportional to the cosine of the same
angle. Therefore, it's possible that this VRand_On_Sphere() function does in
fact work properly.

It needs to be worked out formally though.

 - Slime
 [ http://www.slimeland.com/ ]


Post a reply to this message

From: Wolfgang Wieser
Subject: Re: Slightly faster VRand_On/In_Sphere()
Date: 7 Nov 2003 15:35:36
Message: <3fac0217@news.povray.org>
JC (Exether) wrote:

> I haven't answered this one yet, and I would really like to avoid doing
> it with my rusty math, isn't any math student around willing to do it ??
> There's just a few arcos integrals and stuff like that implied I think. 
> :-)
> 
Well... I just posted an article until it popped up in my mind that 
it was complete nonsense, so I canceled it again (hope that helps...)
Furthermore (as non-native speaker) I mixed up longitude and latitude 
again -- @#$%!!!

I had to take pencil and paper and have a closer look: 

>> And this macro gives an uniform distribution on the surface of the
>> sphere{0,1}. It's slightly faster than VRand_On_Sphere() from rand.inc:
>> 
>> #macro VRand_On_Sphere(Stream)
>>   #local Y = 2*rand(Stream)-1;
>>   vrotate ( <sqrt(1-Y*Y), Y, 0>, 360*rand(Stream)*y )
>> #end
>
Okay, so the area size on a sphere between latitudes theta1 and theta2 
is (where theta is not really the latitude but measured downwards from 
the pole): 
  integral theta1..theta2 over 2*pi*sin(theta) d(theta)
which is
  area = 2*pi*(cos(theta1)-cos(theta2))

The probability for the vector to be inside that area is: 
  P(v in area) = P(cos(theta2)<= y < cos(theta1))
where y=2*rand(Stream)-1, i.e. uniformly distributed, so 
  P = ( cos(theta1)-cos(theta2) )/2

Now, if the probability is proportional to the area, the distribution 
is uniform. Looking at the formulas, this is evident: 
  P/area = 1/(4*pi)
...which is just what we expect and independent of theta1 and theta2. 

So I think that it really works. 

Cheers,
Wolfgang


Post a reply to this message

From: Warp
Subject: Re: Slightly faster VRand_On/In_Sphere()
Date: 7 Nov 2003 18:58:00
Message: <3fac3188@news.povray.org>
"JC (Exether)" <no### [at] spamfr> wrote:
> I think we can agree that if Frank's VRand_On_Sphere is uniform on the 
> sphere then VRand_In_Sphere is uniform within the sphere.

  Well, I think this is not automatic, but needs proof. :)

  (Btw, has anyone proven mathematically that the current official
macros produce even distributions?)

-- 
plane{-x+y,-1pigment{bozo color_map{[0rgb x][1rgb x+y]}turbulence 1}}
sphere{0,2pigment{rgbt 1}interior{media{emission 1density{spherical
density_map{[0rgb 0][.5rgb<1,.5>][1rgb 1]}turbulence.9}}}scale
<1,1,3>hollow}text{ttf"timrom""Warp".1,0translate<-1,-.1,2>}//  - Warp -


Post a reply to this message

From: Slime
Subject: Re: Slightly faster VRand_On/In_Sphere()
Date: 7 Nov 2003 21:36:47
Message: <3fac56bf$1@news.povray.org>
>   (Btw, has anyone proven mathematically that the current official
> macros produce even distributions?)

I am convinced that they do.

Honestly, I'm not sure of the proper mathematical procedures to use when
evaluating a distribution curve. Nonetheless, I understand the logic behind
what causes an uneven distribution and how to go about correcting it; I have
a very good intuition about this sort of thing (IMO at least ;) ) and during
the POV-Ray 3.5 beta I took a good look at the two functions out of
curiosity. I determined, simply by reading the code, that the
VRand_In_Sphere function provided an uneven distribution, because it wasn't
using the pow(..., 1/3) trick to pull things away from the center. I
verified my suspicions, fixed the problem and submitted what is now the
current VRand_In_Sphere function.

Anyway, I've thought long and hard about both functions, and I am absolutely
certain that both of them provide even distributions.

I have been known to be wrong... =)

 - Slime
 [ http://www.slimeland.com/ ]


Post a reply to this message

From: Wolfgang Wieser
Subject: Re: Slightly faster VRand_On/In_Sphere()
Date: 8 Nov 2003 04:14:40
Message: <3facb400@news.povray.org>
Warp wrote:
> "JC (Exether)" <no### [at] spamfr> wrote:
>> I think we can agree that if Frank's VRand_On_Sphere is uniform on the
>> sphere then VRand_In_Sphere is uniform within the sphere.
> 
>   Well, I think this is not automatic, but needs proof. :)
> 
I think my posting from yesterday proofs it. 
The vector is rotated by a random amount around the Y axis, so it 
is sufficient to prove that the probability for the vector being 
in a certain latitude range is equal to the area covered by it 
(divided by 4pi, of course). Now read on in my previous posting. 

Wolfgang


Post a reply to this message

Goto Latest 10 Messages Next 10 Messages >>>

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