style cleanup: comment blocks
[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         
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         
71         SetCustomFlag1(false);//FancyOutput=false;
72         
73         m_vec[KX_X] = m_transformedvec[KX_X] = vec[KX_X];
74         m_vec[KX_Y] = m_transformedvec[KX_Y] = vec[KX_Y];
75         m_vec[KX_Z] = m_transformedvec[KX_Z] = vec[KX_Z];
76         
77         if (alloctype == STACKVALUE)
78         {
79                 CValue::DisableRefCount();
80                 
81         }
82         
83         
84 }
85 CVectorValue::~CVectorValue()
86 {
87
88 }
89
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 CValue* CVectorValue::CalcFinal(VALUE_DATA_TYPE dtype, VALUE_OPERATOR op, CValue *val)
96 {
97         CValue *ret = NULL;
98         
99         switch(op)
100         { 
101         case VALUE_ADD_OPERATOR: 
102                 {
103                         switch (dtype)
104                         {
105                         case VALUE_EMPTY_TYPE:
106                         case VALUE_VECTOR_TYPE: 
107                                 {
108                                         ret = new CVectorValue(
109                                                 val->GetVector3()[KX_X] + GetVector3()[KX_X],
110                                                 val->GetVector3()[KX_Y] + GetVector3()[KX_Y],
111                                                 val->GetVector3()[KX_Z] + GetVector3()[KX_Z],
112                                                 CValue::HEAPVALUE);
113                                         ret->SetName(GetName());
114                                         break;
115                                 }
116                         
117                         default: 
118                                 ret = new CErrorValue(val->GetText() + op2str(op) +     GetText());
119                         }
120                         break;
121                 }
122         case VALUE_MUL_OPERATOR:
123                 {
124                         switch (dtype)
125                         {
126                                 
127                         case VALUE_EMPTY_TYPE:
128                         case VALUE_VECTOR_TYPE: 
129                                 {
130                                         //MT_Vector3 supports 'scaling' by another vector, instead of using general transform, Gino?
131                                         //ret = new CVectorValue(val->GetVector3().Scaled(GetVector3()),GetName());
132                                         break;
133                                 }
134                         case VALUE_FLOAT_TYPE: 
135                                 {
136                                         ret = new CVectorValue(
137                                                 val->GetVector3()[KX_X] * GetVector3()[KX_X],
138                                                 val->GetVector3()[KX_Y] * GetVector3()[KX_Y],
139                                                 val->GetVector3()[KX_Z] * GetVector3()[KX_Z],
140                                                 CValue::HEAPVALUE);
141                                         ret->SetName(GetName());
142                                         break;
143                                 }
144                         
145                         default: 
146                                 ret = new CErrorValue(val->GetText() + op2str(op) +     GetText());
147                         }
148                         break;
149
150                 }
151         
152         default:
153                 ret = new CErrorValue(val->GetText() + op2str(op) +     GetText());
154         }
155
156         
157         return ret;
158 }
159
160 double CVectorValue::GetNumber()
161 {
162         return m_vec[KX_X];
163 }
164
165
166 double* CVectorValue::GetVector3(bool bGetTransformedVec)
167 {
168         if (bGetTransformedVec)
169                 return m_transformedvec;
170         // else 
171         return m_vec;
172 }
173
174
175
176
177
178 void CVectorValue::SetVector(double newvec[])
179 {
180         m_vec[KX_X] = m_transformedvec[KX_X] = newvec[KX_X];
181         m_vec[KX_Y] = m_transformedvec[KX_Y] = newvec[KX_Y];
182         m_vec[KX_Z] = m_transformedvec[KX_Z] = newvec[KX_Z];
183         
184         SetModified(true);
185 }
186
187
188 void CVectorValue::SetValue(CValue *newval)
189 {
190         
191         double* newvec = ((CVectorValue*)newval)->GetVector3();
192         m_vec[KX_X] = m_transformedvec[KX_X] = newvec[KX_X];
193         m_vec[KX_Y] = m_transformedvec[KX_Y] = newvec[KX_Y];
194         m_vec[KX_Z] = m_transformedvec[KX_Z] = newvec[KX_Z];
195         
196         SetModified(true);
197 }
198
199 static const STR_String gstrVectorStr=STR_String();
200 const STR_String & CVectorValue::GetText()
201 {
202         assertd(false);
203         return gstrVectorStr;
204 }
205
206 CValue* CVectorValue::GetReplica()
207 {
208         CVectorValue* replica = new CVectorValue(*this);
209         replica->ProcessReplica();
210         return replica;
211 };
212
213 #if 0
214 void CVectorValue::Transform(rcMatrix4x4 mat)
215 {
216         m_transformedvec = mat*m_vec;
217 }
218 #endif