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

public abstract class TriangleSampler extends Object implements SharedStateObjectSampler<double[]>
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
  • Field Details

  • Constructor Details

  • Method Details

    • sample

      public double[] sample()
      Description copied from interface: ObjectSampler
      Create an object sample.
      Specified by:
      sample in interface ObjectSampler<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 variates s and t in the interval [0, 1] and s + t <= 1. The sum 1 - 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 s
      t - the second variate t
      Returns:
      the sample
    • withUniformRandomProvider

      public abstract TriangleSampler withUniformRandomProvider(UniformRandomProvider rng)
      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 interface SharedStateSampler<SharedStateObjectSampler<double[]>>
      Parameters:
      rng - Generator of uniformly distributed random numbers.
      Returns:
      the sampler
    • of

      public static TriangleSampler of(UniformRandomProvider rng, double[] a, double[] b, double[] c)
      Create a triangle sampler with vertices a, b and c. 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