Added copyright lines for files I've edited this year.
[synfig.git] / synfig-core / trunk / src / synfig / valuenode_bline.cpp
index 2435814..8365b41 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
@@ -52,6 +53,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 ================================================= */
@@ -340,14 +343,17 @@ ValueNode_BLine::operator()(Time t)const
        BLinePoint prev,first;
        first.set_origin(100.0f);
 
+       // loop through all the list's entries
        for(iter=list.begin();iter!=list.end();++iter,index++)
        {
+               // how 'on' is this vertex?
                float amount(iter->amount_at_time(t,&rising));
 
                assert(amount>=0.0f);
                assert(amount<=1.0f);
 
-               if(amount==1.0f)
+               // it's fully on
+               if (amount > 1.0f - EPSILON)
                {
                        if(first_flag)
                        {
@@ -381,8 +387,8 @@ ValueNode_BLine::operator()(Time t)const
 
                        prev=curr;
                }
-               else
-               if(amount>0.0f)
+               // it's partly on
+               else if(amount>0.0f)
                {
                        std::vector<ListEntry>::const_iterator begin_iter,end_iter;
 
@@ -417,34 +423,28 @@ ValueNode_BLine::operator()(Time t)const
                        blp_here_on=(*iter->value_node)(on_time).get(blp_here_on);
 //                     blp_here_on=(*iter->value_node)(t).get(blp_here_on);
 
-                       // Find "end" of dynamic group
+                       // 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
                        end_iter=iter;
 //                     for(++end_iter;begin_iter!=list.end();++end_iter)
                        for(++end_iter;end_iter!=list.end();++end_iter)
                                if(end_iter->amount_at_time(t)>amount)
-                               {
-                                       blp_next_off=(*end_iter->value_node)(off_time).get(prev);
                                        break;
-                               }
 
                        // If we did not find an end of the dynamic group...
+                       // Writeme!  at least now it doesn't crash if first_iter
+                       // isn't set yet
                        if(end_iter==list.end())
                        {
-                               if(get_loop())
-                               {
+                               if(get_loop() && !first_flag)
                                        end_iter=first_iter;
-                                       blp_next_off=(*end_iter->value_node)(off_time).get(prev);
-//                                     end=first;
-                               }
                                else
-                               {
-                                       // Writeme!
-                                       end_iter=first_iter;
-                                       blp_next_off=(*end_iter->value_node)(off_time).get(prev);
-//                                     end=first;
-                               }
+                                       end_iter=--list.end();
                        }
 
+                       blp_next_off=(*end_iter->value_node)(off_time).get(prev);
+
                        // Find "begin" of dynamic group
                        begin_iter=iter;
                        blp_prev_off.set_origin(100.0f); // set the origin to 100 (which is crazy) so that we can check to see if it was found
@@ -461,6 +461,7 @@ ValueNode_BLine::operator()(Time t)const
                                --begin_iter;
                                dist_from_begin++;
 
+                               // if we've gone all around the loop, give up
                                if(begin_iter==iter)
                                        break;
 
@@ -469,24 +470,18 @@ ValueNode_BLine::operator()(Time t)const
                                        blp_prev_off=(*begin_iter->value_node)(off_time).get(prev);
                                        break;
                                }
-                       }while(begin_iter!=iter);
+                       }while(true);
 
                        // If we did not find a begin
                        if(blp_prev_off.get_origin()==100.0f)
                        {
-                               if(get_loop())
-                               {
-                                       begin_iter=first_iter;
-                                       blp_prev_off=(*begin_iter->value_node)(off_time).get(prev);
-//                                     blp_prev_off=first;
-                               }
+                               // Writeme! - this needs work, but at least now it
+                               // doesn't crash
+                               if(first_flag)
+                                       begin_iter=list.begin();
                                else
-                               {
-                                       // Writeme!
                                        begin_iter=first_iter;
-                                       blp_prev_off=(*begin_iter->value_node)(off_time).get(prev);
-//                                     blp_prev_off=first;
-                               }
+                               blp_prev_off=(*begin_iter->value_node)(off_time).get(prev);
                        }
 
                        // this is how the curve looks when we have completely vanished
@@ -576,73 +571,75 @@ ValueNode_BLine::operator()(Time t)const
                                        curr_coord_origin=(begin_pos_at_current_time + end_pos_at_current_time)/2;
                                        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 matrices, since we use it for transform back
+                                       swap(curr_coord_sys[0][1],curr_coord_sys[1][0]);
                                }
 
                                /* The code that was here before used just end_iter as the origin, rather than the mid-point */
 
-                               // For each of the 3 coordinate systems we've just defined, we convert a point and tangent(s) into that system
-                               Point trans_on_point, trans_off_point, untrans_curr_point;
-                               Vector trans_on_t1, trans_on_t2, trans_off_t1, trans_off_t2, untrans_curr_t1, untrans_curr_t2;
+                               // We know our location and tangent(s) when fully on and fully off
+                               // Transform each of these into their corresponding coordinate system
+                               Point trans_on_point, trans_off_point;
+                               Vector trans_on_t1, trans_on_t2, trans_off_t1, trans_off_t2;
 
-                               // Convert points where vertex is fully on and fully off
                                transform_coords(blp_here_on.get_vertex(),  trans_on_point,  on_coord_origin,  on_coord_sys);
                                transform_coords(blp_here_off.get_vertex(), trans_off_point, off_coord_origin, off_coord_sys);
 
 #define COORD_SYS_RADIAL_TAN_INTERP 1
 
 #ifdef COORD_SYS_RADIAL_TAN_INTERP
