X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;ds=sidebyside;f=synfig-core%2Ftrunk%2Fsrc%2Fmodules%2Fmod_gradient%2Fcurvegradient.cpp;h=7a489b7d00ebf6fe7c5b8ec081e859589aabfc3d;hb=45d32acc437f1ea60269d51bbbb97845cee2dfc9;hp=623074f5e282bf0bf5ac21fb4fe8493436168cee;hpb=7e269984944814ced9a7b031e0049fceff6e11d2;p=synfig.git diff --git a/synfig-core/trunk/src/modules/mod_gradient/curvegradient.cpp b/synfig-core/trunk/src/modules/mod_gradient/curvegradient.cpp index 623074f..7a489b7 100644 --- a/synfig-core/trunk/src/modules/mod_gradient/curvegradient.cpp +++ b/synfig-core/trunk/src/modules/mod_gradient/curvegradient.cpp @@ -1,11 +1,12 @@ /* === S Y N F I G ========================================================= */ /*! \file curvegradient.cpp -** \brief Template Header +** \brief Implementation of the "Curve Gradient" layer ** ** $Id$ ** ** \legal ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley +** Copyright (c) 2007 Chris Moore ** ** This package is free software; you can redistribute it and/or ** modify it under the terms of the GNU General Public License as @@ -53,8 +54,8 @@ SYNFIG_LAYER_INIT(CurveGradient); SYNFIG_LAYER_SET_NAME(CurveGradient,"curve_gradient"); -SYNFIG_LAYER_SET_LOCAL_NAME(CurveGradient,_("Curve Gradient")); -SYNFIG_LAYER_SET_CATEGORY(CurveGradient,_("Gradients")); +SYNFIG_LAYER_SET_LOCAL_NAME(CurveGradient,N_("Curve Gradient")); +SYNFIG_LAYER_SET_CATEGORY(CurveGradient,N_("Gradients")); SYNFIG_LAYER_SET_VERSION(CurveGradient,"0.0"); SYNFIG_LAYER_SET_CVS_ID(CurveGradient,"$Id$"); @@ -72,7 +73,7 @@ inline float calculate_distance(const synfig::BLinePoint& a,const synfig::BLineP #endif } -inline float calculate_distance(const std::vector& bline) +inline float calculate_distance(const std::vector& bline, bool bline_loop) { std::vector::const_iterator iter,next,ret; std::vector::const_iterator end(bline.end()); @@ -83,10 +84,10 @@ inline float calculate_distance(const std::vector& bline) next=bline.begin(); - //if(loop) - // iter=--bline.end(); - //else - iter=next++; + if(bline_loop) + iter=--bline.end(); + else + iter=next++; for(;next!=end;iter=next++) { @@ -105,7 +106,7 @@ inline float calculate_distance(const std::vector& bline) } std::vector::const_iterator -find_closest(const std::vector& bline,const Point& p,bool loop=false,float *bline_dist_ret=0) +find_closest(bool fast, const std::vector& bline,const Point& p,float& t,bool loop=false,float *bline_dist_ret=0) { std::vector::const_iterator iter,next,ret; std::vector::const_iterator end(bline.end()); @@ -118,6 +119,7 @@ find_closest(const std::vector& bline,const Point& p,bool lo float best_bline_dist(0); float best_bline_len(0); float total_bline_dist(0); + float best_pos(0); etl::hermite best_curve; if(loop) @@ -149,23 +151,43 @@ find_closest(const std::vector& bline,const Point& p,bool lo len=curve.length(); } + if (fast) + { #define POINT_CHECK(x) bp=curve(x); thisdist=(bp-p).mag_squared(); if(thisdist& bline,const Point& p,bool lo inline void CurveGradient::sync() { - curve_length_=calculate_distance(bline); + curve_length_=calculate_distance(bline, bline_loop); } CurveGradient::CurveGradient(): - offset(0,0), + origin(0,0), width(0.25), gradient(Color::black(), Color::white()), loop(false), zigzag(false), - perpendicular(false) + perpendicular(false), + fast(true) { bline.push_back(BLinePoint()); bline.push_back(BLinePoint()); @@ -215,6 +238,7 @@ CurveGradient::color_func(const Point &point_, int quality, float supersample)co Real dist; float perp_dist; + bool edge_case = false; if(bline.size()==0) return Color::alpha(); @@ -226,7 +250,8 @@ CurveGradient::color_func(const Point &point_, int quality, float supersample)co } else { - Point point(point_-offset); + float t; + Point point(point_-origin); std::vector::const_iterator iter,next; @@ -234,12 +259,12 @@ CurveGradient::color_func(const Point &point_, int quality, float supersample)co // Taking into account looping. if(perpendicular) { - next=find_closest(bline,point,bline_loop,&perp_dist); + next=find_closest(fast,bline,point,t,bline_loop,&perp_dist); perp_dist/=curve_length_; } - else + else // not perpendicular { - next=find_closest(bline,point,bline_loop); + next=find_closest(fast,bline,point,t,bline_loop); } iter=next++; @@ -262,26 +287,51 @@ CurveGradient::color_func(const Point &point_, int quality, float supersample)co else if(quality<=2)search_iterations=10; else if(quality<=4)search_iterations=8; */ - if(!perpendicular) + if(perpendicular) + { + if(quality>7) + search_iterations=4; + } + else // not perpendicular { if(quality<=6)search_iterations=7; else if(quality<=7)search_iterations=6; else if(quality<=8)search_iterations=5; else search_iterations=4; } - else - { - if(quality>7) - search_iterations=4; - } // Figure out the closest point on the curve - const float t(curve.find_closest(point,search_iterations)); - + if (fast) + t = curve.find_closest(fast, point,search_iterations); // Calculate our values - p1=curve(t); - tangent=deriv(t).norm(); + p1=curve(t); // the closest point on the curve + tangent=deriv(t).norm(); // the unit tangent at that point + + // if the point we're nearest to is at either end of the + // bline, our distance from the curve is the distance from the + // point on the curve. we need to know which side of the + // curve we're on, so find the average of the two tangents at + // this point + if (t<0.00001 || t>0.99999) + { + if (t<0.5) + { + if (iter->get_split_tangent_flag()) + { + tangent=(iter->get_tangent1().norm()+tangent).norm(); + edge_case=true; + } + } + else + { + if (next->get_split_tangent_flag()) + { + tangent=(next->get_tangent2().norm()+tangent).norm(); + edge_case=true; + } + } + } if(perpendicular) { @@ -289,23 +339,12 @@ CurveGradient::color_func(const Point &point_, int quality, float supersample)co p1-=tangent*perp_dist; tangent=-tangent.perp(); } - else - { + else // not perpendicular + // the width of the bline at the closest point on the curve thickness=(next->get_width()-iter->get_width())*t+iter->get_width(); - } - //} } - if(!perpendicular) - { - diff=tangent.perp()*thickness*width; - p1-=diff*0.5; - const Real mag(diff.inv_mag()); - supersample=supersample*mag; - diff*=mag*mag; - dist=((point_-offset)*diff-p1*diff); - } - else + if(perpendicular) { if(quality>7) { @@ -323,9 +362,26 @@ CurveGradient::color_func(const Point &point_, int quality, float supersample)co const Real mag(diff.inv_mag()); supersample=supersample*mag; diff*=mag*mag; - dist=((point_-offset)*diff-p1*diff); + dist=((point_-origin)*diff-p1*diff); } } + else // not perpendicular + { + if (edge_case) + { + diff=(p1-(point_-origin)); + if(diff*tangent.perp()<0) diff=-diff; + diff=diff.norm()*thickness*width; + } + else + diff=tangent.perp()*thickness*width; + + p1-=diff*0.5; + const Real mag(diff.inv_mag()); + supersample=supersample*mag; + diff*=mag*mag; + dist=((point_-origin)*diff-p1*diff); + } if(loop) dist-=floor(dist); @@ -362,7 +418,7 @@ CurveGradient::color_func(const Point &point_, int quality, float supersample)co } float -CurveGradient::calc_supersample(const synfig::Point &x, float pw,float ph)const +CurveGradient::calc_supersample(const synfig::Point &/*x*/, float pw,float /*ph*/)const { return pw; } @@ -384,8 +440,9 @@ CurveGradient::set_param(const String & param, const ValueBase &value) { - IMPORT(offset); + IMPORT(origin); IMPORT(perpendicular); + IMPORT(fast); if(param=="bline" && value.get_type()==ValueBase::TYPE_LIST) { @@ -400,19 +457,23 @@ CurveGradient::set_param(const String & param, const ValueBase &value) IMPORT(gradient); IMPORT(loop); IMPORT(zigzag); + + IMPORT_AS(origin,"offset"); + return Layer_Composite::set_param(param,value); } ValueBase CurveGradient::get_param(const String & param)const { - EXPORT(offset); + EXPORT(origin); EXPORT(bline); EXPORT(gradient); EXPORT(loop); EXPORT(zigzag); EXPORT(width); EXPORT(perpendicular); + EXPORT(fast); EXPORT_NAME(); EXPORT_VERSION(); @@ -425,8 +486,8 @@ CurveGradient::get_param_vocab()const { Layer::Vocab ret(Layer_Composite::get_param_vocab()); - ret.push_back(ParamDesc("offset") - .set_local_name(_("Offset"))); + ret.push_back(ParamDesc("origin") + .set_local_name(_("Origin"))); ret.push_back(ParamDesc("width") .set_is_distance() @@ -434,8 +495,8 @@ CurveGradient::get_param_vocab()const ret.push_back(ParamDesc("bline") .set_local_name(_("Vertices")) - .set_origin("offset") - .set_scalar("width") + .set_origin("origin") + .set_hint("width") .set_description(_("A list of BLine Points"))); ret.push_back(ParamDesc("gradient") @@ -446,6 +507,8 @@ CurveGradient::get_param_vocab()const .set_local_name(_("ZigZag"))); ret.push_back(ParamDesc("perpendicular") .set_local_name(_("Perpendicular"))); + ret.push_back(ParamDesc("fast") + .set_local_name(_("Fast"))); return ret; }