From 66bff9875e5cda32a5472a78f973254bc0963849 Mon Sep 17 00:00:00 2001 From: dooglus Date: Thu, 18 Dec 2008 21:59:12 +0000 Subject: [PATCH] Add a 'loop' parameter to the Random valuenode to cause the randomness to repeat after a given time. git-svn-id: https://synfig.svn.sourceforge.net/svnroot/synfig@2315 1f10aa63-cdf2-0310-b900-c93c546f37ac --- .../trunk/src/modules/mod_noise/random_noise.cpp | 133 ++++++++++++--------- .../trunk/src/modules/mod_noise/random_noise.h | 2 +- .../src/modules/mod_noise/valuenode_random.cpp | 32 +++-- .../trunk/src/modules/mod_noise/valuenode_random.h | 1 + synfig-core/trunk/src/synfig/canvas.h | 6 +- synfig-core/trunk/src/synfig/loadcanvas.cpp | 6 + 6 files changed, 109 insertions(+), 71 deletions(-) diff --git a/synfig-core/trunk/src/modules/mod_noise/random_noise.cpp b/synfig-core/trunk/src/modules/mod_noise/random_noise.cpp index 3d0e58c..344dff9 100644 --- a/synfig-core/trunk/src/modules/mod_noise/random_noise.cpp +++ b/synfig-core/trunk/src/modules/mod_noise/random_noise.cpp @@ -30,6 +30,7 @@ # include #endif +#include #include "random_noise.h" #include #include @@ -71,11 +72,29 @@ RandomNoise::operator()(const int salt,const int x,const int y,const int t)const } float -RandomNoise::operator()(SmoothType smooth,int subseed,float xf,float yf,float tf)const +RandomNoise::operator()(SmoothType smooth,int subseed,float xf,float yf,float tf,int loop)const { int x((int)floor(xf)); int y((int)floor(yf)); int t((int)floor(tf)); + int t_1, t0, t1, t2; + + if (loop) + { + t0 = t % loop; if (t0 < 0 ) t0 += loop; + t_1 = t0 - 1; if (t_1 < 0 ) t_1 += loop; + t1 = t0 + 1; if (t1 >= loop) t1 -= loop; + t2 = t1 + 1; if (t2 >= loop) t2 -= loop; + } + else + { + t0 = t; + t_1 = t - 1; + t1 = t + 1; + t2 = t + 2; + } + + // synfig::info("%s:%d tf %.2f loop %d fraction %.2f ( -1,0,1,2 : %2d %2d %2d %2d)", __FILE__, __LINE__, tf, loop, tf-t, t_1, t0, t1, t2); switch(smooth) { @@ -90,7 +109,7 @@ RandomNoise::operator()(SmoothType smooth,int subseed,float xf,float yf,float tf //precalculate indices (all clamped) and offset const int xa[] = {x-1,x,x+1,x+2}; const int ya[] = {y-1,y,y+1,y+2}; - const int ta[] = {t-1,t,t+1,t+2}; + const int ta[] = {t_1,t0,t1,t2}; const float dx(xf-x); const float dy(yf-y); @@ -140,14 +159,14 @@ RandomNoise::operator()(SmoothType smooth,int subseed,float xf,float yf,float tf case SMOOTH_FAST_SPLINE: // Fast Spline (non-animated) { -#define P(x) (((x)>0)?((x)*(x)*(x)):0.0f) -#define R(x) ( P(x+2) - 4.0f*P(x+1) + 6.0f*P(x) - 4.0f*P(x-1) )*(1.0f/6.0f) -#define F(i,j) ((*this)(subseed,i+x,j+y)*(R((i)-a)*R(b-(j)))) -#define FT(i,j,k) ((*this)(subseed,i+x,j+y,k+t)*(R((i)-a)*R(b-(j))*R((k)-c))) -#define Z(i,j) ret+=F(i,j) -#define ZT(i,j,k) ret+=FT(i,j,k) -#define X(i,j) // placeholder... To make box more symmetric -#define XT(i,j,k) // placeholder... To make box more symmetric +#define P(x) (((x)>0)?((x)*(x)*(x)):0.0f) +#define R(x) ( P(x+2) - 4.0f*P(x+1) + 6.0f*P(x) - 4.0f*P(x-1) )*(1.0f/6.0f) +#define F(i,j) ((*this)(subseed,i+x,j+y)*(R((i)-a)*R(b-(j)))) +#define FT(i,j,k,l) ((*this)(subseed,i+x,j+y,l)*(R((i)-a)*R(b-(j))*R((k)-c))) +#define Z(i,j) ret+=F(i,j) +#define ZT(i,j,k,l) ret+=FT(i,j,k,l) +#define X(i,j) // placeholder... To make box more symmetric +#define XT(i,j,k,l) // placeholder... To make box more symmetric float a(xf-x), b(yf-y); @@ -166,26 +185,26 @@ RandomNoise::operator()(SmoothType smooth,int subseed,float xf,float yf,float tf float a(xf-x), b(yf-y), c(tf-t); // Interpolate - float ret(FT(0,0,0)); - ZT(-1,-1,-1); ZT(-1, 0,-1);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float tf float d=1.0-b; int x2=x+1,y2=y+1; return - (*this)(subseed,x,y,t)*(c*d)+ - (*this)(subseed,x2,y,t)*(a*d)+ - (*this)(subseed,x,y2,t)*(c*b)+ - (*this)(subseed,x2,y2,t)*(a*b); + (*this)(subseed,x,y,t0)*(c*d)+ + (*this)(subseed,x2,y,t0)*(a*d)+ + (*this)(subseed,x,y2,t0)*(c*b)+ + (*this)(subseed,x2,y2,t0)*(a*b); } else { @@ -246,17 +265,17 @@ RandomNoise::operator()(SmoothType smooth,int subseed,float xf,float yf,float tf float e=1.0-b; float f=1.0-c; - int x2=x+1,y2=y+1,t2=t+1; + int x2=x+1,y2=y+1; return - (*this)(subseed,x,y,t)*(d*e*f)+ - (*this)(subseed,x2,y,t)*(a*e*f)+ - (*this)(subseed,x,y2,t)*(d*b*f)+ - (*this)(subseed,x2,y2,t)*(a*b*f)+ - (*this)(subseed,x,y,t2)*(d*e*c)+ - (*this)(subseed,x2,y,t2)*(a*e*c)+ - (*this)(subseed,x,y2,t2)*(d*b*c)+ - (*this)(subseed,x2,y2,t2)*(a*b*c); + (*this)(subseed,x,y,t0)*(d*e*f)+ + (*this)(subseed,x2,y,t0)*(a*e*f)+ + (*this)(subseed,x,y2,t0)*(d*b*f)+ + (*this)(subseed,x2,y2,t0)*(a*b*f)+ + (*this)(subseed,x,y,t1)*(d*e*c)+ + (*this)(subseed,x2,y,t1)*(a*e*c)+ + (*this)(subseed,x,y2,t1)*(d*b*c)+ + (*this)(subseed,x2,y2,t1)*(a*b*c); } case SMOOTH_LINEAR: if((float)t==tf) @@ -269,10 +288,10 @@ RandomNoise::operator()(SmoothType smooth,int subseed,float xf,float yf,float tf float d=1.0-b; int x2=x+1,y2=y+1; return - (*this)(subseed,x,y,t)*(c*d)+ - (*this)(subseed,x2,y,t)*(a*d)+ - (*this)(subseed,x,y2,t)*(c*b)+ - (*this)(subseed,x2,y2,t)*(a*b); + (*this)(subseed,x,y,t0)*(c*d)+ + (*this)(subseed,x2,y,t0)*(a*d)+ + (*this)(subseed,x,y2,t0)*(c*b)+ + (*this)(subseed,x2,y2,t0)*(a*b); } else { @@ -285,20 +304,20 @@ RandomNoise::operator()(SmoothType smooth,int subseed,float xf,float yf,float tf float e=1.0-b; float f=1.0-c; - int x2=x+1,y2=y+1,t2=t+1; + int x2=x+1,y2=y+1; return - (*this)(subseed,x,y,t)*(d*e*f)+ - (*this)(subseed,x2,y,t)*(a*e*f)+ - (*this)(subseed,x,y2,t)*(d*b*f)+ - (*this)(subseed,x2,y2,t)*(a*b*f)+ - (*this)(subseed,x,y,t2)*(d*e*c)+ - (*this)(subseed,x2,y,t2)*(a*e*c)+ - (*this)(subseed,x,y2,t2)*(d*b*c)+ - (*this)(subseed,x2,y2,t2)*(a*b*c); + (*this)(subseed,x,y,t0)*(d*e*f)+ + (*this)(subseed,x2,y,t0)*(a*e*f)+ + (*this)(subseed,x,y2,t0)*(d*b*f)+ + (*this)(subseed,x2,y2,t0)*(a*b*f)+ + (*this)(subseed,x,y,t1)*(d*e*c)+ + (*this)(subseed,x2,y,t1)*(a*e*c)+ + (*this)(subseed,x,y2,t1)*(d*b*c)+ + (*this)(subseed,x2,y2,t1)*(a*b*c); } default: case SMOOTH_DEFAULT: - return (*this)(subseed,x,y,t); + return (*this)(subseed,x,y,t0); } } diff --git a/synfig-core/trunk/src/modules/mod_noise/random_noise.h b/synfig-core/trunk/src/modules/mod_noise/random_noise.h index a95dc60..9a06e94 100644 --- a/synfig-core/trunk/src/modules/mod_noise/random_noise.h +++ b/synfig-core/trunk/src/modules/mod_noise/random_noise.h @@ -55,7 +55,7 @@ public: }; float operator()(int subseed,int x,int y=0, int t=0)const; - float operator()(SmoothType smooth,int subseed,float x,float y=0, float t=0)const; + float operator()(SmoothType smooth,int subseed,float x,float y=0,float t=0,int loop=0)const; }; /* === E N D =============================================================== */ diff --git a/synfig-core/trunk/src/modules/mod_noise/valuenode_random.cpp b/synfig-core/trunk/src/modules/mod_noise/valuenode_random.cpp index a4436fd..1ae09a4 100644 --- a/synfig-core/trunk/src/modules/mod_noise/valuenode_random.cpp +++ b/synfig-core/trunk/src/modules/mod_noise/valuenode_random.cpp @@ -61,6 +61,7 @@ ValueNode_Random::ValueNode_Random(const ValueBase &value): set_link("seed",ValueNode_Const::create(random.get_seed())); set_link("speed",ValueNode_Const::create(Real(1))); set_link("smooth",ValueNode_Const::create(int(RandomNoise::SMOOTH_CUBIC))); + set_link("loop",ValueNode_Const::create(Real(0))); switch(get_type()) { @@ -117,7 +118,9 @@ ValueNode_Random::operator()(Time t)const Real radius = (*radius_)(t).get(Real()); int seed = (*seed_)(t).get(int()); int smooth = (*smooth_)(t).get(int()); - float speed = (*speed_ )(t).get(Real()) * t; + float speed = (*speed_ )(t).get(Real()); + int loop = int((((*loop_ )(t).get(Real())) * speed) + 0.5); + speed *= t; random.set_seed(seed); @@ -125,34 +128,34 @@ ValueNode_Random::operator()(Time t)const { case ValueBase::TYPE_ANGLE: return ((*link_)(t).get( Angle()) + - Angle::deg(random(Smooth(smooth), 0, 0, 0, speed) * radius)); + Angle::deg(random(Smooth(smooth), 0, 0, 0, speed, loop) * radius)); case ValueBase::TYPE_BOOL: return round_to_int((*link_)(t).get( bool()) + - random(Smooth(smooth), 0, 0, 0, speed) * radius) > 0; + random(Smooth(smooth), 0, 0, 0, speed, loop) * radius) > 0; case ValueBase::TYPE_COLOR: return (((*link_)(t).get( Color()) + - Color(random(Smooth(smooth), 0, 0, 0, speed), - random(Smooth(smooth), 1, 0, 0, speed), - random(Smooth(smooth), 2, 0, 0, speed), 0) * radius).clamped()); + Color(random(Smooth(smooth), 0, 0, 0, speed, loop), + random(Smooth(smooth), 1, 0, 0, speed, loop), + random(Smooth(smooth), 2, 0, 0, speed, loop), 0) * radius).clamped()); case ValueBase::TYPE_INTEGER: return round_to_int((*link_)(t).get( int()) + - random(Smooth(smooth), 0, 0, 0, speed) * radius); + random(Smooth(smooth), 0, 0, 0, speed, loop) * radius); case ValueBase::TYPE_REAL: return ((*link_)(t).get( Real()) + - random(Smooth(smooth), 0, 0, 0, speed) * radius); + random(Smooth(smooth), 0, 0, 0, speed, loop) * radius); case ValueBase::TYPE_TIME: return ((*link_)(t).get( Time()) + - random(Smooth(smooth), 0, 0, 0, speed) * radius); + random(Smooth(smooth), 0, 0, 0, speed, loop) * radius); case ValueBase::TYPE_VECTOR: { - float length(random(Smooth(smooth), 0, 0, 0, speed) * radius); - Angle::rad angle(random(Smooth(smooth), 1, 0, 0, speed) * PI); + float length(random(Smooth(smooth), 0, 0, 0, speed, loop) * radius); + Angle::rad angle(random(Smooth(smooth), 1, 0, 0, speed, loop) * PI); return ((*link_)(t).get(Vector()) + Vector(Angle::cos(angle).get(), Angle::sin(angle).get()) * length); @@ -191,6 +194,7 @@ ValueNode_Random::set_link_vfunc(int i,ValueNode::Handle value) case 2: CHECK_TYPE_AND_SET_VALUE(seed_, ValueBase::TYPE_INTEGER); case 3: CHECK_TYPE_AND_SET_VALUE(speed_, ValueBase::TYPE_REAL); case 4: CHECK_TYPE_AND_SET_VALUE(smooth_, ValueBase::TYPE_INTEGER); + case 5: CHECK_TYPE_AND_SET_VALUE(loop_, ValueBase::TYPE_REAL); } return false; } @@ -207,6 +211,7 @@ ValueNode_Random::get_link_vfunc(int i)const case 2: return seed_; case 3: return speed_; case 4: return smooth_; + case 5: return loop_; } return 0; } @@ -214,7 +219,7 @@ ValueNode_Random::get_link_vfunc(int i)const int ValueNode_Random::link_count()const { - return 5; + return 6; } String @@ -229,6 +234,7 @@ ValueNode_Random::link_name(int i)const case 2: return "seed"; case 3: return "speed"; case 4: return "smooth"; + case 5: return "loop"; } return String(); } @@ -245,6 +251,7 @@ ValueNode_Random::link_local_name(int i)const case 2: return _("Seed"); case 3: return _("Animation Speed"); case 4: return _("Interpolation"); + case 5: return _("Loop Time"); } return String(); } @@ -257,6 +264,7 @@ ValueNode_Random::get_link_index_from_name(const String &name)const if(name=="seed" ) return 2; if(name=="speed" ) return 3; if(name=="smooth") return 4; + if(name=="loop" ) return 5; throw Exception::BadLinkName(name); } diff --git a/synfig-core/trunk/src/modules/mod_noise/valuenode_random.h b/synfig-core/trunk/src/modules/mod_noise/valuenode_random.h index f29d0e4..beafcf2 100644 --- a/synfig-core/trunk/src/modules/mod_noise/valuenode_random.h +++ b/synfig-core/trunk/src/modules/mod_noise/valuenode_random.h @@ -44,6 +44,7 @@ class ValueNode_Random : public LinkableValueNode ValueNode::RHandle seed_; ValueNode::RHandle speed_; ValueNode::RHandle smooth_; + ValueNode::RHandle loop_; mutable RandomNoise random; RandomNoise::SmoothType smooth; diff --git a/synfig-core/trunk/src/synfig/canvas.h b/synfig-core/trunk/src/synfig/canvas.h index d2906be..e309948 100644 --- a/synfig-core/trunk/src/synfig/canvas.h +++ b/synfig-core/trunk/src/synfig/canvas.h @@ -97,9 +97,13 @@ * Added "scale" link to the "BLine Width" ValueNode in svn r1872. * * Added "loop" link to the "Gradient Color" ValueNode in svn r1901. + * + * 0.7: svn r2??? + * + * Added "loop" link to the "Random" ValueNode in svn r????. */ -#define CURRENT_CANVAS_VERSION "0.6" +#define CURRENT_CANVAS_VERSION "0.7" /* === T Y P E D E F S ===================================================== */ diff --git a/synfig-core/trunk/src/synfig/loadcanvas.cpp b/synfig-core/trunk/src/synfig/loadcanvas.cpp index 40260b7..56ae540 100644 --- a/synfig-core/trunk/src/synfig/loadcanvas.cpp +++ b/synfig-core/trunk/src/synfig/loadcanvas.cpp @@ -1319,6 +1319,12 @@ CanvasParser::parse_linkable_value_node(xmlpp::Element *element,Canvas::Handle c value_node->link_name(i) == "loop") continue; + // 'loop' was added while canvas version 0.6 was in use; the 'random' node was added back when 0.1 was in use + if ((version == "0.1" || version == "0.2" || version == "0.3" || version == "0.4" || version == "0.5" || version == "0.6") && + element->get_name() == "random" && + value_node->link_name(i) == "loop") + continue; + error(element, strprintf(_("<%s> is missing link %d (%s)"), element->get_name().c_str(), i, -- 2.7.4