Keep the value the same when converting to "Dot Product".
[synfig.git] / synfig-core / trunk / 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         }
103
104         assert(0);
105         return ValueBase();
106 }
107
108 String
109 ValueNode_DotProduct::get_name()const
110 {
111         return "dotproduct";
112 }
113
114 String
115 ValueNode_DotProduct::get_local_name()const
116 {
117         return _("Dot Product");
118 }
119
120 bool
121 ValueNode_DotProduct::set_link_vfunc(int i,ValueNode::Handle value)
122 {
123         assert(i>=0 && i<link_count());
124
125         switch(i)
126         {
127         case 0: CHECK_TYPE_AND_SET_VALUE(lhs_, ValueBase::TYPE_VECTOR);
128         case 1: CHECK_TYPE_AND_SET_VALUE(rhs_, ValueBase::TYPE_VECTOR);
129         }
130         return false;
131 }
132
133 ValueNode::LooseHandle
134 ValueNode_DotProduct::get_link_vfunc(int i)const
135 {
136         assert(i>=0 && i<link_count());
137
138         switch(i)
139         {
140         case 0: return lhs_;
141         case 1: return rhs_;
142         }
143
144         return 0;
145 }
146
147 int
148 ValueNode_DotProduct::link_count()const
149 {
150         return 2;
151 }
152
153 String
154 ValueNode_DotProduct::link_name(int i)const
155 {
156         assert(i>=0 && i<link_count());
157
158         switch(i)
159         {
160                 case 0: return "lhs";
161                 case 1: return "rhs";
162         }
163         return String();
164 }
165
166 String
167 ValueNode_DotProduct::link_local_name(int i)const
168 {
169         assert(i>=0 && i<link_count());
170
171         switch(i)
172         {
173                 case 0: return _("LHS");
174                 case 1: return _("RHS");
175         }
176         return String();
177 }
178
179 int
180 ValueNode_DotProduct::get_link_index_from_name(const String &name)const
181 {
182         if (name=="lhs") return 0;
183         if (name=="rhs") return 1;
184
185         throw Exception::BadLinkName(name);
186 }
187
188 bool
189 ValueNode_DotProduct::check_type(ValueBase::Type type)
190 {
191         return
192                 type==ValueBase::TYPE_ANGLE ||
193                 type==ValueBase::TYPE_REAL;
194 }