Remove .gitignore do nothing is ignored.
[synfig.git] / synfig-core / trunk / src / modules / mod_gradient / curvegradient.cpp
index d296d4b..4fa704e 100644 (file)
@@ -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-2008 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
 
 /* === M A C R O S ========================================================= */
 
+#define FAKE_TANGENT_STEP 0.000001
+
 /* === G L O B A L S ======================================================= */
 
 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 +75,7 @@ inline float calculate_distance(const synfig::BLinePoint& a,const synfig::BLineP
 #endif
 }
 
-inline float calculate_distance(const std::vector<synfig::BLinePoint>& bline)
+inline float calculate_distance(const std::vector<synfig::BLinePoint>& bline, bool bline_loop)
 {
        std::vector<synfig::BLinePoint>::const_iterator iter,next,ret;
        std::vector<synfig::BLinePoint>::const_iterator end(bline.end());
@@ -83,9 +86,9 @@ inline float calculate_distance(const std::vector<synfig::BLinePoint>& bline)
 
        next=bline.begin();
 
-       //if(loop)
-       //      iter=--bline.end();
-       //else
+       if(bline_loop)
+               iter=--bline.end();
+       else
                iter=next++;
 
        for(;next!=end;iter=next++)
@@ -95,8 +98,7 @@ inline float calculate_distance(const std::vector<synfig::BLinePoint>& bline)
                        iter->get_vertex(),
                        next->get_vertex(),
                        iter->get_tangent2(),
-                       next->get_tangent1()
-               );
+                       next->get_tangent1());
 
 //             dist+=calculate_distance(*iter,*next);
                dist+=curve.length();
@@ -106,7 +108,7 @@ inline float calculate_distance(const std::vector<synfig::BLinePoint>& bline)
 }
 
 std::vector<synfig::BLinePoint>::const_iterator
-find_closest(const std::vector<synfig::BLinePoint>& bline,const Point& p,bool loop=false,float *bline_dist_ret=0)
+find_closest(bool fast, const std::vector<synfig::BLinePoint>& bline,const Point& p,float& t,bool loop=false,float *bline_dist_ret=0)
 {
        std::vector<synfig::BLinePoint>::const_iterator iter,next,ret;
        std::vector<synfig::BLinePoint>::const_iterator end(bline.end());
@@ -119,6 +121,7 @@ find_closest(const std::vector<synfig::BLinePoint>& 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<Vector> best_curve;
 
        if(loop)
@@ -135,8 +138,7 @@ find_closest(const std::vector<synfig::BLinePoint>& bline,const Point& p,bool lo
                        iter->get_vertex(),
                        next->get_vertex(),
                        iter->get_tangent2(),
-                       next->get_tangent1()
-               );
+                       next->get_tangent1());
 
                /*
                const float t(curve.find_closest(p,6,0.01,0.99));
@@ -151,23 +153,43 @@ find_closest(const std::vector<synfig::BLinePoint>& 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<dist) { ret=iter; dist=thisdist; best_bline_dist=total_bline_dist; best_bline_len=len; best_curve=curve; }
-
-               POINT_CHECK(0.0001);
-               POINT_CHECK((1.0/6.0));
-               POINT_CHECK((2.0/6.0));
-               POINT_CHECK((3.0/6.0));
-               POINT_CHECK((4.0/6.0));
-               POINT_CHECK((5.0/6.0));
-               POINT_CHECK(0.9999);
+                       POINT_CHECK(0.0001);
+                       POINT_CHECK((1.0/6.0));
+                       POINT_CHECK((2.0/6.0));
+                       POINT_CHECK((3.0/6.0));
+                       POINT_CHECK((4.0/6.0));
+                       POINT_CHECK((5.0/6.0));
+                       POINT_CHECK(0.9999);
+               }
+               else
+               {
+                       float pos = curve.find_closest(fast, p);
+                       thisdist=(curve(pos)-p).mag_squared();
+                       if(thisdist<dist)
+                       {
+                               ret=iter;
+                               dist=thisdist;
+                               best_bline_dist=total_bline_dist;
+                               best_bline_len=len;
+                               best_curve=curve;
+                               best_pos = pos;
+                       }
+               }
 
                total_bline_dist+=len;
        }
 
+       t = best_pos;
+
        if(bline_dist_ret)
        {
-               *bline_dist_ret=best_bline_dist+best_curve.find_distance(0,best_curve.find_closest(p));
-//             *bline_dist_ret=best_bline_dist+best_curve.find_closest(p)*best_bline_len;
+               //! \todo is this a redundant call to find_closest()?
+               // note bline_dist_ret is null except when 'perpendicular' is true
+               *bline_dist_ret=best_bline_dist+best_curve.find_distance(0,best_curve.find_closest(fast, p));
+//             *bline_dist_ret=best_bline_dist+best_curve.find_closest(fast, p)*best_bline_len;
        }
 
        return ret;
@@ -178,23 +200,18 @@ find_closest(const std::vector<synfig::BLinePoint>& bline,const Point& p,bool lo
 inline void
 CurveGradient::sync()
 {
-       diff=(p2-p1);
-       const Real mag(diff.inv_mag());
-       diff*=mag*mag;
-
-       curve_length_=calculate_distance(bline);
+       curve_length_=calculate_distance(bline, bline_loop);
 }
 
 
 CurveGradient::CurveGradient():
-       p1(1,1),
-       p2(-1,-1),
-       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());
@@ -223,6 +240,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();
@@ -234,7 +252,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<synfig::BLinePoint>::const_iterator iter,next;
 
@@ -242,78 +261,133 @@ 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++;
-                       if(next==bline.end()) next=bline.begin();
-
-                       // Setup the curve
-                       etl::hermite<Vector> curve(
-                               iter->get_vertex(),
-                               next->get_vertex(),
-                               iter->get_tangent2(),
-                               next->get_tangent1()
+
+               iter=next++;
+               if(next==bline.end()) next=bline.begin();
+
+               // Setup the curve
+               etl::hermite<Vector> curve(
+                       iter->get_vertex(),
+                       next->get_vertex(),
+                       iter->get_tangent2(),
+                       next->get_tangent1()
                        );
 
-                       // Setup the derivative function
-                       etl::derivative<etl::hermite<Vector> > deriv(curve);
+               // Setup the derivative function
+               etl::derivative<etl::hermite<Vector> > deriv(curve);
 
-                       int search_iterations(7);
+               int search_iterations(7);
 
-                       /*if(quality==0)search_iterations=8;
-                       else if(quality<=2)search_iterations=10;
-                       else if(quality<=4)search_iterations=8;
-                       */
-                       if(!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;
-                       }
+               /*if(quality==0)search_iterations=8;
+                 else if(quality<=2)search_iterations=10;
+                 else if(quality<=4)search_iterations=8;
+               */
+               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;
+               }
 
