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
9 ** Copyright (c) 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_blinecalcvertex.h"
34 #include "valuenode_bline.h"
35 #include "valuenode_const.h"
36 #include "valuenode_composite.h"
38 #include "exception.h"
39 #include <ETL/hermite>
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_BLineCalcVertex::ValueNode_BLineCalcVertex(const ValueBase::Type &x):
61 Vocab ret(get_children_vocab());
62 set_children_vocab(ret);
63 if(x!=ValueBase::TYPE_VECTOR)
64 throw Exception::BadType(ValueBase::type_local_name(x));
66 ValueNode_BLine* value_node(new ValueNode_BLine());
67 set_link("bline",value_node);
68 set_link("loop",ValueNode_Const::create(bool(false)));
69 set_link("amount",ValueNode_Const::create(Real(0.5)));
73 ValueNode_BLineCalcVertex::create_new()const
75 return new ValueNode_BLineCalcVertex(ValueBase::TYPE_VECTOR);
78 ValueNode_BLineCalcVertex*
79 ValueNode_BLineCalcVertex::create(const ValueBase &x)
81 return new ValueNode_BLineCalcVertex(x.get_type());
84 ValueNode_BLineCalcVertex::~ValueNode_BLineCalcVertex()
90 ValueNode_BLineCalcVertex::operator()(Time t)const
92 if (getenv("SYNFIG_DEBUG_VALUENODE_OPERATORS"))
93 printf("%s:%d operator()\n", __FILE__, __LINE__);
95 const std::vector<ValueBase> bline((*bline_)(t).get_list());
96 handle<ValueNode_BLine> bline_value_node(bline_);
97 const bool looped(bline_value_node->get_loop());
98 int size = bline.size(), from_vertex;
99 bool loop((*loop_)(t).get(bool()));
100 Real amount((*amount_)(t).get(Real()));
101 BLinePoint blinepoint0, blinepoint1;
104 if (size < 1) return Vector();
107 amount = amount - int(amount);
108 if (amount < 0) amount++;
112 if (amount < 0) amount = 0;
113 if (amount > 1) amount = 1;
116 vector<ValueBase>::const_iterator iter, next(bline.begin());
118 iter = looped ? --bline.end() : next++;
119 amount = amount * size;
120 from_vertex = int(amount);
121 if (from_vertex > size-1) from_vertex = size-1;
122 blinepoint0 = from_vertex ? *(next+from_vertex-1) : *iter;
123 blinepoint1 = *(next+from_vertex);
125 etl::hermite<Vector> curve(blinepoint0.get_vertex(), blinepoint1.get_vertex(),
126 blinepoint0.get_tangent2(), blinepoint1.get_tangent1());
127 return curve(amount-from_vertex);
137 ValueNode_BLineCalcVertex::get_name()const
139 return "blinecalcvertex";
143 ValueNode_BLineCalcVertex::get_local_name()const
145 return _("BLine Vertex");
149 ValueNode_BLineCalcVertex::set_link_vfunc(int i,ValueNode::Handle value)
151 assert(i>=0 && i<link_count());
155 case 0: CHECK_TYPE_AND_SET_VALUE(bline_, ValueBase::TYPE_LIST);
156 case 1: CHECK_TYPE_AND_SET_VALUE(loop_, ValueBase::TYPE_BOOL);
157 case 2: CHECK_TYPE_AND_SET_VALUE(amount_, ValueBase::TYPE_REAL);
162 ValueNode::LooseHandle
163 ValueNode_BLineCalcVertex::get_link_vfunc(int i)const
165 assert(i>=0 && i<link_count());
169 case 0: return bline_;
170 case 1: return loop_;
171 case 2: return amount_;
178 ValueNode_BLineCalcVertex::check_type(ValueBase::Type type)
180 return type==ValueBase::TYPE_VECTOR;
183 LinkableValueNode::Vocab
184 ValueNode_BLineCalcVertex::get_children_vocab_vfunc()const
186 if(children_vocab.size())
187 return children_vocab;
189 LinkableValueNode::Vocab ret;
191 ret.push_back(ParamDesc(ValueBase(),"bline")
192 .set_local_name(_("BLine"))
193 .set_description(_("The BLine where the vertex is linked to"))
196 ret.push_back(ParamDesc(ValueBase(),"loop")
197 .set_local_name(_("Loop"))
198 .set_description(_("When checked, the amount would loop"))
201 ret.push_back(ParamDesc(ValueBase(),"amount")
202 .set_local_name(_("Amount"))
203 .set_description(_("The position of the linked vertex on the BLine (0,1]"))