Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / 0.61.09 / src / synfig / valuenode_dotproduct.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode_dotproduct.cpp
3 **      \brief Implementation of the "DotProduct" valuenode conversion.
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 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_dotproduct.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_DotProduct::ValueNode_DotProduct(const ValueBase &value):
54         LinkableValueNode(value.get_type())
55 {
56         switch(value.get_type())
57         {
58         case ValueBase::TYPE_REAL:
59                 set_link("lhs",ValueNode_Const::create(Vector(value.get(Real()),0)));
60                 set_link("rhs",ValueNode_Const::create(Vector(1,0)));
61                 break;
62         case ValueBase::TYPE_ANGLE:
63                 set_link("lhs",ValueNode_Const::create(Vector(Angle::cos(value.get(Angle())).get(), Angle::sin(value.get(Angle())).get())));
64                 set_link("rhs",ValueNode_Const::create(Vector(1,0)));
65                 break;
66         default:
67                 throw Exception::BadType(ValueBase::type_local_name(value.get_type()));
68         }
69
70         DCAST_HACK_ENABLE();
71 }
72
73 LinkableValueNode*
74 ValueNode_DotProduct::create_new()const
75 {
76         return new ValueNode_DotProduct(get_type());
77 }
78
79 ValueNode_DotProduct*
80 ValueNode_DotProduct::create(const ValueBase &x)
81 {
82         return new ValueNode_DotProduct(x);
83 }
84
85 ValueNode_DotProduct::~ValueNode_DotProduct()
86 {
87         unlink_all();
88 }
89
90 ValueBase
91 ValueNode_DotProduct::operator()(Time t)const
92 {
93         Vector lhs((*lhs_)(t).get(Vector()));
94         Vector rhs((*rhs_)(t).get(Vector()));
95
96         switch (get_type())
97         {
98         case ValueBase::TYPE_ANGLE:
99                 return Angle::cos(lhs * rhs / lhs.mag() / rhs.mag()).mod();
100         case ValueBase::TYPE_REAL:
101                 return lhs * rhs;
102         default:
103                 break;
104         }
105
106         assert(0);
107         return ValueBase();
108 }
109
110 String
111 ValueNode_DotProduct::get_name()const
112 {
113         return "dotproduct";
114 }
115
116 String
117 ValueNode_DotProduct::get_local_name()const
118 {
119         return _("Dot Product");
120 }
121
122 bool
123 ValueNode_DotProduct::set_link_vfunc(int i,ValueNode::Handle value)
124 {
125         assert(i>=0 && i<link_count());
126
127         switch(i)
128         {
129         case 0: CHECK_TYPE_AND_SET_VALUE(lhs_, ValueBase::TYPE_VECTOR);
130         case 1: CHECK_TYPE_AND_SET_VALUE(rhs_, ValueBase::TYPE_VECTOR);
131         }
132         return false;
133 }
134
135 ValueNode::LooseHandle
136 ValueNode_DotProduct::get_link_vfunc(int i)const
137 {
138         assert(i>=0 && i<link_count());
139
140         switch(i)
141         {
142         case 0: return lhs_;
143         case 1: return rhs_;
144         }
145
146         return 0;
147 }
148
149 int
150 ValueNode_DotProduct::link_count()const
151 {
152         return 2;
153 }
154
155 String
156 ValueNode_DotProduct::link_name(int i)const
157 {
158         assert(i>=0 && i<link_count());
159
160         switch(i)
161         {
162                 case 0: return "lhs";
163                 case 1: return "rhs";
164         }
165         return String();
166 }
167
168 String
169 ValueNode_DotProduct::link_local_name(int i)const
170 {
171         assert(i>=0 && i<link_count());
172
173         switch(i)
174         {
175                 case 0: return _("LHS");
176                 case 1: return _("RHS");
177         }
178         return String();
179 }
180
181 int
182 ValueNode_DotProduct::get_link_index_from_name(const String &name)const
183 {
184         if (name=="lhs") return 0;
185         if (name=="rhs") return 1;
186
187         throw Exception::BadLinkName(name);
188 }
189
190 bool
191 ValueNode_DotProduct::check_type(ValueBase::Type type)
192 {
193         return
194                 type==ValueBase::TYPE_ANGLE ||
195                 type==ValueBase::TYPE_REAL;
196 }