1 /* === S Y N F I G ========================================================= */
2 /*! \file valuenode_bline.cpp
3 ** \brief Implementation of the "BLine" valuenode conversion.
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2007 Chris Moore
11 ** This package is free software; you can redistribute it and/or
12 ** modify it under the terms of the GNU General Public License as
13 ** published by the Free Software Foundation; either version 2 of
14 ** the License, or (at your option) any later version.
16 ** This package is distributed in the hope that it will be useful,
17 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ** General Public License for more details.
22 /* ========================================================================= */
24 /* === H E A D E R S ======================================================= */
33 #include "valuenode_bline.h"
34 #include "valuenode_const.h"
35 #include "valuenode_composite.h"
37 #include "exception.h"
38 #include "blinepoint.h"
42 #include <ETL/hermite>
43 #include <ETL/calculus>
48 /* === U S I N G =========================================================== */
52 using namespace synfig;
54 /* === M A C R O S ========================================================= */
56 #define EPSILON 0.0000001f
58 /* === G L O B A L S ======================================================= */
60 /* === P R O C E D U R E S ================================================= */
63 linear_interpolation(const float& a, const float& b, float c)
67 linear_interpolation(const Vector& a, const Vector& b, float c)
71 radial_interpolation(const Vector& a, const Vector& b, float c)
73 // if either extreme is zero then use linear interpolation instead
74 if (a.is_equal_to(Vector::zero()) || b.is_equal_to(Vector::zero()))
75 return linear_interpolation(a, b, c);
77 affine_combo<Real,float> mag_combo;
78 affine_combo<Angle,float> ang_combo;
80 Real mag(mag_combo(a.mag(),b.mag(),c));
81 Angle ang(ang_combo(Angle::tan(a[1],a[0]),Angle::tan(b[1],b[0]),c));
83 return Point( mag*Angle::cos(ang).get(),mag*Angle::sin(ang).get() );
87 transform_coords(Vector in, Vector& out, const Point& coord_origin, const Point *coord_sys)
90 out[0] = in * coord_sys[0];
91 out[1] = in * coord_sys[1];
95 untransform_coords(const Vector& in, Vector& out, const Point& coord_origin, const Point *coord_sys)
97 out[0] = in * coord_sys[0];
98 out[1] = in * coord_sys[1];
103 synfig::convert_bline_to_segment_list(const ValueBase& bline)
105 std::vector<Segment> ret;
107 // std::vector<BLinePoint> list(bline.operator std::vector<BLinePoint>());
108 //std::vector<BLinePoint> list(bline);
109 std::vector<BLinePoint> list(bline.get_list().begin(),bline.get_list().end());
110 std::vector<BLinePoint>::const_iterator iter;
112 BLinePoint prev,first;
114 //start with prev = first and iter on the second...
116 if(list.empty()) return ValueBase(ret,bline.get_loop());
117 first = prev = list.front();
119 for(iter=++list.begin();iter!=list.end();++iter)
142 return ValueBase(ret,bline.get_loop());
146 synfig::convert_bline_to_width_list(const ValueBase& bline)
148 std::vector<Real> ret;
149 // std::vector<BLinePoint> list(bline.operator std::vector<BLinePoint>());
150 //std::vector<BLinePoint> list(bline);
151 std::vector<BLinePoint> list(bline.get_list().begin(),bline.get_list().end());
152 std::vector<BLinePoint>::const_iterator iter;
155 return ValueBase(ValueBase::TYPE_LIST);
157 for(iter=list.begin();iter!=list.end();++iter)
158 ret.push_back(iter->get_width());
161 ret.push_back(list.front().get_width());
163 return ValueBase(ret,bline.get_loop());
167 /* === M E T H O D S ======================================================= */
170 ValueNode_BLine::ValueNode_BLine():
171 ValueNode_DynamicList(ValueBase::TYPE_BLINEPOINT)
175 ValueNode_BLine::~ValueNode_BLine()
180 ValueNode_BLine::create(const ValueBase &value)
182 if(value.get_type()!=ValueBase::TYPE_LIST)
185 ValueNode_BLine* value_node(new ValueNode_BLine());
189 switch(value.get_contained_type())
191 case ValueBase::TYPE_BLINEPOINT:
193 // std::vector<BLinePoint> bline_points(value.operator std::vector<BLinePoint>());
194 //std::vector<BLinePoint> bline_points(value);
195 std::vector<BLinePoint> bline_points(value.get_list().begin(),value.get_list().end());
196 std::vector<BLinePoint>::const_iterator iter;
198 for(iter=bline_points.begin();iter!=bline_points.end();iter++)
200 value_node->add(ValueNode::Handle(ValueNode_Composite::create(*iter)));
202 value_node->set_loop(value.get_loop());
205 case ValueBase::TYPE_SEGMENT:
207 // Here, we want to convert a list of segments
208 // into a list of BLinePoints. We make an assumption
209 // that the segment list is continuous(sp), but not necessarily
212 value_node->set_loop(false);
213 // std::vector<Segment> segments(value.operator std::vector<Segment>());
214 // std::vector<Segment> segments(value);
215 std::vector<Segment> segments(value.get_list().begin(),value.get_list().end());
216 std::vector<Segment>::const_iterator iter,last(segments.end());
218 ValueNode_Const::Handle prev,first;
220 for(iter=segments.begin();iter!=segments.end();iter++)
222 #define PREV_POINT prev->get_value().get(BLinePoint())
223 #define FIRST_POINT first->get_value().get(BLinePoint())
224 #define CURR_POINT curr->get_value().get(BLinePoint())
225 if(iter==segments.begin())
227 prev=ValueNode_Const::create(ValueBase::TYPE_BLINEPOINT);
229 BLinePoint prev_point(PREV_POINT);
230 prev_point.set_vertex(iter->p1);
231 prev_point.set_tangent1(iter->t1);
232 prev_point.set_width(0.01);
233 prev_point.set_origin(0.5);
234 prev_point.set_split_tangent_flag(false);
235 prev->set_value(prev_point);
238 value_node->add(ValueNode::Handle(prev));
241 if(iter==last && iter->p2.is_equal_to(FIRST_POINT.get_vertex()))
243 value_node->set_loop(true);
244 if(!iter->t2.is_equal_to(FIRST_POINT.get_tangent1()))
246 BLinePoint first_point(FIRST_POINT);
247 first_point.set_tangent1(iter->t2);
248 first->set_value(first_point);
253 ValueNode_Const::Handle curr;
254 curr=ValueNode_Const::create(ValueBase::TYPE_BLINEPOINT);
256 BLinePoint curr_point(CURR_POINT);
257 curr_point.set_vertex(iter->p2);
258 curr_point.set_tangent1(iter->t2);
259 curr_point.set_width(0.01);
260 curr_point.set_origin(0.5);
261 curr_point.set_split_tangent_flag(false);
262 curr->set_value(curr_point);
264 if(!PREV_POINT.get_tangent1().is_equal_to(iter->t1))
266 BLinePoint prev_point(PREV_POINT);
267 prev_point.set_split_tangent_flag(true);
268 prev_point.set_tangent2(iter->t1);
269 prev->set_value(prev_point);
271 value_node->add(ValueNode::Handle(curr));
278 // We got a list of who-knows-what. We don't have any idea
279 // what to do with it.
289 ValueNode_BLine::ListEntry
290 ValueNode_BLine::create_list_entry(int index, Time time, Real origin)
292 ValueNode_BLine::ListEntry ret;
295 synfig::BLinePoint prev,next;
299 index=index%link_count();
303 ret.set_parent_value_node(this);
305 if(!list[index].status_at_time(time))
306 next_i=find_next_valid_entry(index,time);
309 prev_i=find_prev_valid_entry(index,time);
311 synfig::info("index=%d, next_i=%d, prev_i=%d",index,next_i,prev_i);
313 next=(*list[next_i].value_node)(time);
314 prev=(*list[prev_i].value_node)(time);
316 etl::hermite<Vector> curve(prev.get_vertex(),next.get_vertex(),prev.get_tangent2(),next.get_tangent1());
317 etl::derivative< etl::hermite<Vector> > deriv(curve);
319 synfig::BLinePoint bline_point;
320 bline_point.set_vertex(curve(origin));
321 bline_point.set_width((next.get_width()-prev.get_width())*origin+prev.get_width());
322 bline_point.set_tangent1(deriv(origin)*min(1.0-origin,origin));
323 bline_point.set_tangent2(bline_point.get_tangent1());
324 bline_point.set_split_tangent_flag(false);
325 bline_point.set_origin(origin);
327 ret.value_node=ValueNode_Composite::create(bline_point);
333 ValueNode_BLine::operator()(Time t)const
335 std::vector<BLinePoint> ret_list;
337 std::vector<ListEntry>::const_iterator iter,first_iter;
338 bool first_flag(true);
341 float next_scale(1.0f);
343 BLinePoint prev,first;
344 first.set_origin(100.0f);
346 // loop through all the list's entries
347 for(iter=list.begin();iter!=list.end();++iter,index++)
349 // how 'on' is this vertex?
350 float amount(iter->amount_at_time(t,&rising));
352 assert(amount>=0.0f);
353 assert(amount<=1.0f);
356 if (amount > 1.0f - EPSILON)
361 first=prev=(*iter->value_node)(t).get(prev);
363 ret_list.push_back(first);
368 curr=(*iter->value_node)(t).get(prev);
372 ret_list.back().set_split_tangent_flag(true);
373 ret_list.back().set_tangent2(prev.get_tangent2()*next_scale);
375 ret_list.push_back(curr);
377 ret_list.back().set_split_tangent_flag(true);
378 ret_list.back().set_tangent2(curr.get_tangent2());
379 ret_list.back().set_tangent1(curr.get_tangent1()*next_scale);
385 ret_list.push_back(curr);
393 std::vector<ListEntry>::const_iterator begin_iter,end_iter;
395 // This is where the interesting stuff happens
396 // We need to seek forward in the list to see what the next
399 BLinePoint blp_here_on; // the current vertex, when fully on
400 BLinePoint blp_here_off; // the current vertex, when fully off
401 BLinePoint blp_here_now; // the current vertex, right now (between on and off)
402 BLinePoint blp_prev_off; // the beginning of dynamic group when fully off
403 BLinePoint blp_next_off; // the end of the dynamic group when fully off
405 int dist_from_begin(0), dist_from_end(0);
406 Time off_time, on_time;
408 if(!rising) // if not rising, then we were fully on in the past, and will be fully off in the future
410 try{ on_time=iter->find_prev(t)->get_time(); }
411 catch(...) { on_time=Time::begin(); }
412 try{ off_time=iter->find_next(t)->get_time(); }
413 catch(...) { off_time=Time::end(); }
415 else // otherwise we were fully off in the past, and will be fully on in the future
417 try{ off_time=iter->find_prev(t)->get_time(); }
418 catch(...) { off_time=Time::begin(); }
419 try{ on_time=iter->find_next(t)->get_time(); }
420 catch(...) { on_time=Time::end(); }
423 blp_here_on=(*iter->value_node)(on_time).get(blp_here_on);
424 // blp_here_on=(*iter->value_node)(t).get(blp_here_on);
426 // Find "end" of dynamic group - ie. search forward along
427 // the bline from the current point until we find a point
428 // which is more 'on' than the current one
430 // for(++end_iter;begin_iter!=list.end();++end_iter)
431 for(++end_iter;end_iter!=list.end();++end_iter)
432 if(end_iter->amount_at_time(t)>amount)
435 // If we did not find an end of the dynamic group...
436 // Writeme! at least now it doesn't crash if first_iter
438 if(end_iter==list.end())
440 if(get_loop() && !first_flag)
443 end_iter=--list.end();
446 blp_next_off=(*end_iter->value_node)(off_time).get(prev);
448 // Find "begin" of dynamic group
450 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
453 if(begin_iter==list.begin())
456 begin_iter=list.end();
464 // if we've gone all around the loop, give up
468 if(begin_iter->amount_at_time(t)>amount)
470 blp_prev_off=(*begin_iter->value_node)(off_time).get(prev);
475 // If we did not find a begin
476 if(blp_prev_off.get_origin()==100.0f)
478 // Writeme! - this needs work, but at least now it
481 begin_iter=list.begin();
483 begin_iter=first_iter;
484 blp_prev_off=(*begin_iter->value_node)(off_time).get(prev);
487 // this is how the curve looks when we have completely vanished
488 etl::hermite<Vector> curve(blp_prev_off.get_vertex(), blp_next_off.get_vertex(),
489 blp_prev_off.get_tangent2(), blp_next_off.get_tangent1());
490 etl::derivative< etl::hermite<Vector> > deriv(curve);
492 // where would we be on this curve, how wide will we be, and
493 // where will our tangents point (all assuming that we hadn't vanished)
494 blp_here_off.set_vertex(curve(blp_here_on.get_origin()));
495 blp_here_off.set_width((blp_next_off.get_width()-blp_prev_off.get_width())*blp_here_on.get_origin()+blp_prev_off.get_width());
496 blp_here_off.set_tangent1(deriv(blp_here_on.get_origin()));
497 blp_here_off.set_tangent2(deriv(blp_here_on.get_origin()));
499 float prev_tangent_scalar(1.0f);
500 float next_tangent_scalar(1.0f);
502 //synfig::info("index_%d:dist_from_begin=%d",index,dist_from_begin);
503 //synfig::info("index_%d:dist_from_end=%d",index,dist_from_end);
505 // If we are the next to the begin
506 if(begin_iter==--std::vector<ListEntry>::const_iterator(iter) || dist_from_begin==1)
507 prev_tangent_scalar=linear_interpolation(blp_here_on.get_origin(), 1.0f, amount);
509 prev_tangent_scalar=linear_interpolation(blp_here_on.get_origin()-prev.get_origin(), 1.0f, amount);
511 // If we are the next to the end
512 if(end_iter==++std::vector<ListEntry>::const_iterator(iter) || dist_from_end==1)
513 next_tangent_scalar=linear_interpolation(1.0-blp_here_on.get_origin(), 1.0f, amount);
514 else if(list.end()!=++std::vector<ListEntry>::const_iterator(iter))
517 next=((*(++std::vector<ListEntry>::const_iterator(iter))->value_node)(t).get(prev));
518 next_tangent_scalar=linear_interpolation(next.get_origin()-blp_here_on.get_origin(), 1.0f, amount);
521 //! \todo this isn't quite right; we should handle looped blines identically no matter where the loop happens
522 //! and we currently don't. this at least makes it a lot better than it was before
523 next_tangent_scalar=linear_interpolation(blp_next_off.get_origin()-blp_here_on.get_origin(), 1.0f, amount);
524 next_scale=next_tangent_scalar;
526 //blp_here_now.set_vertex(linear_interpolation(blp_here_off.get_vertex(), blp_here_on.get_vertex(), amount));
530 // Point ref_point_begin(((*begin_iter->value_node)(off_time).get(prev).get_vertex() +
531 // (*end_iter->value_node)(off_time).get(prev).get_vertex()) * 0.5);
532 // Point ref_point_end(((*begin_iter->value_node)(on_time).get(prev).get_vertex() +
533 // (*end_iter->value_node)(on_time).get(prev).get_vertex()) * 0.5);
534 // Point ref_point_now(((*begin_iter->value_node)(t).get(prev).get_vertex() +
535 // (*end_iter->value_node)(t).get(prev).get_vertex()) * 0.5);
536 // Point ref_point_linear(linear_interpolation(ref_point_begin, ref_point_end, amount));
538 // blp_here_now.set_vertex(linear_interpolation(blp_here_off.get_vertex(), blp_here_on.get_vertex(), amount) +
539 // (ref_point_now-ref_point_linear));
540 // blp_here_now.set_tangent1(linear_interpolation(blp_here_off.get_tangent1(), blp_here_on.get_tangent1(), amount));
541 // blp_here_now.set_split_tangent_flag(blp_here_on.get_split_tangent_flag());
542 // if(blp_here_now.get_split_tangent_flag())
543 // blp_here_now.set_tangent2(linear_interpolation(blp_here_off.get_tangent2(), blp_here_on.get_tangent2(), amount));
549 // define 3 coordinate systems:
550 Point off_coord_sys[2], off_coord_origin; // when the current vertex is completely off
551 Point on_coord_sys[2] , on_coord_origin; // when the current vertex is completely on
552 Point curr_coord_sys[2], curr_coord_origin; // the current state - somewhere in between
554 // for each of the 3 systems, the origin is half way between the previous and next active point
555 // and the axes are based on a vector from the next active point to the previous
557 const Point end_pos_at_off_time(( *end_iter->value_node)(off_time).get(prev).get_vertex());
558 const Point begin_pos_at_off_time((*begin_iter->value_node)(off_time).get(prev).get_vertex());
559 off_coord_origin=(begin_pos_at_off_time + end_pos_at_off_time)/2;
560 off_coord_sys[0]=(begin_pos_at_off_time - end_pos_at_off_time).norm();
561 off_coord_sys[1]=off_coord_sys[0].perp();
563 const Point end_pos_at_on_time(( *end_iter->value_node)(on_time).get(prev).get_vertex());
564 const Point begin_pos_at_on_time((*begin_iter->value_node)(on_time).get(prev).get_vertex());
565 on_coord_origin=(begin_pos_at_on_time + end_pos_at_on_time)/2;
566 on_coord_sys[0]=(begin_pos_at_on_time - end_pos_at_on_time).norm();
567 on_coord_sys[1]=on_coord_sys[0].perp();
569 const Point end_pos_at_current_time(( *end_iter->value_node)(t).get(prev).get_vertex());
570 const Point begin_pos_at_current_time((*begin_iter->value_node)(t).get(prev).get_vertex());
571 curr_coord_origin=(begin_pos_at_current_time + end_pos_at_current_time)/2;
572 curr_coord_sys[0]=(begin_pos_at_current_time - end_pos_at_current_time).norm();
573 curr_coord_sys[1]=curr_coord_sys[0].perp();
575 // Invert (transpose) the last of these matrices, since we use it for transform back
576 swap(curr_coord_sys[0][1],curr_coord_sys[1][0]);
579 /* The code that was here before used just end_iter as the origin, rather than the mid-point */
581 // We know our location and tangent(s) when fully on and fully off
582 // Transform each of these into their corresponding coordinate system
583 Point trans_on_point, trans_off_point;
584 Vector trans_on_t1, trans_on_t2, trans_off_t1, trans_off_t2;
586 transform_coords(blp_here_on.get_vertex(), trans_on_point, on_coord_origin, on_coord_sys);
587 transform_coords(blp_here_off.get_vertex(), trans_off_point, off_coord_origin, off_coord_sys);
589 #define COORD_SYS_RADIAL_TAN_INTERP 1
591 #ifdef COORD_SYS_RADIAL_TAN_INTERP
592 transform_coords(blp_here_on.get_tangent1(), trans_on_t1, Point::zero(), on_coord_sys);
593 transform_coords(blp_here_off.get_tangent1(), trans_off_t1, Point::zero(), off_coord_sys);
595 if(blp_here_on.get_split_tangent_flag())
597 transform_coords(blp_here_on.get_tangent2(), trans_on_t2, Point::zero(), on_coord_sys);
598 transform_coords(blp_here_off.get_tangent2(), trans_off_t2, Point::zero(), off_coord_sys);
603 // Interpolate between the 'on' point and the 'off' point and untransform to get our point's location
605 untransform_coords(linear_interpolation(trans_off_point, trans_on_point, amount),
606 tmp, curr_coord_origin, curr_coord_sys);
607 blp_here_now.set_vertex(tmp);
610 #define INTERP_FUNCTION radial_interpolation
611 //#define INTERP_FUNCTION linear_interpolation
613 #ifdef COORD_SYS_RADIAL_TAN_INTERP
616 untransform_coords(INTERP_FUNCTION(trans_off_t1,trans_on_t1,amount), tmp, Point::zero(), curr_coord_sys);
617 blp_here_now.set_tangent1(tmp);
620 blp_here_now.set_tangent1(radial_interpolation(blp_here_off.get_tangent1(),blp_here_on.get_tangent1(),amount));
623 if (blp_here_on.get_split_tangent_flag())
625 blp_here_now.set_split_tangent_flag(true);
626 #ifdef COORD_SYS_RADIAL_TAN_INTERP
629 untransform_coords(INTERP_FUNCTION(trans_off_t2,trans_on_t2,amount), tmp, Point::zero(), curr_coord_sys);
630 blp_here_now.set_tangent2(tmp);
633 blp_here_now.set_tangent2(radial_interpolation(blp_here_off.get_tangent2(),blp_here_on.get_tangent2(),amount));
637 blp_here_now.set_split_tangent_flag(false);
640 blp_here_now.set_origin(blp_here_on.get_origin());
641 blp_here_now.set_width(linear_interpolation(blp_here_off.get_width(), blp_here_on.get_width(), amount));
643 // Handle the case where we are the first vertex
646 blp_here_now.set_tangent1(blp_here_now.get_tangent1()*prev_tangent_scalar);
648 first=prev=blp_here_now;
650 ret_list.push_back(blp_here_now);
654 ret_list.back().set_split_tangent_flag(true);
655 ret_list.back().set_tangent2(prev.get_tangent2()*prev_tangent_scalar);
656 ret_list.push_back(blp_here_now);
657 ret_list.back().set_split_tangent_flag(true);
658 //ret_list.back().set_tangent2(blp_here_now.get_tangent1());
659 ret_list.back().set_tangent1(blp_here_now.get_tangent1()*prev_tangent_scalar);
667 ret_list.back().set_split_tangent_flag(true);
668 ret_list.back().set_tangent2(prev.get_tangent2()*next_scale);
672 if(get_loop() && !first_flag)
686 synfig::warning(string("ValueNode_BLine::operator()():")+_("No entries in list"));
689 synfig::warning(string("ValueNode_BLine::operator()():")+_("No entries in ret_list"));
691 return ValueBase(ret_list,get_loop());
695 ValueNode_BLine::link_local_name(int i)const
697 assert(i>=0 && (unsigned)i<list.size());
698 return etl::strprintf(_("Vertex %03d"),i+1);
702 ValueNode_BLine::clone(const GUID& deriv_guid)const
704 { ValueNode* x(find_value_node(get_guid()^deriv_guid).get()); if(x)return x; }
706 ValueNode_BLine* ret=new ValueNode_BLine();
707 ret->set_guid(get_guid()^deriv_guid);
709 std::vector<ListEntry>::const_iterator iter;
711 for(iter=list.begin();iter!=list.end();++iter)
713 if(iter->value_node->is_exported())
717 ListEntry list_entry(*iter);
718 //list_entry.value_node=find_value_node(iter->value_node->get_guid()^deriv_guid).get();
719 //if(!list_entry.value_node)
720 list_entry.value_node=iter->value_node->clone(deriv_guid);
721 ret->add(list_entry);
722 //ret->list.back().value_node=iter->value_node.clone();
725 ret->set_loop(get_loop());
731 ValueNode_BLine::get_name()const
737 ValueNode_BLine::get_local_name()const
743 ValueNode_BLine::create_new()const
750 ValueNode_BLine::check_type(ValueBase::Type type)
752 return type==ValueBase::TYPE_LIST;