From 662897d4c1380dd8f9748688ce7aa8e574e7c61c Mon Sep 17 00:00:00 2001 From: Ivan Mahonin Date: Oct 18 2019 07:28:13 +0000 Subject: bake action (closes #979) --- diff --git a/synfig-studio/src/synfigapp/Makefile.am b/synfig-studio/src/synfigapp/Makefile.am index d9a33a1..3229bd8 100644 --- a/synfig-studio/src/synfigapp/Makefile.am +++ b/synfig-studio/src/synfigapp/Makefile.am @@ -80,7 +80,8 @@ VALUEDESC_ACTION_HH = \ actions/valuedescbonelink.h \ actions/valuedescskeletonlink.h \ actions/valuedesccreatechildbone.h \ - actions/valuedescresetpose.h + actions/valuedescresetpose.h \ + actions/valuedescbake.h VALUEDESC_ACTION_CC = \ actions/valuedescconnect.cpp \ @@ -95,7 +96,8 @@ VALUEDESC_ACTION_CC = \ actions/valuedescbonelink.cpp \ actions/valuedescskeletonlink.cpp \ actions/valuedesccreatechildbone.cpp \ - actions/valuedescresetpose.cpp + actions/valuedescresetpose.cpp \ + actions/valuedescbake.cpp VALUENODE_ACTION_HH = \ diff --git a/synfig-studio/src/synfigapp/action.cpp b/synfig-studio/src/synfigapp/action.cpp index 750c53a..d05ca64 100644 --- a/synfig-studio/src/synfigapp/action.cpp +++ b/synfig-studio/src/synfigapp/action.cpp @@ -99,6 +99,7 @@ #include "actions/valuedescskeletonlink.h" #include "actions/valuedesccreatechildbone.h" #include "actions/valuedescresetpose.h" +#include "actions/valuedescbake.h" #include "actions/vectorization.h" #include "actions/waypointadd.h" #include "actions/waypointset.h" @@ -255,6 +256,7 @@ Action::Main::Main() ADD_ACTION(Action::ValueDescSkeletonLink); ADD_ACTION(Action::ValueDescCreateChildBone); ADD_ACTION(Action::ValueDescResetPose); + ADD_ACTION(Action::ValueDescBake); ADD_ACTION(Action::Vectorization); ADD_ACTION(Action::WaypointAdd); ADD_ACTION(Action::WaypointSet); diff --git a/synfig-studio/src/synfigapp/actions/CMakeLists.txt b/synfig-studio/src/synfigapp/actions/CMakeLists.txt index cc1e77c..2c35272 100644 --- a/synfig-studio/src/synfigapp/actions/CMakeLists.txt +++ b/synfig-studio/src/synfigapp/actions/CMakeLists.txt @@ -40,6 +40,7 @@ target_sources(synfigapp "${CMAKE_CURRENT_LIST_DIR}/valuedescskeletonlink.cpp" "${CMAKE_CURRENT_LIST_DIR}/valuedesccreatechildbone.cpp" "${CMAKE_CURRENT_LIST_DIR}/valuedescresetpose.cpp" + "${CMAKE_CURRENT_LIST_DIR}/valuedescbake.cpp" "${CMAKE_CURRENT_LIST_DIR}/valuenodeadd.cpp" "${CMAKE_CURRENT_LIST_DIR}/valuenodeconstset.cpp" diff --git a/synfig-studio/src/synfigapp/actions/valuedescbake.cpp b/synfig-studio/src/synfigapp/actions/valuedescbake.cpp new file mode 100644 index 0000000..b9f5670 --- /dev/null +++ b/synfig-studio/src/synfigapp/actions/valuedescbake.cpp @@ -0,0 +1,224 @@ +/* === S Y N F I G ========================================================= */ +/*! \file valuedescbake.cpp +** \brief Template File +** +** $Id$ +** +** \legal +** ......... ... 2019 Ivan Mahonin +** +** This package is free software; you can redistribute it and/or +** modify it under the terms of the GNU General Public License as +** published by the Free Software Foundation; either version 2 of +** the License, or (at your option) any later version. +** +** This package is distributed in the hope that it will be useful, +** but WITHOUT ANY WARRANTY; without even the implied warranty of +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +** General Public License for more details. +** \endlegal +*/ +/* ========================================================================= */ + +/* === H E A D E R S ======================================================= */ + +#ifdef USING_PCH +# include "pch.h" +#else +#ifdef HAVE_CONFIG_H +# include +#endif + +#include + +#include +#include + +#include + +#include "valuedescconnect.h" + +#include "valuedescbake.h" + +#include + +#endif + +using namespace synfig; +using namespace synfigapp; +using namespace Action; + +/* === M A C R O S ========================================================= */ + +ACTION_INIT_NO_GET_LOCAL_NAME(Action::ValueDescBake); +ACTION_SET_NAME(Action::ValueDescBake,"ValueDescBake"); +ACTION_SET_LOCAL_NAME(Action::ValueDescBake,N_("Bake")); +ACTION_SET_TASK(Action::ValueDescBake,"convert"); +ACTION_SET_CATEGORY(Action::ValueDescBake,Action::CATEGORY_VALUEDESC); +ACTION_SET_PRIORITY(Action::ValueDescBake,0); +ACTION_SET_VERSION(Action::ValueDescBake,"0.0"); +ACTION_SET_CVS_ID(Action::ValueDescBake,"$Id$"); + +/* === G L O B A L S ======================================================= */ + +/* === P R O C E D U R E S ================================================= */ + +/* === M E T H O D S ======================================================= */ + +Action::ValueDescBake::ValueDescBake() + { } + +synfig::String +Action::ValueDescBake::get_local_name()const + { return _("Bake"); } + +Action::ParamVocab +Action::ValueDescBake::get_param_vocab() +{ + ParamVocab ret(Action::CanvasSpecific::get_param_vocab()); + ret.push_back(ParamDesc("value_desc",Param::TYPE_VALUEDESC) + .set_local_name(_("ValueDesc")) + ); + return ret; +} + +bool +Action::ValueDescBake::is_candidate(const ParamList &x) +{ + if (!candidate_check(get_param_vocab(), x)) + return false; + const ValueDesc value_desc = x.find("value_desc")->second.get_value_desc(); + if (!value_desc && !value_desc.is_value_node()) + return false; + ValueNode::Handle value_node = value_desc.get_value_node(); + if (!value_node) + return false; + if (ValueNode_Const::Handle::cast_dynamic(value_node)) + return false; + if (!is_type_supported(value_node->get_type())) + return false; + return true; +} + +bool +Action::ValueDescBake::set_param(const synfig::String& name, const Action::Param ¶m) +{ + if(name=="value_desc" && param.get_type()==Param::TYPE_VALUEDESC) + { + value_desc = param.get_value_desc(); + return true; + } + return Action::CanvasSpecific::set_param(name, param); +} + +bool +Action::ValueDescBake::is_ready()const +{ + if (!value_desc) + return false; + return Action::CanvasSpecific::is_ready(); +} + +void +Action::ValueDescBake::prepare() +{ + clear(); + + const RendDesc &renddesc = get_canvas()->rend_desc(); + + ValueNode::Handle value_node = value_desc.get_value_node(); + ValueNode::Handle baked = bake( + value_node, + renddesc.get_time_start(), + renddesc.get_time_end(), + renddesc.get_frame_rate() ); + if (!baked) + throw Error(_("Unable to bake")); + + Action::Handle action(ValueDescConnect::create()); + action->set_param("canvas", get_canvas()); + action->set_param("canvas_interface", get_canvas_interface()); + action->set_param("src", baked); + action->set_param("dest", value_desc); + assert(action->is_ready()); + if(!action->is_ready()) + throw Error(Error::TYPE_NOTREADY); + + add_action_front(action); +} + + +bool +Action::ValueDescBake::is_type_supported(synfig::Type &type) +{ + try { ValueNode_Animated::create(type); } + catch(...) { return false; } + return true; +} + +ValueNode::Handle +Action::ValueDescBake::bake( + const ValueNode::Handle &value_node, + Time begin, + Time end, + float fps ) +{ + if (!value_node) + return ValueNode::Handle(); + if (!approximate_greater_lp(fps, 0.f)) + fps = end - begin; + if (end < begin) { + end = begin; + fps = 1; + } + double step = 1/(double)fps; + + Type &type = value_node->get_type(); + ValueNode_Animated::Handle animated; + try { + animated = ValueNode_Animated::create(type); + } catch(...) { + error("ValueDescBake: Cannot create ValueNode_Animated, may be unsopported value type."); + return ValueNode::Handle(); + } + + Interpolation interpolation = INTERPOLATION_CONSTANT; + if ( type == type_time + || type == type_real + || type == type_angle + || type == type_vector + || type == type_color + || type == type_gradient ) + interpolation = INTERPOLATION_CLAMPED; + + ValueBase prev_value; + int index = 0; + while(true) { + Time t = begin + step*index++; + if (index > 10000000) { + error("ValueDescBake: Reached limit of iterations."); + return ValueNode::Handle(); + } + if (approximate_greater_lp((double)t, (double)end)) + break; + if (t > end) // set exact end, for the last iteration + t = end; + + ValueBase value = (*value_node)(t); + if (prev_value != value) { + Waypoint &wp = *animated->new_waypoint(t, value); + wp.set_before(interpolation); + wp.set_after(interpolation); + prev_value = value; + } + + if (t == end) // stop the cycle (see upper comment) + break; + } + + assert(!animated->waypoint_list().empty()); + if (animated->waypoint_list().size() == 1) + return ValueNode_Const::create(prev_value); + + return animated; +} diff --git a/synfig-studio/src/synfigapp/actions/valuedescbake.h b/synfig-studio/src/synfigapp/actions/valuedescbake.h new file mode 100644 index 0000000..841aeb0 --- /dev/null +++ b/synfig-studio/src/synfigapp/actions/valuedescbake.h @@ -0,0 +1,79 @@ +/* === S Y N F I G ========================================================= */ +/*! \file valuedescbake.h +** \brief Template File +** +** $Id$ +** +** \legal +** ......... ... 2019 Ivan Mahonin +** +** This package is free software; you can redistribute it and/or +** modify it under the terms of the GNU General Public License as +** published by the Free Software Foundation; either version 2 of +** the License, or (at your option) any later version. +** +** This package is distributed in the hope that it will be useful, +** but WITHOUT ANY WARRANTY; without even the implied warranty of +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +** General Public License for more details. +** \endlegal +*/ +/* ========================================================================= */ + +/* === S T A R T =========================================================== */ + +#ifndef __SYNFIG_APP_ACTION_VALUEDESCBAKE_H +#define __SYNFIG_APP_ACTION_VALUEDESCBAKE_H + +/* === H E A D E R S ======================================================= */ + +#include +#include +#include + +/* === M A C R O S ========================================================= */ + +/* === T Y P E D E F S ===================================================== */ + +/* === C L A S S E S & S T R U C T S ======================================= */ + +namespace synfigapp { + +namespace Action { + +class ValueDescBake : + public Super +{ +private: + ValueDesc value_desc; + +public: + + ValueDescBake(); + + static ParamVocab get_param_vocab(); + static bool is_candidate(const ParamList &x); + + static bool is_type_supported(synfig::Type &type); + + static synfig::ValueNode::Handle bake( + const synfig::ValueNode::Handle &value_node, + synfig::Time begin, + synfig::Time end, + float fps ); + + virtual bool set_param(const synfig::String& name, const Param &); + virtual bool is_ready()const; + + virtual void prepare(); + + ACTION_MODULE_EXT +}; + +}; // END of namespace action +}; // END of namespace studio + +/* === E N D =============================================================== */ + +#endif +