Skip to content

Commit 087d1a8

Browse files
extension of the sampling template.
1 parent 9718871 commit 087d1a8

6 files changed

Lines changed: 88 additions & 16 deletions

File tree

include/geode/stochastic/geometry/distributions.hpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@
2323

2424
#pragma once
2525

26+
#include <geode/geometry/bounding_box.hpp>
2627
#include <geode/stochastic/geometry/common.hpp>
28+
2729
#include <optional>
28-
#include <variant>
2930

3031
namespace geode
3132
{
@@ -42,6 +43,7 @@ namespace geode
4243
struct Uniform
4344
{
4445
Uniform() = default;
46+
// toremove
4547
Uniform( Type min_val, Type max_val )
4648
: min{ IntervalLimit< Type >{ min_val, true } },
4749
max{ IntervalLimit< Type >{ max_val, true } }
@@ -54,6 +56,7 @@ namespace geode
5456
struct Gaussian
5557
{
5658
Gaussian() = default;
59+
// to remove
5760
Gaussian( double mean_val, double standard_deviation_val )
5861
: mean{ mean_val }, standard_deviation{ standard_deviation_val }
5962
{
@@ -75,6 +78,10 @@ namespace geode
7578
std::optional< double > min;
7679
std::optional< double > max;
7780
};
78-
using Distribution = std::variant< Uniform< double >, Gaussian >;
81+
82+
struct UniformBox3D
83+
{
84+
geode::BoundingBox3D box;
85+
};
7986

8087
} // namespace geode

include/geode/stochastic/geometry/random_engine.hpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
#include <geode/stochastic/geometry/distributions.hpp>
2929

3030
#include <geode/basic/pimpl.hpp>
31-
31+
#include <geode/geometry/point.hpp>
3232
namespace geode
3333
{
3434
class opengeode_stochastic_geometry_api RandomEngine
@@ -42,7 +42,7 @@ namespace geode
4242
void set_seed( uint64_t number );
4343
void set_seed( std::string_view word );
4444

45-
double sample( const Distribution& dist );
45+
// double sample( const Distribution& dist );
4646

4747
template < typename Type >
4848
Type sample_uniform( const Uniform< Type >& law );
@@ -51,6 +51,12 @@ namespace geode
5151

5252
bool sample_bernoulli( double probability_of_success );
5353

54+
geode::Point3D sample_box_uniform( const UniformBox3D& box_law )
55+
{
56+
geode_unused( box_law );
57+
return geode::Point3D{};
58+
}
59+
5460
private:
5561
IMPLEMENTATION_MEMBER( impl_ );
5662
};
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include <geode/stochastic/geometry/common.hpp>
2+
3+
#include <geode/stochastic/geometry/distributions.hpp>
4+
#include <geode/stochastic/geometry/random_engine.hpp>
5+
6+
#include <geode/geometry/point.hpp>
7+
#include <variant>
8+
9+
namespace geode
10+
{
11+
using DoubleDistribution = std::variant< Uniform< double >, Gaussian >;
12+
using Domain = std::variant< UniformBox3D >;
13+
14+
// Interface générique
15+
template < typename T >
16+
struct Sampler;
17+
18+
template <>
19+
struct Sampler< double >
20+
{
21+
static double sample(
22+
RandomEngine& engine, const DoubleDistribution& dist )
23+
{
24+
return std::visit(
25+
[&engine]( auto&& d ) -> double {
26+
using D = std::decay_t< decltype( d ) >;
27+
if constexpr( std::is_same_v< D, Uniform< double > > )
28+
return engine.sample_uniform< double >( d );
29+
else if constexpr( std::is_same_v< D, Gaussian > )
30+
return engine.sample_gaussian( d );
31+
else
32+
throw std::runtime_error(
33+
"Unsupported distribution for double" );
34+
},
35+
dist );
36+
}
37+
};
38+
39+
// Spécialisation pour `Point2D`
40+
template <>
41+
struct Sampler< geode::Point3D >
42+
{
43+
static geode::Point3D sample( RandomEngine& engine, const Domain& dist )
44+
{
45+
return std::visit(
46+
[&engine]( auto&& d ) -> geode::Point3D {
47+
using D = std::decay_t< decltype( d ) >;
48+
if constexpr( std::is_same_v< D, UniformBox3D > )
49+
return engine.sample_box_uniform( d );
50+
else
51+
throw std::runtime_error(
52+
"Unsupported distribution for Point2D" );
53+
},
54+
dist );
55+
}
56+
};
57+
} // namespace geode

src/geode/stochastic/geometry/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@ add_geode_library(
2424
SOURCES
2525
"common.cpp"
2626
"random_engine.cpp"
27+
"random_sampler.cpp"
2728
"hello_world.cpp"
2829
PUBLIC_HEADERS
2930
"common.hpp"
3031
"random_engine.hpp"
32+
"random_sampler.hpp"
3133
"distributions.hpp"
3234
"hello_world.hpp"
3335
PRIVATE_DEPENDENCIES

src/geode/stochastic/geometry/random_engine.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,14 @@ namespace geode
6969
rand_gen_ = absl::BitGen{ create_seed_seq( word ) };
7070
}
7171

72-
double sample( const Distribution& dist )
73-
{
74-
return std::visit(
75-
[this]( auto&& d ) {
76-
return sample_distribution( d );
77-
},
78-
dist );
79-
}
72+
// double sample( const Distribution& dist )
73+
// {
74+
// return std::visit(
75+
// [this]( auto&& d ) {
76+
// return sample_distribution( d );
77+
// },
78+
// dist );
79+
// }
8080
double sample_distribution( const Uniform< double >& law )
8181
{
8282
return absl::Uniform( absl::IntervalClosedOpen, rand_gen_,
@@ -175,10 +175,10 @@ namespace geode
175175
impl_->set_seed( word );
176176
}
177177

178-
double RandomEngine::sample( const Distribution& dist )
179-
{
180-
return impl_->sample( dist );
181-
}
178+
// double RandomEngine::sample( const Distribution& dist )
179+
// {
180+
// return impl_->sample( dist );
181+
// }
182182

183183
template < typename Type >
184184
Type RandomEngine::sample_uniform( const Uniform< Type >& law )

src/geode/stochastic/geometry/random_sampler.cpp

Whitespace-only changes.

0 commit comments

Comments
 (0)