Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/geode/stochastic/sampling/direct/double_sampler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ namespace geode

static Distribution create_distribution(
const DistributionDescription& desc );
static Distribution create_angle_distribution_in_rad(
Comment thread
francoisbonneau marked this conversation as resolved.
Outdated
const DistributionDescription& desc );

static double sample( RandomEngine& engine, const Distribution& dist );
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ namespace geode
{
if( !is_valid_object( obj ) )
{
Logger::warn( "[ObjectSetSampler] - invalid object proposed." );
return LOG_PROB_INVALID;
}
return log_pdf_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ namespace geode
const auto& extremities = obj.vertices();
const auto current =
static_cast< local_index_t >( engine.sample_bernoulli( 0.5 ) );
const auto other = 1 - current;

geode::Sphere< 2 > ball{ extremities[current],
ratio * obj.length() };
Expand All @@ -71,7 +72,8 @@ namespace geode
constexpr index_t max_try{ 100 };
for( const auto try_id : geode::Range{ max_try } )
{
if( box_.contains( new_point ) )
if( box_.contains( new_point )
|| box_.contains( extremities[other] ) )
{
OwnerSegment2D new_segment{ obj };
new_segment.set_point( current, new_point );
Expand All @@ -91,7 +93,7 @@ namespace geode
const auto& extremities = obj.vertices();

return box_.contains( extremities[0] )
&& box_.contains( extremities[1] );
|| box_.contains( extremities[1] );
}

private:
Expand Down
30 changes: 30 additions & 0 deletions include/geode/stochastic/sampling/distributions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,34 @@ namespace geode
}
};

struct opengeode_stochastic_stochastic_api VonMises
{
VonMises() = default;

// 2D von Mises parameters
double mean{ 0.0 }; // mean direction in radians
double concentration{ 1.0 }; // kappa (concentration parameter)

[[nodiscard]] static DistributionType distribution_type_static()
{
return DistributionType{ "VonMises" };
}

[[nodiscard]] DistributionType distribution_type() const
{
return distribution_type_static();
}

[[nodiscard]] bool is_valid() const
{
return concentration >= 0.0 && std::isfinite( mean );
}

std::string string() const
{
return absl::StrCat( distribution_type().get(), "(mean=", mean,
", kappa=", concentration, ")" );
}
};

} // namespace geode
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ namespace geode
auto length_distribution =
DoubleSampler::create_distribution( set_desc.length );
auto azimuth_distribution =
DoubleSampler::create_distribution( set_desc.azimuth );
DoubleSampler::create_angle_distribution_in_rad(
set_desc.azimuth );
this->set_samplers_.push_back(
std::make_unique< UniformSegmentSetSampler >(
box_, length_distribution, azimuth_distribution ) );
Expand Down
37 changes: 37 additions & 0 deletions src/geode/stochastic/sampling/direct/double_sampler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@

#include <geode/stochastic/sampling/direct/double_sampler.hpp>

#include <geode/geometry/angle.hpp>

#include <geode/stochastic/common.hpp>
#include <geode/stochastic/sampling/random_engine.hpp>

namespace
{
struct DistributionParams
Expand Down Expand Up @@ -129,6 +132,40 @@ namespace geode
return it->second( desc );
}

DoubleSampler::Distribution DoubleSampler::create_angle_distribution_in_rad(
const DistributionDescription& desc_deg )
{
DistributionDescription desc_rad = desc_deg;
if( desc_rad.mean )
{
auto mean_angle = Angle::create_from_degrees( *( desc_rad.mean ) );
desc_rad.mean = mean_angle.normalized_between_0_and_2pi().radians();
}
if( desc_rad.standard_deviation )
{
auto std_angle =
Angle::create_from_degrees( *( desc_rad.standard_deviation ) );
desc_rad.standard_deviation =
std_angle.normalized_between_0_and_2pi().radians();
}
if( desc_rad.min_value )
{
auto min_angle =
Angle::create_from_degrees( *( desc_rad.min_value ) );
desc_rad.min_value =
min_angle.normalized_between_0_and_2pi().radians();
}
if( desc_rad.max_value )
{
auto max_angle =
Angle::create_from_degrees( *( desc_rad.max_value ) );
desc_rad.max_value =
max_angle.normalized_between_0_and_2pi().radians();
}
// Call the general create_distribution
return create_distribution( desc_rad );
}

double DoubleSampler::sample(
RandomEngine& engine, const Distribution& dist )
{
Expand Down
22 changes: 18 additions & 4 deletions src/geode/stochastic/sampling/direct/segment_uniform_sampler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,34 @@
#include <geode/stochastic/common.hpp>
#include <geode/stochastic/sampling/random_engine.hpp>

#include <geode/geometry/angle.hpp>
#include <geode/geometry/basic_objects/segment.hpp>
#include <geode/geometry/vector.hpp>

namespace
{
geode::Vector2D direction_from_azimuth_angle( const geode::Angle& azimuth )
{
return geode::Vector2D( { azimuth.sin(), azimuth.cos() } );
}
} // namespace
namespace geode
{

OwnerSegment2D SegmentUniformSampler::sample( RandomEngine& engine,
const PointUniformSampler::Object< 2 >& object,
const DoubleSampler::Distribution& length,
const DoubleSampler::Distribution& azimuth )
const DoubleSampler::Distribution& azimuth_rad )
{
geode_unused( length );
geode_unused( azimuth );
auto point1 = PointUniformSampler::sample( engine, object );
auto point2 = PointUniformSampler::sample( engine, object );

auto segment_length = DoubleSampler::sample( engine, length );
auto segment_azimuth = Angle::create_from_radians(
DoubleSampler::sample( engine, azimuth_rad ) );

auto point2 =
point1
+ direction_from_azimuth_angle( segment_azimuth ) * segment_length;

return OwnerSegment2D{ point1, point2 };
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,16 @@ void test_sample_segment(
{
geode::UniformClosed< double > length;
geode::UniformClosed< double > az;
auto box_enlarged = box;
box_enlarged.extends( 1.01 );

for( const auto i : geode::Range{ NUMBER_OF_SAMPLES } )
{
auto value =
geode::SegmentUniformSampler::sample( engine, box, length, az );
OPENGEODE_EXCEPTION( box.contains( value.vertices()[0] ),
OPENGEODE_EXCEPTION( box_enlarged.contains( value.vertices()[0] ),
"[SegmentUniformSampler] - segment out of box." );
OPENGEODE_EXCEPTION( box.contains( value.vertices()[1] ),
OPENGEODE_EXCEPTION( box_enlarged.contains( value.vertices()[1] ),
"[SegmentUniformSampler] - segment out of box." );
}
}
Expand Down
Loading