Removed unused members p1, p2, diff.
[synfig.git] / synfig-core / trunk / src / modules / mod_gradient / curvegradient.cpp
index a373041..89b967b 100644 (file)
@@ -1,18 +1,22 @@
-/*! ========================================================================
-** Sinfg
-** Template File
-** $Id: curvegradient.cpp,v 1.2 2005/01/13 06:48:39 darco Exp $
+/* === S Y N F I G ========================================================= */
+/*!    \file curvegradient.cpp
+**     \brief Template Header
 **
-** Copyright (c) 2002 Robert B. Quattlebaum Jr.
+**     $Id$
 **
-** This software and associated documentation
-** are CONFIDENTIAL and PROPRIETARY property of
-** the above-mentioned copyright holder.
+**     \legal
+**     Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
 **
-** You may not copy, print, publish, or in any
-** other way distribute this software without
-** a prior written agreement with
-** the copyright holder.
+**     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
 **
 ** === N O T E S ===========================================================
 **
 
 #include "curvegradient.h"
 
-#include <sinfg/string.h>
-#include <sinfg/time.h>
-#include <sinfg/context.h>
-#include <sinfg/paramdesc.h>
-#include <sinfg/renddesc.h>
-#include <sinfg/surface.h>
-#include <sinfg/value.h>
-#include <sinfg/valuenode.h>
+#include <synfig/string.h>
+#include <synfig/time.h>
+#include <synfig/context.h>
+#include <synfig/paramdesc.h>
+#include <synfig/renddesc.h>
+#include <synfig/surface.h>
+#include <synfig/value.h>
+#include <synfig/valuenode.h>
 #include <ETL/bezier>
 #include <ETL/hermite>
 #include <ETL/calculus>
 
 /* === G L O B A L S ======================================================= */
 
-SINFG_LAYER_INIT(CurveGradient);
-SINFG_LAYER_SET_NAME(CurveGradient,"curve_gradient");
-SINFG_LAYER_SET_LOCAL_NAME(CurveGradient,_("Curve Gradient"));
-SINFG_LAYER_SET_CATEGORY(CurveGradient,_("Gradients"));
-SINFG_LAYER_SET_VERSION(CurveGradient,"0.0");
-SINFG_LAYER_SET_CVS_ID(CurveGradient,"$Id: curvegradient.cpp,v 1.2 2005/01/13 06:48:39 darco Exp $");
+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_VERSION(CurveGradient,"0.0");
+SYNFIG_LAYER_SET_CVS_ID(CurveGradient,"$Id$");
 
 /* === P R O C E D U R E S ================================================= */
 
