2.50: svn merge https://svn.blender.org/svnroot/bf-blender/trunk/blender -r19323...
[blender-staging.git] / source / gameengine / Expressions / VectorValue.cpp
1 // VectorValue.cpp: implementation of the CVectorValue class.
2 /*
3  * Copyright (c) 1996-2000 Erwin Coumans <coockie@acm.org>
4  *
5  * Permission to use, copy, modify, distribute and sell this software
6  * and its documentation for any purpose is hereby granted without fee,
7  * provided that the above copyright notice appear in all copies and
8  * that both that copyright notice and this permission notice appear
9  * in supporting documentation.  Erwin Coumans makes no
10  * representations about the suitability of this software for any
11  * purpose.  It is provided "as is" without express or implied warranty.
12  *
13  */
14
15 #ifdef HAVE_CONFIG_H
16 #include <config.h>
17 #endif
18
19 #ifdef WIN32
20 #pragma warning (disable:4786)
21 #endif
22
23 #include "Value.h"
24 #include "VectorValue.h"
25 #include "ErrorValue.h"
26 //#include "MatrixValue.h"
27 #include "VoidValue.h"
28 #include "StringValue.h"
29 //#include "FactoryManager.h"
30
31
32
33 //////////////////////////////////////////////////////////////////////
34 // Construction/Destruction
35 //////////////////////////////////////////////////////////////////////
36
37 CVectorValue::CVectorValue(float x,float y,float z, AllocationTYPE alloctype)
38 {
39         SetCustomFlag1(false);//FancyOutput=false;
40         
41         if (alloctype == STACKVALUE)
42         {
43                 CValue::DisableRefCount();
44         };
45         
46         m_vec[KX_X] = m_transformedvec[KX_X] = x;
47         m_vec[KX_Y] = m_transformedvec[KX_Y] = y;
48         m_vec[KX_Z] = m_transformedvec[KX_Z] = z;
49         
50 }
51 CVectorValue::CVectorValue(double vec[],STR_String name,AllocationTYPE alloctype) {
52         
53         SetCustomFlag1(false);//FancyOutput=false;
54         
55         m_vec[KX_X] = m_transformedvec[KX_X] = vec[KX_X];
56         m_vec[KX_Y] = m_transformedvec[KX_Y] = vec[KX_Y];
57         m_vec[KX_Z] = m_transformedvec[KX_Z] = vec[KX_Z];
58                 
59         if (alloctype == STACKVALUE)
60         {
61                 CValue::DisableRefCount();
62                 
63         }
64         
65         SetName(name);
66 }
67
68 CVectorValue::CVectorValue(double vec[],AllocationTYPE alloctype) {
69         
70         SetCustomFlag1(false);//FancyOutput=false;
71         
72         m_vec[KX_X] = m_transformedvec[KX_X] = vec[KX_X];
73         m_vec[KX_Y] = m_transformedvec[KX_Y] = vec[KX_Y];
74         m_vec[KX_Z] = m_transformedvec[KX_Z] = vec[KX_Z];
75         
76         if (alloctype == STACKVALUE)
77         {
78                 CValue::DisableRefCount();
79                 
80         }
81         
82         
83 }
84 CVectorValue::~CVectorValue()
85 {
86
87 }
88
89 CValue* CVectorValue::CalcFinal(VALUE_DATA_TYPE dtype, VALUE_OPERATOR op, CValue *val)
90 /*
91 pre: the type of val is dtype
92 ret: a new object containing the result of applying operator op to val and
93 this object
94 */
95 {
96         CValue *ret = NULL;
97         
98         switch(op)
99         { 
100         case VALUE_ADD_OPERATOR: 
101                 {
102                         switch (dtype)
103                         {
104                         case VALUE_EMPTY_TYPE:
105                         case VALUE_VECTOR_TYPE: 
106                                 {
107                                         ret = new CVectorValue(
108                                                 val->GetVector3()[KX_X] + GetVector3()[KX_X],
109                                                 val->GetVector3()[KX_Y] + GetVector3()[KX_Y],
110                                                 val->GetVector3()[KX_Z] + GetVector3()[KX_Z],
111                                                 CValue::HEAPVALUE);
112                                         ret->SetName(GetName());
113                                         break;
114                                 }
115                         
116                         default: 
117                                 ret = new CErrorValue(val->GetText() + op2str(op) +     GetText());
118                         }
119                         break;
120                 }
121         case VALUE_MUL_OPERATOR:
122                 {
123                         switch (dtype)
124                         {
125                                 
126                         case VALUE_EMPTY_TYPE:
127                         case VALUE_VECTOR_TYPE: 
128                                 {
129                                         //MT_Vector3 supports 'scaling' by another vector, instead of using general transform, Gino?
130                                         //ret = new CVectorValue(val->GetVector3().Scaled(GetVector3()),GetName());
131                                         break;
132                                 }
133                         case VALUE_FLOAT_TYPE: 
134                                 {
135                                         ret = new CVectorValue(
136                                                 val->GetVector3()[KX_X] * GetVector3()[KX_X],
137                                                 val->GetVector3()[KX_Y] * GetVector3()[KX_Y],
138                                                 val->GetVector3()[KX_Z] * GetVector3()[KX_Z],
139                                                 CValue::HEAPVALUE);
140                                         ret->SetName(GetName());
141                                         break;
142                                 }
143                         
144                         default: 
145                                 ret = new CErrorValue(val->GetText() + op2str(op) +     GetText());
146                         }
147                         break;
148
149                 }
150         
151         default:
152                 ret = new CErrorValue(val->GetText() + op2str(op) +     GetText());
153         }
154
155         
156         return ret;
157 }
158
159 double CVectorValue::GetNumber()
160 {
161         return m_vec[KX_X];
162 }
163
164
165 double* CVectorValue::GetVector3(bool bGetTransformedVec)
166 {
167         if (bGetTransformedVec)
168                 return m_transformedvec;
169         // else 
170         return m_vec;
171 }
172
173
174
175
176
177 void CVectorValue::SetVector(double newvec[])
178 {
179         m_vec[KX_X] = m_transformedvec[KX_X] = newvec[KX_X];
180         m_vec[KX_Y] = m_transformedvec[KX_Y] = newvec[KX_Y];
181         m_vec[KX_Z] = m_transformedvec[KX_Z] = newvec[KX_Z];
182         
183         SetModified(true);
184 }
185
186
187 void CVectorValue::SetValue(CValue *newval)
188 {
189         
190         double* newvec = ((CVectorValue*)newval)->GetVector3();
191         m_vec[KX_X] = m_transformedvec[KX_X] = newvec[KX_X];
192         m_vec[KX_Y] = m_transformedvec[KX_Y] = newvec[KX_Y];
193         m_vec[KX_Z] = m_transformedvec[KX_Z] = newvec[KX_Z];
194         
195         SetModified(true);
196 }
197
198 static const STR_String gstrVectorStr=STR_String();
199 const STR_String & CVectorValue::GetText()
200 {
201         assertd(false);
202         return gstrVectorStr;
203 }
204
205 CValue* CVectorValue::GetReplica() { 
206         CVectorValue* replica = new CVectorValue(*this);
207         CValue::AddDataToReplica(replica);
208         return replica;
209 };
210
211 /*void CVectorValue::Transform(rcMatrix4x4 mat)
212 {
213         m_transformedvec = mat*m_vec;
214 }
215 */
216
217