Skip to content

Commit

Permalink
- Update trajectory reader
Browse files Browse the repository at this point in the history
- Link onAtomPosition callback
  • Loading branch information
Nicolas Dacquay committed Mar 18, 2024
1 parent 891aae5 commit 7d9461b
Show file tree
Hide file tree
Showing 20 changed files with 131 additions and 97 deletions.
5 changes: 5 additions & 0 deletions lib/app/include/app/component/chemistry/trajectory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "app/application/system/ecs_system.hpp"
#include "app/core/trajectory_player/base_player.hpp"
#include "enum_trajectory.hpp"
#include <util/callback.hpp>

namespace VTX::App::Component::Chemistry
{
Expand All @@ -23,11 +24,15 @@ namespace VTX::App::Component::Chemistry
Molecule * const getMoleculePtr() const { return _moleculePtr; }

size_t getCurrentFrame() const;
void setCurrentFrame( const size_t p_frameIndex );

size_t getFrameCount() const;

App::Core::TrajectoryPlayer::BasePlayer & getPlayer() const { return *_player; }
void setPlayer( std::unique_ptr<App::Core::TrajectoryPlayer::BasePlayer> & p_player );

Util::Callback<size_t> onFrameChange;

private:
void _update( const float p_deltaTime );
void _referenceUpdateFunction();
Expand Down
1 change: 1 addition & 0 deletions lib/app/include/app/component/render/proxy_molecule.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ namespace VTX::App::Component::Render

void _applyVisibilityCallbacks();
void _applySelectionCallbacks() const;
void _applyAtomPositionCallbacks() const;

std::unique_ptr<VTX::Renderer::Proxy::Molecule> _proxyPtr = nullptr;
};
Expand Down
22 changes: 14 additions & 8 deletions lib/app/include/app/core/trajectory_player/base_player.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#ifndef __VTX_APP_CORE_TRAJECTORY_PLAYER_BASE_PLAYER__
#define __VTX_APP_CORE_TRAJECTORY_PLAYER_BASE_PLAYER__

#include "app/component/chemistry/_fwd.hpp"
#include "app/core/collection.hpp"
#include <core/struct/trajectory.hpp>
#include <memory>
#include <string>
#include <util/callback.hpp>
#include <util/types.hpp>

namespace VTX::App::Core::TrajectoryPlayer
Expand All @@ -14,16 +15,14 @@ namespace VTX::App::Core::TrajectoryPlayer
public:
BasePlayer() = default;
BasePlayer( const BasePlayer & p_source ) = default;
BasePlayer( VTX::Core::Struct::Trajectory * const p_trajectory ) : _trajectoryPtr( p_trajectory ) {}

virtual ~BasePlayer() = default;

bool isLinkedToTrajectory() const { return _trajectoryPtr != nullptr; }
void setTrajectory( VTX::Core::Struct::Trajectory & p_trajectory );
size_t getCount() const { return _count; }
void setCount( const size_t p_count );

size_t getCurrentFrameIndex() const;
void setCurrentFrameIndex( const size_t p_frameIndex );
size_t getFrameCount() const;
size_t getCurrent() const;
void setCurrent( const size_t p_frameIndex );

virtual void play();
virtual void pause();
Expand All @@ -43,8 +42,15 @@ namespace VTX::App::Core::TrajectoryPlayer
virtual const std::string & getDisplayName() const = 0;
virtual const CollectionKey & getCollectionKey() const = 0;

Util::Callback<> onPlay;
Util::Callback<> onPause;
Util::Callback<> onStop;
Util::Callback<size_t> onFrameChange;
Util::Callback<uint> onFPSChange;

private:
VTX::Core::Struct::Trajectory * _trajectoryPtr = nullptr;
size_t _count = 0;
size_t _current = 0;

bool _isPlaying = true;
uint _fps = 1u;
Expand Down
3 changes: 0 additions & 3 deletions lib/app/include/app/core/trajectory_player/loop.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#ifndef __VTX_APP_CORE_TRAJECTORY_PLAYER_LOOP__
#define __VTX_APP_CORE_TRAJECTORY_PLAYER_LOOP__

