Sync - precompute all values for a frame

This commit is contained in:
Emmanuel BENOîT 2017-10-16 09:35:53 +02:00
parent 647e247916
commit a327445b71
2 changed files with 82 additions and 37 deletions

106
sync.cc
View file

@ -17,8 +17,9 @@ T_SyncData::T_SyncData(
T_SyncData::T_VarHelper_::T_VarHelper_(
__rw__ T_SyncVariable&& v ,
__rd__ const uint32_t duration ) noexcept
: variable( std::move( v ) )
__rd__ const uint32_t duration ,
__rd__ const uint32_t position ) noexcept
: variable( std::move( v ) ) , position( position )
{
const auto n( variable.size( ) );
uint32_t s = 0;
@ -40,13 +41,53 @@ T_SyncData::T_VarHelper_::T_VarHelper_(
}
float T_SyncData::T_VarHelper_::valueAt(
__rd__ const float position ,
__rd__ const float units ) const noexcept
{
// Find segment
const auto tu( uint32_t( floor( position / units ) ) );
uint32_t s( 0 );
for ( auto ss : segStarts ) {
if ( ss > tu ) {
break;
}
s ++;
}
assert( s > 0 );
s --;
const auto sid( segRefs[ s ].first );
auto const& seg( variable[ sid ] );
const auto pid( segRefs[ s ].second );
// Interpolation factor
const float st( segStarts[ s ] * units );
const float et( ( segStarts[ s ] + seg.durations[ pid ] ) * units );
const float v0 = ( position - st ) / ( et - st );
float v = v0;
if ( seg.type != E_SyncSegment::LINEAR ) {
v *= v0;
if ( seg.type == E_SyncSegment::SMOOTH ) {
v *= 3 - 2 * v0;
}
}
const float sv( seg.values[ pid ] );
const float ev( seg.values[ pid + 1 ] );
return v * ( ev - sv ) + sv;
}
void T_SyncData::setSyncVariable(
__rd__ std::string const& name ,
__rw__ T_SyncVariable&& variable ) noexcept
{
variables_.erase( name );
if ( !variable.empty( ) ) {
variables_.emplace( name , T_VarHelper_{ std::move( variable ) , duration_ } );
variables_.emplace( name , T_VarHelper_{
std::move( variable ) , duration_ ,
uint32_t( variables_.size( ) )
} );
}
}
@ -61,6 +102,16 @@ T_SyncVariable const& T_SyncData::variable(
return pos->second.variable;
}
uint32_t T_SyncData::offsetOf(
__rd__ std::string const& name ) const noexcept
{
auto pos( variables_.find( name ) );
if ( pos == variables_.end( ) ) {
return 0xffffffff;
}
return pos->second.position;
}
float T_SyncData::valueOf(
__rd__ std::string const& name ,
@ -72,37 +123,20 @@ float T_SyncData::valueOf(
if ( pos == variables_.end( ) ) {
return 0;
}
// Find segment
auto const& var( pos->second );
const uint32_t tu( std::min( duration_ ,
uint32_t( floor( time / units_ ) ) ) );
uint32_t s( 0 );
for ( auto ss : var.segStarts ) {
if ( ss > tu ) {
break;
}
s ++;
}
assert( s > 0 );
s --;
const auto sid( var.segRefs[ s ].first );
auto const& seg( var.variable[ sid ] );
const auto pid( var.segRefs[ s ].second );
// Interpolation factor
const float st( var.segStarts[ s ] * units_ );
const float et( ( var.segStarts[ s ] + seg.durations[ pid ] ) * units_ );
const float v0 = ( time - st ) / ( et - st );
float v = v0;
if ( seg.type != E_SyncSegment::LINEAR ) {
v *= v0;
if ( seg.type == E_SyncSegment::SMOOTH ) {
v *= 3 - 2 * v0;
}
}
const float sv( seg.values[ pid ] );
const float ev( seg.values[ pid + 1 ] );
return v * ( ev - sv ) + sv;
return pos->second.valueAt( std::min( duration_ * units_ , time ) , units_ );
}
void T_SyncData::computeValues(
__rd__ const float time ,
__wr__ std::vector< float >& values ) const noexcept
{
assert( time >= 0 );
values.resize( variables_.size( ) );
auto vit( values.begin( ) );
const float t( std::min( duration_ * units_ , time ) );
for ( auto const& var : variables_ ) {
*vit = var.second.valueAt( t , units_ );
vit ++;
}
}

13
sync.hh
View file

@ -39,11 +39,17 @@ struct T_SyncData
T_SyncVariable const& variable(
__rd__ std::string const& name ) const noexcept;
uint32_t offsetOf(
__rd__ std::string const& name ) const noexcept;
float valueOf(
__rd__ std::string const& variable ,
__rd__ const float time ) const noexcept;
void computeValues(
__rd__ const float time ,
__wr__ std::vector< float >& values ) const noexcept;
private:
static const T_SyncVariable MissingVariable_;
@ -54,10 +60,15 @@ struct T_SyncData
T_SyncVariable variable;
std::vector< T_SegRef_ > segRefs;
std::vector< float > segStarts;
uint32_t position;
T_VarHelper_(
__rw__ T_SyncVariable&& variable ,
__rd__ const uint32_t duration ) noexcept;
__rd__ const uint32_t duration ,
__rd__ const uint32_t position ) noexcept;
float valueAt(
__rd__ const float position ,
__rd__ const float units ) const noexcept;
};
uint32_t duration_;