-                               // this I don't understand.  why add on this bit ---v
-                               transform_coords(blp_here_on.get_tangent1()  + blp_here_on.get_vertex(),  trans_on_t1,  on_coord_origin,  on_coord_sys);
-                               transform_coords(blp_here_off.get_tangent1() + blp_here_off.get_vertex(), trans_off_t1, off_coord_origin, off_coord_sys);
+                               transform_coords(blp_here_on.get_tangent1(),  trans_on_t1,  Point::zero(), on_coord_sys);
+                               transform_coords(blp_here_off.get_tangent1(), trans_off_t1, Point::zero(), off_coord_sys);
 
                                if(blp_here_on.get_split_tangent_flag())
                                {
-                                       transform_coords(blp_here_on.get_tangent2()  + blp_here_on.get_vertex(),  trans_on_t2,  on_coord_origin,  on_coord_sys);
-                                       transform_coords(blp_here_off.get_tangent2() + blp_here_off.get_vertex(), trans_off_t2, off_coord_origin, off_coord_sys);
+                                       transform_coords(blp_here_on.get_tangent2(),  trans_on_t2,  Point::zero(), on_coord_sys);
+                                       transform_coords(blp_here_off.get_tangent2(), trans_off_t2, Point::zero(), off_coord_sys);
                                }
 #endif
-                               // Convert current point
-                               // Transpose (invert)
-                               swap(curr_coord_sys[0][1],curr_coord_sys[1][0]);
 
-                               // interpolate between the 'on' point and the 'off' point and untransform to get our point's location
-                               untransform_coords(linear_interpolation(trans_off_point, trans_on_point, amount),
-                                                                  untrans_curr_point, curr_coord_origin, curr_coord_sys);
+                               {
+                                       // Interpolate between the 'on' point and the 'off' point and untransform to get our point's location
+                                       Point tmp;
+                                       untransform_coords(linear_interpolation(trans_off_point, trans_on_point, amount),
+                                                                          tmp, curr_coord_origin, curr_coord_sys);
+                                       blp_here_now.set_vertex(tmp);
+                               }
 
 #define INTERP_FUNCTION                radial_interpolation
 //#define INTERP_FUNCTION      linear_interpolation
 
 #ifdef COORD_SYS_RADIAL_TAN_INTERP
-                               untransform_coords(INTERP_FUNCTION(trans_off_t1,trans_on_t1,amount),
-                                                                  untrans_curr_t1, curr_coord_origin, curr_coord_sys);
-                               untrans_curr_t1 -= untrans_curr_point;
-
-                               if(blp_here_on.get_split_tangent_flag())
                                {
-                                       untransform_coords(INTERP_FUNCTION(trans_off_t2,trans_on_t2,amount),
-                                                                          untrans_curr_t2, curr_coord_origin, curr_coord_sys);
-                                       untrans_curr_t2 -= untrans_curr_point;
+                                       Vector tmp;
+                                       untransform_coords(INTERP_FUNCTION(trans_off_t1,trans_on_t1,amount), tmp, Point::zero(), curr_coord_sys);
+                                       blp_here_now.set_tangent1(tmp);
                                }
+#else
+                               blp_here_now.set_tangent1(radial_interpolation(blp_here_off.get_tangent1(),blp_here_on.get_tangent1(),amount));
 #endif
 
-                               blp_here_now.set_vertex(untrans_curr_point);
-#ifndef COORD_SYS_RADIAL_TAN_INTERP
-                               blp_here_now.set_tangent1(radial_interpolation(blp_here_off.get_tangent1(),blp_here_on.get_tangent1(),amount));
-                               blp_here_now.set_split_tangent_flag(blp_here_on.get_split_tangent_flag());
-                               if(blp_here_now.get_split_tangent_flag())
-                                       blp_here_now.set_tangent2(radial_interpolation(blp_here_off.get_tangent2(),blp_here_on.get_tangent2(),amount));
+                               if (blp_here_on.get_split_tangent_flag())
+                               {
+                                       blp_here_now.set_split_tangent_flag(true);
+#ifdef COORD_SYS_RADIAL_TAN_INTERP
+                                       {
+                                               Vector tmp;
+                                               untransform_coords(INTERP_FUNCTION(trans_off_t2,trans_on_t2,amount), tmp, Point::zero(), curr_coord_sys);
+                                               blp_here_now.set_tangent2(tmp);
+                                       }
 #else
-                               blp_here_now.set_tangent1(untrans_curr_t1);
-                               blp_here_now.set_split_tangent_flag(blp_here_on.get_split_tangent_flag());
-                               if(blp_here_now.get_split_tangent_flag())
-                                       blp_here_now.set_tangent2(untrans_curr_t2);
+                                       blp_here_now.set_tangent2(radial_interpolation(blp_here_off.get_tangent2(),blp_here_on.get_tangent2(),amount));
 #endif
+                               }
+                               else
+                                       blp_here_now.set_split_tangent_flag(false);
                        }
 
                        blp_here_now.set_origin(blp_here_on.get_origin());
                        blp_here_now.set_width(linear_interpolation(blp_here_off.get_width(), blp_here_on.get_width(), amount));
 
-
                        // Handle the case where we are the first vertex
                        if(first_flag)
                        {