uniform random point inside a triangle
#1
Posted 25 July 2007 - 12:51 AM
Can someone please help? I want to be able to pick a random point within a triangle facet in 3-space. Selection is weighted for equal probability per unit area. The only thing that is given is the vertices and normal. I am stuck at this stage of my code and have no idea how to solve this problem. Any help is much appreciated.
Thanks
#2
Posted 25 July 2007 - 06:18 AM
#3
Posted 25 July 2007 - 07:12 AM
float tmp = sqrt(u); x = 1 - tmp; y = v * tmp;
x and y are now barycentric coordinates in your triangle.
If I find the time I will post an explanation later today. Have to go to work now
#4
Posted 25 July 2007 - 09:42 AM
if (x+y > 1) { x = 1-x; y = 1-y; }
#5
Posted 25 July 2007 - 04:37 PM
anubis, I haven't seen that mapping before. Nice one :yes:
#6
Posted 25 July 2007 - 06:10 PM
Quote
You arrive at that mapping if you compute one of the marginal densities p(u) or p(v) and the corresponding conditional density, knowing that p(u,v) = 1 / area
#7
Posted 26 July 2007 - 06:49 AM
Reedbeta said:
My only beef with that mapping is that the points where x+y = 1 has half the chances to appear in the triangle as any other point, therefore the distribution is not perfectly uniform.
#8
Posted 26 July 2007 - 07:32 AM
#9
Posted 26 July 2007 - 04:41 PM
Jare said:
I'm not 100% sure of the details myself, but true random numbers tend to have a lot of "clumping", which tends to increase the variance in a Monte Carlo process. That's why it's common to use things like stratified sampling or low-discrepancy sequences which attempt to produce a "better than random" distribution, i.e. with no visible regularity, but not so much clumping either. The trouble is that discontinuous mappings spoil these attempts by reintroducing clumping, since two random samples with values far apart can map to values close together.
I think Pharr and Humphreys talk about this some more in the PBR book, if you're interested in finding out more.
Quote
The points where x + y = 1 are a line, which technically should have probability 0 anyway...
#10
Posted 26 July 2007 - 11:16 PM
Quote
That is actually were I first learned about the mapping I gave :D
#11
Posted 29 July 2007 - 07:23 PM
#12
Posted 30 July 2007 - 02:12 AM
tobeythorn said:
That doesn't give a mapping with constant probability density with respect to area though...
#13
Posted 01 August 2007 - 07:06 AM
ranged bet [0-1] if both are one, 3rd vertex will be your point, and so on
then, get the point using parametric equation of the triangle.
cheers
#14
Posted 01 August 2007 - 02:25 PM
// Note: Using rand() will cause points to bunch up in one of the corners.
// Can't remember which one though... You really should use a different random number generator.
inline float randf()
{
return ( float(rand()) / float( RAND_MAX ) );
}
...
float b0 = randf();
float b1 = ( 1.0f - b0 ) * randf();
float b2 = 1 - b0 - b1;
position = vertex1 * b0
+ vertex2 * b1
+ vertex3 * b2;
#15
Posted 07 August 2007 - 04:46 AM
#16
Posted 07 August 2007 - 11:12 AM
#17
Posted 08 October 2007 - 10:18 AM
I think that guy is wrong and the C.L.C site was right,
Heres the CLC fix of the problem rewritten by me with a multiply,
and you should see it works->
(float)rand()/(float)rand_max*(float)range
it simply squishes the number scale, and there is the exact same
amount of random possible samples, but the C.L.C said that it only
works when rand_max is bigger than range (it only has to be 1 integer
lower and it still works) and that is correct but they wrote it like this->
x = rand() / (RAND_MAX / RANGE + 1)
because it works with integer division and it doesnt need a float convert
(its written that way as an optimization) and its the same just with the
inverted divide instead of the multiply.
Am i wrong?
But I didnt even consider that rand()%range didnt work, i just didnt think. :)
#18
Posted 08 October 2007 - 10:28 AM
it converts back to integer (or when the integer divide happens)
and then it repeats some samples more than others
like an aliased line.
I guess he is right then, rerandomizing is the only way to
get exact results.
#19
Posted 08 October 2007 - 11:09 AM
-
Currently working on: the 3D engine for Tomb Raider.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











