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::ModelManager>&mm,
constkc::ks_ptr<ks::ProxyScene>&scene)
:KModel<GraphicalSceneMovie,GraphicalSceneMovieParams>(name,mm)
,_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::ModelManager>&mm,
constkc::ks_ptr<ks::ProxyScene>&scene){
kc::ks_ptr<GraphicalSceneMovie>gsm=std::allocate_shared<GraphicalSceneMovie>(
kc::Allocator<GraphicalSceneMovie>{},name,mm,scene);
mm->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::isReady()const{
boolflag=true;
if(dir==""){
kc::warn("Parameterdirisnotready.");
flag=false;
}
if(filename_prefix==""){
kc::warn("Parameterfilename_prefixisnotready.");
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.
kc::ks_ptr<kd::StatePropagator>sp=
kc::dynamic_pointer_cast<kd::StatePropagator>(model_manager);
if(spandparams->render_first_frame){
autote=kd::TimedEvent::create(
std::format("{}_first_step",name()),
model_manager->getTime(),
[t,sp](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(sp->getTime(),sp->getIntegrator()->getX());
},
true);
sp->registerTimedEvent(te);
}
};
voidGraphicalSceneMovie::_unregisterModel(){
//Calltheparent.Thiswilldothenormalunregistration.
KModel<GraphicalSceneMovie, GraphicalSceneMovieParams>::_unregisterModel();
//Tryandunregisterthiseventregardlessoftheparametersetting.
//It'spossibletheparameterhasbeenchangedsincethelastregistration.
kc::ks_ptr<kd::StatePropagator>sp=
kc::dynamic_pointer_cast<kd::StatePropagator>(model_manager);
if(sp){
sp->unregisterTimedEvent(std::format("{}_first_step",name()),true,true);
}
}
//DestructorincludedforMacOSbuilds.Musthaveakey-functionout-of-linetoavoiddulpicate
//symbols.
GraphicalSceneMovie::~GraphicalSceneMovie(){};
}//namespaceKarana::Models