Class TriangleSampler
java.lang.Object
org.apache.commons.rng.sampling.shape.TriangleSampler
- All Implemented Interfaces:
ObjectSampler<double[]>
,SharedStateObjectSampler<double[]>
,SharedStateSampler<SharedStateObjectSampler<double[]>>
- Direct Known Subclasses:
TriangleSampler.TriangleSampler2D
,TriangleSampler.TriangleSampler3D
,TriangleSampler.TriangleSamplerND
Generate points
uniformly distributed within a triangle.
-
Uses the algorithm described in:
Turk, G. Generating random points in triangles. Glassner, A. S. (ed) (1990).
Graphic Gems Academic Press, pp. 24-28.
Sampling uses:
- Since:
- 1.4
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static class
Sample uniformly from a triangle in 2D.private static class
Sample uniformly from a triangle in 3D.private static class
Sample uniformly from a triangle in ND. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final UniformRandomProvider
The source of randomness.private static final int
The dimension for 3D sampling.private static final int
The dimension for 2D sampling. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract double[]
createSample
(double p1msmt, double s, double t) Creates the sample given the random variatess
andt
in the interval[0, 1]
ands + t <= 1
.static TriangleSampler
of
(UniformRandomProvider rng, double[] a, double[] b, double[] c) Create a triangle sampler with verticesa
,b
andc
.double[]
sample()
Create an object sample.abstract TriangleSampler
Create a new instance of the sampler with the same underlying state using the given uniform random provider as the source of randomness.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.apache.commons.rng.sampling.ObjectSampler
samples, samples
-
Field Details
-
TWO_D
private static final int TWO_DThe dimension for 2D sampling.- See Also:
-
THREE_D
private static final int THREE_DThe dimension for 3D sampling.- See Also:
-
rng
The source of randomness.
-
-
Constructor Details
-
TriangleSampler
TriangleSampler(UniformRandomProvider rng) - Parameters:
rng
- Source of randomness.
-
-
Method Details
-
sample
public double[] sample()Description copied from interface:ObjectSampler
Create an object sample.- Specified by:
sample
in interfaceObjectSampler<double[]>
- Returns:
- a random Cartesian coordinate within the triangle.
-
createSample
protected abstract double[] createSample(double p1msmt, double s, double t) Creates the sample given the random variatess
andt
in the interval[0, 1]
ands + t <= 1
. The sum1 - s - t
is provided. The sample can be obtained from the triangle abc using:p = a(1 - s - t) + sb + tc
- Parameters:
p1msmt
- plus 1 minus s minus t (1 - s - t)s
- the first variate st
- the second variate t- Returns:
- the sample
-
withUniformRandomProvider
Create a new instance of the sampler with the same underlying state using the given uniform random provider as the source of randomness.- Specified by:
withUniformRandomProvider
in interfaceSharedStateSampler<SharedStateObjectSampler<double[]>>
- Parameters:
rng
- Generator of uniformly distributed random numbers.- Returns:
- the sampler
-
of
Create a triangle sampler with verticesa
,b
andc
. Sampled points are uniformly distributed within the triangle.Sampling is supported in dimensions of 2 or above. Samples will lie in the plane (2D Euclidean space) defined by using the three triangle vertices to create two vectors starting at a point in the plane and orientated in different directions along the plane.
No test for collinear points is performed. If the points are collinear the sampling distribution is undefined.
- Parameters:
rng
- Source of randomness.a
- The first vertex.b
- The second vertex.c
- The third vertex.- Returns:
- the sampler
- Throws:
IllegalArgumentException
- If the vertices do not have the same dimension; the dimension is less than 2; or vertices have non-finite coordinates
-