POV-Ray : Newsgroups : povray.advanced-users : Random points on surface of an object Server Time
24 Nov 2024 12:23:24 EST (-0500)
  Random points on surface of an object (Message 1 to 9 of 9)  
From: Tim Attwood
Subject: Random points on surface of an object
Date: 22 Jan 2007 16:38:53
Message: <45b52eed$1@news.povray.org>
This morning I woke up with a solution
to placing a random distribution of points
onto arbitrary objects. I'm not sure if any
of you have ever had one of these dreaming
moments of inspiration, but I figured it was
worth sharing it. I think someone had asked
about something like this, but I've lost the
thread.

Basically, you just check that the distance
from a random point in the bounding to
a trace hit on the object is shorter than
some threshold distance. That way the
random distribution approaches that of
the surface.

Here's a sample scene, with the macro.

#macro RandOnSurface(Obj, rsd, thresh)
   #local mn = min_extent(Obj);
   #local mx = max_extent(Obj);
   #local dist = 1000000;
   #while (dist>thresh)
      #local flag = 0;
      #while (flag = 0)
         #local rfrom = VRand_In_Box(mn, mx, rsd);
         #local rdir = vrotate(<1,0,0>,
            <360*rand(rsd),360*rand(rsd),360*rand(rsd)>);
         #declare Norm = <0, 0, 0>;
         #local hit = trace(Obj, rfrom, rdir, Norm);
         #if (vlength(Norm) != 0)
            #local flag = 1;
         #end
      #end
      #local dist = vlength( hit - rfrom);
   #end
   #local result = hit;
   (result)
#end

#declare Bent = merge {
   cylinder{<0,-1,0>,<-1,0,0>,0.2}
   cylinder{<-1,0,0>,<1,0,0>,0.2}
   cylinder{<1,0,0>,<0,1,0>,0.2}
   sphere {<-1,0,0>,0.2}
   sphere {<1,0,0>,0.2}
};

object {Bent pigment {Blue transmit 0.7} }

#declare rsd = seed(1);

#local c = 0;
#while (c<1000)
   #local p = RandOnSurface(Bent, rsd, 0.05);
   cone{<0,0,0>,0.025,<0,0.1,0>,0
     Point_At_Trans(Norm)
     translate p
     pigment{Red}
   }
   #local c = c + 1;
#end


Post a reply to this message

From: Chris B
Subject: Re: Random points on surface of an object
Date: 22 Jan 2007 17:34:13
Message: <45b53be5$1@news.povray.org>
"Tim Attwood" <tim### [at] comcastnet> wrote in message 
news:45b52eed$1@news.povray.org...
> ...  I think someone had asked
> about something like this, but I've lost the
> thread. ...

Hi Tim,
I think that would be the thread entitled 'trace function question' in 
povray.newusers.

Regards,
Chris B.


Post a reply to this message

From: Christian Froeschlin
Subject: Re: Random points on surface of an object
Date: 22 Jan 2007 18:59:34
Message: <45b54fe6$1@news.povray.org>
Tim Attwood wrote:

> Basically, you just check that the distance
> from a random point in the bounding to
> a trace hit on the object is shorter than
> some threshold distance. That way the
> random distribution approaches that of
> the surface.

brute force but should be a reasonable approximation if you
are willing to wait a bit for all those missing rolls.

Although I think you should also pick a uniform distribution
of trace directions, otherwise you will have some unnecessary
systematic error. But this can be fixed by

http://mathworld.wolfram.com/SpherePointPicking.html

Also, the bounding box should be extended by "threshold",
else areas at the border only have 50% chance of hitting.


Post a reply to this message

From: Tim Attwood
Subject: Re: Random points on surface of an object
Date: 22 Jan 2007 22:31:34
Message: <45b58196$1@news.povray.org>
> brute force but should be a reasonable approximation if you
> are willing to wait a bit for all those missing rolls.
>
> Although I think you should also pick a uniform distribution
> of trace directions, otherwise you will have some unnecessary
> systematic error. But this can be fixed by
>
> http://mathworld.wolfram.com/SpherePointPicking.html