#include "app/core/collection.hpp"
#include "base_player.hpp"
#include "ping_pong.hpp"
#include "players.hpp"

namespace VTX::App::Core::TrajectoryPlayer
Expand All @@ -20,7 +18,6 @@ namespace VTX::App::Core::TrajectoryPlayer
public:
Loop() = default;
Loop( const Loop & p_source ) = default;
Loop( VTX::Core::Struct::Trajectory * const p_trajectory );

const std::string & getDisplayName() const override { return DISPLAYED_NAME; }
const CollectionKey & getCollectionKey() const override { return COLLECTION_ID; }
Expand Down
2 changes: 0 additions & 2 deletions lib/app/include/app/core/trajectory_player/once.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#include "base_player.hpp"
#include "players.hpp"
#include <string>

namespace VTX::App::Core::TrajectoryPlayer
{
Expand All @@ -19,7 +18,6 @@ namespace VTX::App::Core::TrajectoryPlayer
public:
Once() = default;
Once( const Once & p_source ) = default;
Once( VTX::Core::Struct::Trajectory * const p_trajectory );

const std::string & getDisplayName() const override { return DISPLAYED_NAME; }
const CollectionKey & getCollectionKey() const override { return COLLECTION_ID; }
Expand Down
2 changes: 0 additions & 2 deletions lib/app/include/app/core/trajectory_player/ping_pong.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#include "base_player.hpp"
#include "players.hpp"
#include <string>

namespace VTX::App::Core::TrajectoryPlayer
{
Expand All @@ -19,7 +18,6 @@ namespace VTX::App::Core::TrajectoryPlayer
public:
PingPong() = default;
PingPong( const PingPong & p_source ) = default;
PingPong( VTX::Core::Struct::Trajectory * const p_trajectory );

const std::string & getDisplayName() const override { return DISPLAYED_NAME; }
const CollectionKey & getCollectionKey() const override { return COLLECTION_ID; }
Expand Down
2 changes: 0 additions & 2 deletions lib/app/include/app/core/trajectory_player/revert_loop.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#include "base_player.hpp"
#include "players.hpp"
#include <string>

namespace VTX::App::Core::TrajectoryPlayer
{
Expand All @@ -19,7 +18,6 @@ namespace VTX::App::Core::TrajectoryPlayer
public:
RevertLoop() = default;
RevertLoop( const RevertLoop & p_source ) = default;
RevertLoop( VTX::Core::Struct::Trajectory * const p_trajectory );

const std::string & getDisplayName() const override { return DISPLAYED_NAME; }
const CollectionKey & getCollectionKey() const override { return COLLECTION_ID; }
Expand Down
2 changes: 0 additions & 2 deletions lib/app/include/app/core/trajectory_player/revert_once.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#include "base_player.hpp"
#include "players.hpp"
#include <string>

namespace VTX::App::Core::TrajectoryPlayer
{
Expand All @@ -19,7 +18,6 @@ namespace VTX::App::Core::TrajectoryPlayer
public:
RevertOnce() = default;
RevertOnce( const RevertOnce & p_source ) = default;
RevertOnce( VTX::Core::Struct::Trajectory * const p_trajectory );

const std::string & getDisplayName() const override { return DISPLAYED_NAME; }
const CollectionKey & getCollectionKey() const override { return COLLECTION_ID; }
Expand Down
2 changes: 0 additions & 2 deletions lib/app/include/app/core/trajectory_player/stop.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#include "base_player.hpp"
#include "players.hpp"
#include <string>

namespace VTX::App::Core::TrajectoryPlayer
{
Expand All @@ -19,7 +18,6 @@ namespace VTX::App::Core::TrajectoryPlayer
public:
Stop();
Stop( const Stop & p_source );
Stop( VTX::Core::Struct::Trajectory * const p_trajectory );

void play() override;

Expand Down
17 changes: 13 additions & 4 deletions lib/app/src/app/component/chemistry/trajectory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,28 @@ namespace VTX::App::Component::Chemistry

Trajectory::Trajectory( Molecule * const p_molecule ) : _moleculePtr( p_molecule ) { _referenceUpdateFunction(); }

size_t Trajectory::getCurrentFrame() const
size_t Trajectory::getCurrentFrame() const { return _moleculePtr->getTrajectory().currentFrameIndex; }
void Trajectory::setCurrentFrame( const size_t p_frameIndex )
{
return _moleculePtr->getMoleculeStruct().trajectory.currentFrameIndex;
_moleculePtr->getTrajectory().currentFrameIndex = p_frameIndex;
}

size_t Trajectory::getFrameCount() const { return _moleculePtr->getMoleculeStruct().trajectory.frames.size(); }
size_t Trajectory::getFrameCount() const { return _moleculePtr->getTrajectory().frames.size(); }

void Trajectory::setPlayer( std::unique_ptr<App::Core::TrajectoryPlayer::BasePlayer> & p_player )
{
const bool resetPlayer = _player == nullptr;

onFrameChange.clear();

_player = std::move( p_player );
_player->setTrajectory( _moleculePtr->getTrajectory() );
_player->setCount( _moleculePtr->getTrajectory().getFrameCount() );

_player->onFrameChange += [ this ]( const size_t p_frameIndex )
{
_moleculePtr->getTrajectory().currentFrameIndex = p_frameIndex;
onFrameChange( p_frameIndex );
};

if ( resetPlayer )
_player->reset();
Expand Down
19 changes: 19 additions & 0 deletions lib/app/src/app/component/render/proxy_molecule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "app/application/selection/selection.hpp"
#include "app/application/selection/selection_manager.hpp"
#include "app/application/system/ecs_system.hpp"
#include "app/component/chemistry/trajectory.hpp"
#include "app/component/scene/transform_component.hpp"
#include <core/chemdb/atom.hpp>
#include <renderer/facade.hpp>
Expand Down Expand Up @@ -63,6 +64,7 @@ namespace VTX::App::Component::Render
{
_applyVisibilityCallbacks();
_applySelectionCallbacks();
_applyAtomPositionCallbacks();
}

std::vector<uchar> ProxyMolecule::_generateAtomColors( const VTX::Core::Struct::Molecule & p_molStruct ) const
Expand Down Expand Up @@ -169,5 +171,22 @@ namespace VTX::App::Component::Render
_proxyPtr->onAtomSelections( castedSelectionData.getAtomIds(), false );
};
}
void ProxyMolecule::_applyAtomPositionCallbacks() const
{
if ( MAIN_REGISTRY().hasComponent<Component::Chemistry::Trajectory>( *this ) )
{
Component::Chemistry::Trajectory & trajectoryComponent
= MAIN_REGISTRY().getComponent<Component::Chemistry::Trajectory>( *this );

trajectoryComponent.onFrameChange += [ this ]( const size_t p_frameIndex )
{
Component::Chemistry::Molecule & moleculeComponent
= MAIN_REGISTRY().getComponent<Component::Chemistry::Molecule>( *this );

_proxyPtr->atomPositions = &moleculeComponent.getTrajectory().getCurrentFrame();
_proxyPtr->onAtomPositions();
};
}
}

} // namespace VTX::App::Component::Render
61 changes: 40 additions & 21 deletions lib/app/src/app/core/trajectory_player/base_player.cpp
Original file line number Diff line number Diff line change
@@ -1,32 +1,44 @@
#include "app/core/trajectory_player/base_player.hpp"
#include <core/struct/trajectory.hpp>
#include <util/math.hpp>

namespace VTX::App::Core::TrajectoryPlayer
{
void BasePlayer::setTrajectory( VTX::Core::Struct::Trajectory & p_trajectory ) { _trajectoryPtr = &p_trajectory; }
void BasePlayer::setCount( const size_t p_count )
{
_count = p_count;

size_t BasePlayer::getCurrentFrameIndex() const
if ( _current >= p_count )
{
_current = p_count - 1;
onFrameChange( _current );
}
}

size_t BasePlayer::getCurrent() const { return _current; }
void BasePlayer::setCurrent( const size_t p_frameIndex )
{
assert( isLinkedToTrajectory() );
return _trajectoryPtr->currentFrameIndex;
if ( _current = p_frameIndex )
{
_current = p_frameIndex;
onFrameChange( p_frameIndex );
}
}
void BasePlayer::setCurrentFrameIndex( const size_t p_frameIndex )

void BasePlayer::play()
{
assert( isLinkedToTrajectory() );
_trajectoryPtr->currentFrameIndex = p_frameIndex;
_isPlaying = true;
onPlay();
}
size_t BasePlayer::getFrameCount() const
void BasePlayer::pause()
{
assert( isLinkedToTrajectory() );
return _trajectoryPtr->frames.size();
_isPlaying = false;
onPause();
}

void BasePlayer::play() { _isPlaying = true; }
void BasePlayer::pause() { _isPlaying = false; }
void BasePlayer::stop()
{
pause();
_isPlaying = false;
reset();
onStop();
}

void BasePlayer::update( const float p_deltaTime )
Expand All @@ -42,22 +54,29 @@ namespace VTX::App::Core::TrajectoryPlayer
}
else
{
const size_t currentFrameIndex = getCurrentFrameIndex();
size_t nextFrameIndex = currentFrameIndex;
const size_t previousCurrent = _current;
size_t nextIndex = _current;

const float offset = 1.f / float( _fps );
while ( _trajectoryTimer >= offset )
{
nextFrameIndex++;
nextIndex++;
_trajectoryTimer -= offset;
}

if ( nextFrameIndex != currentFrameIndex )
if ( nextIndex != previousCurrent )
{
nextFrame( nextFrameIndex - currentFrameIndex );
nextFrame( nextIndex - previousCurrent );
}
}
}

void BasePlayer::setFPS( const uint p_fps ) { _fps = p_fps; }
void BasePlayer::setFPS( const uint p_fps )
{
if ( _fps != p_fps )
{
_fps = p_fps;
onFPSChange( p_fps );
}
}
} // namespace VTX::App::Core::TrajectoryPlayer
10 changes: 4 additions & 6 deletions lib/app/src/app/core/trajectory_player/loop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

namespace VTX::App::Core::TrajectoryPlayer
{
Loop::Loop( VTX::Core::Struct::Trajectory * const p_trajectory ) : BasePlayer( p_trajectory ) {}

void Loop::reset() { setCurrentFrameIndex( 0 ); }
void Loop::nextFrame( const size_t p_frameCount )
void Loop::reset() { setCurrent( 0 ); }
void Loop::nextFrame( const size_t p_count )
{
const size_t newFrameIndex = ( getCurrentFrameIndex() + p_frameCount ) % getFrameCount();
setCurrentFrameIndex( newFrameIndex );
const size_t newFrameIndex = ( getCurrent() + p_count ) % getCount();
setCurrent( newFrameIndex );
}

} // namespace VTX::App::Core::TrajectoryPlayer
14 changes: 6 additions & 8 deletions lib/app/src/app/core/trajectory_player/once.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@

namespace VTX::App::Core::TrajectoryPlayer
{
Once::Once( VTX::Core::Struct::Trajectory * const p_trajectory ) : BasePlayer( p_trajectory ) {}

void Once::reset() { setCurrentFrameIndex( 0 ); }
void Once::nextFrame( const size_t p_frameCount )
void Once::reset() { setCurrent( 0 ); }
void Once::nextFrame( const size_t p_count )
{
const size_t newFrameIndex = getCurrentFrameIndex() + p_frameCount;
const size_t newFrameIndex = getCurrent() + p_count;

if ( newFrameIndex < getFrameCount() - 1 )
if ( newFrameIndex < getCount() - 1 )
{
setCurrentFrameIndex( newFrameIndex );
setCurrent( newFrameIndex );
}
else
{
setCurrentFrameIndex( getFrameCount() - 1 );
setCurrent( getCount() - 1 );
pause();
}
}
Expand Down
Loading

0 comments on commit 7d9461b

Please sign in to comment.