Write "operator()" instead of "operator ()", "operator&" instead of "operator &"...
[synfig.git] / synfig-studio / trunk / src / synfigapp / blineconvert.cpp
index 697e428..957e667 100644 (file)
@@ -2,10 +2,11 @@
 /*!    \file blineconvert.cpp
 **     \brief Template File
 **
-**     $Id: blineconvert.cpp,v 1.1.1.1 2005/01/07 03:34:37 darco Exp $
+**     $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
@@ -39,7 +40,7 @@
 #include <synfig/general.h>
 #include <cassert>
 
-
+#include "general.h"
 
 #endif
 
@@ -66,127 +67,114 @@ using namespace synfig;
 template < class T >
 inline void FivePointdt(T &df, const T &f1, const T &f2, const T &f3, const T &f4, const T &f5, int bias)
 {
-       if(bias == 0)
-       {
-               //middle
+       if (bias == 0)                          // middle
                df = (f1 - f2*8 + f4*8 - f5)*(1/12.0f);
-       }else if(bias < 0)
-       {
-               //left
+       else if (bias < 0)                      // left
                df = (-f1*25 + f2*48 - f3*36 + f4*16 - f5*3)*(1/12.0f);
-       }else
-       {
-               //right
+       else                                            // right
                df = (f1*3 - f2*16 + f3*36 - f4*48 + f5*25)*(1/12.0f);
-       }
 }
 
 template < class T >
 inline void ThreePointdt(T &df, const T &f1, const T &f2, const T &f3, int bias)
 {
-       if(bias == 0)
-       {
-               //middle
+       if (bias == 0)                          // middle
                df = (-f1 + f3)*(1/2.0f);
-       }else if(bias < 0)
-       {
-               //left
+       else if (bias < 0)                      // left
                df = (-f1*3 + f2*4 - f3)*(1/2.0f);
-       }else
-       {
-               //right
+       else                                            // right
                df = (f1 - f2*4 + f3*3)*(1/2.0f);
-       }
 }
 
-template < class T >
-inline void ThreePointddt(T &df, const T &f1, const T &f2, const T &f3, int bias)
-{
-       //a 3 point approximation pretends to have constant acceleration, so only one algorithm needed for left, middle, or right
-       df = (f1 -f2*2 + f3)*(1/2.0f);
-}
-
-// WARNING -- totaly broken
-template < class T >
-inline void FivePointddt(T &df, const T &f1, const T &f2, const T &f3, int bias)
-{
-       if(bias == 0)
-       {
-               assert(0); // !?
-               //middle
-               //df = (- f1 + f2*16 - f3*30 +  f4*16 - f5)*(1/12.0f);
-       }/*else if(bias < 0)
-       {
-               //left
-               df = (f1*7 - f2*26*4 + f3*19*6 - f4*14*4 + f5*11)*(1/12.0f);
-       }else
-       {
-               //right
-               df = (f1*3 - f2*16 + f3*36 - f4*48 + f5*25)*(1/12.0f);
-       }*/
-       //side ones don't work, use 3 point
-}
-
-//implement an arbitrary derivative
-//dumb algorithm
-template < class T >
-void DerivativeApprox(T &df, const T f[], const Real t[], int npoints, int indexval)
-{
-       /*
-       Lj(x) = PI_i!=j (x - xi) / PI_i!=j (xj - xi)
-
-       so Lj'(x) = SUM_k PI_i!=j|k (x - xi) / PI_i!=j (xj - xi)
-       */
-
-       unsigned int i,j,k,i0,i1;
-
-       Real Lpj,mult,div,tj;
-       Real tval = t[indexval];
-
-       //sum k
-       for(j=0;j<npoints;++j)
-       {
-               Lpj = 0;
-               div = 1;
-               tj = t[j];
-
-               for(k=0;k<npoints;++k)
-               {
-                       if(k != j) //because there is no summand for k == j, since that term is missing from the original equation
-                       {
-                               //summation for k
-                               for(i=0;i<npoints;++i)
-                               {
-                                       if(i != k)
-                                       {
-                                               mult *= tval - t[i];
-                                       }
-                               }
-
-                               Lpj += mult; //add into the summation
-
-                               //since the ks follow the exact patern we need for the divisor (use that too)
-                               div *= tj - t[k];
-                       }
-               }
-
-               //get the actual coefficient
-               Lpj /= div;
-
-               //add it in to the equation
-               df += f[j]*Lpj;
-       }
-}
+// template < class T >
+// inline void ThreePointddt(T &df, const T &f1, const T &f2, const T &f3, int bias)
+// {
+//     // a 3 point approximation pretends to have constant acceleration,
+//     // so only one algorithm needed for left, middle, or right
+//     df = (f1 -f2*2 + f3)*(1/2.0f);
+// }
+// 
+// // WARNING -- totally broken
+// template < class T >
+// inline void FivePointddt(T &df, const T &f1, const T &f2, const T &f3, int bias)
+// {
+//     if(bias == 0)
+//     {
+//             assert(0); // !?
+//             //middle
+//             //df = (- f1 + f2*16 - f3*30 +  f4*16 - f5)*(1/12.0f);
+//     }/*else if(bias < 0)
+//     {
+//             //left
+//             df = (f1*7 - f2*26*4 + f3*19*6 - f4*14*4 + f5*11)*(1/12.0f);
+//     }else
+//     {
+//             //right
+//             df = (f1*3 - f2*16 + f3*36 - f4*48 + f5*25)*(1/12.0f);
+//     }*/
+//     //side ones don't work, use 3 point
+// }
+// 
+// //implement an arbitrary derivative
+// //dumb algorithm
+// template < class T >
+// void DerivativeApprox(T &df, const T f[], const Real t[], int npoints, int indexval)
+// {
+//     /*
+//     Lj(x) = PI_i!=j (x - xi) / PI_i!=j (xj - xi)
+// 
+//     so Lj'(x) = SUM_k PI_i!=j|k (x - xi) / PI_i!=j (xj - xi)
+//     */
+// 
+//     unsigned int i,j,k,i0,i1;
+// 
+//     Real Lpj,mult,div,tj;
+//     Real tval = t[indexval];
+// 
+//     //sum k
+//     for(j=0;j<npoints;++j)
+//     {
+//             Lpj = 0;
+//             div = 1;
+//             tj = t[j];
+// 
+//             for(k=0;k<npoints;++k)
+//             {
+//                     if(k != j) //because there is no summand for k == j, since that term is missing from the original equation
+//                     {
+//                             //summation for k
+//                             for(i=0;i<npoints;++i)
+//                             {
+//                                     if(i != k)
+//                                     {
+//                                             mult *= tval - t[i];
+//                                     }
+//                             }
+// 
+//                             Lpj += mult; //add into the summation
+// 
+//                             //since the ks follow the exact pattern we need for the divisor (use that too)
+//                             div *= tj - t[k];
+//                     }
+//             }
+// 
+//             //get the actual coefficient
+//             Lpj /= div;
+// 
+//             //add it in to the equation
+//             df += f[j]*Lpj;
+//     }
+// }
 
 //END numerical derivatives
 
