1 /* === S Y N F I G ========================================================= */
2 /*! \file valuenode_blinecalctangent.cpp
3 ** \brief Implementation of the "BLine Tangent" valuenode conversion.
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2007, 2008 Chris Moore
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.
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.
22 /* ========================================================================= */
24 /* === H E A D E R S ======================================================= */
33 #include "valuenode_blinecalctangent.h"
34 #include "valuenode_bline.h"
35 #include "valuenode_const.h"
36 #include "valuenode_composite.h"
38 #include "exception.h"
39 #include <ETL/hermite>
40 #include <ETL/calculus>
44 /* === U S I N G =========================================================== */
48 using namespace synfig;
50 /* === M A C R O S ========================================================= */
52 /* === G L O B A L S ======================================================= */
54 /* === P R O C E D U R E S ================================================= */
56 /* === M E T H O D S ======================================================= */
58 ValueNode_BLineCalcTangent::ValueNode_BLineCalcTangent(const ValueBase::Type &x):
61 if(x!=ValueBase::TYPE_ANGLE && x!=ValueBase::TYPE_REAL && x!=ValueBase::TYPE_VECTOR)
62 throw Exception::BadType(ValueBase::type_local_name(x));
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)));
74 ValueNode_BLineCalcTangent::create_new()const
76 return new ValueNode_BLineCalcTangent(get_type());
79 ValueNode_BLineCalcTangent*
80 ValueNode_BLineCalcTangent::create(const ValueBase &x)
82 return new ValueNode_BLineCalcTangent(x.get_type());
85 ValueNode_BLineCalcTangent::~ValueNode_BLineCalcTangent()
91 ValueNode_BLineCalcTangent::operator()(Time t)const
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 Real amount((*amount_)(t).get(Real()));
99 Angle offset((*offset_)(t).get(Angle()));
100 Real scale((*scale_)(t).get(Real()));
101 bool fixed_length((*fixed_length_)(t).get(bool()));
102 BLinePoint blinepoint0, blinepoint1;
108 case ValueBase::TYPE_ANGLE: return Angle();
109 case ValueBase::TYPE_REAL: return Real();
110 case ValueBase::TYPE_VECTOR: return Vector();
111 default: assert(0); return ValueBase();
115 amount = amount - int(amount);
116 if (amount < 0) amount++;
120 if (amount < 0) amount = 0;
121 if (amount > 1) amount = 1;
124 vector<ValueBase>::const_iterator iter, next(bline.begin());
126 iter = looped ? --bline.end() : next++;
127 amount = amount * size;
128 from_vertex = int(amount);
129 if (from_vertex > size-1) from_vertex = size-1;
130 blinepoint0 = from_vertex ? *(next+from_vertex-1) : *iter;
131 blinepoint1 = *(next+from_vertex);
133 etl::hermite<Vector> curve(blinepoint0.get_vertex(), blinepoint1.get_vertex(),
134 blinepoint0.get_tangent2(), blinepoint1.get_tangent1());
135 etl::derivative< etl::hermite<Vector> > deriv(curve);
139 case ValueBase::TYPE_ANGLE: return deriv(amount-from_vertex).angle() + offset;
140 case ValueBase::TYPE_REAL:
142 if (fixed_length) return scale;
143 return deriv(amount-from_vertex).mag() * scale;
145 case ValueBase::TYPE_VECTOR:
147 Vector tangent(deriv(amount-from_vertex));
148 Angle angle(tangent.angle() + offset);
149 Real mag = fixed_length ? scale : (tangent.mag() * scale);
150 return Vector(Angle::cos(angle).get()*mag,
151 Angle::sin(angle).get()*mag);
153 default: assert(0); return ValueBase();
158 ValueNode_BLineCalcTangent::get_name()const
160 return "blinecalctangent";
164 ValueNode_BLineCalcTangent::get_local_name()const
166 return _("BLine Tangent");
170 ValueNode_BLineCalcTangent::set_link_vfunc(int i,ValueNode::Handle value)
172 assert(i>=0 && i<link_count());
176 case 0: CHECK_TYPE_AND_SET_VALUE(bline_, ValueBase::TYPE_LIST);
177 case 1: CHECK_TYPE_AND_SET_VALUE(loop_, ValueBase::TYPE_BOOL);
178 case 2: CHECK_TYPE_AND_SET_VALUE(amount_, ValueBase::TYPE_REAL);
179 case 3: CHECK_TYPE_AND_SET_VALUE(offset_, ValueBase::TYPE_ANGLE);
180 case 4: CHECK_TYPE_AND_SET_VALUE(scale_, ValueBase::TYPE_REAL);
181 case 5: CHECK_TYPE_AND_SET_VALUE(fixed_length_, ValueBase::TYPE_BOOL);
186 ValueNode::LooseHandle
187 ValueNode_BLineCalcTangent::get_link_vfunc(int i)const
189 assert(i>=0 && i<link_count());
193 case 0: return bline_;
194 case 1: return loop_;
195 case 2: return amount_;
196 case 3: return offset_;
197 case 4: return scale_;
198 case 5: return fixed_length_;
205 ValueNode_BLineCalcTangent::link_count()const
211 ValueNode_BLineCalcTangent::link_name(int i)const
213 assert(i>=0 && i<link_count());
217 case 0: return "bline";
218 case 1: return "loop";
219 case 2: return "amount";
220 case 3: return "offset";
221 case 4: return "scale";
222 case 5: return "fixed_length";
228 ValueNode_BLineCalcTangent::link_local_name(int i)const
230 assert(i>=0 && i<link_count());
234 case 0: return _("BLine");
235 case 1: return _("Loop");
236 case 2: return _("Amount");
237 case 3: return _("Offset");
238 case 4: return _("Scale");
239 case 5: return _("Fixed Length");
245 ValueNode_BLineCalcTangent::get_link_index_from_name(const String &name)const
247 if (name=="bline") return 0;
248 if (name=="loop") return 1;
249 if (name=="amount") return 2;
250 if (name=="offset") return 3;
251 if (name=="scale") return 4;
252 if (name=="fixed_length") return 5;
253 throw Exception::BadLinkName(name);
257 ValueNode_BLineCalcTangent::check_type(ValueBase::Type type)
259 return (type==ValueBase::TYPE_ANGLE ||
260 type==ValueBase::TYPE_REAL ||
261 type==ValueBase::TYPE_VECTOR);