5#include <marenv/wake/WakeField.h>
6#include <marenv/wake/WakeTypes.h>
23template<
class S,
class Value>
24concept BlendSnapshot =
requires(
const S& snapshot,
double t,
const double* pos, Value& out) {
25 { snapshot.Sample(t, pos, out) } -> std::convertible_to<bool>;
65template<
class S,
class Value = WakeSample,
class SumType = WakeBlendSum,
class SamplesType = WakeBlendSamples>
83 static double Weight(
double t,
double start,
double time) {
return BlendWeight(t, start, time); }
86 [[nodiscard]] std::shared_ptr<const State>
Load()
const {
return m_state.load(); }
95 void Publish(std::shared_ptr<const S> next,
double start,
double time)
97 const auto inUse =
Load();
98 if (inUse && inUse->start + inUse->time > start)
99 throw std::logic_error(
"WakeBlend::Publish: the previous blend has not ended (R22)");
100 auto previous = inUse && time > 0.0 ? Settled(inUse->current) :
nullptr;
101 Install(std::move(next), std::move(previous), start, time);
107 return Evaluate(state, t, pos, SampleSnapshot);
116 template<
class Sampler>
117 static Sum Evaluate(
const State& state,
double t,
const double pos[3],
const Sampler& sample)
122 if (weight > 0.0 && sample(*state.
current, t, pos, value))
123 sum.Add(weight, value);
124 if (weight < 1.0 && state.
previous) {
127 sum.Add(1.0 - weight, previous);
136 [[nodiscard]] std::uint64_t
Revision()
const {
return m_revision.load(std::memory_order_acquire); }
144 const auto state =
Load();
145 return !state || (!
Blends(*state, t) && state->current->Stationary());
155 static SamplesType
Samples(
const State& state,
double t,
const double pos[3])
157 return Samples(state, t, pos, SampleSnapshot);
161 template<
class Sampler>
162 static SamplesType
Samples(
const State& state,
double t,
const double pos[3],
const Sampler& sample)
165 samples.currentInside = sample(*state.
current, t, pos, samples.current);
168 samples.start = state.
start;
169 samples.time = state.
time;
188 static bool SampleSnapshot(
const S& snapshot,
double t,
const double pos[3], Value& out) {
return snapshot.Sample(t, pos, out); }
191 static std::shared_ptr<const State> Settled(std::shared_ptr<const S> snapshot)
193 return std::make_shared<const State>(State {std::move(snapshot),
nullptr, 0.0, 0.0});
203 void Install(std::shared_ptr<const S> next, std::shared_ptr<const State> previous,
double start,
double time)
205 m_state.store(std::make_shared<const State>(State {std::move(next), std::move(previous), start, time}));
206 m_revision.fetch_add(1, std::memory_order_release);
209 std::atomic<std::shared_ptr<const State>> m_state;
210 std::atomic<std::uint64_t> m_revision {0};
Definition WakeBlend.h:68
static double Weight(double t, double start, double time)
Smoothstep weight of the new snapshot at t; 1 without blending (time <= 0 or NaN)....
Definition WakeBlend.h:83
std::uint64_t Revision() const
The number of publishes so far (WakeField::Revision()).
Definition WakeBlend.h:136
std::shared_ptr< const State > Load() const
The published state, one atomic load; null before the first Publish(). A query loads it once and uses...
Definition WakeBlend.h:86
void Publish(std::shared_ptr< const S > next, double start, double time)
Definition WakeBlend.h:95
static bool Blends(const State &state, double t)
Whether the state blends at t or later: the weight of t is below 1, so it still changes (owner ruling...
Definition WakeBlend.h:133
static SamplesType Samples(const State &state, double t, const double pos[3])
Definition WakeBlend.h:155
static bool MayReach(const State &state, const WakeQueryRegion ®ion)
Definition WakeBlend.h:180
SumType Sum
The sums over the snapshots that contributed at one point.
Definition WakeBlend.h:80
static Sum Evaluate(const State &state, double t, const double pos[3])
The sums of the snapshots of state at pos and t, weighted by the blend.
Definition WakeBlend.h:105
static SamplesType Samples(const State &state, double t, const double pos[3], const Sampler &sample)
Samples() with each snapshot sampled by sample(snapshot, t, pos, value), as Evaluate(state,...
Definition WakeBlend.h:162
static Sum Evaluate(const State &state, double t, const double pos[3], const Sampler &sample)
Definition WakeBlend.h:117
bool StaticFrom(double t) const
Definition WakeBlend.h:142
Definition WakeBlend.h:24
Definition WakeBlend.h:32
double BlendWeight(double t, double start, double time)
Definition WakeSampleParts.h:13
One immutable step of the blend chain.
Definition WakeBlend.h:72
double start
Start of the blend, s.
Definition WakeBlend.h:75
std::shared_ptr< const S > current
The snapshot blended to.
Definition WakeBlend.h:73
double time
Duration of the blend, s; 0 means no blending.
Definition WakeBlend.h:76
std::shared_ptr< const State > previous
The state blended from; null means no wake.
Definition WakeBlend.h:74
Definition WakeField.h:23
double tBegin
First query time, s.
Definition WakeField.h:26