Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / 0.61.09 / 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         const std::vector<ValueBase> bline((*bline_)(t));
94         handle<ValueNode_BLine> bline_value_node(bline_);
95         const bool looped(bline_value_node->get_loop());
96         int size = bline.size(), from_vertex;
97         bool loop((*loop_)(t).get(bool()));
98         Angle offset((*offset_)(t).get(Angle()));
99         Real scale((*scale_)(t).get(Real()));
100         bool fixed_length((*fixed_length_)(t).get(bool()));
101         BLinePoint blinepoint0, blinepoint1;
102
103         if (!looped) size--;
104         if (size < 1)
105                 switch (get_type())
106                 {
107                         case ValueBase::TYPE_ANGLE:  return Angle();
108                         case ValueBase::TYPE_REAL:   return Real();
109                         case ValueBase::TYPE_VECTOR: return Vector();
110                         default: assert(0); return ValueBase();
111                 }
112         if (loop)
113         {
114                 amount = amount - int(amount);
115                 if (amount < 0) amount++;
116         }
117         else
118         {
119                 if (amount < 0) amount = 0;
120                 if (amount > 1) amount = 1;
121         }
122
123         vector<ValueBase>::const_iterator iter, next(bline.begin());
124
125         iter = looped ? --bline.end() : next++;
126         amount = amount * size;
127         from_vertex = int(amount);
128         if (from_vertex > size-1) from_vertex = size-1;
129         blinepoint0 = from_vertex ? *(next+from_vertex-1) : *iter;
130         blinepoint1 = *(next+from_vertex);
131
132         etl::hermite<Vector> curve(blinepoint0.get_vertex(),   blinepoint1.get_vertex(),
133                                                            blinepoint0.get_tangent2(), blinepoint1.get_tangent1());
134         etl::derivative< etl::hermite<Vector> > deriv(curve);
135
136         switch (get_type())
137         {
138                 case ValueBase::TYPE_ANGLE:  return deriv(amount-from_vertex).angle() + offset;
139                 case ValueBase::TYPE_REAL:
140                 {
141                         if (fixed_length) return scale;
142                         return deriv(amount-from_vertex).mag() * scale;
143                 }
144                 case ValueBase::TYPE_VECTOR:
145                 {
146                         Vector tangent(deriv(amount-from_vertex));
147                         Angle angle(tangent.angle() + offset);
148                         Real mag = fixed_length ? scale : (tangent.mag() * scale);
149                         return Vector(Angle::cos(angle).get()*mag,
150                                                   Angle::sin(angle).get()*mag);
151                 }
152                 default: assert(0); return ValueBase();
153         }
154 }
155
156 ValueBase
157 ValueNode_BLineCalcTangent::operator()(Time t)const
158 {
159         Real amount((*amount_)(t).get(Real()));
160         return (*this)(t, amount);
161 }
162
163 String
164 ValueNode_BLineCalcTangent::get_name()const
165 {
166         return "blinecalctangent";
167 }
168
169 String
170 ValueNode_BLineCalcTangent::get_local_name()const
171 {
172         return _("BLine Tangent");
173 }
174
175 bool
176 ValueNode_BLineCalcTangent::set_link_vfunc(int i,ValueNode::Handle value)
177 {
178         assert(i>=0 && i<link_count());
179
180         switch(i)
181         {
182         case 0: CHECK_TYPE_AND_SET_VALUE(bline_,                ValueBase::TYPE_LIST);
183         case 1: CHECK_TYPE_AND_SET_VALUE(loop_,                 ValueBase::TYPE_BOOL);
184         case 2: CHECK_TYPE_AND_SET_VALUE(amount_,               ValueBase::TYPE_REAL);
185         case 3: CHECK_TYPE_AND_SET_VALUE(offset_,               ValueBase::TYPE_ANGLE);
186         case 4: CHECK_TYPE_AND_SET_VALUE(scale_,                ValueBase::TYPE_REAL);
187         case 5: CHECK_TYPE_AND_SET_VALUE(fixed_length_, ValueBase::TYPE_BOOL);
188         }
189         return false;
190 }
191
192 ValueNode::LooseHandle
193 ValueNode_BLineCalcTangent::get_link_vfunc(int i)const
194 {
195         assert(i>=0 && i<link_count());
196
197         switch(i)
198         {
199                 case 0: return bline_;
200                 case 1: return loop_;
201                 case 2: return amount_;
202                 case 3: return offset_;
203                 case 4: return scale_;
204                 case 5: return fixed_length_;
205         }
206
207         return 0;
208 }
209
210 int
211 ValueNode_BLineCalcTangent::link_count()const
212 {
213         return 6;
214 }
215
216 String
217 ValueNode_BLineCalcTangent::link_name(int i)const
218 {
219         assert(i>=0 && i<link_count());
220
221         switch(i)
222         {
223                 case 0: return "bline";
224                 case 1: return "loop";
225                 case 2: return "amount";
226                 case 3: return "offset";
227                 case 4: return "scale";
228                 case 5: return "fixed_length";
229         }
230         return String();
231 }
232
233 String
234 ValueNode_BLineCalcTangent::link_local_name(int i)const
235 {
236         assert(i>=0 && i<link_count());
237
238         switch(i)
239         {
240                 case 0: return _("BLine");
241                 case 1: return _("Loop");
242                 case 2: return _("Amount");
243                 case 3: return _("Offset");
244                 case 4: return _("Scale");
245                 case 5: return _("Fixed Length");
246         }
247         return String();
248 }
249
250 int
251 ValueNode_BLineCalcTangent::get_link_index_from_name(const String &name)const
252 {
253         if (name=="bline")                return 0;
254         if (name=="loop")                 return 1;
255         if (name=="amount")               return 2;
256         if (name=="offset")               return 3;
257         if (name=="scale")                return 4;
258         if (name=="fixed_length") return 5;
259         throw Exception::BadLinkName(name);
260 }
261
262 bool
263 ValueNode_BLineCalcTangent::check_type(ValueBase::Type type)
264 {
265         return (type==ValueBase::TYPE_ANGLE ||
266                         type==ValueBase::TYPE_REAL  ||
267                         type==ValueBase::TYPE_VECTOR);
268 }