Blame synfig-core/src/synfig/valuenodes/valuenode_blinecalctangent.cpp

Carlos Lopez a09598
/* === S Y N F I G ========================================================= */
Carlos Lopez a09598
/*!	\file valuenode_blinecalctangent.cpp
Carlos Lopez a09598
**	\brief Implementation of the "BLine Tangent" valuenode conversion.
Carlos Lopez a09598
**
Carlos Lopez a09598
**	$Id$
Carlos Lopez a09598
**
Carlos Lopez a09598
**	\legal
Carlos Lopez a09598
**	Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
Carlos Lopez a09598
**	Copyright (c) 2007, 2008 Chris Moore
Carlos Lopez 42fd7a
**  Copyright (c) 2011 Carlos Lรณpez
Carlos Lopez a09598
**
Carlos Lopez a09598
**	This package is free software; you can redistribute it and/or
Carlos Lopez a09598
**	modify it under the terms of the GNU General Public License as
Carlos Lopez a09598
**	published by the Free Software Foundation; either version 2 of
Carlos Lopez a09598
**	the License, or (at your option) any later version.
Carlos Lopez a09598
**
Carlos Lopez a09598
**	This package is distributed in the hope that it will be useful,
Carlos Lopez a09598
**	but WITHOUT ANY WARRANTY; without even the implied warranty of
Carlos Lopez a09598
**	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Carlos Lopez a09598
**	General Public License for more details.
Carlos Lopez a09598
**	\endlegal
Carlos Lopez a09598
*/
Carlos Lopez a09598
/* ========================================================================= */
Carlos Lopez a09598
Carlos Lopez a09598
/* === H E A D E R S ======================================================= */
Carlos Lopez a09598
Carlos Lopez a09598
#ifdef USING_PCH
Carlos Lopez a09598
#	include "pch.h"
Carlos Lopez a09598
#else
Carlos Lopez a09598
#ifdef HAVE_CONFIG_H
Carlos Lopez a09598
#	include <config.h></config.h>
Carlos Lopez a09598
#endif
Carlos Lopez a09598
Carlos Lopez a09598
#include "valuenode_blinecalctangent.h"
Carlos Lopez a09598
#include "valuenode_bline.h"
Carlos Lopez a09598
#include "valuenode_const.h"
Carlos Lopez a09598
#include "valuenode_composite.h"
Diego Barrios Romero 07d34c
#include <synfig general.h=""></synfig>
bw 94d8a6
#include <synfig localization.h=""></synfig>
caryoscelus 5eb54b
#include <synfig valuenode_registry.h=""></synfig>
Diego Barrios Romero 07d34c
#include <synfig exception.h=""></synfig>
Carlos Lopez a09598
#include <etl hermite=""></etl>
Carlos Lopez a09598
#include <etl calculus=""></etl>
Carlos Lopez a09598
Carlos Lopez a09598
#endif
Carlos Lopez a09598
Carlos Lopez a09598
/* === U S I N G =========================================================== */
Carlos Lopez a09598
Carlos Lopez a09598
using namespace std;
Carlos Lopez a09598
using namespace etl;
Carlos Lopez a09598
using namespace synfig;
Carlos Lopez a09598
Carlos Lopez a09598
/* === M A C R O S ========================================================= */
Carlos Lopez a09598
Carlos Lopez a09598
/* === G L O B A L S ======================================================= */
Carlos Lopez a09598
caryoscelus 5eb54b
REGISTER_VALUENODE(ValueNode_BLineCalcTangent, RELEASE_VERSION_0_61_07, "blinecalctangent", "Spline Tangent")
caryoscelus 5eb54b
Carlos Lopez a09598
/* === P R O C E D U R E S ================================================= */
Carlos Lopez a09598
Carlos Lopez a09598
/* === M E T H O D S ======================================================= */
Carlos Lopez a09598
d89783
ValueNode_BLineCalcTangent::ValueNode_BLineCalcTangent(Type &x):
Carlos Lopez a09598
	LinkableValueNode(x)