-                       // Figure out the closest point on the curve
-                       const float t(curve.find_closest(point,search_iterations));
+               // Figure out the closest point on the curve
+               if (fast)
+                       t = curve.find_closest(fast, point,search_iterations);
 
+               // Calculate our values
+               p1=curve(t);                     // the closest point on the curve
+               tangent=deriv(t);                // the tangent at that point
 
-                       // Calculate our values
-                       p1=curve(t);
-                       tangent=deriv(t).norm();
+               // 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)
+               {
+                       bool zero_tangent = (tangent[0] == 0 && tangent[1] == 0);
 
-                       if(perpendicular)
+                       if (t<0.5)
                        {
-                               tangent*=curve_length_;
-                               p1-=tangent*perp_dist;
-                               tangent=-tangent.perp();
+                               if (iter->get_split_tangent_flag() || zero_tangent)
+                               {
+                                       // fake the current tangent if we need to
+                                       if (zero_tangent) tangent = curve(FAKE_TANGENT_STEP) - curve(0);
+
+                                       // calculate the other tangent
+                                       Vector other_tangent(iter->get_tangent1());
+                                       if (other_tangent[0] == 0 && other_tangent[1] == 0)
+                                       {
+                                               // find the previous blinepoint
+                                               std::vector<synfig::BLinePoint>::const_iterator prev;
+                                               if (iter != bline.begin()) (prev = iter)--;
+                                               else if (loop) (prev = bline.end())--;
+                                               else prev = iter;
+
+                                               etl::hermite<Vector> other_curve(prev->get_vertex(), iter->get_vertex(), prev->get_tangent2(), iter->get_tangent1());
+                                               other_tangent = other_curve(1) - other_curve(1-FAKE_TANGENT_STEP);
+                                       }
+
+                                       // normalise and sum the two tangents
+                                       tangent=(other_tangent.norm()+tangent.norm());
+                                       edge_case=true;
+                               }
                        }
                        else
                        {
-                               thickness=(next->get_width()-iter->get_width())*t+iter->get_width();
+                               if (next->get_split_tangent_flag() || zero_tangent)
+                               {
+                                       // fake the current tangent if we need to
+                                       if (zero_tangent) tangent = curve(1) - curve(1-FAKE_TANGENT_STEP);
+
+                                       // calculate the other tangent
+                                       Vector other_tangent(next->get_tangent2());
+                                       if (other_tangent[0] == 0 && other_tangent[1] == 0)
+                                       {
+                                               // find the next blinepoint
+                                               std::vector<synfig::BLinePoint>::const_iterator next2(next);
+                                               if (++next2 == bline.end())
+                                               {
+                                                       if (loop) next2 = bline.begin();
+                                                       else next2 = next;
+                                               }
+
+                                               etl::hermite<Vector> other_curve(next->get_vertex(), next2->get_vertex(), next->get_tangent2(), next2->get_tangent1());
+                                               other_tangent = other_curve(FAKE_TANGENT_STEP) - other_curve(0);
+                                       }
+
+                                       // normalise and sum the two tangents
+                                       tangent=(other_tangent.norm()+tangent.norm());
+                                       edge_case=true;
+                               }
                        }
-               //}
-       }
-
+               }
+               tangent = tangent.norm();
 
