Program Listing for File GraphicalSceneMovie.cc

Program Listing for File GraphicalSceneMovie.cc#

Return to documentation for file (doxygen_docs/GeneralKModels/GraphicalSceneMovie.cc)

#include"Karana/GeneralKModels/GraphicalSceneMovie.h"
#include"Karana/KCore/Allocator.h"
#include<filesystem>


namespaceKarana::Models{

namespaceks=Karana::Scene;
namespacekm=Karana::Math;

GraphicalSceneMovie::GraphicalSceneMovie(std::string_viewname,
constkc::ks_ptr<kd::StatePropagator>&sp,
constkc::ks_ptr<ks::ProxyScene>&scene)
:KModel<GraphicalSceneMovie,GraphicalSceneMovieParams>(name,sp)
,_scene(scene){
params=std::allocate_shared<GraphicalSceneMovieParams>(
kc::Allocator<GraphicalSceneMovieParams>{},std::format("{}_params",name));
};

kc::ks_ptr<GraphicalSceneMovie>
GraphicalSceneMovie::create(std::string_viewname,
constkc::ks_ptr<kd::StatePropagator>&sp,
constkc::ks_ptr<ks::ProxyScene>&scene){
kc::ks_ptr<GraphicalSceneMovie>gsm=std::allocate_shared<GraphicalSceneMovie>(
kc::Allocator<GraphicalSceneMovie>{},name,sp,scene);
sp->registerModel(gsm);
returngsm;
}

voidGraphicalSceneMovie::postModelStep(constkm::Ktime&,constkm::Vec&){
_scene->update(*_scene->graphics());
_scene->graphics()->renderToFile(
std::format("{}/{}_{:06d}.png",params->dir,params->filename_prefix,_frame_no));
_frame_no++;
}

GraphicalSceneMovieParams::GraphicalSceneMovieParams(std::string_viewname)
:KModelParams(name){}

boolGraphicalSceneMovieParams::isFinalized()const{
boolflag=true;
if(dir==""){
kc::warn("Parameterdirisuninitialized.");
flag=false;
}
if(filename_prefix==""){
kc::warn("Parameterfilename_prefixisuninitialized.");
flag=false;
}
returnflag;
}

std::stringGraphicalSceneMovie::getFfmpegCommand()const{
returnstd::format(
"ffmpeg-framerate{}-i{}/{}_%06d.png-c:vlibx264-pix_fmtyuv420poutput.mp4",
1.0/km::ktimeToSeconds(_period),
params->dir,
params->filename_prefix);
}

voidGraphicalSceneMovie::_registerModel(){
//Calltheparent.Thiswilldothenormalregistration.
KModel<GraphicalSceneMovie, GraphicalSceneMovieParams>::_registerModel();

kc::ks_ptr<Base>dark=shared_from_this();
autot=kc::static_pointer_cast<GraphicalSceneMovie>(dark);

//Alsoregisterafirststeptorunnowifweshouldrenderthefirstframe.
if(params->render_first_frame){
autote=kd::TimedEvent::create(
std::format("{}_first_step",name()),
state_propagator->getTime(),
[t](constkm::Ktime&){
if(!std::filesystem::exists(t->params->dir)){
if(notstd::filesystem::create_directories(t->params->dir)){
throwstd::invalid_argument(
std::format("Failedtocreatedirectory{}",t->params->dir));
}
}
returnt->postModelStep(t->state_propagator->getTime(),
t->state_propagator->getIntegrator()->getX());
},
true);
state_propagator->registerTimedEvent(te);
}
};

voidGraphicalSceneMovie::_unregisterModel(){
//Calltheparent.Thiswilldothenormalunregistration.
KModel<GraphicalSceneMovie, GraphicalSceneMovieParams>::_unregisterModel();

//Tryandunregisterthiseventregardlessoftheparametersetting.
//It'spossibletheparameterhasbeenchangedsincethelastregistration.
state_propagator->unregisterTimedEvent(std::format("{}_first_step",name()),true,true);
}

//DestructorincludedforMacOSbuilds.Musthaveakey-functionout-of-linetoavoiddulpicate
//symbols.
GraphicalSceneMovie::~GraphicalSceneMovie(){};

}//namespaceKarana::Models