-inline float calculate_distance(const sinfg::BLinePoint& a,const sinfg::BLinePoint& b)
+inline float calculate_distance(const synfig::BLinePoint& a,const synfig::BLinePoint& b)
 {
 #if 1
        const Point& c1(a.get_vertex());
@@ -68,20 +72,22 @@ inline float calculate_distance(const sinfg::BLinePoint& a,const sinfg::BLinePoi
 #endif
 }
 
-inline float calculate_distance(const std::vector<sinfg::BLinePoint>& bline)
+inline float calculate_distance(const std::vector<synfig::BLinePoint>& bline)
 {
-       std::vector<sinfg::BLinePoint>::const_iterator iter,next,ret;
-       std::vector<sinfg::BLinePoint>::const_iterator end(bline.end());
-       
+       std::vector<synfig::BLinePoint>::const_iterator iter,next,ret;
+       std::vector<synfig::BLinePoint>::const_iterator end(bline.end());
+
        float dist(0);
-       
+
+       if (bline.empty()) return dist;
+
        next=bline.begin();
-       
+
        //if(loop)
        //      iter=--bline.end();
        //else
                iter=next++;
-       
+
        for(;next!=end;iter=next++)
        {
                // Setup the curve
@@ -99,29 +105,29 @@ inline float calculate_distance(const std::vector<sinfg::BLinePoint>& bline)
        return dist;
 }
 
-std::vector<sinfg::BLinePoint>::const_iterator
-find_closest(const std::vector<sinfg::BLinePoint>& bline,const Point& p,bool loop=false,float *bline_dist_ret=0)
+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)
 {
-       std::vector<sinfg::BLinePoint>::const_iterator iter,next,ret;
-       std::vector<sinfg::BLinePoint>::const_iterator end(bline.end());
-       
+       std::vector<synfig::BLinePoint>::const_iterator iter,next,ret;
+       std::vector<synfig::BLinePoint>::const_iterator end(bline.end());
+
        ret=bline.end();
        float dist(100000000000.0);
-       
+
        next=bline.begin();
 
        float best_bline_dist(0);
        float best_bline_len(0);
        float total_bline_dist(0);
        etl::hermite<Vector> best_curve;
-       
+
        if(loop)
                iter=--bline.end();
        else
                iter=next++;
 
        Point bp;
-       
+
        for(;next!=end;iter=next++)
        {
                // Setup the curve
@@ -134,7 +140,7 @@ find_closest(const std::vector<sinfg::BLinePoint>& bline,const Point& p,bool loo
 
                /*
                const float t(curve.find_closest(p,6,0.01,0.99));
-               bp=curve(t);if((bp-p).mag_squared()<dist) { ret=iter; dist=(bp-p).mag_squared(); ret_t=t; }             
+               bp=curve(t);if((bp-p).mag_squared()<dist) { ret=iter; dist=(bp-p).mag_squared(); ret_t=t; }
                */
 
                float thisdist(0);
@@ -144,7 +150,7 @@ find_closest(const std::vector<sinfg::BLinePoint>& bline,const Point& p,bool loo
                        //len=calculate_distance(*iter,*next);
                        len=curve.length();
                }
-               
+
 #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);
@@ -163,7 +169,7 @@ find_closest(const std::vector<sinfg::BLinePoint>& bline,const Point& p,bool loo
                *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;
        }
-       
+
        return ret;
 }
 
@@ -172,17 +178,11 @@ find_closest(const std::vector<sinfg::BLinePoint>& bline,const Point& p,bool loo
 inline void
 CurveGradient::sync()
 {
-       diff=(p2-p1);
-       const Real mag(diff.inv_mag());
-       diff*=mag*mag;
-
        curve_length_=calculate_distance(bline);
 }
 
 
 CurveGradient::CurveGradient():
-       p1(1,1),
-       p2(-1,-1),
        offset(0,0),
        width(0.25),
        gradient(Color::black(), Color::white()),
@@ -193,17 +193,17 @@ CurveGradient::CurveGradient():
        bline.push_back(BLinePoint());
        bline.push_back(BLinePoint());
        bline.push_back(BLinePoint());
-       bline[0].set_vertex(Point(0,1));        
-       bline[1].set_vertex(Point(0,-1));       
+       bline[0].set_vertex(Point(0,1));
+       bline[1].set_vertex(Point(0,-1));
        bline[2].set_vertex(Point(1,0));
-       bline[0].set_tangent(bline[1].get_vertex()-bline[2].get_vertex()*0.5f); 
-       bline[1].set_tangent(bline[2].get_vertex()-bline[0].get_vertex()*0.5f); 
-       bline[2].set_tangent(bline[0].get_vertex()-bline[1].get_vertex()*0.5f); 
-       bline[0].set_width(1.0f);       
-       bline[1].set_width(1.0f);       
-       bline[2].set_width(1.0f);       
+       bline[0].set_tangent(bline[1].get_vertex()-bline[2].get_vertex()*0.5f);
+       bline[1].set_tangent(bline[2].get_vertex()-bline[0].get_vertex()*0.5f);
+       bline[2].set_tangent(bline[0].get_vertex()-bline[1].get_vertex()*0.5f);
+       bline[0].set_width(1.0f);
+       bline[1].set_width(1.0f);
+       bline[2].set_width(1.0f);
        bline_loop=true;
-       
+
        sync();
 }
 
@@ -217,7 +217,7 @@ CurveGradient::color_func(const Point &point_, int quality, float supersample)co
        Real dist;
 
        float perp_dist;
-       
+
        if(bline.size()==0)
                return Color::alpha();
        else if(bline.size()==1)
@@ -229,9 +229,9 @@ CurveGradient::color_func(const Point &point_, int quality, float supersample)co
        else
        {
                Point point(point_-offset);
-               
-               std::vector<sinfg::BLinePoint>::const_iterator iter,next;
-               
+
+               std::vector<synfig::BLinePoint>::const_iterator iter,next;
+
                // Figure out the BLinePoints we will be using,
                // Taking into account looping.
                if(perpendicular)
@@ -245,7 +245,7 @@ CurveGradient::color_func(const Point &point_, int quality, float supersample)co
                }
                        iter=next++;
                        if(next==bline.end()) next=bline.begin();
-                       
+
                        // Setup the curve
                        etl::hermite<Vector> curve(
                                iter->get_vertex(),
@@ -253,12 +253,12 @@ CurveGradient::color_func(const Point &point_, int quality, float supersample)co
                                iter->get_tangent2(),
                                next->get_tangent1()
                        );
-                       
+
                        // Setup the derivative function
                        etl::derivative<etl::hermite<Vector> > deriv(curve);
-       
+
                        int search_iterations(7);
-                       
+
                        /*if(quality==0)search_iterations=8;
                        else if(quality<=2)search_iterations=10;
                        else if(quality<=4)search_iterations=8;
@@ -275,15 +275,15 @@ CurveGradient::color_func(const Point &point_, int quality, float supersample)co
                                if(quality>7)
                                        search_iterations=4;
                        }
-                       
+
                        // Figure out the closest point on the curve
                        const float t(curve.find_closest(point,search_iterations));
-                               
-                       
+
+
                        // Calculate our values
                        p1=curve(t);
                        tangent=deriv(t).norm();
-                       
+
                        if(perpendicular)
                        {
                                tangent*=curve_length_;
@@ -294,9 +294,9 @@ CurveGradient::color_func(const Point &point_, int quality, float supersample)co
                        {
                                thickness=(next->get_width()-iter->get_width())*t+iter->get_width();
                        }
-               //}             
+               //}
        }
-       
+
 
        if(!perpendicular)
        {
@@ -328,10 +328,10 @@ CurveGradient::color_func(const Point &point_, int quality, float supersample)co
                dist=((point_-offset)*diff-p1*diff);
                }
        }
-       
+
        if(loop)
                dist-=floor(dist);
-       
+
        if(zigzag)
        {
                dist*=2.0;
@@ -343,14 +343,20 @@ CurveGradient::color_func(const Point &point_, int quality, float supersample)co
        {
                if(dist+supersample*0.5>1.0)
                {
-                       Color pool(gradient(dist,supersample*0.5).premult_alpha()*(1.0-(dist-supersample*0.5)));
-                       pool+=gradient((dist+supersample*0.5)-1.0,supersample*0.5).premult_alpha()*((dist+supersample*0.5)-1.0);
+                       float  left(supersample*0.5-(dist-1.0));
+                       float right(supersample*0.5+(dist-1.0));
+                       Color pool(gradient(1.0-(left*0.5),left).premult_alpha()*left/supersample);
+                       if (zigzag) pool+=gradient(1.0-right*0.5,right).premult_alpha()*right/supersample;
+                       else            pool+=gradient(right*0.5,right).premult_alpha()*right/supersample;
                        return pool.demult_alpha();
                }
                if(dist-supersample*0.5<0.0)
                {
-                       Color pool(gradient(dist,supersample*0.5).premult_alpha()*(dist+supersample*0.5));
-                       pool+=gradient(1.0-(dist-supersample*0.5),supersample*0.5).premult_alpha()*(-(dist-supersample*0.5));
+                       float  left(supersample*0.5-dist);
+                       float right(supersample*0.5+dist);
+                       Color pool(gradient(right*0.5,right).premult_alpha()*right/supersample);
+                       if (zigzag) pool+=gradient(left*0.5,left).premult_alpha()*left/supersample;
+                       else            pool+=gradient(1.0-left*0.5,left).premult_alpha()*left/supersample;
                        return pool.demult_alpha();
                }
        }
@@ -358,14 +364,13 @@ CurveGradient::color_func(const Point &point_, int quality, float supersample)co
 }
 
 float
-CurveGradient::calc_supersample(const sinfg::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;
 }
 
-sinfg::Layer::Handle
-CurveGradient::hit_check(sinfg::Context context, const sinfg::Point &point)const
+synfig::Layer::Handle
+CurveGradient::hit_check(synfig::Context context, const synfig::Point &point)const
 {
        if(get_blend_method()==Color::BLEND_STRAIGHT && get_amount()>=0.5)
                return const_cast<CurveGradient*>(this);
@@ -379,22 +384,8 @@ CurveGradient::hit_check(sinfg::Context context, const sinfg::Point &point)const
 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(perpendicular);
 
@@ -411,7 +402,7 @@ CurveGradient::set_param(const String & param, const ValueBase &value)
        IMPORT(gradient);
        IMPORT(loop);
        IMPORT(zigzag);
-       return Layer_Composite::set_param(param,value); 
+       return Layer_Composite::set_param(param,value);
 }
 
 ValueBase
@@ -419,18 +410,16 @@ CurveGradient::get_param(const String & param)const
 {
        EXPORT(offset);
        EXPORT(bline);
-       EXPORT(p1);
-       EXPORT(p2);
        EXPORT(gradient);
        EXPORT(loop);
        EXPORT(zigzag);
        EXPORT(width);
        EXPORT(perpendicular);
-       
+
        EXPORT_NAME();
        EXPORT_VERSION();
-               
-       return Layer_Composite::get_param(param);       
+
+       return Layer_Composite::get_param(param);
 }
 
 Layer::Vocab
@@ -467,7 +456,7 @@ CurveGradient::get_param_vocab()const
        ret.push_back(ParamDesc("perpendicular")
                .set_local_name(_("Perpendicular"))
        );
-       
+
        return ret;
 }
 
@@ -499,7 +488,7 @@ CurveGradient::accelerated_render(Context context,Surface *surface,int quality,
                        return true;
        }
 
-               
+
        int x,y;
 
        Surface::pen pen(surface->begin());
@@ -508,7 +497,7 @@ CurveGradient::accelerated_render(Context context,Surface *surface,int quality,
        Point tl(renddesc.get_tl());
        const int w(surface->get_w());
        const int h(surface->get_h());
-       
+
        if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
        {
                for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)