I didn't rotate 360 y and 180 relative as in polar notation,
which is what causes that common error pointed out on wolfram,
but by 360 on 3 fixed axes. The result should be uniform already?
I guess you could use Ingo's VRand_On_Sphere() from rand.inc

#local rdir = VRand_On_Sphere(rsd);

> Also, the bounding box should be extended by "threshold",
> else areas at the border only have 50% chance of hitting.

Oops, yep.

   #local mn = min_extent(Obj)-thresh;
   #local mx = max_extent(Obj)+thresh;


Post a reply to this message

From: Janet
Subject: Re: Random points on surface of an object
Date: 28 Jan 2007 18:30:01
Message: <web.45bd314015df458d8a6768e40@news.povray.org>
Amazing! Thanks for sharing this.


Post a reply to this message

From: Thomas de Groot
Subject: Re: Random points on surface of an object
Date: 31 Jan 2007 04:03:37
Message: <45c05b69@news.povray.org>
Brilliant!!! Very useful!
Thank you very much indeed!

Thomas


Post a reply to this message

From: Thomas de Groot
Subject: Re: Random points on surface of an object
Date: 19 Feb 2007 07:30:29
Message: <45d99865$1@news.povray.org>
Tim,

I have been playing with your macro and I have the following question, as I 
am at a loss about how to solve it myself (see the image in p.b.i.).

I scaled here the cones to a flat shape. What I would like is to always have 
the eliptic bases oriented horizontally, whatever their location, not like 
in the image, where part of them are oriented vertically. How could that be 
achieved with your macro? I experimented a bit and read the docs, but I am 
at a loss.

Thanks a lot!

Thomas


Post a reply to this message

From: Tim Attwood
Subject: Re: Random points on surface of an object
Date: 20 Feb 2007 03:46:11
Message: <45dab553$1@news.povray.org>
> I have been playing with your macro and I have the following question, as 
> I am at a loss about how to solve it myself (see the image in p.b.i.).
>
> I scaled here the cones to a flat shape. What I would like is to always 
> have the eliptic bases oriented horizontally, whatever their location, not 
> like in the image, where part of them are oriented vertically. How could 
> that be achieved with your macro? I experimented a bit and read the docs, 
> but I am at a loss.

Trace and the RandOnSurface macro only return a point and a normal.

You could ignore the vertical portion of the normal and force some
object to be horizontal in respect to the horizon.
Point_At_Trans(vnormalize(<Norm.x,0,Norm.z>))
but you would need to decide what to do if the normal is vertical
or it might error out.

However, I think what you intend is to follow the shape of the object?
A normal isn't enough to generate info about how the surface
shape slopes nearby to some random point. You could trace nearby
points and rotate based on that perhaps.


Post a reply to this message

From: Thomas de Groot
Subject: Re: Random points on surface of an object
Date: 20 Feb 2007 07:57:57
Message: <45daf055$1@news.povray.org>
"Tim Attwood" <tim### [at] comcastnet> schreef in bericht 
news:45dab553$1@news.povray.org...
>
> Trace and the RandOnSurface macro only return a point and a normal.

Exactly.
>
> You could ignore the vertical portion of the normal and force some
> object to be horizontal in respect to the horizon.
> Point_At_Trans(vnormalize(<Norm.x,0,Norm.z>))
> but you would need to decide what to do if the normal is vertical
> or it might error out.

This works as I want it to do!! I shall think about something for those 
cases where Norm.y is vertical (probably just skip it).
Thank you!! A simple solution indeed.
>
> However, I think what you intend is to follow the shape of the object?
> A normal isn't enough to generate info about how the surface
> shape slopes nearby to some random point. You could trace nearby
> points and rotate based on that perhaps.

Not necessarily for my purpose. I want to experiment with a given shape on 
which "leaves" are growing. Basically, the leaves are all oriented 
horizontally and bending slightly down.

Thomas


Post a reply to this message

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