e5959158b8c854d3f7847874e9a2c8803ce6bc60
[synfig.git] / synfig-core / src / synfig / valuenode_blinecalctangent.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode_blinecalctangent.cpp
3 **      \brief Implementation of the "BLine Tangent" valuenode conversion.
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2007, 2008 Chris Moore
10 **
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.
15 **
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.
20 **      \endlegal
21 */
22 /* ========================================================================= */
23
24 /* === H E A D E R S ======================================================= */
25
26 #ifdef USING_PCH
27 #       include "pch.h"
28 #else
29 #ifdef HAVE_CONFIG_H
30 #       include <config.h>
31 #endif
32
33 #include "valuenode_blinecalctangent.h"
34 #include "valuenode_bline.h"
35 #include "valuenode_const.h"
36 #include "valuenode_composite.h"
37 #include "general.h"
38 #include "exception.h"
39 #include <ETL/hermite>
40 #include <ETL/calculus>
41
42 #endif
43
44 /* === U S I N G =========================================================== */
45
46 using namespace std;
47 using namespace etl;
48 using namespace synfig;
49
50 /* === M A C R O S ========================================================= */
51
52 /* === G L O B A L S ======================================================= */
53
54 /* === P R O C E D U R E S ================================================= */
55
56 /* === M E T H O D S ======================================================= */
57
58 ValueNode_BLineCalcTangent::ValueNode_BLineCalcTangent(const ValueBase::Type &x):
59         LinkableValueNode(x)
60 {
61         if(x!=ValueBase::TYPE_ANGLE && x!=ValueBase::TYPE_REAL && x!=ValueBase::TYPE_VECTOR)
62                 throw Exception::BadType(ValueBase::type_local_name(x));
63
64         ValueNode_BLine* value_node(new ValueNode_BLine());
65         set_link("bline",value_node);
66         set_link("loop",ValueNode_Const::create(bool(false)));
67         set_link("amount",ValueNode_Const::create(Real(0.5)));
68         set_link("offset",ValueNode_Const::create(Angle::deg(0)));
69         set_link("scale",ValueNode_Const::create(Real(1.0)));
70         set_link("fixed_length",ValueNode_Const::create(bool(false)));
71 }
72
73 LinkableValueNode*
74 ValueNode_BLineCalcTangent::create_new()const
75 {
76         return new ValueNode_BLineCalcTangent(get_type());
77 }
78
79 ValueNode_BLineCalcTangent*
80 ValueNode_BLineCalcTangent::create(const ValueBase &x)
81 {
82         return new ValueNode_BLineCalcTangent(x.get_type());
83 }
84
85 ValueNode_BLineCalcTangent::~ValueNode_BLineCalcTangent()
86 {
87         unlink_all();
88 }
89
90 ValueBase
91 ValueNode_BLineCalcTangent::operator()(Time t, Real amount)const
92 {
93         if (getenv("SYNFIG_DEBUG_VALUENODE_OPERATORS"))
94                 printf("%s:%d operator()\n", __FILE__, __LINE__);
95
96         const std::vector<ValueBase> bline((*bline_)(t).get_list());
97         handle<ValueNode_BLine> bline_value_node(bline_);
98         const bool looped(bline_value_node->get_loop());
99         int size = bline.size(), from_vertex;
100         bool loop((*loop_)(t).get(bool()));
101         Angle offset((*offset_)(t).get(Angle()));
102         Real scale((*scale_)(t).get(Real()));
103         bool fixed_length((*fixed_length_)(t).get(bool()));
104         BLinePoint blinepoint0, blinepoint1;
105
106         if (!looped) size--;
107         if (size < 1)
108                 switch (get_type())
109                 {
110                         case ValueBase::TYPE_ANGLE:  return Angle();
111                         case ValueBase::TYPE_REAL:   return Real();
112                         case ValueBase::TYPE_VECTOR: return Vector();
113                         default: assert(0); return ValueBase();
114                 }
115         if (loop)
116         {
117                 amount = amount - int(amount);
118                 if (amount < 0) amount++;
119         }
120         else
121         {
122                 if (amount < 0) amount = 0;
123                 if (amount > 1) amount = 1;
124         }
125
126         vector<ValueBase>::const_iterator iter, next(bline.begin());
127
128         iter = looped ? --bline.end() : next++;
129         amount = amount * size;
130         from_vertex = int(amount);
131         if (from_vertex > size-1) from_vertex = size-1;
132         blinepoint0 = from_vertex ? *(next+from_vertex-1) : *iter;
133         blinepoint1 = *(next+from_vertex);
134
135         etl::hermite<Vector> curve(blinepoint0.get_vertex(),   blinepoint1.get_vertex(),
136                                                            blinepoint0.get_tangent2(), blinepoint1.get_tangent1());
137         etl::derivative< etl::hermite<Vector> > deriv(curve);
138
139         switch (get_type())
140         {
141                 case ValueBase::TYPE_ANGLE:  return deriv(amount-from_vertex).angle() + offset;
142                 case ValueBase::TYPE_REAL:
143                 {
144                         if (fixed_length) return scale;
145                         return deriv(amount-from_vertex).mag() * scale;
146                 }
147                 case ValueBase::TYPE_VECTOR:
148                 {
149                         Vector tangent(deriv(amount-from_vertex));
150                         Angle angle(tangent.angle() + offset);
151                         Real mag = fixed_length ? scale : (tangent.mag() * scale);
152                         return Vector(Angle::cos(angle).get()*mag,
153                                                   Angle::sin(angle).get()*mag);
154                 }
155                 default: assert(0); return ValueBase();
156         }
157 }
158
159 ValueBase
160 ValueNode_BLineCalcTangent::operator()(Time t)const
161 {
162         Real amount((*amount_)(t).get(Real()));
163         return (*this)(t, amount);
164 }
165
166 String
167 ValueNode_BLineCalcTangent::get_name()const
168 {
169         return "blinecalctangent";
170 }
171
172 String
173 ValueNode_BLineCalcTangent::get_local_name()const
174 {
175         return _("BLine Tangent");
176 }
177
178 bool
179 ValueNode_BLineCalcTangent::set_link_vfunc(int i,ValueNode::Handle value)
180 {
181         assert(i>=0 && i<link_count());
182
183         switch(i)
184         {
185         case 0: CHECK_TYPE_AND_SET_VALUE(bline_,                ValueBase::TYPE_LIST);
186         case 1: CHECK_TYPE_AND_SET_VALUE(loop_,                 ValueBase::TYPE_BOOL);
187         case 2: CHECK_TYPE_AND_SET_VALUE(amount_,               ValueBase::TYPE_REAL);
188         case 3: CHECK_TYPE_AND_SET_VALUE(offset_,               ValueBase::TYPE_ANGLE);
189         case 4: CHECK_TYPE_AND_SET_VALUE(scale_,                ValueBase::TYPE_REAL);
190         case 5: CHECK_TYPE_AND_SET_VALUE(fixed_length_, ValueBase::TYPE_BOOL);
191         }
192         return false;
193 }
194
195 ValueNode::LooseHandle
196 ValueNode_BLineCalcTangent::get_link_vfunc(int i)const
197 {
198         assert(i>=0 && i<link_count());
199
200         switch(i)
201         {
202                 case 0: return bline_;
203                 case 1: return loop_;
204                 case 2: return amount_;
205                 case 3: return offset_;
206                 case 4: return scale_;
207                 case 5: return fixed_length_;
208         }
209
210         return 0;
211 }
212
213 int
214 ValueNode_BLineCalcTangent::link_count()const
215 {
216         return 6;
217 }
218
219 String
220 ValueNode_BLineCalcTangent::link_name(int i)const
221 {
222         assert(i>=0 && i<link_count());
223
224         switch(i)
225         {
226                 case 0: return "bline";
227                 case 1: return "loop";
228                 case 2: return "amount";
229                 case 3: return "offset";
230                 case 4: return "scale";
231                 case 5: return "fixed_length";
232         }
233         return String();
234 }
235
236 String
237 ValueNode_BLineCalcTangent::link_local_name(int i)const
238 {
239         assert(i>=0 && i<link_count());
240
241         switch(i)
242         {
243                 case 0: return _("BLine");
244                 case 1: return _("Loop");
245                 case 2: return _("Amount");
246                 case 3: return _("Offset");
247                 case 4: return _("Scale");
248                 case 5: return _("Fixed Length");
249         }
250         return String();
251 }
252
253 int
254 ValueNode_BLineCalcTangent::get_link_index_from_name(const String &name)const
255 {
256         if (name=="bline")                return 0;
257         if (name=="loop")                 return 1;
258         if (name=="amount")               return 2;
259         if (name=="offset")               return 3;
260         if (name=="scale")                return 4;
261         if (name=="fixed_length") return 5;
262         throw Exception::BadLinkName(name);
263 }
264
265 bool
266 ValueNode_BLineCalcTangent::check_type(ValueBase::Type type)
267 {
268         return (type==ValueBase::TYPE_ANGLE ||
269                         type==ValueBase::TYPE_REAL  ||
270                         type==ValueBase::TYPE_VECTOR);
271 }
272
273 LinkableValueNode::Vocab
274 ValueNode_BLineCalcTangent::get_param_vocab()const
275 {
276         LinkableValueNode::Vocab ret;
277
278         ret.push_back(ParamDesc(ValueBase(),"bline")
279                 .set_local_name(_("BLine"))
280                 .set_description(_("The BLine where the tangent is linked to"))
281         );
282
283         ret.push_back(ParamDesc(ValueBase(),"loop")
284                 .set_local_name(_("Loop"))
285                 .set_description(_("When checked, the amount would loop"))
286         );
287
288         ret.push_back(ParamDesc(ValueBase(),"amount")
289                 .set_local_name(_("Amount"))
290                 .set_description(_("The position of the linked tangent on the BLine (0,1]"))
291         );
292
293         ret.push_back(ParamDesc(ValueBase(),"offset")
294                 .set_local_name(_("Offset"))
295                 .set_description(_("Angle offset of the tangent"))
296         );
297
298         ret.push_back(ParamDesc(ValueBase(),"scale")
299                 .set_local_name(_("Scale"))
300                 .set_description(_("Scale of the tangent"))
301         );
302
303         ret.push_back(ParamDesc(ValueBase(),"fixed_length")
304                 .set_local_name(_("Fixed Length"))
305                 .set_description(_("When checked, the tangent's length is fixed"))
306         );
307         return ret;
308 }