Allow the 'BLine Tangent' to be used to convert angles. The resulting value is the...
[synfig.git] / synfig-core / trunk / src / synfig / valuenode_blinecalctangent.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode_blinecalctangent.cpp
3 **      \brief Template File
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **
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.
14 **
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.
19 **      \endlegal
20 */
21 /* ========================================================================= */
22
23 /* === H E A D E R S ======================================================= */
24
25 #ifdef USING_PCH
26 #       include "pch.h"
27 #else
28 #ifdef HAVE_CONFIG_H
29 #       include <config.h>
30 #endif
31
32 #include "valuenode_blinecalctangent.h"
33 #include "valuenode_bline.h"
34 #include "valuenode_const.h"
35 #include "valuenode_composite.h"
36 #include "general.h"
37 #include "exception.h"
38 #include <ETL/hermite>
39 #include <ETL/calculus>
40
41 #endif
42
43 /* === U S I N G =========================================================== */
44
45 using namespace std;
46 using namespace etl;
47 using namespace synfig;
48
49 /* === M A C R O S ========================================================= */
50
51 /* === G L O B A L S ======================================================= */
52
53 /* === P R O C E D U R E S ================================================= */
54
55 /* === M E T H O D S ======================================================= */
56
57 ValueNode_BLineCalcTangent::ValueNode_BLineCalcTangent(const ValueBase::Type &x):
58         LinkableValueNode(x)
59 {
60         if(x!=ValueBase::TYPE_ANGLE && x!=ValueBase::TYPE_VECTOR)
61                 throw Exception::BadType(ValueBase::type_name(x));
62
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)));
67 }
68
69 LinkableValueNode*
70 ValueNode_BLineCalcTangent::create_new()const
71 {
72         return new ValueNode_BLineCalcTangent(get_type());
73 }
74
75 ValueNode_BLineCalcTangent*
76 ValueNode_BLineCalcTangent::create(const ValueBase &x)
77 {
78         return new ValueNode_BLineCalcTangent(x.get_type());
79 }
80
81 ValueNode_BLineCalcTangent::~ValueNode_BLineCalcTangent()
82 {
83         unlink_all();
84 }
85
86 ValueBase
87 ValueNode_BLineCalcTangent::operator()(Time t)const
88 {
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;
96
97         if (!looped) size--;
98         if (size < 1)
99                 switch (get_type())
100                 {
101                         case ValueBase::TYPE_ANGLE:  return Angle();
102                         case ValueBase::TYPE_VECTOR: return Vector();
103                         default: assert(0); return ValueBase();
104                 }
105         if (loop)
106         {
107                 amount = amount - int(amount);
108                 if (amount < 0) amount++;
109         }
110         else
111         {
112                 if (amount < 0) amount = 0;
113                 if (amount > 1) amount = 1;
114         }
115
116         vector<ValueBase>::const_iterator iter, next(bline.begin());
117
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);
124
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);
128
129 #ifdef ETL_FIXED_DERIVATIVE
130         switch (get_type())
131         {
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();
135         }
136 #else
137         switch (get_type())
138         {
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();
142         }
143 #endif
144 }
145
146 String
147 ValueNode_BLineCalcTangent::get_name()const
148 {
149         return "blinecalctangent";
150 }
151
152 String
153 ValueNode_BLineCalcTangent::get_local_name()const
154 {
155         return _("BLine Tangent");
156 }
157
158 bool
159 ValueNode_BLineCalcTangent::set_link_vfunc(int i,ValueNode::Handle x)
160 {
161         assert(i>=0 && i<link_count());
162         switch(i)
163         {
164                 case 0:
165                         bline_=x;
166                         signal_child_changed()(i);signal_value_changed()();
167                         return true;
168                 case 1:
169                         loop_=x;
170                         signal_child_changed()(i);signal_value_changed()();
171                         return true;
172                 case 2:
173                         amount_=x;
174                         signal_child_changed()(i);signal_value_changed()();
175                         return true;
176         }
177         return false;
178 }
179
180 ValueNode::LooseHandle
181 ValueNode_BLineCalcTangent::get_link_vfunc(int i)const
182 {
183         assert(i>=0 && i<link_count());
184         switch(i)
185         {
186                 case 0: return bline_;
187                 case 1: return loop_;
188                 case 2: return amount_;
189         }
190
191         return 0;
192 }
193
194 int
195 ValueNode_BLineCalcTangent::link_count()const
196 {
197         return 3;
198 }
199
200 String
201 ValueNode_BLineCalcTangent::link_name(int i)const
202 {
203         assert(i>=0 && i<link_count());
204         switch(i)
205         {
206                 case 0: return "bline";
207                 case 1: return "loop";
208                 case 2: return "amount";
209         }
210         return String();
211 }
212
213 String
214 ValueNode_BLineCalcTangent::link_local_name(int i)const
215 {
216         assert(i>=0 && i<link_count());
217         switch(i)
218         {
219                 case 0: return _("BLine");
220                 case 1: return _("Loop");
221                 case 2: return _("Amount");
222         }
223         return String();
224 }
225
226 int
227 ValueNode_BLineCalcTangent::get_link_index_from_name(const String &name)const
228 {
229         if(name=="bline")  return 0;
230         if(name=="loop")   return 1;
231         if(name=="amount") return 2;
232         throw Exception::BadLinkName(name);
233 }
234
235 bool
236 ValueNode_BLineCalcTangent::check_type(ValueBase::Type type)
237 {
238         return (type==ValueBase::TYPE_ANGLE ||
239                         type==ValueBase::TYPE_VECTOR);
240 }