Use LinkableValueNode members functions when possible in the derived valuenodes.
[synfig.git] / synfig-core / src / synfig / valuenode_pow.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode_pow.cpp
3 **      \brief Implementation of the "Power" valuenode conversion.
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2007, 2008 Chris Moore
10 **      Copyright (c) 2009 Nikita Kitaev
11 **
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.
16 **
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.
21 **      \endlegal
22 */
23 /* ========================================================================= */
24
25 /* === H E A D E R S ======================================================= */
26
27 #ifdef USING_PCH
28 #       include "pch.h"
29 #else
30 #ifdef HAVE_CONFIG_H
31 #       include <config.h>
32 #endif
33
34 #include "valuenode_pow.h"
35 #include "valuenode_const.h"
36 #include "general.h"
37
38 #endif
39
40 /* === U S I N G =========================================================== */
41
42 using namespace std;
43 using namespace etl;
44 using namespace synfig;
45
46 /* === M A C R O S ========================================================= */
47
48 /* === G L O B A L S ======================================================= */
49
50 /* === P R O C E D U R E S ================================================= */
51
52 /* === M E T H O D S ======================================================= */
53
54 ValueNode_Pow::ValueNode_Pow(const ValueBase &x):
55         LinkableValueNode(x.get_type())
56 {
57         Vocab ret(get_children_vocab());
58         set_children_vocab(ret);
59         Real value(x.get(Real()));
60         Real infinity(999999.0);
61         Real epsilon(0.000001);
62
63         set_link("base",     ValueNode_Const::create(Real(value)));
64         set_link("power",    ValueNode_Const::create(Real(1)));
65         set_link("epsilon",  ValueNode_Const::create(Real(epsilon)));
66         set_link("infinite", ValueNode_Const::create(Real(infinity)));
67 }
68
69 ValueNode_Pow*
70 ValueNode_Pow::create(const ValueBase &x)
71 {
72         return new ValueNode_Pow(x);
73 }
74
75 LinkableValueNode*
76 ValueNode_Pow::create_new()const
77 {
78         return new ValueNode_Pow(get_type());
79 }
80
81 ValueNode_Pow::~ValueNode_Pow()
82 {
83         unlink_all();
84 }
85
86 bool
87 ValueNode_Pow::set_link_vfunc(int i,ValueNode::Handle value)
88 {
89         assert(i>=0 && i<link_count());
90
91         switch(i)
92         {
93         case 0: CHECK_TYPE_AND_SET_VALUE(base_,     ValueBase::TYPE_REAL);
94         case 1: CHECK_TYPE_AND_SET_VALUE(power_,    ValueBase::TYPE_REAL);
95         case 2: CHECK_TYPE_AND_SET_VALUE(epsilon_,  ValueBase::TYPE_REAL);
96         case 3: CHECK_TYPE_AND_SET_VALUE(infinite_, ValueBase::TYPE_REAL);
97         }
98         return false;
99 }
100
101 ValueNode::LooseHandle
102 ValueNode_Pow::get_link_vfunc(int i)const
103 {
104         assert(i>=0 && i<link_count());
105
106         if(i==0) return base_;
107         if(i==1) return power_;
108         if(i==2) return epsilon_;
109         if(i==3) return infinite_;
110         return 0;
111 }
112
113 ValueBase
114 ValueNode_Pow::operator()(Time t)const
115 {
116         if (getenv("SYNFIG_DEBUG_VALUENODE_OPERATORS"))
117                 printf("%s:%d operator()\n", __FILE__, __LINE__);
118
119         Real base     = (*base_)    (t).get(Real());
120         Real power    = (*power_)   (t).get(Real());
121         Real epsilon  = (*epsilon_) (t).get(Real());
122         Real infinite = (*infinite_)(t).get(Real());
123
124
125
126         if (epsilon < 0.00000001)
127                 epsilon = 0.00000001;
128
129         //Filters for special/undefined cases
130
131         if (abs(power) < epsilon) //x^0 = 1
132                 return 1;
133         if (abs(base) < epsilon)
134         {
135                 if (power > 0) //0^x=0
136                         return Real(0);
137                 else
138                 {
139                         if ( ( ((int) power) % 2 != 0) && (base < 0) ) //(-0)^(-odd)=-inf
140                                 return -infinite;
141                         else
142                                 return infinite;
143                 }
144         }
145         if (base <= epsilon && ((int) power) != power) //negative number to fractional power -> undefined
146                 power = ((int) power);  //so round off power to nearest integer
147
148         return pow(base,power);
149
150 }
151
152 String
153 ValueNode_Pow::get_name()const
154 {
155         return "power";
156 }
157
158 String
159 ValueNode_Pow::get_local_name()const
160 {
161         return _("Power");
162 }
163
164 bool
165 ValueNode_Pow::check_type(ValueBase::Type type)
166 {
167         return type==ValueBase::TYPE_REAL;
168 }
169
170 LinkableValueNode::Vocab
171 ValueNode_Pow::get_children_vocab_vfunc()const
172 {
173         if(children_vocab.size())
174                 return children_vocab;
175
176         LinkableValueNode::Vocab ret;
177
178         ret.push_back(ParamDesc(ValueBase(),"base")
179                 .set_local_name(_("Base"))
180                 .set_description(_("The base to be raised to the power"))
181         );
182
183         ret.push_back(ParamDesc(ValueBase(),"power")
184                 .set_local_name(_("Power"))
185                 .set_description(_("The power used to raise the base"))
186         );
187
188                 ret.push_back(ParamDesc(ValueBase(),"epsilon")
189                 .set_local_name(_("Epsilon"))
190                 .set_description(_("Value used to compare base or power with zero "))
191         );
192
193                 ret.push_back(ParamDesc(ValueBase(),"infinite")
194                 .set_local_name(_("Infinite"))
195                 .set_description(_("Returned value when result tends to infinite"))
196         );
197
198         return ret;
199 }