1 /* === S Y N F I G ========================================================= */
2 /*! \file valuenode_blinecalcvertex.cpp
3 ** \brief Implementation of the "BLine Vertex" valuenode conversion.
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
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.
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.
21 /* ========================================================================= */
23 /* === H E A D E R S ======================================================= */
32 #include "valuenode_blinecalcvertex.h"
33 #include "valuenode_bline.h"
34 #include "valuenode_const.h"
35 #include "valuenode_composite.h"
37 #include "exception.h"
38 #include <ETL/hermite>
43 /* === U S I N G =========================================================== */
47 using namespace synfig;
49 /* === M A C R O S ========================================================= */
51 /* === G L O B A L S ======================================================= */
53 /* === P R O C E D U R E S ================================================= */
55 /* === M E T H O D S ======================================================= */
57 ValueNode_BLineCalcVertex::ValueNode_BLineCalcVertex(const ValueBase::Type &x):
60 if(x!=ValueBase::TYPE_VECTOR)
61 throw Exception::BadType(ValueBase::type_local_name(x));
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)));
70 ValueNode_BLineCalcVertex::create_new()const
72 return new ValueNode_BLineCalcVertex(ValueBase::TYPE_VECTOR);
75 ValueNode_BLineCalcVertex*
76 ValueNode_BLineCalcVertex::create(const ValueBase &x)
78 return new ValueNode_BLineCalcVertex(x.get_type());
81 ValueNode_BLineCalcVertex::~ValueNode_BLineCalcVertex()
87 ValueNode_BLineCalcVertex::operator()(Time t)const
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;
98 if (size < 1) return Vector();
101 amount = amount - int(amount);
102 if (amount < 0) amount++;
106 if (amount < 0) amount = 0;
107 if (amount > 1) amount = 1;
110 vector<ValueBase>::const_iterator iter, next(bline.begin());
112 iter = looped ? --bline.end() : next++;
113 amount = amount * size;
114 from_vertex = int(amount);
115 if (from_vertex > size-1) from_vertex = size-1;
116 blinepoint0 = from_vertex ? *(next+from_vertex-1) : *iter;
117 blinepoint1 = *(next+from_vertex);
119 etl::hermite<Vector> curve(blinepoint0.get_vertex(), blinepoint1.get_vertex(),
120 blinepoint0.get_tangent2(), blinepoint1.get_tangent1());
121 return curve(amount-from_vertex);
131 ValueNode_BLineCalcVertex::get_name()const
133 return "blinecalcvertex";
137 ValueNode_BLineCalcVertex::get_local_name()const
139 return _("BLine Vertex");
143 ValueNode_BLineCalcVertex::set_link_vfunc(int i,ValueNode::Handle value)
145 assert(i>=0 && i<link_count());
149 case 0: CHECK_TYPE_AND_SET_VALUE(bline_, ValueBase::TYPE_LIST);
150 case 1: CHECK_TYPE_AND_SET_VALUE(loop_, ValueBase::TYPE_BOOL);
151 case 2: CHECK_TYPE_AND_SET_VALUE(amount_, ValueBase::TYPE_REAL);
156 ValueNode::LooseHandle
157 ValueNode_BLineCalcVertex::get_link_vfunc(int i)const
159 assert(i>=0 && i<link_count());
163 case 0: return bline_;
164 case 1: return loop_;
165 case 2: return amount_;
172 ValueNode_BLineCalcVertex::link_count()const
178 ValueNode_BLineCalcVertex::link_name(int i)const
180 assert(i>=0 && i<link_count());
184 case 0: return "bline";
185 case 1: return "loop";
186 case 2: return "amount";
192 ValueNode_BLineCalcVertex::link_local_name(int i)const
194 assert(i>=0 && i<link_count());
198 case 0: return _("BLine");
199 case 1: return _("Loop");
200 case 2: return _("Amount");
206 ValueNode_BLineCalcVertex::get_link_index_from_name(const String &name)const
208 if(name=="bline") return 0;
209 if(name=="loop") return 1;
210 if(name=="amount") return 2;
211 throw Exception::BadLinkName(name);
215 ValueNode_BLineCalcVertex::check_type(ValueBase::Type type)
217 return type==ValueBase::TYPE_VECTOR;