-template < class T >
-inline int sign(T f, T tol)
-{
-       if(f < -tol) return -1;
-       if(f > tol) return 1;
-       return 0;
-}
+// template < class T >
+// inline int sign(T f, T tol)
+// {
+//     if(f < -tol) return -1;
+//     if(f > tol) return 1;
+//     return 0;
+// }
 
 void GetFirstDerivatives(const std::vector<synfig::Point> &f, unsigned int left, unsigned int right, char *out, unsigned int dfstride)
 {
@@ -194,7 +182,7 @@ void GetFirstDerivatives(const std::vector<synfig::Point> &f, unsigned int left,
 
        if(right - left < 2)
                return;
-       else if(right - left < 3)
+       else if(right - left == 2)
        {
                synfig::Vector v = f[left+1] - f[left];
 
@@ -208,13 +196,11 @@ void GetFirstDerivatives(const std::vector<synfig::Point> &f, unsigned int left,
        {
                //left then middle then right
                ThreePointdt(*(synfig::Vector*)out,f[left+0], f[left+1], f[left+2], -1);
-               current += 1;
+               current++;
                out += dfstride;
 
                for(;current < right-1; current++, out += dfstride)
-               {
                        ThreePointdt(*(synfig::Vector*)out,f[current-1], f[current], f[current+1], 0);
-               }
 
                ThreePointdt(*(synfig::Vector*)out,f[right-3], f[right-2], f[right-1], 1);
                current++;
@@ -232,21 +218,19 @@ void GetFirstDerivatives(const std::vector<synfig::Point> &f, unsigned int left,
                current += 2;
 
                for(;current < right-2; current++, out += dfstride)
-               {
                        FivePointdt(*(synfig::Vector*)out,f[current-2], f[current-1], f[current], f[current+1], f[current+2], 0);
-               }
 
-               FivePointdt(*(synfig::Vector*)out,f[right-5], f[right-4], f[right-3], f[right-2], f[right-1], 1);
-               out += dfstride;
                FivePointdt(*(synfig::Vector*)out,f[right-6], f[right-5], f[right-4], f[right-3], f[right-2], 2);
                out += dfstride;
+               FivePointdt(*(synfig::Vector*)out,f[right-5], f[right-4], f[right-3], f[right-2], f[right-1], 1);
+               out += dfstride;
                current += 2;
        }
 }
 
 void GetSimpleDerivatives(const std::vector<synfig::Point> &f, int left, int right,
                                                        std::vector<synfig::Point> &df, int outleft,
-                                                       const std::vector<synfig::Real> &di)
+                                                       const std::vector<synfig::Real> &/*di*/)
 {
        int i1,i2,i;
        int offset = 2; //df = 1/2 (f[i+o]-f[i-o])
@@ -326,7 +310,7 @@ Real CurveError(const synfig::Point *pts, unsigned int n, std::vector<synfig::Po
 typedef synfigapp::BLineConverter::cpindex cpindex;
 
 //has the index data and the tangent scale data (relevant as it may be)
-int tesselate_curves(const std::vector<cpindex> &inds, const std::vector<Point> &f, const std::vector<synfig::Vector> &df, std::vector<Point> &work)
+int tessellate_curves(const std::vector<cpindex> &inds, const std::vector<Point> &f, const std::vector<synfig::Vector> &df, std::vector<Point> &work)
 {
        if(inds.size() < 2)
                return 0;
@@ -341,7 +325,7 @@ int tesselate_curves(const std::vector<cpindex> &inds, const std::vector<Point>
        j2 = j++;
        for(; j != end; j2 = j++)
        {
-               //if this curve has invalid error (in j) then retesselate it's work points (requires reparametrization, etc.)
+               //if this curve has invalid error (in j) then retessellate its work points (requires reparametrization, etc.)
                if(j->error < 0)
                {
                        //get the stepsize etc. for the number of points in here
@@ -361,8 +345,12 @@ int tesselate_curves(const std::vector<cpindex> &inds, const std::vector<Point>
                        //build hermite curve, it's easier
                        curve.p1() = f[i0];
                        curve.p2() = f[i3];
-                       curve.t1() = df[i0]*(df[i0].mag_squared() > 1e-4 ? j2->tangentscale/df[i0].mag() : j2->tangentscale);
-                       curve.t2() = df[i3]*(df[i3].mag_squared() > 1e-4 ? j->tangentscale/df[i3].mag() : j->tangentscale);
+                       curve.t1() = df[i0-ibase] * (df[i0-ibase].mag_squared() > 1e-4
+                                                                                ? j2->tangentscale/df[i0-ibase].mag()
+                                                                                : j2->tangentscale);
+                       curve.t2() = df[i3-ibase] * (df[i3-ibase].mag_squared() > 1e-4
+                                                                                ? j->tangentscale/df[i3-ibase].mag()
+                                                                                : j->tangentscale);
                        curve.sync();
 
                        //MUST include the end point (since we are ignoring left one)
@@ -402,7 +390,7 @@ synfigapp::BLineConverter::clear()
 }
 
 void
-synfigapp::BLineConverter::operator () (std::list<synfig::BLinePoint> &out, const std::list<synfig::Point> &in,const std::list<synfig::Real> &in_w)
+synfigapp::BLineConverter::operator()(std::list<synfig::BLinePoint> &out, const std::list<synfig::Point> &in,const std::list<synfig::Real> &in_w)
 {
        //Profiling information
        /*etl::clock::value_type initialprocess=0, curveval=0, breakeval=0, disteval=0;
@@ -437,24 +425,17 @@ synfigapp::BLineConverter::operator () (std::list<synfig::BLinePoint> &out, cons
                if(in.size() == in_w.size())
                {
                        for(;i != end; ++i,++iw)
-                       {
-                               //eliminate duplicate points
-                               if(*i != c)
+                               if(*i != c)             // eliminate duplicate points
                                {
                                        f.push_back(c = *i);
                                        f_w.push_back(*iw);
                                }
-                       }
-               }else
+               }
+               else
                {
                        for(;i != end; ++i)
-                       {
-                               //eliminate duplicate points
-                               if(*i != c)
-                               {
+                               if(*i != c)             // eliminate duplicate points
                                        f.push_back(c = *i);
-                               }
-                       }
                }
        }
        //initialprocess = timer();
@@ -514,7 +495,8 @@ synfigapp::BLineConverter::operator () (std::list<synfig::BLinePoint> &out, cons
                                        minc = cvt[i];
                                        maxi = i;
                                }
-                       }else if(maxi >= 0)
+                       }
+                       else if(maxi >= 0)
                        {
                                if(maxi >= last + 8)
                                {
@@ -529,7 +511,7 @@ synfigapp::BLineConverter::operator () (std::list<synfig::BLinePoint> &out, cons
 
                brk.push_back(i);
 
-               //postprocess for breaks too close to eachother
+               //postprocess for breaks too close to each other
                Real d = 0;
                Point p = f[brk.front()];
 
@@ -623,13 +605,28 @@ synfigapp::BLineConverter::operator () (std::list<synfig::BLinePoint> &out, cons
                                gaussian_blur_3(ftemp.begin(),ftemp.end(),false);
 
                        df.resize(size);
+
+                       // Wondering whether the modification of the df vector
+                       // using a char* pointer and pointer arithmetric was safe,
+                       // I looked it up...
+                       // 
+                       // http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2369.pdf tells me:
+                       // 
+                       //      23.2.5  Class template vector [vector]
+                       // 
+                       //      [...] The elements of a vector are stored contiguously,
+                       //      meaning that if v is a vector<T,Allocator> where T is
+                       //      some type other than bool, then it obeys the identity
+                       //      &v[n] == &v[0] + n for all 0 <= n < v.size().
+                       // 
                        GetFirstDerivatives(ftemp,0,size,(char*)&df[0],sizeof(df[0]));
+
                        //GetSimpleDerivatives(ftemp,0,size,df,0,di);
-                       //< don't have to worry about indexing stuff as it is all being taken car of right now
+                       //< don't have to worry about indexing stuff as it is all being taken care of right now
                        //preproceval += timer();
                        //numpre++;
 
-                       work.resize(size*2-1); //guarantee that all points will be tesselated correctly (one point inbetween every 2 adjacent points)
+                       work.resize(size*2-1); //guarantee that all points will be tessellated correctly (one point in between every 2 adjacent points)
 
                        //if size of work is size*2-1, the step size should be 1/(size*2 - 2)
                        //Real step = 1/(Real)(size*2 - 1);
@@ -645,18 +642,18 @@ synfigapp::BLineConverter::operator () (std::list<synfig::BLinePoint> &out, cons
                        //while there are still enough points between us, and the error is too high subdivide (and invalidate neighbors that share tangents)
                        while(!done)
                        {
-                               //tesselate all curves with invalid error values
+                               //tessellate all curves with invalid error values
                                work[0] = f[i0];
 
                                //timer.reset();
-                               /*numtess += */tesselate_curves(curind,f,df,work);
+                               /*numtess += */tessellate_curves(curind,f,df,work);
                                //tesseval += timer();
 
                                //now get all error values
                                //timer.reset();
                                for(i = 1; i < (int)curind.size(); ++i)
                                {
-                                       if(curind[i].error < 0) //must have been retesselated, so now recalculate error value
+                                       if(curind[i].error < 0) //must have been retessellated, so now recalculate error value
                                        {
                                                //evaluate error from points (starting at current index)
                                                int size = curind[i].curind - curind[i-1].curind + 1;
@@ -667,7 +664,7 @@ synfigapp::BLineConverter::operator () (std::list<synfig::BLinePoint> &out, cons
                                                {
                                                        synfig::info("Holy crap %d-%d error %f",curind[i-1].curind,curind[i].curind,curind[i].error);
                                                        curind[i].error = -1;
-                                                       numtess += tesselate_curves(curind,f,df,work);
+                                                       numtess += tessellate_curves(curind,f,df,work);
                                                        curind[i].error = CurveError(&f[curind[i-1].curind], size,
                                                                                                         work,0,work.size());//(curind[i-1].curind - i0)*2,(curind[i].curind - i0)*2+1);
                                                }*/
@@ -807,7 +804,7 @@ synfigapp::BLineConverter::operator () (std::list<synfig::BLinePoint> &out, cons
                        "\tDistance Calculation:  %f\n"
                        "  Algorithm: (numtimes,totaltime)\n"
                        "\tPreprocess step:      (%d,%f)\n"
-                       "\tTesselation step:     (%d,%f)\n"
+                       "\tTessellation step:    (%d,%f)\n"
                        "\tError step:           (%d,%f)\n"
                        "\tSplit step:           (%d,%f)\n"
                        "  Num Input: %d, Num Output: %d\n"
@@ -827,10 +824,6 @@ void synfigapp::BLineConverter::EnforceMinWidth(std::list<synfig::BLinePoint> &b
                                                                                        end = bline.end();
 
        for(i = bline.begin(); i != end; ++i)
-       {
                if(i->get_width() < min_pressure)
-               {
                        i->set_width(min_pressure);
-               }
-       }
 }