• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // intersections.cpp
2 //
3 // Copyright (c) 2018
4 // Justinas V. Daugmaudis
5 //
6 // Distributed under the Boost Software License, Version 1.0. (See
7 // accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
9 
10 //[intersections
11 /*`
12     For the source of this example see
13     [@boost://libs/random/example/intersections.cpp intersections.cpp].
14 
15     This example demonstrates generating quasi-randomly distributed chord
16     entry and exit points on an S[sup 2] sphere.
17 
18     First we include the headers we need for __niederreiter_base2
19     and __uniform_01 distribution.
20  */
21 
22 #include <boost/random/niederreiter_base2.hpp>
23 #include <boost/random/uniform_01.hpp>
24 
25 #include <boost/math/constants/constants.hpp>
26 
27 #include <boost/tuple/tuple.hpp>
28 
29 /*`
30   We use 4-dimensional __niederreiter_base2 as a source of randomness.
31  */
32 boost::random::niederreiter_base2 gen(4);
33 
34 
main()35 int main()
36 {
37   typedef boost::tuple<double, double, double> point_t;
38 
39   const std::size_t n_points = 100; // we will generate 100 points
40 
41   std::vector<point_t> points;
42   points.reserve(n_points);
43 
44   /*<< __niederreiter_base2 produces integers in the range [0, 2[sup 64]-1].
45   However, we want numbers in the range [0, 1). The distribution
46   __uniform_01 performs this transformation.
47   >>*/
48   boost::random::uniform_01<double> dist;
49 
50   for (std::size_t i = 0; i != n_points; ++i)
51   {
52     /*`
53       Using formula from J. Rovira et al., "Point sampling with uniformly distributed lines", 2005
54       to compute uniformly distributed chord entry and exit points on the surface of a sphere.
55     */
56     double cos_theta = 1 - 2 * dist(gen);
57     double sin_theta = std::sqrt(1 - cos_theta * cos_theta);
58     double phi = boost::math::constants::two_pi<double>() * dist(gen);
59     double sin_phi = std::sin(phi), cos_phi = std::cos(phi);
60 
61     point_t point_on_sphere(sin_theta*sin_phi, cos_theta, sin_theta*cos_phi);
62 
63     /*`
64       Here we assume that our sphere is a unit sphere at origin. If your sphere was
65       different then now would be the time to scale and translate the `point_on_sphere`.
66     */
67 
68     points.push_back(point_on_sphere);
69   }
70 
71   /*`
72     Vector `points` now holds generated 3D points on a sphere.
73   */
74 
75   return 0;
76 }
77 
78 //]
79