Carlos Lopez a09598
{
Carlos Lopez dbc88f
	Vocab ret(get_children_vocab());
Carlos Lopez dbc88f
	set_children_vocab(ret);
d89783
	if(x!=type_angle && x!=type_real && x!=type_vector)
d89783
		throw Exception::BadType(x.description.local_name);
Carlos Lopez a09598
Carlos Lopez a09598
	ValueNode_BLine* value_node(new ValueNode_BLine());
Carlos Lopez a09598
	set_link("bline",value_node);
Carlos Lopez a09598
	set_link("loop",ValueNode_Const::create(bool(false)));
Carlos Lopez a09598
	set_link("amount",ValueNode_Const::create(Real(0.5)));
Carlos Lopez a09598
	set_link("offset",ValueNode_Const::create(Angle::deg(0)));
Carlos Lopez a09598
	set_link("scale",ValueNode_Const::create(Real(1.0)));
Carlos Lopez a09598
	set_link("fixed_length",ValueNode_Const::create(bool(false)));
Carlos Lopez 1d78ec
	set_link("homogeneous",ValueNode_Const::create(bool(true)));
Carlos Lopez a09598
}
Carlos Lopez a09598
Carlos Lopez a09598
LinkableValueNode*
Carlos Lopez a09598
ValueNode_BLineCalcTangent::create_new()const
Carlos Lopez a09598
{
Carlos Lopez a09598
	return new ValueNode_BLineCalcTangent(get_type());
Carlos Lopez a09598
}
Carlos Lopez a09598
Carlos Lopez a09598
ValueNode_BLineCalcTangent*
Carlos Lopez a09598
ValueNode_BLineCalcTangent::create(const ValueBase &x)
Carlos Lopez a09598
{
Carlos Lopez a09598
	return new ValueNode_BLineCalcTangent(x.get_type());
Carlos Lopez a09598
}
Carlos Lopez a09598
Carlos Lopez a09598
ValueNode_BLineCalcTangent::~ValueNode_BLineCalcTangent()
Carlos Lopez a09598
{
Carlos Lopez a09598
	unlink_all();
Carlos Lopez a09598
}
Carlos Lopez a09598
Carlos Lopez a09598
ValueBase
Carlos Lopez a09598
ValueNode_BLineCalcTangent::operator()(Time t, Real amount)const
Carlos Lopez a09598
{
Carlos Lopez a09598
	if (getenv("SYNFIG_DEBUG_VALUENODE_OPERATORS"))
Carlos Lopez a09598
		printf("%s:%d operator()\n", __FILE__, __LINE__);
Carlos Lopez a09598
a90d01
	const ValueBase::List bline = (*bline_)(t).get_list();
ceaaaf
	handle<valuenode_bline> bline_value_node( handle<valuenode_bline>::cast_dynamic(bline_) );</valuenode_bline></valuenode_bline>
ceaaaf
	assert(bline_value_node);
ceaaaf
a90d01
	const bool looped = bline_value_node->get_loop();
a90d01
	int size = (int)bline.size();
a90d01
	int count = looped ? size : size - 1;
a90d01
	if (count < 1) return Vector();
Carlos Lopez a09598
a90d01
	bool loop         = (*loop_)(t).get(bool());
a90d01
	bool homogeneous  = (*homogeneous_)(t).get(bool());
a90d01
	Angle offset      = (*offset_)(t).get(Angle());
a90d01
	Real scale        = (*scale_)(t).get(Real());
a90d01
	bool fixed_length = (*fixed_length_)(t).get(bool());
a90d01
a90d01
	if (loop) amount -= floor(amount);
a90d01
	if (homogeneous) amount = hom_to_std(bline, amount, loop, looped);
a90d01
	if (amount < 0) amount = 0;
a90d01
	if (amount > 1) amount = 1;
a90d01
	amount *= count;
Carlos Lopez a09598
a90d01
	int i0 = std::max(0, std::min(size-1, (int)floor(amount)));
a90d01
	int i1 = (i0 + 1) % size;
a90d01
	Real part = amount - i0;
Carlos Lopez a09598
a90d01
	const BLinePoint &blinepoint0 = bline[i0].get(BLinePoint());
a90d01
	const BLinePoint &blinepoint1 = bline[i1].get(BLinePoint());
Carlos Lopez a09598
Carlos Lopez a09598
	etl::hermite<vector> curve(blinepoint0.get_vertex(),   blinepoint1.get_vertex(),</vector>
Carlos Lopez a09598
							   blinepoint0.get_tangent2(), blinepoint1.get_tangent1());
Carlos Lopez a09598
	etl::derivative< etl::hermite<vector> > deriv(curve);</vector>
Carlos Lopez a09598
a90d01
	Vector tangent = deriv(part);
a90d01
d89783
	Type &type(get_type());
d89783
	if (type == type_angle)
a90d01
		return tangent.angle() + offset;
a90d01
	if (type == type_real) {
d89783
		if (fixed_length) return scale;
a90d01
		return tangent.mag() * scale;
Carlos Lopez a09598
	}
a90d01
	if (type == type_vector) {
d89783
		Angle angle(tangent.angle() + offset);
d89783
		Real mag = fixed_length ? scale : (tangent.mag() * scale);
a90d01
		return Vector( Angle::cos(angle).get()*mag,
a90d01
					   Angle::sin(angle).get()*mag );
d89783
	}
d89783
d89783
	assert(0);
d89783
	return ValueBase();
Carlos Lopez a09598
}
Carlos Lopez a09598
Carlos Lopez a09598
ValueBase
Carlos Lopez a09598
ValueNode_BLineCalcTangent::operator()(Time t)const
Carlos Lopez a09598
{
Carlos Lopez a09598
	Real amount((*amount_)(t).get(Real()));
Carlos Lopez a09598
	return (*this)(t, amount);
Carlos Lopez a09598
}
Carlos Lopez a09598
Carlos Lopez a09598
Carlos Lopez a09598
Carlos Lopez a09598
bool
Carlos Lopez a09598
ValueNode_BLineCalcTangent::set_link_vfunc(int i,ValueNode::Handle value)
Carlos Lopez a09598
{
Carlos Lopez a09598
	assert(i>=0 && i
Carlos Lopez a09598
Carlos Lopez a09598
	switch(i)
Carlos Lopez a09598
	{
d89783
	case 0: CHECK_TYPE_AND_SET_VALUE(bline_,		type_list);
d89783
	case 1: CHECK_TYPE_AND_SET_VALUE(loop_,			type_bool);
d89783
	case 2: CHECK_TYPE_AND_SET_VALUE(amount_,		type_real);
d89783
	case 3: CHECK_TYPE_AND_SET_VALUE(offset_,		type_angle);
d89783
	case 4: CHECK_TYPE_AND_SET_VALUE(scale_,		type_real);
d89783
	case 5: CHECK_TYPE_AND_SET_VALUE(fixed_length_,	type_bool);
d89783
	case 6: CHECK_TYPE_AND_SET_VALUE(homogeneous_,	type_bool);
Carlos Lopez a09598
	}
Carlos Lopez a09598
	return false;
Carlos Lopez a09598
}
Carlos Lopez a09598
Carlos Lopez a09598
ValueNode::LooseHandle
Carlos Lopez a09598
ValueNode_BLineCalcTangent::get_link_vfunc(int i)const
Carlos Lopez a09598
{
Carlos Lopez a09598
	assert(i>=0 && i
Carlos Lopez a09598
Carlos Lopez a09598
	switch(i)
Carlos Lopez a09598
	{
Carlos Lopez a09598
		case 0: return bline_;
Carlos Lopez a09598
		case 1: return loop_;
Carlos Lopez a09598
		case 2: return amount_;
Carlos Lopez a09598
		case 3: return offset_;
Carlos Lopez a09598
		case 4: return scale_;
Carlos Lopez a09598
		case 5: return fixed_length_;
Carlos Lopez bd748d
		case 6: return homogeneous_;
Carlos Lopez a09598
	}
Carlos Lopez a09598
Carlos Lopez a09598
	return 0;
Carlos Lopez a09598
}
Carlos Lopez a09598
Carlos Lopez a09598
bool
d89783
ValueNode_BLineCalcTangent::check_type(Type &type)
Carlos Lopez a09598
{
d89783
	return (type==type_angle ||
d89783
			type==type_real  ||
d89783
			type==type_vector);
Carlos Lopez a09598
}
Carlos Lopez 8eed22
Carlos Lopez 8eed22
LinkableValueNode::Vocab
Carlos Lopez d4cb32
ValueNode_BLineCalcTangent::get_children_vocab_vfunc()const
Carlos Lopez 8eed22
{
Carlos Lopez dbc88f
	if(children_vocab.size())
Carlos Lopez dbc88f
		return children_vocab;
Carlos Lopez dbc88f
Carlos Lopez 8eed22
	LinkableValueNode::Vocab ret;
Carlos Lopez 8eed22
Carlos Lopez 8eed22
	ret.push_back(ParamDesc(ValueBase(),"bline")
Yu Chen 82ed87
		.set_local_name(_("Spline"))
Carlos Lopez 35e469
		.set_description(_("The Spline where the tangent is linked to"))
Carlos Lopez 8eed22
	);
Carlos Lopez 8eed22
Carlos Lopez 8eed22
	ret.push_back(ParamDesc(ValueBase(),"loop")
Carlos Lopez 8eed22
		.set_local_name(_("Loop"))
Carlos Lopez 8eed22
		.set_description(_("When checked, the amount would loop"))
Carlos Lopez 8eed22
	);
Carlos Lopez 8eed22
Carlos Lopez 8eed22
	ret.push_back(ParamDesc(ValueBase(),"amount")
Carlos Lopez 8eed22
		.set_local_name(_("Amount"))
Carlos Lopez 35e469
		.set_description(_("The position of the linked tangent on the Spline (0,1]"))
Carlos Lopez 8eed22
	);
Carlos Lopez 8eed22
Carlos Lopez 8eed22
	ret.push_back(ParamDesc(ValueBase(),"offset")
Carlos Lopez 8eed22
		.set_local_name(_("Offset"))
Carlos Lopez 8eed22
		.set_description(_("Angle offset of the tangent"))
Carlos Lopez 8eed22
	);
Carlos Lopez 8eed22
Carlos Lopez 8eed22
	ret.push_back(ParamDesc(ValueBase(),"scale")
Carlos Lopez 8eed22
		.set_local_name(_("Scale"))
Carlos Lopez 8eed22
		.set_description(_("Scale of the tangent"))
Carlos Lopez 8eed22
	);
Carlos Lopez 8eed22
Carlos Lopez 8eed22
	ret.push_back(ParamDesc(ValueBase(),"fixed_length")
Carlos Lopez 8eed22
		.set_local_name(_("Fixed Length"))
Carlos Lopez 8eed22
		.set_description(_("When checked, the tangent's length is fixed"))
Carlos Lopez 8eed22
	);
Carlos Lopez bd748d
Carlos Lopez bd748d
	ret.push_back(ParamDesc(ValueBase(),"homogeneous")
Carlos Lopez bd748d
		.set_local_name(_("Homogeneous"))
Carlos Lopez 35e469
		.set_description(_("When checked, the tangent is Spline length based"))
Carlos Lopez bd748d
	);
Carlos Lopez 8eed22
	return ret;
Carlos Lopez 8eed22
}