Add my copyright to files I've modified.
[synfig.git] / synfig-core / trunk / src / modules / mod_gradient / curvegradient.cpp
index 2dde697..2fb682f 100644 (file)
@@ -6,6 +6,7 @@
 **
 **     \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
@@ -72,7 +73,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,10 +84,10 @@ inline float calculate_distance(const std::vector<synfig::BLinePoint>& 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++)
        {
@@ -197,7 +198,7 @@ find_closest(bool fast, const std::vector<synfig::BLinePoint>& bline,const Point
 inline void
 CurveGradient::sync()
 {
-       curve_length_=calculate_distance(bline);
+       curve_length_=calculate_distance(bline, bline_loop);
 }
 
 
@@ -237,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();
@@ -260,7 +262,7 @@ CurveGradient::color_func(const Point &point_, int quality, float supersample)co
                        next=find_closest(fast,bline,point,t,bline_loop,&perp_dist);
                        perp_dist/=curve_length_;
                }
-               else
+               else                                    // not perpendicular
                {
                        next=find_closest(fast,bline,point,t,bline_loop);
                }
@@ -290,7 +292,7 @@ CurveGradient::color_func(const Point &point_, int quality, float supersample)co
                        if(quality>7)
                                search_iterations=4;
                }
-               else
+               else                                    // not perpendicular
                {
                        if(quality<=6)search_iterations=7;
                        else if(quality<=7)search_iterations=6;
@@ -302,18 +304,42 @@ CurveGradient::color_func(const Point &point_, int quality, float supersample)co
                if (fast)
                        t = curve.find_closest(fast, point,search_iterations);
 
-
                // Calculate our values
-               p1=curve(t);                    // the closest point on the curve
+               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)
                {
                        tangent*=curve_length_;
                        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();
        }
@@ -339,9 +365,17 @@ CurveGradient::color_func(const Point &point_, int quality, float supersample)co
                        dist=((point_-offset)*diff-p1*diff);
                }
        }
-       else
+       else                                            // not perpendicular
        {
-               diff=tangent.perp()*thickness*width;
+               if (edge_case)
+               {
+                       diff=(p1-(point_-offset));
+                       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;