ENH: Added ramdom point func using cachedRandom

This commit is contained in:
andy
2011-03-14 12:07:59 +00:00
parent dce6ce2658
commit e759866be0
2 changed files with 41 additions and 0 deletions

View File

@ -42,6 +42,7 @@ SourceFiles
#include "point.H"
#include "primitiveFieldsFwd.H"
#include "pointHit.H"
#include "cachedRandom.H"
#include "Random.H"
#include "FixedList.H"
#include "UList.H"
@ -164,6 +165,10 @@ public:
// uniform distribution
inline Point randomPoint(Random& rndGen) const;
//- Return a random point in the tetrahedron from a
// uniform distribution
inline Point randomPoint(cachedRandom& rndGen) const;
//- Calculate the barycentric coordinates of the given
// point, in the same order as a, b, c, d. Returns the
// determinant of the solution.

View File

@ -240,6 +240,42 @@ inline Point Foam::tetrahedron<Point, PointRef>::randomPoint
}
template<class Point, class PointRef>
inline Point Foam::tetrahedron<Point, PointRef>::randomPoint
(
cachedRandom& rndGen
) const
{
// Adapted from
// http://vcg.isti.cnr.it/activities/geometryegraphics/pointintetraedro.html
scalar s = rndGen.sample01<scalar>();
scalar t = rndGen.sample01<scalar>();
scalar u = rndGen.sample01<scalar>();
if (s + t > 1.0)
{
s = 1.0 - s;
t = 1.0 - t;
}
if (t + u > 1.0)
{
scalar tmp = u;
u = 1.0 - s - t;
t = 1.0 - tmp;
}
else if (s + t + u > 1.0)
{
scalar tmp = u;
u = s + t + u - 1.0;
s = 1.0 - t - tmp;
}
return (1 - s - t - u)*a_ + s*b_ + t*c_ + u*d_;
}
template<class Point, class PointRef>
Foam::scalar Foam::tetrahedron<Point, PointRef>::barycentric
(