Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / synfig_0_61_07_rc3 / 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 **      Copyright (c) 2007 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_VECTOR)
62                 throw Exception::BadType(ValueBase::type_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 }
69
70 LinkableValueNode*
71 ValueNode_BLineCalcTangent::create_new()const
72 {
73         return new ValueNode_BLineCalcTangent(get_type());
74 }
75
76 ValueNode_BLineCalcTangent*
77 ValueNode_BLineCalcTangent::create(const ValueBase &x)
78 {
79         return new ValueNode_BLineCalcTangent(x.get_type());
80 }
81
82 ValueNode_BLineCalcTangent::~ValueNode_BLineCalcTangent()
83 {
84         unlink_all();
85 }
86
87 ValueBase
88 ValueNode_BLineCalcTangent::operator()(Time t)const
89 {
90         const std::vector<ValueBase> bline((*bline_)(t));
91         handle<ValueNode_BLine> bline_value_node(bline_);
92         const bool looped(bline_value_node->get_loop());
93         int size = bline.size(), from_vertex;
94         bool loop((*loop_)(t).get(bool()));
95         Real amount((*amount_)(t).get(Real()));
96         BLinePoint blinepoint0, blinepoint1;
97
98         if (!looped) size--;
99         if (size < 1)
100                 switch (get_type())
101                 {
102                         case ValueBase::TYPE_ANGLE:  return Angle();
103                         case ValueBase::TYPE_VECTOR: return Vector();
104                         default: assert(0); return ValueBase();
105                 }
106         if (loop)
107         {
108                 amount = amount - int(amount);
109                 if (amount < 0) amount++;
110         }
111         else
112         {
113                 if (amount < 0) amount = 0;
114                 if (amount > 1) amount = 1;
115         }
116
117         vector<ValueBase>::const_iterator iter, next(bline.begin());
118
119         iter = looped ? --bline.end() : next++;
120         amount = amount * size;
121         from_vertex = int(amount);
122         if (from_vertex > size-1) from_vertex = size-1;
123         blinepoint0 = from_vertex ? *(next+from_vertex-1) : *iter;
124         blinepoint1 = *(next+from_vertex);
125
126         etl::hermite<Vector> curve(blinepoint0.get_vertex(),   blinepoint1.get_vertex(),
127                                                            blinepoint0.get_tangent2(), blinepoint1.get_tangent1());
128         etl::derivative< etl::hermite<Vector> > deriv(curve);
129
130 #ifdef ETL_FIXED_DERIVATIVE
131         switch (get_type())
132         {
133                 case ValueBase::TYPE_ANGLE:  return (deriv(amount-from_vertex)*(0.5)).angle();
134                 case ValueBase::TYPE_VECTOR: return deriv(amount-from_vertex)*(0.5);
135                 default: assert(0); return ValueBase();
136         }
137 #else
138         switch (get_type())
139         {
140                 case ValueBase::TYPE_ANGLE:  return (deriv(amount-from_vertex)*(-0.5)).angle();
141                 case ValueBase::TYPE_VECTOR: return deriv(amount-from_vertex)*(-0.5);
142                 default: assert(0); return ValueBase();
143         }
144 #endif
145 }
146
147 String
148 ValueNode_BLineCalcTangent::get_name()const
149 {
150         return "blinecalctangent";
151 }
152
153 String
154 ValueNode_BLineCalcTangent::get_local_name()const
155 {
156         return _("BLine Tangent");
157 }
158
159 bool
160 ValueNode_BLineCalcTangent::set_link_vfunc(int i,ValueNode::Handle x)
161 {
162         assert(i>=0 && i<link_count());
163         switch(i)
164         {
165                 case 0:
166                         bline_=x;
167                         signal_child_changed()(i);signal_value_changed()();
168                         return true;
169                 case 1:
170                         loop_=x;
171                         signal_child_changed()(i);signal_value_changed()();
172                         return true;
173                 case 2:
174                         amount_=x;
175                         signal_child_changed()(i);signal_value_changed()();
176                         return true;
177         }
178         return false;
179 }
180
181 ValueNode::LooseHandle
182 ValueNode_BLineCalcTangent::get_link_vfunc(int i)const
183 {
184         assert(i>=0 && i<link_count());
185         switch(i)
186         {
187                 case 0: return bline_;
188                 case 1: return loop_;
189                 case 2: return amount_;
190         }
191
192         return 0;
193 }
194
195 int
196 ValueNode_BLineCalcTangent::link_count()const
197 {
198         return 3;
199 }
200
201 String
202 ValueNode_BLineCalcTangent::link_name(int i)const
203 {
204         assert(i>=0 && i<link_count());
205         switch(i)
206         {
207                 case 0: return "bline";
208                 case 1: return "loop";
209                 case 2: return "amount";
210         }
211         return String();
212 }
213
214 String
215 ValueNode_BLineCalcTangent::link_local_name(int i)const
216 {
217         assert(i>=0 && i<link_count());
218         switch(i)
219         {
220                 case 0: return _("BLine");
221                 case 1: return _("Loop");
222                 case 2: return _("Amount");
223         }
224         return String();
225 }
226
227 int
228 ValueNode_BLineCalcTangent::get_link_index_from_name(const String &name)const
229 {
230         if(name=="bline")  return 0;
231         if(name=="loop")   return 1;
232         if(name=="amount") return 2;
233         throw Exception::BadLinkName(name);
234 }
235
236 bool
237 ValueNode_BLineCalcTangent::check_type(ValueBase::Type type)
238 {
239         return (type==ValueBase::TYPE_ANGLE ||
240                         type==ValueBase::TYPE_VECTOR);
241 }