X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=synfig-core%2Ftrunk%2Fsrc%2Fsynfig%2Fvaluenode_bline.cpp;h=a5dc1def09180ed3f4ce338b5b049bdf26568ade;hb=376a3dcd5c49955bebcb04543446e7378b8ef298;hp=ee0d83148a242916265ab6f16074d5a7d77332d0;hpb=170f9a7e60b16e212cb7ab740627376be68713cb;p=synfig.git diff --git a/synfig-core/trunk/src/synfig/valuenode_bline.cpp b/synfig-core/trunk/src/synfig/valuenode_bline.cpp index ee0d831..a5dc1de 100644 --- a/synfig-core/trunk/src/synfig/valuenode_bline.cpp +++ b/synfig-core/trunk/src/synfig/valuenode_bline.cpp @@ -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 #include #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 ================================================= */ @@ -160,6 +164,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 list(bline.get_list().begin(),bline.get_list().end()); + typedef std::vector::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 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 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 +395,6 @@ ValueNode_BLine::create(const ValueBase &value) } } - return value_node; } @@ -288,7 +403,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 +419,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); @@ -340,14 +454,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 +498,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::const_iterator begin_iter,end_iter; @@ -417,34 +534,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 +572,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 +581,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 @@ -577,7 +683,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]); }