0b9d319929245b53ed9d2ee03683d3fe6f6069d6
[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         Real value(x.get(Real()));
58         Real infinity(999999.0);
59         Real epsilon(0.000001);
60
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)));
65 }
66
67 ValueNode_Pow*
68 ValueNode_Pow::create(const ValueBase &x)
69 {
70         return new ValueNode_Pow(x);
71 }
72
73 LinkableValueNode*
74 ValueNode_Pow::create_new()const
75 {
76         return new ValueNode_Pow(get_type());
77 }
78
79 ValueNode_Pow::~ValueNode_Pow()
80 {
81         unlink_all();
82 }
83
84 bool
85 ValueNode_Pow::set_link_vfunc(int i,ValueNode::Handle value)
86 {
87         assert(i>=0 && i<link_count());
88
89         switch(i)
90         {
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);
95         }
96         return false;
97 }
98
99 ValueNode::LooseHandle
100 ValueNode_Pow::get_link_vfunc(int i)const
101 {
102         assert(i>=0 && i<link_count());
103
104         if(i==0) return base_;
105         if(i==1) return power_;
106         if(i==2) return epsilon_;
107         if(i==3) return infinite_;
108         return 0;
109 }
110
111 int
112 ValueNode_Pow::link_count()const
113 {
114         return 4;
115 }
116
117 String
118 ValueNode_Pow::link_local_name(int i)const
119 {
120         assert(i>=0 && i<link_count());
121
122         if(i==0) return _("Base");
123         if(i==1) return _("Power");
124         if(i==2) return _("Epsilon");
125         if(i==3) return _("Infinite");
126         return String();
127 }
128
129 String
130 ValueNode_Pow::link_name(int i)const
131 {
132         assert(i>=0 && i<link_count());
133
134         if(i==0) return "base";
135         if(i==1) return "power";
136         if(i==2) return "epsilon";
137         if(i==3) return "infinite";
138         return String();
139 }
140
141 int
142 ValueNode_Pow::get_link_index_from_name(const String &name)const
143 {
144         if(name=="base")     return 0;
145         if(name=="power")    return 1;
146         if(name=="epsilon")  return 2;
147         if(name=="infinite") return 3;
148
149         throw Exception::BadLinkName(name);
150 }
151
152 ValueBase
153 ValueNode_Pow::operator()(Time t)const
154 {
155         if (getenv("SYNFIG_DEBUG_VALUENODE_OPERATORS"))
156                 printf("%s:%d operator()\n", __FILE__, __LINE__);
157
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());
162
163
164
165         if (epsilon < 0.00000001)
166                 epsilon = 0.00000001;
167
168         //Filters for special/undefined cases
169
170         if (abs(power) < epsilon) //x^0 = 1
171                 return 1;
172         if (abs(base) < epsilon)
173         {
174                 if (power > 0) //0^x=0
175                         return Real(0);
176                 else
177                 {
178                         if ( ( ((int) power) % 2 != 0) && (base < 0) ) //(-0)^(-odd)=-inf
179                                 return -infinite;
180                         else
181                                 return infinite;
182                 }
183         }
184         if (base <= epsilon && ((int) power) != power) //negative number to fractional power -> undefined
185                 power = ((int) power);  //so round off power to nearest integer
186
187         return pow(base,power);
188
189 }
190
191 String
192 ValueNode_Pow::get_name()const
193 {
194         return "power";
195 }
196
197 String
198 ValueNode_Pow::get_local_name()const
199 {
200         return _("Power");
201 }
202
203 bool
204 ValueNode_Pow::check_type(ValueBase::Type type)
205 {
206         return type==ValueBase::TYPE_REAL;
207 }
208
209 LinkableValueNode::Vocab
210 ValueNode_Pow::get_children_vocab_vfunc()const
211 {
212         LinkableValueNode::Vocab ret;
213
214         ret.push_back(ParamDesc(ValueBase(),"base")
215                 .set_local_name(_("Base"))
216                 .set_description(_("The base to be raised to the power"))
217         );
218
219         ret.push_back(ParamDesc(ValueBase(),"power")
220                 .set_local_name(_("Power"))
221                 .set_description(_("The power used to raise the base"))
222         );
223
224                 ret.push_back(ParamDesc(ValueBase(),"epsilon")
225                 .set_local_name(_("Epsilon"))
226                 .set_description(_("Value used to compare base or power with zero "))
227         );
228
229                 ret.push_back(ParamDesc(ValueBase(),"infinite")
230                 .set_local_name(_("Infinite"))
231                 .set_description(_("Returned value when result tends to infinite"))
232         );
233
234         return ret;
235 }