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