1 /* === S Y N F I G ========================================================= */
2 /*! \file valuenode_pow.cpp
3 ** \brief Implementation of the "Power" valuenode conversion.
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2007, 2008 Chris Moore
10 ** Copyright (c) 2009 Nikita Kitaev
12 ** This package is free software; you can redistribute it and/or
13 ** modify it under the terms of the GNU General Public License as
14 ** published by the Free Software Foundation; either version 2 of
15 ** the License, or (at your option) any later version.
17 ** This package is distributed in the hope that it will be useful,
18 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 ** General Public License for more details.
23 /* ========================================================================= */
25 /* === H E A D E R S ======================================================= */
34 #include "valuenode_pow.h"
35 #include "valuenode_const.h"
40 /* === U S I N G =========================================================== */
44 using namespace synfig;
46 /* === M A C R O S ========================================================= */
48 /* === G L O B A L S ======================================================= */
50 /* === P R O C E D U R E S ================================================= */
52 /* === M E T H O D S ======================================================= */
54 ValueNode_Pow::ValueNode_Pow(const ValueBase &x):
55 LinkableValueNode(x.get_type())
57 Real value(x.get(Real()));
58 Real infinity(999999.0);
59 Real epsilon(0.000001);
61 set_link("base", ValueNode_Const::create(Real(value)));
62 set_link("power", ValueNode_Const::create(Real(1)));
63 set_link("epsilon", ValueNode_Const::create(Real(epsilon)));
64 set_link("infinite", ValueNode_Const::create(Real(infinity)));
68 ValueNode_Pow::create(const ValueBase &x)
70 return new ValueNode_Pow(x);
74 ValueNode_Pow::create_new()const
76 return new ValueNode_Pow(get_type());
79 ValueNode_Pow::~ValueNode_Pow()
85 ValueNode_Pow::set_link_vfunc(int i,ValueNode::Handle value)
87 assert(i>=0 && i<link_count());
91 case 0: CHECK_TYPE_AND_SET_VALUE(base_, ValueBase::TYPE_REAL);
92 case 1: CHECK_TYPE_AND_SET_VALUE(power_, ValueBase::TYPE_REAL);
93 case 2: CHECK_TYPE_AND_SET_VALUE(epsilon_, ValueBase::TYPE_REAL);
94 case 3: CHECK_TYPE_AND_SET_VALUE(infinite_, ValueBase::TYPE_REAL);
99 ValueNode::LooseHandle
100 ValueNode_Pow::get_link_vfunc(int i)const
102 assert(i>=0 && i<link_count());
104 if(i==0) return base_;
105 if(i==1) return power_;
106 if(i==2) return epsilon_;
107 if(i==3) return infinite_;
112 ValueNode_Pow::link_count()const
118 ValueNode_Pow::link_local_name(int i)const
120 assert(i>=0 && i<link_count());
122 if(i==0) return _("Base");
123 if(i==1) return _("Power");
124 if(i==2) return _("Epsilon");
125 if(i==3) return _("Infinite");
130 ValueNode_Pow::link_name(int i)const
132 assert(i>=0 && i<link_count());
134 if(i==0) return "base";
135 if(i==1) return "power";
136 if(i==2) return "epsilon";
137 if(i==3) return "infinite";
142 ValueNode_Pow::get_link_index_from_name(const String &name)const
144 if(name=="base") return 0;
145 if(name=="power") return 1;
146 if(name=="epsilon") return 2;
147 if(name=="infinite") return 3;
149 throw Exception::BadLinkName(name);
153 ValueNode_Pow::operator()(Time t)const
155 if (getenv("SYNFIG_DEBUG_VALUENODE_OPERATORS"))
156 printf("%s:%d operator()\n", __FILE__, __LINE__);
158 Real base = (*base_) (t).get(Real());
159 Real power = (*power_) (t).get(Real());
160 Real epsilon = (*epsilon_) (t).get(Real());
161 Real infinite = (*infinite_)(t).get(Real());
165 if (epsilon < 0.00000001)
166 epsilon = 0.00000001;
168 //Filters for special/undefined cases
170 if (abs(power) < epsilon) //x^0 = 1
172 if (abs(base) < epsilon)
174 if (power > 0) //0^x=0
178 if ( ( ((int) power) % 2 != 0) && (base < 0) ) //(-0)^(-odd)=-inf
184 if (base <= epsilon && ((int) power) != power) //negative number to fractional power -> undefined
185 power = ((int) power); //so round off power to nearest integer
187 return pow(base,power);
192 ValueNode_Pow::get_name()const
198 ValueNode_Pow::get_local_name()const
204 ValueNode_Pow::check_type(ValueBase::Type type)
206 return type==ValueBase::TYPE_REAL;