-       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);
+               if(perpendicular)
+               {
+                       tangent*=curve_length_;
+                       p1-=tangent*perp_dist;
+                       tangent=-tangent.perp();
+               }
+               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();
        }
-       else
+
+       if(perpendicular)
        {
                if(quality>7)
                {
@@ -326,13 +400,30 @@ CurveGradient::color_func(const Point &point_, int quality, float supersample)co
                }
                else
                {
-               diff=tangent.perp();
-               //p1-=diff*0.5;
+                       diff=tangent.perp();
+                       //p1-=diff*0.5;
+                       const Real mag(diff.inv_mag());
+                       supersample=supersample*mag;
+                       diff*=mag*mag;
+                       dist=(point_-origin - 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_-offset)*diff-p1*diff);
-               }
+               dist=(point_-origin - p1)*diff;
        }
 
        if(loop)
@@ -370,9 +461,8 @@ 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/(p2-p1).mag();
        return pw;
 }
 
@@ -391,24 +481,11 @@ CurveGradient::hit_check(synfig::Context context, const synfig::Point &point)con
 bool
 CurveGradient::set_param(const String & param, const ValueBase &value)
 {
-       if(param=="p1" && value.same_as(p1))
-       {
-               p1=value.get(p1);
-               sync();
-               return true;
-       }
-       if(param=="p2" && value.same_as(p2))
-       {
-               p2=value.get(p2);
-               sync();
-               return true;
-       }
-       //IMPORT(p1);
-       //IMPORT(p2);
 
 
-       IMPORT(offset);
+       IMPORT(origin);
        IMPORT(perpendicular);
+       IMPORT(fast);
 
        if(param=="bline" && value.get_type()==ValueBase::TYPE_LIST)
        {
@@ -423,21 +500,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(p1);
-       EXPORT(p2);
        EXPORT(gradient);
        EXPORT(loop);
        EXPORT(zigzag);
        EXPORT(width);
        EXPORT(perpendicular);
+       EXPORT(fast);
 
        EXPORT_NAME();
        EXPORT_VERSION();
@@ -450,35 +529,29 @@ 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()
-               .set_local_name(_("Width"))
-       );
+                                 .set_is_distance()
+                                 .set_local_name(_("Width")));
 
        ret.push_back(ParamDesc("bline")
-               .set_local_name(_("Vertices"))
-               .set_origin("offset")
-               .set_scalar("width")
-               .set_description(_("A list of BLine Points"))
-       );
-
+                                 .set_local_name(_("Vertices"))
+                                 .set_origin("origin")
+                                 .set_hint("width")
+                                 .set_description(_("A list of BLine Points")));
 
        ret.push_back(ParamDesc("gradient")
-               .set_local_name(_("Gradient"))
-       );
+                                 .set_local_name(_("Gradient")));
        ret.push_back(ParamDesc("loop")
-               .set_local_name(_("Loop"))
-       );
+                                 .set_local_name(_("Loop")));
        ret.push_back(ParamDesc("zigzag")
-               .set_local_name(_("ZigZag"))
-       );
+                                 .set_local_name(_("ZigZag")));
        ret.push_back(ParamDesc("perpendicular")
-               .set_local_name(_("Perpendicular"))
-       );
+                                 .set_local_name(_("Perpendicular")));
+       ret.push_back(ParamDesc("fast")
+                                 .set_local_name(_("Fast")));
 
        return ret;
 }