1 /* === S Y N F I G ========================================================= */
2 /*! \file valuenode_blinecalctangent.cpp
3 ** \brief Template File
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
10 ** This package is free software; you can redistribute it and/or
11 ** modify it under the terms of the GNU General Public License as
12 ** published by the Free Software Foundation; either version 2 of
13 ** the License, or (at your option) any later version.
15 ** This package is distributed in the hope that it will be useful,
16 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ** General Public License for more details.
21 /* ========================================================================= */
23 /* === H E A D E R S ======================================================= */
32 #include "valuenode_blinecalctangent.h"
33 #include "valuenode_bline.h"
34 #include "valuenode_const.h"
35 #include "valuenode_composite.h"
37 #include "exception.h"
38 #include <ETL/hermite>
39 #include <ETL/calculus>
43 /* === U S I N G =========================================================== */
47 using namespace synfig;
49 /* === M A C R O S ========================================================= */
51 /* === G L O B A L S ======================================================= */
53 /* === P R O C E D U R E S ================================================= */
55 /* === M E T H O D S ======================================================= */
57 ValueNode_BLineCalcTangent::ValueNode_BLineCalcTangent(const ValueBase::Type &x):
60 if(x!=ValueBase::TYPE_ANGLE && x!=ValueBase::TYPE_VECTOR)
61 throw Exception::BadType(ValueBase::type_name(x));
63 ValueNode_BLine* value_node(new ValueNode_BLine());
64 set_link("bline",value_node);
65 set_link("loop",ValueNode_Const::create(bool(false)));
66 set_link("amount",ValueNode_Const::create(Real(0.5)));
70 ValueNode_BLineCalcTangent::create_new()const
72 return new ValueNode_BLineCalcTangent(get_type());
75 ValueNode_BLineCalcTangent*
76 ValueNode_BLineCalcTangent::create(const ValueBase &x)
78 return new ValueNode_BLineCalcTangent(x.get_type());
81 ValueNode_BLineCalcTangent::~ValueNode_BLineCalcTangent()
87 ValueNode_BLineCalcTangent::operator()(Time t)const
89 const std::vector<ValueBase> bline((*bline_)(t));
90 handle<ValueNode_BLine> bline_value_node(bline_);
91 const bool looped(bline_value_node->get_loop());
92 int size = bline.size(), from_vertex;
93 bool loop((*loop_)(t).get(bool()));
94 Real amount((*amount_)(t).get(Real()));
95 BLinePoint blinepoint0, blinepoint1;
101 case ValueBase::TYPE_ANGLE: return Angle();
102 case ValueBase::TYPE_VECTOR: return Vector();
103 default: assert(0); return ValueBase();
107 amount = amount - int(amount);
108 if (amount < 0) amount++;
112 if (amount < 0) amount = 0;
113 if (amount > 1) amount = 1;
116 vector<ValueBase>::const_iterator iter, next(bline.begin());
118 iter = looped ? --bline.end() : next++;
119 amount = amount * size;
120 from_vertex = int(amount);
121 if (from_vertex > size-1) from_vertex = size-1;
122 blinepoint0 = from_vertex ? *(next+from_vertex-1) : *iter;
123 blinepoint1 = *(next+from_vertex);
125 etl::hermite<Vector> curve(blinepoint0.get_vertex(), blinepoint1.get_vertex(),
126 blinepoint0.get_tangent2(), blinepoint1.get_tangent1());
127 etl::derivative< etl::hermite<Vector> > deriv(curve);
129 #ifdef ETL_FIXED_DERIVATIVE
132 case ValueBase::TYPE_ANGLE: return (deriv(amount-from_vertex)*(0.5)).angle();
133 case ValueBase::TYPE_VECTOR: return deriv(amount-from_vertex)*(0.5);
134 default: assert(0); return ValueBase();
139 case ValueBase::TYPE_ANGLE: return (deriv(amount-from_vertex)*(-0.5)).angle();
140 case ValueBase::TYPE_VECTOR: return deriv(amount-from_vertex)*(-0.5);
141 default: assert(0); return ValueBase();
147 ValueNode_BLineCalcTangent::get_name()const
149 return "blinecalctangent";
153 ValueNode_BLineCalcTangent::get_local_name()const
155 return _("BLine Tangent");
159 ValueNode_BLineCalcTangent::set_link_vfunc(int i,ValueNode::Handle x)
161 assert(i>=0 && i<link_count());
166 signal_child_changed()(i);signal_value_changed()();
170 signal_child_changed()(i);signal_value_changed()();
174 signal_child_changed()(i);signal_value_changed()();
180 ValueNode::LooseHandle
181 ValueNode_BLineCalcTangent::get_link_vfunc(int i)const
183 assert(i>=0 && i<link_count());
186 case 0: return bline_;
187 case 1: return loop_;
188 case 2: return amount_;
195 ValueNode_BLineCalcTangent::link_count()const
201 ValueNode_BLineCalcTangent::link_name(int i)const
203 assert(i>=0 && i<link_count());
206 case 0: return "bline";
207 case 1: return "loop";
208 case 2: return "amount";
214 ValueNode_BLineCalcTangent::link_local_name(int i)const
216 assert(i>=0 && i<link_count());
219 case 0: return _("BLine");
220 case 1: return _("Loop");
221 case 2: return _("Amount");
227 ValueNode_BLineCalcTangent::get_link_index_from_name(const String &name)const
229 if(name=="bline") return 0;
230 if(name=="loop") return 1;
231 if(name=="amount") return 2;
232 throw Exception::BadLinkName(name);
236 ValueNode_BLineCalcTangent::check_type(ValueBase::Type type)
238 return (type==ValueBase::TYPE_ANGLE ||
239 type==ValueBase::TYPE_VECTOR);