Loading [MathJax]/jax/input/TeX/config.js
Radium Engine  1.5.28
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
RandomPointSet.hpp
1#pragma once
2#include <Core/RaCore.hpp>
3#include <Core/Types.hpp>
4#include <Eigen/Core>
5#include <cmath>
6#include <random>
7
8namespace Ra {
9namespace Core {
10
17namespace Random {
18
22namespace internal {
23Scalar constexpr sqrtNewtonRaphsonHelper( Scalar x, Scalar curr, Scalar prev ) {
24 return curr == prev ? curr : sqrtNewtonRaphsonHelper( x, 0.5_ra * ( curr + x / curr ), curr );
25}
26Scalar constexpr sqrtConstExpr( Scalar x ) {
27 return sqrtNewtonRaphsonHelper( x, x, 0_ra );
28}
29} // namespace internal
30
35class RA_CORE_API FibonacciSequence
36{
37
38 static constexpr Scalar phi = ( 1_ra + internal::sqrtConstExpr( 5_ra ) ) / 2_ra;
39 size_t n;
40
41 public:
42 explicit FibonacciSequence( size_t number );
43 // copyable
44 FibonacciSequence( const FibonacciSequence& ) = default;
45 FibonacciSequence& operator=( const FibonacciSequence& ) = default;
46 // movable
48 FibonacciSequence& operator=( FibonacciSequence&& ) = default;
49 virtual ~FibonacciSequence() = default;
50
51 size_t range();
52 Scalar operator()( size_t i );
53};
54
58struct RA_CORE_API VanDerCorputSequence {
59 Scalar operator()( unsigned int bits );
60};
61
66class RA_CORE_API FibonacciPointSet
67{
69
70 public:
71 explicit FibonacciPointSet( size_t n );
72 // copyable
73 FibonacciPointSet( const FibonacciPointSet& ) = default;
74 FibonacciPointSet& operator=( const FibonacciPointSet& ) = default;
75 // movable
77 FibonacciPointSet& operator=( FibonacciPointSet&& ) = default;
78 virtual ~FibonacciPointSet() = default;
79
80 size_t range();
81 Ra::Core::Vector2 operator()( size_t i );
82};
83
87class RA_CORE_API HammersleyPointSet
88{
90 size_t n;
91
92 public:
93 explicit HammersleyPointSet( size_t number );
94 // copyable
95 HammersleyPointSet( const HammersleyPointSet& ) = default;
96 HammersleyPointSet& operator=( const HammersleyPointSet& ) = default;
97 // movable
99 HammersleyPointSet& operator=( HammersleyPointSet&& ) = default;
100 virtual ~HammersleyPointSet() = default;
101
102 size_t range();
103 Ra::Core::Vector2 operator()( size_t i );
104};
105
108class RA_CORE_API MersenneTwisterPointSet
109{
110 std::mt19937 gen;
112 size_t n;
113
114 public:
115 explicit MersenneTwisterPointSet( size_t number );
116 // copyable
118 MersenneTwisterPointSet& operator=( const MersenneTwisterPointSet& ) = default;
119 // movable
121 MersenneTwisterPointSet& operator=( MersenneTwisterPointSet&& ) = default;
122 virtual ~MersenneTwisterPointSet() = default;
123
124 size_t range();
125 Ra::Core::Vector2 operator()( size_t );
126};
127
128// https://observablehq.com/@mbostock/spherical-fibonacci-lattice
129// http://holger.dammertz.org/stuff/notes_HammersleyOnHemisphere.html
133template <class PointSet>
135{
136 PointSet p;
137 Ra::Core::Vector3 projectOnSphere( const Ra::Core::Vector2&& pt );
138
139 public:
140 explicit SphericalPointSet( size_t n );
141 // copyable
142 SphericalPointSet( const SphericalPointSet& ) = default;
143 SphericalPointSet& operator=( const SphericalPointSet& ) = default;
144 // movable
146 SphericalPointSet& operator=( SphericalPointSet&& ) = default;
147 virtual ~SphericalPointSet() = default;
148
149 size_t range();
150 Ra::Core::Vector3 operator()( size_t i );
151};
152
153// ------------------------------------------------------------------------
154// ------------------------ inline methods --------------------------------
155
156template <class PointSet>
157Ra::Core::Vector3 SphericalPointSet<PointSet>::projectOnSphere( const Ra::Core::Vector2&& pt ) {
158 Scalar theta = std::acos( 2 * pt[1] - 1 ); // 0 <= tetha <= pi
159 Scalar phi = 2_ra * Scalar( M_PI ) * pt[0];
160 return { std::sin( theta ) * std::cos( phi ),
161 std::sin( theta ) * std::sin( phi ),
162 std::cos( theta ) };
163}
164
165template <class PointSet>
166SphericalPointSet<PointSet>::SphericalPointSet( size_t n ) : p( n ) {}
167
168template <class PointSet>
169size_t SphericalPointSet<PointSet>::range() {
170 return p.range();
171}
172
173template <class PointSet>
174Ra::Core::Vector3 SphericalPointSet<PointSet>::operator()( size_t i ) {
175 return projectOnSphere( p( i ) );
176}
177
178} // namespace Random
179} // namespace Core
180} // namespace Ra
T acos(T... args)
Implements the 2D fibonacci Point set points follow the FibonacciSequence (i, N) => [i / phi,...
Implements the fibonacci sequence i --> i/phi where phi = (1 + sqrt(5)) / 2.
Map a [0, 1)^2 point set on the unit sphere.
T cos(T... args)
hepler function to manage enum as underlying types in VariableSet
Definition Cage.cpp:4
T sin(T... args)
1D Van der Corput sequence only implemented for 32bits floats (converted out to Scalar)