Remove .gitignore do nothing is ignored.
[synfig.git] / synfig-core / trunk / src / synfig / valuenode_bline.cpp
index 87e8702..fc3e916 100644 (file)
@@ -1,11 +1,12 @@
 /* === S Y N F I G ========================================================= */
 /*!    \file valuenode_bline.cpp
-**     \brief Template File
+**     \brief Implementation of the "BLine" valuenode conversion.
 **
 **     $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
@@ -41,6 +42,7 @@
 #include <ETL/hermite>
 #include <ETL/calculus>
 #include "segment.h"
+#include "curve_helper.h"
 
 #endif
 
@@ -52,6 +54,8 @@ using namespace synfig;
 
 /* === M A C R O S ========================================================= */
 
+#define EPSILON 0.0000001f
+
 /* === G L O B A L S ======================================================= */
 
 /* === P R O C E D U R E S ================================================= */
@@ -75,7 +79,12 @@ radial_interpolation(const Vector& a, const Vector& b, float c)
        affine_combo<Angle,float> ang_combo;
 
        Real mag(mag_combo(a.mag(),b.mag(),c));
-       Angle ang(ang_combo(Angle::tan(a[1],a[0]),Angle::tan(b[1],b[0]),c));
+       Angle angle_a(Angle::tan(a[1],a[0]));
+       Angle angle_b(Angle::tan(b[1],b[0]));
+       float diff = Angle::deg(angle_b - angle_a).get();
+       if (diff < -180) angle_b += Angle::deg(360);
+       else if (diff > 180) angle_a += Angle::deg(360);
+       Angle ang(ang_combo(angle_a, angle_b, c));
 
        return Point( mag*Angle::cos(ang).get(),mag*Angle::sin(ang).get() );
 }
@@ -160,6 +169,118 @@ synfig::convert_bline_to_width_list(const ValueBase& bline)
        return ValueBase(ret,bline.get_loop());
 }
 
+Real
+synfig::find_closest_point(const ValueBase &bline, const Point &pos, Real &radius, bool loop, Point *out_point)
+{
+       Real d,step;
+       float time = 0;
+       float best_time = 0;
+       int best_index = -1;
+       synfig::Point best_point;
+
+       if(radius==0)radius=10000000;
+       Real closest(10000000);
+
+       int i=0;
+       std::vector<BLinePoint> list(bline.get_list().begin(),bline.get_list().end());
+       typedef std::vector<BLinePoint>::const_iterator iterT;
+       iterT iter, prev, first;
+       for(iter=list.begin(); iter!=list.end(); ++i, ++iter)
+       {
+               if( first == iterT() )
+                       first = iter;
+
+               if( prev != iterT() )
+               {
+                       bezier<Point>   curve;
+
+                       curve[0] = (*prev).get_vertex();
+                       curve[1] = curve[0] + (*prev).get_tangent2()/3;
+                       curve[3] = (*iter).get_vertex();
+                       curve[2] = curve[3] - (*iter).get_tangent1()/3;
+                       curve.sync();
+
+                       #if 0
+                       // I don't know why this doesn't work
+                       time=curve.find_closest(pos,6);
+                       d=((curve(time)-pos).mag_squared());
+
+                       #else
+                       //set the step size based on the size of the picture
+                       d = (curve[1] - curve[0]).mag() + (curve[2]-curve[1]).mag()     + (curve[3]-curve[2]).mag();
+
+                       step = d/(2*radius); //want to make the distance between lines happy
+
+                       step = max(step,0.01); //100 samples should be plenty
+                       step = min(step,0.1); //10 is minimum
+
+                       d = find_closest(curve,pos,step,&closest,&time);
+                       #endif
+
+                       if(d < closest)
+                       {
+                               closest = d;
+                               best_time = time;
+                               best_index = i;
+                               best_point = curve(best_time);
+                       }
+
+               }
+
+               prev = iter;
+       }
+
+       // Loop if necessary
+       if( loop && ( first != iterT() ) && ( prev != iterT() ) )
+       {
+               bezier<Point>   curve;
+
+               curve[0] = (*prev).get_vertex();
+               curve[1] = curve[0] + (*prev).get_tangent2()/3;
+               curve[3] = (*first).get_vertex();
+               curve[2] = curve[3] - (*first).get_tangent1()/3;
+               curve.sync();
+
+               #if 0
+               // I don't know why this doesn't work
+               time=curve.find_closest(pos,6);
+               d=((curve(time)-pos).mag_squared());
+
+               #else
+               //set the step size based on the size of the picture
+               d = (curve[1] - curve[0]).mag() + (curve[2]-curve[1]).mag()     + (curve[3]-curve[2]).mag();
+
+               step = d/(2*radius); //want to make the distance between lines happy
+
+               step = max(step,0.01); //100 samples should be plenty
+               step = min(step,0.1); //10 is minimum
+
+                       d = find_closest(curve,pos,step,&closest,&time);
+               #endif
+
+               if(d < closest)
+               {
+                       closest = d;
+                       best_time = time;
+                       best_index = 0;
+                       best_point = curve(best_time);
+               }
+       }
+
+       if(best_index != -1)
+       {
+               if(out_point)
+                       *out_point = best_point;
+
+               int loop_adjust(loop ? 0 : -1);
+               int size = list.size();
+               Real amount = (best_index + best_time + loop_adjust) / (size + loop_adjust);
+               return amount;
+       }
+
+       return 0.0;
+
+}
 
 /* === M E T H O D S ======================================================= */
 
