summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/random/example/intersections.cpp
blob: fea40d6a4a9c05d061db296922c880ac25c549e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// intersections.cpp
//
// Copyright (c) 2018
// Justinas V. Daugmaudis
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

//[intersections
/*`
    For the source of this example see
    [@boost://libs/random/example/intersections.cpp intersections.cpp].

    This example demonstrates generating quasi-randomly distributed chord
    entry and exit points on an S[sup 2] sphere.

    First we include the headers we need for __niederreiter_base2
    and __uniform_01 distribution.
 */

#include <boost/random/niederreiter_base2.hpp>
#include <boost/random/uniform_01.hpp>

#include <boost/math/constants/constants.hpp>

#include <boost/tuple/tuple.hpp>

/*`
  We use 4-dimensional __niederreiter_base2 as a source of randomness.
 */
boost::random::niederreiter_base2 gen(4);


int main()
{
  typedef boost::tuple<double, double, double> point_t;

  const std::size_t n_points = 100; // we will generate 100 points

  std::vector<point_t> points;
  points.reserve(n_points);

  /*<< __niederreiter_base2 produces integers in the range [0, 2[sup 64]-1].
  However, we want numbers in the range [0, 1). The distribution
  __uniform_01 performs this transformation.
  >>*/
  boost::random::uniform_01<double> dist;

  for (std::size_t i = 0; i != n_points; ++i)
  {
    /*`
      Using formula from J. Rovira et al., "Point sampling with uniformly distributed lines", 2005
      to compute uniformly distributed chord entry and exit points on the surface of a sphere.
    */
    double cos_theta = 1 - 2 * dist(gen);
    double sin_theta = std::sqrt(1 - cos_theta * cos_theta);
    double phi = boost::math::constants::two_pi<double>() * dist(gen);
    double sin_phi = std::sin(phi), cos_phi = std::cos(phi);

    point_t point_on_sphere(sin_theta*sin_phi, cos_theta, sin_theta*cos_phi);

    /*`
      Here we assume that our sphere is a unit sphere at origin. If your sphere was
      different then now would be the time to scale and translate the `point_on_sphere`.
    */

    points.push_back(point_on_sphere);
  }

  /*`
    Vector `points` now holds generated 3D points on a sphere.
  */

  return 0;
}

//]