Use link_count from children vocabulary and return the stored vocabulary if already...
[synfig.git] / synfig-core / src / synfig / valuenode_reciprocal.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode_reciprocal.cpp
3 **      \brief Implementation of the "Reciprocal" 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 **
11 **      This package is free software; you can redistribute it and/or
12 **      modify it under the terms of the GNU General Public License as
13 **      published by the Free Software Foundation; either version 2 of
14 **      the License, or (at your option) any later version.
15 **
16 **      This package is distributed in the hope that it will be useful,
17 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
18 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 **      General Public License for more details.
20 **      \endlegal
21 */
22 /* ========================================================================= */
23
24 /* === H E A D E R S ======================================================= */
25
26 #ifdef USING_PCH
27 #       include "pch.h"
28 #else
29 #ifdef HAVE_CONFIG_H
30 #       include <config.h>
31 #endif
32
33 #include "valuenode_reciprocal.h"
34 #include "valuenode_const.h"
35 #include "general.h"
36
37 #endif
38
39 /* === U S I N G =========================================================== */
40
41 using namespace std;
42 using namespace etl;
43 using namespace synfig;
44
45 /* === M A C R O S ========================================================= */
46
47 /* === G L O B A L S ======================================================= */
48
49 /* === P R O C E D U R E S ================================================= */
50
51 /* === M E T H O D S ======================================================= */
52
53 ValueNode_Reciprocal::ValueNode_Reciprocal(const ValueBase &x):
54         LinkableValueNode(x.get_type())
55 {
56         Vocab ret(get_children_vocab());
57         set_children_vocab(ret);
58         Real value(x.get(Real()));
59         Real infinity(999999.0);
60         Real epsilon(0.000001);
61
62         if (value == 0)
63                 value = infinity;
64         else
65                 value = 1.0/value;
66
67         set_link("link",     ValueNode_Const::create(Real(value)));
68         set_link("epsilon",  ValueNode_Const::create(Real(epsilon)));
69         set_link("infinite", ValueNode_Const::create(Real(infinity)));
70 }
71
72 ValueNode_Reciprocal*
73 ValueNode_Reciprocal::create(const ValueBase &x)
74 {
75         return new ValueNode_Reciprocal(x);
76 }
77
78 LinkableValueNode*
79 ValueNode_Reciprocal::create_new()const
80 {
81         return new ValueNode_Reciprocal(get_type());
82 }
83
84 ValueNode_Reciprocal::~ValueNode_Reciprocal()
85 {
86         unlink_all();
87 }
88
89 bool
90 ValueNode_Reciprocal::set_link_vfunc(int i,ValueNode::Handle value)
91 {
92         assert(i>=0 && i<link_count());
93
94         switch(i)
95         {
96         case 0: CHECK_TYPE_AND_SET_VALUE(link_,     ValueBase::TYPE_REAL);
97         case 1: CHECK_TYPE_AND_SET_VALUE(epsilon_,  ValueBase::TYPE_REAL);
98         case 2: CHECK_TYPE_AND_SET_VALUE(infinite_, ValueBase::TYPE_REAL);
99         }
100         return false;
101 }
102
103 ValueNode::LooseHandle
104 ValueNode_Reciprocal::get_link_vfunc(int i)const
105 {
106         assert(i>=0 && i<link_count());
107
108         if(i==0) return link_;
109         if(i==1) return epsilon_;
110         if(i==2) return infinite_;
111
112         return 0;
113 }
114
115 int
116 ValueNode_Reciprocal::link_count()const
117 {
118         return 3;
119 }
120
121 String
122 ValueNode_Reciprocal::link_local_name(int i)const
123 {
124         assert(i>=0 && i<link_count());
125
126         if(i==0) return _("Link");
127         if(i==1) return _("Epsilon");
128         if(i==2) return _("Infinite");
129         return String();
130 }
131
132 String
133 ValueNode_Reciprocal::link_name(int i)const
134 {
135         assert(i>=0 && i<link_count());
136
137         if(i==0) return "link";
138         if(i==1) return "epsilon";
139         if(i==2) return "infinite";
140         return String();
141 }
142
143 int
144 ValueNode_Reciprocal::get_link_index_from_name(const String &name)const
145 {
146         if(name=="link")     return 0;
147         if(name=="epsilon")  return 1;
148         if(name=="infinite") return 2;
149
150         throw Exception::BadLinkName(name);
151 }
152
153 ValueBase
154 ValueNode_Reciprocal::operator()(Time t)const
155 {
156         if (getenv("SYNFIG_DEBUG_VALUENODE_OPERATORS"))
157                 printf("%s:%d operator()\n", __FILE__, __LINE__);
158
159         Real link     = (*link_)    (t).get(Real());
160         Real epsilon  = (*epsilon_) (t).get(Real());
161         Real infinite = (*infinite_)(t).get(Real());
162
163         if (epsilon < 0.00000001)
164                 epsilon = 0.00000001;
165
166         if (abs(link) < epsilon)
167                 if (link < 0)
168                         return -infinite;
169                 else
170                         return infinite;
171         else
172                 return 1.0f / link;
173 }
174
175 String
176 ValueNode_Reciprocal::get_name()const
177 {
178         return "reciprocal";
179 }
180
181 String
182 ValueNode_Reciprocal::get_local_name()const
183 {
184         return _("Reciprocal");
185 }
186
187 bool
188 ValueNode_Reciprocal::check_type(ValueBase::Type type)
189 {
190         return type==ValueBase::TYPE_REAL;
191 }
192
193 LinkableValueNode::Vocab
194 ValueNode_Reciprocal::get_children_vocab_vfunc()const
195 {
196         if(children_vocab.size())
197                 return children_vocab;
198
199         LinkableValueNode::Vocab ret;
200
201         ret.push_back(ParamDesc(ValueBase(),"link")
202                 .set_local_name(_("Link"))
203                 .set_description(_("The value node used to calculate its reciprocal"))
204         );
205
206         ret.push_back(ParamDesc(ValueBase(),"epsilon")
207                 .set_local_name(_("Epsilon"))
208                 .set_description(_("The value used to decide whether 'Link' is too small to obtain its reciprocal"))
209         );
210
211                 ret.push_back(ParamDesc(ValueBase(),"infinite")
212                 .set_local_name(_("Infinite"))
213                 .set_description(_("The resulting value when 'Link' < 'Epsilon'"))
214         );
215
216         return ret;
217 }