POV-Ray : Newsgroups : povray.newusers : trace function question Server Time
29 Jul 2024 04:27:00 EDT (-0400)
  trace function question (Message 1 to 7 of 7)  
From: serfurj
Subject: trace function question
Date: 15 Jan 2007 20:15:00
Message: <web.45ac26897946a67f75f97caf0@news.povray.org>
Hi, I'm trying to make the following script place the cylinders evenly on
the sphere, but they're showing up clustered.  Any idea why?  How can I
place them evenly on the sphere?

The original script was from:
http://www.povray.org/documentation/view/3.6.1/229/#s02_02_01_04_05_i5

Thanks.



#include "colors.inc"

camera {
      angle 20
      location<-5,10,10>
      look_at<0,0,0>
}

light_source { <0, 15, 10> color White }

background { White }

#declare MySphere = sphere { <0, 0, 0>, 1 }

object {
  MySphere
  texture {
    pigment { rgb 1}
  }
}

#declare R1 = seed(339);
#declare Cnt = 0;

#while (Cnt<500)
  #declare Norm = <0, 0, 0>;
  #declare Start = <rand(R1), rand(R1), rand(R1)>;
  #declare Inter = trace ( MySphere, Start, <0, 0, 0>-Start, Norm );

  #if (vlength(Norm)!=0)
    cylinder {
      Inter, Inter+Norm, .1
      texture {
        pigment {color red 1}
      }
    }
  #end

  #declare Cnt = Cnt+1;
#end


Post a reply to this message

From: Jim Charter
Subject: Re: trace function question
Date: 16 Jan 2007 01:28:22
Message: <45ac7086$1@news.povray.org>
serfurj wrote:
> Hi, I'm trying to make the following script place the cylinders evenly on
> the sphere, but they're showing up clustered.  Any idea why?  How can I
> place them evenly on the sphere?
> 




Your Start positions are clustered, specifically in the positive 
quadrant, that is why.  Also note that they are posibly < the radius of 
your sphere

You might get to adequately even results most quickly by using the 
"vrotate" function

ie #local Start = vrotate ( <0,0,-2>, <rand(S)*360,rand(S)*360,0> )
*remember that the triplet specifying the rotation is in degrees


Otherwise the most methodical approach would be to begin with an even 
distribution of Start points around the sphere then "jitter" each 
location slightly with a random factor between -1 and +1

look in the rand.inc file, there are macros there that will return a 
distribution around zero and within -1 to +1.  There are also some 
different types of random distributions


Post a reply to this message

From: serfurj
Subject: Re: trace function question
Date: 17 Jan 2007 01:30:01
Message: <web.45adc1ac1c5723b375f97caf0@news.povray.org>
Jim Charter <jrc### [at] msncom> wrote:
> serfurj wrote:
> > Hi, I'm trying to make the following script place the cylinders evenly on
> > the sphere, but they're showing up clustered.  Any idea why?  How can I
> > place them evenly on the sphere?
> >
>
>
> Your Start positions are clustered, specifically in the positive
> quadrant, that is why.  Also note that they are posibly < the radius of
> your sphere
>
> You might get to adequately even results most quickly by using the
> "vrotate" function
>
> ie #local Start = vrotate ( <0,0,-2>, <rand(S)*360,rand(S)*360,0> )
> *remember that the triplet specifying the rotation is in degrees
>

Not sure I understand it yet, but that was what I was looking for.  Thanks
for the help!


Post a reply to this message

From: Chris B
Subject: Re: trace function question
Date: 17 Jan 2007 05:41:58
Message: <45adfd76$1@news.povray.org>
"serfurj" <nomail@nomail> wrote in message 
news:web.45adc1ac1c5723b375f97caf0@news.povray.org...
> Jim Charter <jrc### [at] msncom> wrote:
>> serfurj wrote:
>> > Hi, I'm trying to make the following script place the cylinders evenly 
>> > on
>> > the sphere, but they're showing up clustered.  Any idea why?  How can I
>> > place them evenly on the sphere?
>> > ...
>> > #declare Start = <rand(R1), rand(R1), rand(R1)>;
>>

This creates a random distribution of points in a space within a cube from 
<0,0,0> to <1,1,1>. Some of those points will be inside a sphere of radius 1 
centred at the origin and some will be outside. With the trace function you 
used, the starting points outside the sphere would hit the surface of the 
sphere in the +X, +Y, +Z quadrant and those inside the sphere would strike 
the opposite side of the sphere (-X,-Y,-Z).

If you scale your start points up and displace them (rand(R1)*2-1), you get 
a random distribution within a cube <-1,-1,-1> to <1,1,1>, which would be 
closer to what you want, but would still not map to the surface of the 
sphere in a uniform way, because you get more points on the sphere where you 
have the edges (and particularly the corners) of the box and less towards 
the centre of the boxes surface.

