Use LinkableValueNode members functions when possible in the derived valuenodes.
[synfig.git] / synfig-core / src / synfig / valuenode_log.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode_log.cpp
3 **      \brief Implementation of the "Natural Logarithm" 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) 2008 Carlos López
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_log.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_Logarithm::ValueNode_Logarithm(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         value = exp(value);
64
65         set_link("link",     ValueNode_Const::create(Real(value)));
66         set_link("epsilon",  ValueNode_Const::create(Real(epsilon)));
67         set_link("infinite", ValueNode_Const::create(Real(infinity)));
68 }
69
70 ValueNode_Logarithm*
71 ValueNode_Logarithm::create(const ValueBase &x)
72 {
73         return new ValueNode_Logarithm(x);
74 }
75
76 LinkableValueNode*
77 ValueNode_Logarithm::create_new()const
78 {
79         return new ValueNode_Logarithm(get_type());
80 }
81
82 ValueNode_Logarithm::~ValueNode_Logarithm()
83 {
84         unlink_all();
85 }
86
87 bool
88 ValueNode_Logarithm::set_link_vfunc(int i,ValueNode::Handle value)
89 {
90         assert(i>=0 && i<link_count());
91
92         switch(i)
93         {
94         case 0: CHECK_TYPE_AND_SET_VALUE(link_,     ValueBase::TYPE_REAL);
95         case 1: CHECK_TYPE_AND_SET_VALUE(epsilon_,  ValueBase::TYPE_REAL);
96         case 2: CHECK_TYPE_AND_SET_VALUE(infinite_, ValueBase::TYPE_REAL);
97         }
98         return false;
99 }
100
101 ValueNode::LooseHandle
102 ValueNode_Logarithm::get_link_vfunc(int i)const
103 {
104         assert(i>=0 && i<link_count());
105
106         if(i==0) return link_;
107         if(i==1) return epsilon_;
108         if(i==2) return infinite_;
109
110         return 0;
111 }
112
113 ValueBase
114 ValueNode_Logarithm::operator()(Time t)const
115 {
116         if (getenv("SYNFIG_DEBUG_VALUENODE_OPERATORS"))
117                 printf("%s:%d operator()\n", __FILE__, __LINE__);
118
119         Real link     = (*link_)    (t).get(Real());
120         Real epsilon  = (*epsilon_) (t).get(Real());
121         Real infinite = (*infinite_)(t).get(Real());
122
123         if (epsilon < 0.00000001)
124                 epsilon = 0.00000001;
125
126         if (link < epsilon)
127                         return -infinite;
128         else
129                 return log(link);
130 }
131
132 String
133 ValueNode_Logarithm::get_name()const
134 {
135         return "logarithm";
136 }
137
138 String
139 ValueNode_Logarithm::get_local_name()const
140 {
141         return _("Logarithm");
142 }
143
144 bool
145 ValueNode_Logarithm::check_type(ValueBase::Type type)
146 {
147         return type==ValueBase::TYPE_REAL;
148 }
149
150 LinkableValueNode::Vocab
151 ValueNode_Logarithm::get_children_vocab_vfunc()const
152 {
153         if(children_vocab.size())
154                 return children_vocab;
155
156         LinkableValueNode::Vocab ret;
157
158         ret.push_back(ParamDesc(ValueBase(),"link")
159                 .set_local_name(_("Link"))
160                 .set_description(_("Value node used to calculate the Neperian logarithm"))
161         );
162
163                 ret.push_back(ParamDesc(ValueBase(),"epsilon")
164                 .set_local_name(_("Epsilon"))
165                 .set_description(_("Value used to compare 'link' with zero "))
166         );
167
168                 ret.push_back(ParamDesc(ValueBase(),"infinite")
169                 .set_local_name(_("Infinite"))
170                 .set_description(_("Returned value when result tends to infinite"))
171         );
172
173         return ret;
174 }
175