@@ -279,7 +400,6 @@ ValueNode_BLine::create(const ValueBase &value)
                }
        }
 
-
        return value_node;
 }
 
@@ -288,7 +408,6 @@ ValueNode_BLine::create_list_entry(int index, Time time, Real origin)
 {
        ValueNode_BLine::ListEntry ret;
 
-
        synfig::BLinePoint prev,next;
 
        int prev_i,next_i;
@@ -305,7 +424,7 @@ ValueNode_BLine::create_list_entry(int index, Time time, Real origin)
                next_i=index;
        prev_i=find_prev_valid_entry(index,time);
 
-       synfig::info("index=%d, next_i=%d, prev_i=%d",index,next_i,prev_i);
+       //synfig::info("index=%d, next_i=%d, prev_i=%d",index,next_i,prev_i);
 
        next=(*list[next_i].value_node)(time);
        prev=(*list[prev_i].value_node)(time);
@@ -326,9 +445,14 @@ ValueNode_BLine::create_list_entry(int index, Time time, Real origin)
        return ret;
 }
 
+static int instance_count;
+
 ValueBase
 ValueNode_BLine::operator()(Time t)const
 {
+       if (getenv("SYNFIG_DEBUG_VALUENODE_OPERATORS"))
+               printf("%s:%d operator()\n", __FILE__, __LINE__);
+
        std::vector<BLinePoint> ret_list;
 
        std::vector<ListEntry>::const_iterator iter,first_iter;
@@ -350,7 +474,7 @@ ValueNode_BLine::operator()(Time t)const
                assert(amount<=1.0f);
 
                // it's fully on
-               if(amount==1.0f)
+               if (amount > 1.0f - EPSILON)
                {
                        if(first_flag)
                        {
@@ -422,7 +546,7 @@ ValueNode_BLine::operator()(Time t)const
 
                        // Find "end" of dynamic group - ie. search forward along
                        // the bline from the current point until we find a point
-                       // which is more 'on'than the current one
+                       // which is more 'on' than the current one
                        end_iter=iter;
 //                     for(++end_iter;begin_iter!=list.end();++end_iter)
                        for(++end_iter;end_iter!=list.end();++end_iter)
@@ -569,7 +693,7 @@ ValueNode_BLine::operator()(Time t)const
                                        curr_coord_sys[0]=(begin_pos_at_current_time - end_pos_at_current_time).norm();
                                        curr_coord_sys[1]=curr_coord_sys[0].perp();
 
-                                       // Invert (transpose) the last of these matricies, since we use it for transform back
+                                       // Invert (transpose) the last of these matrices, since we use it for transform back
                                        swap(curr_coord_sys[0][1],curr_coord_sys[1][0]);
                                }
 
@@ -695,35 +819,6 @@ ValueNode_BLine::link_local_name(int i)const
        return etl::strprintf(_("Vertex %03d"),i+1);
 }
 
-ValueNode*
-ValueNode_BLine::clone(const GUID& deriv_guid)const
-{
-       { ValueNode* x(find_value_node(get_guid()^deriv_guid).get()); if(x)return x; }
-
-       ValueNode_BLine* ret=new ValueNode_BLine();
-       ret->set_guid(get_guid()^deriv_guid);
-
-       std::vector<ListEntry>::const_iterator iter;
-
-       for(iter=list.begin();iter!=list.end();++iter)
-       {
-               if(iter->value_node->is_exported())
-                       ret->add(*iter);
-               else
-               {
-                       ListEntry list_entry(*iter);
-                       //list_entry.value_node=find_value_node(iter->value_node->get_guid()^deriv_guid).get();
-                       //if(!list_entry.value_node)
-                               list_entry.value_node=iter->value_node->clone(deriv_guid);
-                       ret->add(list_entry);
-                       //ret->list.back().value_node=iter->value_node.clone();
-               }
-       }
-       ret->set_loop(get_loop());
-
-       return ret;
-}
-
 String
 ValueNode_BLine::get_name()const
 {
@@ -739,8 +834,7 @@ ValueNode_BLine::get_local_name()const
 LinkableValueNode*
 ValueNode_BLine::create_new()const
 {
-       assert(0);
-       return 0;
+       return new ValueNode_BLine();
 }
 
 bool