>>
>> You might get to adequately even results most quickly by using the
>> "vrotate" function
>>
>> ie #local Start = vrotate ( <0,0,-2>, <rand(S)*360,rand(S)*360,0> )
>> *remember that the triplet specifying the rotation is in degrees
>>

This will give you a distribution a bit like the lines of longitude and 
latitude on a globe with a greater density at the poles than around the 
equator. This is because the x rotation is uniform, so you get as many 
points at a latitude of 80 degrees (clustered around a small circle) as you 
would at the equator (a larger circle).

>
> Not sure I understand it yet, but that was what I was looking for.  Thanks
> for the help!
>

If you want a uniform distribution you probably want   #declare Start = 
vrotate ( <0,0,-2>, <asind(rand(R1)*2-1),rand(R1)*360,0> );
This compensates by adjusting the distribution of the random numbers used 
for the x rotation, providing more points in the middle (xRotation=0) and 
less as you reach the extremes (xRotation=90 and xRotation= -90). It does 
this by taking the arcsin of a random number between -1 and 1.

Regards,
Chris B.


Post a reply to this message

From: Chris B
Subject: Re: trace function question
Date: 17 Jan 2007 05:50:52
Message: <45adff8c@news.povray.org>
ps. You'll need to #include "math.inc"


Post a reply to this message

From: serfurj
Subject: Re: trace function question
Date: 18 Jan 2007 16:25:00
Message: <web.45afe5111c5723b3e73422500@news.povray.org>
"Chris B" <c_b### [at] btconnectcomnospam> wrote:
>
> If you want a uniform distribution you probably want   #declare Start =
> vrotate ( <0,0,-2>, <asind(rand(R1)*2-1),rand(R1)*360,0> );
> This compensates by adjusting the distribution of the random numbers used
> for the x rotation, providing more points in the middle (xRotation=0) and
> less as you reach the extremes (xRotation=90 and xRotation= -90). It does
> this by taking the arcsin of a random number between -1 and 1.
>

Thank you for the thorough explanation!  You were correct, I meant uniformly
distributed, not evenly spaced.

I understand your approach, but it seems to only apply to placing objects on
a sphere.  Is there a way to place the cylinders uniformly on the
sphere_sweep or cylinder shapes below?  Is it possible to create a random
distribution of points in a space within a sphere_sweep or cylinder?


#include "colors.inc"
#include "debug.inc"
#include "math.inc"

camera {
      angle 20
      location<0,0,-50>
      look_at<0,0,0>
}

light_source { <0, 15, 10> color White }

background { White }

#declare MySphere =   sphere_sweep {
      linear_spline
      4,
      <-5, -5, 0>, 1
      <-5,  5, 0>, 1
      < 5, -5, 0>, 1
      < 5,  5, 0>, 1 }
//#declare MySphere = cylinder { <-1, -3, -1>, <1, 3, 1> 0.5 }

object {
  MySphere
  texture {
    pigment { rgb 1}
  }
}

#declare R1 = seed(33);
#declare Cnt = 0;

#while (Cnt<500)
  #declare Norm = <0, 0, 0>;
  #declare Start = vrotate (<0,0,-2>, <asind(rand(R1)*2-1),rand(R1)*360,0>);
  #declare Inter = trace ( MySphere, Start, <0, 0, 0>-Start, Norm );

  #debug concat(vstr(3,Start,",",3,3)"n")

  #if (vlength(Norm)!=0)
    cylinder {
      Inter, Inter+Norm, .1
      texture {
        pigment {color red 1}
      }
    }
  #end

  #declare Cnt = Cnt+1;
#end


Post a reply to this message

From: Chris B
Subject: Re: trace function question
Date: 18 Jan 2007 17:11:13
Message: <45aff081$1@news.povray.org>
"serfurj" <nomail@nomail> wrote in message 
news:web.45afe5111c5723b3e73422500@news.povray.org...
> "Chris B" <c_b### [at] btconnectcomnospam> wrote:
>>
>> If you want a uniform distribution you probably want   #declare Start =
>> vrotate ( <0,0,-2>, <asind(rand(R1)*2-1),rand(R1)*360,0> );
>
> I understand your approach, but it seems to only apply to placing objects 
> on
> a sphere.  Is there a way to place the cylinders uniformly on the
> sphere_sweep or cylinder shapes below?  Is it possible to create a random
> distribution of points in a space within a sphere_sweep or cylinder?
>

You're right it does only apply to a sphere.
Cylinders are easy, you just rotate by rand(R1)*360 around the axis of the 
cylinder and translate by rand(R1)*cylinderLength along the length of the 
centre line of the cylinder.

Getting uniformly random distribution on the surface of a sphere sweep is 
beyond me I'm afraid. You can define a spline that corresponds to the path 
followed by the sphere sweep and take a point at a random distance along it, 
then you could send out traces at right angles to the line of the spline. 
This would give you a uniform random distribution on the straight sections, 
but you'd still get more on the inside of the bends and less on the outside 
of the bends.

Regards,
Chris B.


Post a reply to this message

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