00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "asVector3d.h"
00018
00019 #ifdef __COMPILE_WITH_ANGELSCRIPT__
00020
00021 #include "asIrrHelper.h"
00022
00023
00024
00028 template<typename T>
00029 void constructor(void *memory)
00030 {
00031 new (memory) vector3d<T>();
00032 }
00033
00037 template<typename T>
00038 void constructor(const vector3d<T> &other, void *memory)
00039 {
00040 new (memory) vector3d<T>(other);
00041 }
00042
00046 template<typename T>
00047 void constructor(T n, void *memory)
00048 {
00049 new (memory) vector3d<T>(n);
00050 }
00051
00055 template<typename T>
00056 void constructor(T x, T y, T z, void *memory)
00057 {
00058 new (memory) vector3d<T>(x, y, z);
00059 }
00060
00064 template<typename T>
00065 void deconstructor(void *memory)
00066 {
00067 ((vector3d<T>*)memory)->~vector3d<T>();
00068 }
00069
00072 template<typename T>
00073 int compare(const vector3d<T> &a, const vector3d<T> &b)
00074 {
00075 if( a < b ) return -1;
00076 if( b > a ) return 1;
00077 return 0;
00078 }
00079
00080
00085 template<typename T>
00086 void bindVector3dT(asIScriptEngine *engine, const char *typeName, const char *asType)
00087 {
00088
00089 int r;
00090 std::stringstream ss;
00091
00092
00093 r = engine->RegisterObjectType(typeName, sizeof(vector3d<T>), asOBJ_VALUE | asOBJ_APP_CLASS_CDA); assert(r >= 0);
00094
00095
00096
00097 r = engine->RegisterObjectBehaviour(typeName, asBEHAVE_CONSTRUCT, "void f()", asFUNCTIONPR(constructor<T>,
00098 (void*), void), asCALL_CDECL_OBJLAST); assert(r >= 0);
00099
00100 ss.str("");
00101 ss << "void f(const " << typeName << "&in)";
00102 r = engine->RegisterObjectBehaviour(typeName, asBEHAVE_CONSTRUCT, ss.str().c_str(), asFUNCTIONPR(constructor<T>,
00103 (const vector3d<T>&, void*), void), asCALL_CDECL_OBJLAST); assert(r >= 0);
00104
00105 ss.str("");
00106 ss << "void f(" << asType << ")";
00107 r = engine->RegisterObjectBehaviour(typeName, asBEHAVE_CONSTRUCT, ss.str().c_str(), asFUNCTIONPR(constructor<T>,
00108 (T, void*), void), asCALL_CDECL_OBJLAST); assert(r >= 0);
00109
00110 ss.str("");
00111 ss << "void f(" << asType << ", " << asType << ", " << asType << ")";
00112 r = engine->RegisterObjectBehaviour(typeName, asBEHAVE_CONSTRUCT, ss.str().c_str(), asFUNCTIONPR(constructor<T>,
00113 (T, T, T, void*), void), asCALL_CDECL_OBJLAST); assert(r >= 0);
00114
00115 r = engine->RegisterObjectBehaviour(typeName, asBEHAVE_DESTRUCT, "void f()", asFUNCTION(deconstructor<T>),
00116 asCALL_CDECL_OBJLAST); assert(r >= 0);
00117
00118
00119
00120 ss.str("");
00121 ss << typeName << "& opAssign(const " << typeName << " &in)";
00122 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00123 asMETHODPR(vector3d<T>, operator=, (const vector3d<T>&),
00124 vector3d<T>&), asCALL_THISCALL); assert(r >= 0);
00125
00126
00127 ss.str("");
00128 ss << typeName << " opMul(const " << typeName << " &in)";
00129 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00130 asFUNCTIONPR((multiply< vector3d<T>, vector3d<T>, vector3d<T> >),
00131 (const vector3d<T>&, const vector3d<T>&), vector3d<T>),
00132 asCALL_CDECL_OBJFIRST); assert(r >= 0);
00133
00134 ss.str("");
00135 ss << typeName << " opMul(" << asType << ")";
00136 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00137 asFUNCTIONPR((multiply< vector3d<T>, vector3d<T>, T >),
00138 (const vector3d<T>&, const T&), vector3d<T>),
00139 asCALL_CDECL_OBJFIRST); assert(r >= 0);
00140
00141
00142 ss.str("");
00143 ss << typeName << "& opMulAssign(const " << typeName << " &in)";
00144 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00145 asMETHODPR(vector3d<T>, operator*=, (const vector3d<T>&),
00146 vector3d<T>&), asCALL_THISCALL); assert(r >= 0);
00147
00148 ss.str("");
00149 ss << typeName << "& opMulAssign(const " << asType << ")";
00150 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00151 asMETHODPR(vector3d<T>, operator*=, (const T), vector3d<T>&),
00152 asCALL_THISCALL); assert(r >= 0);
00153
00154
00155 ss.str("");
00156 ss << typeName << " opAdd(const " << typeName << " &in)";
00157 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00158 asFUNCTIONPR((add< vector3d<T>, vector3d<T>, vector3d<T> >),
00159 (const vector3d<T>&, const vector3d<T>&), vector3d<T>),
00160 asCALL_CDECL_OBJFIRST); assert(r >= 0);
00161
00162 ss.str("");
00163 ss << typeName << " opAdd(" << asType << ")";
00164 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00165 asFUNCTIONPR((add< vector3d<T>, vector3d<T>, T >),
00166 (const vector3d<T>&, const T&), vector3d<T>),
00167 asCALL_CDECL_OBJFIRST); assert(r >= 0);
00168
00169
00170 ss.str("");
00171 ss << typeName << "& opAddAssign(const " << typeName << " &in)";
00172 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00173 asMETHODPR(vector3d<T>, operator+=, (const vector3d<T>&),
00174 vector3d<T>&), asCALL_THISCALL); assert(r >= 0);
00175
00176 ss.str("");
00177 ss << typeName << "& opAddAssign(const " << asType << ")";
00178 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00179 asMETHODPR(vector3d<T>, operator+=, (const T), vector3d<T>&),
00180 asCALL_THISCALL); assert(r >= 0);
00181
00182
00183 ss.str("");
00184 ss << typeName << " opSub(const " << typeName << " &in)";
00185 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00186 asFUNCTIONPR((substract< vector3d<T>, vector3d<T>, vector3d<T> >),
00187 (const vector3d<T>&, const vector3d<T>&), vector3d<T>),
00188 asCALL_CDECL_OBJFIRST); assert(r >= 0);
00189
00190 ss.str("");
00191 ss << typeName << " opSub(" << asType << ")";
00192 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00193 asFUNCTIONPR((substract< vector3d<T>, vector3d<T>, T >),
00194 (const vector3d<T>&, const T&), vector3d<T>),
00195 asCALL_CDECL_OBJFIRST); assert(r >= 0);
00196
00197
00198 ss.str("");
00199 ss << typeName << "& opSubAssign(const " << typeName << " &in)";
00200 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00201 asMETHODPR(vector3d<T>, operator-=, (const vector3d<T>&),
00202 vector3d<T>&), asCALL_THISCALL); assert(r >= 0);
00203
00204 ss.str("");
00205 ss << typeName << "& opSubAssign(const " << asType << ")";
00206 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00207 asMETHODPR(vector3d<T>, operator-=, (const T), vector3d<T>&),
00208 asCALL_THISCALL); assert(r >= 0);
00209
00210
00211 ss.str("");
00212 ss << typeName << " opDiv(const " << typeName << " &in)";
00213 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00214 asFUNCTIONPR((divide< vector3d<T>, vector3d<T>, vector3d<T> >),
00215 (const vector3d<T>&, const vector3d<T>&), vector3d<T>),
00216 asCALL_CDECL_OBJFIRST); assert(r >= 0);
00217
00218 ss.str("");
00219 ss << typeName << " opDiv(" << asType << ")";
00220 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00221 asFUNCTIONPR((divide< vector3d<T>, vector3d<T>, T >),
00222 (const vector3d<T>&, const T&), vector3d<T>),
00223 asCALL_CDECL_OBJFIRST); assert(r >= 0);
00224
00225
00226 ss.str("");
00227 ss << typeName << "& opDivAssign(const " << typeName << " &in)";
00228 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00229 asMETHODPR(vector3d<T>, operator/=, (const vector3d<T>&),
00230 vector3d<T>&), asCALL_THISCALL); assert(r >= 0);
00231
00232 ss.str("");
00233 ss << typeName << "& opDivAssign(const " << asType << ")";
00234 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00235 asMETHODPR(vector3d<T>, operator/=, (const T), vector3d<T>&),
00236 asCALL_THISCALL); assert(r >= 0);
00237
00238
00239 ss.str("");
00240 ss << "bool opEquals(const " << typeName << " &in)";
00241
00242 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(), asFUNCTIONPR(equal< vector3d<T> >,
00243 (const vector3d<T>&, const vector3d<T>&), bool), asCALL_CDECL_OBJFIRST); assert(r >= 0);
00244
00245 ss.str("");
00246 ss << "bool opCmp(const " << typeName << " &in)";
00247 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(), asFUNCTIONPR(compare< T >,
00248 (const vector3d<T>&, const vector3d<T>&), int), asCALL_CDECL_OBJFIRST); assert(r >= 0);
00249
00250
00251 ss.str("");
00252 ss << typeName << " crossProduct(const " << typeName << " &in)";
00253 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00254 asMETHOD(vector3d<T>, crossProduct), asCALL_THISCALL); assert(r >= 0);
00255
00256 ss.str("");
00257 ss << asType << " dotProduct(const " << typeName << " &in)";
00258 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00259 asMETHOD(vector3d<T>, dotProduct), asCALL_THISCALL); assert(r >= 0);
00260
00261 ss.str("");
00262 ss << asType << " getDistanceFrom(const " << typeName << " &in)";
00263 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00264 asMETHOD(vector3d<T>, getDistanceFrom), asCALL_THISCALL); assert(r >= 0);
00265
00266 ss.str("");
00267 ss << asType << " getDistanceFromSQ(const " << typeName << " &in)";
00268 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00269 asMETHOD(vector3d<T>, getDistanceFromSQ), asCALL_THISCALL); assert(r >= 0);
00270
00271 ss.str("");
00272 ss << typeName << " getHorizontalAngle()";
00273 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00274 asMETHOD(vector3d<f64>, getHorizontalAngle), asCALL_THISCALL); assert(r >= 0);
00275
00276 ss.str("");
00277 ss << typeName << " getInterpolated(const " << typeName << " &in, f64)";
00278 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00279 asMETHOD(vector3d<T>, getInterpolated), asCALL_THISCALL); assert(r >= 0);
00280
00281 ss.str("");
00282 ss << typeName << " getInterpolated_quadratic(const " << typeName << " &in, const " << typeName << "&in, f64)";
00283 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00284 asMETHOD(vector3d<T>, getInterpolated_quadratic), asCALL_THISCALL); assert(r >= 0);
00285
00286 ss.str("");
00287 ss << asType << " getLength()";
00288 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00289 asMETHOD(vector3d<T>, getLength), asCALL_THISCALL); assert(r >= 0);
00290
00291 ss.str("");
00292 ss << asType << " getLengthSQ()";
00293 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00294 asMETHOD(vector3d<T>, getLengthSQ), asCALL_THISCALL); assert(r >= 0);
00295
00296 ss.str("");
00297 ss << typeName << "& interpolate(const " << typeName << " &in, const " << typeName << "&in, f64)";
00298 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00299 asMETHOD(vector3d<T>, interpolate), asCALL_THISCALL); assert(r >= 0);
00300
00301 ss.str("");
00302 ss << typeName << "& invert()";
00303 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00304 asMETHOD(vector3d<T>, invert), asCALL_THISCALL); assert(r >= 0);
00305
00306 ss.str("");
00307 ss << "bool isBetweenPoints(const " << typeName << " &in, const " << typeName << "&in)";
00308 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00309 asMETHOD(vector3d<T>, isBetweenPoints), asCALL_THISCALL); assert(r >= 0);
00310
00311 ss.str("");
00312 ss << typeName << "& normalize()";
00313 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00314 asMETHOD(vector2d<T>, normalize), asCALL_THISCALL); assert(r >= 0);
00315
00316 ss.str("");
00317 ss << "void rotateXYBy(f64, const " << typeName << "&in)";
00318 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00319 asMETHOD(vector3d<T>, rotateXYBy), asCALL_THISCALL); assert(r >= 0);
00320
00321 ss.str("");
00322 ss << "void rotateXZBy(f64, const " << typeName << "&in)";
00323 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00324 asMETHOD(vector3d<T>, rotateXZBy), asCALL_THISCALL); assert(r >= 0);
00325
00326 ss.str("");
00327 ss << "void rotateYZBy(f64, const " << typeName << "&in)";
00328 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00329 asMETHOD(vector3d<T>, rotateYZBy), asCALL_THISCALL); assert(r >= 0);
00330
00331 ss.str("");
00332 ss << "void rotationToDirection(const " << typeName << "&in)";
00333 r = engine->RegisterObjectMethod(typeName, ss.str().c_str(),
00334 asMETHOD(vector3d<T>, rotationToDirection), asCALL_THISCALL); assert(r >= 0);
00335
00336
00337 ss.str("");
00338 ss << asType << " X";
00339 r = engine->RegisterObjectProperty(typeName, ss.str().c_str(), offsetof(vector3d<T>, X)); assert(r >= 0);
00340
00341 ss.str("");
00342 ss << asType << " Y";
00343 r = engine->RegisterObjectProperty(typeName, ss.str().c_str(), offsetof(vector3d<T>, Y)); assert(r >= 0);
00344
00345 ss.str("");
00346 ss << asType << " Z";
00347 r = engine->RegisterObjectProperty(typeName, ss.str().c_str(), offsetof(vector3d<T>, Z)); assert(r >= 0);
00348 }
00349
00352 void bindVector3d(asIScriptEngine *engine)
00353 {
00354 bindVector3dT<f32>(engine, "vector3df", "f32");
00355 bindVector3dT<s32>(engine, "vector3di", "s32");
00356 }
00357
00360 std::string getVector3dName(const std::string &asType)
00361 {
00362 if(asType == "f32") return std::string("vector3df");
00363 if(asType == "s32") return std::string("vector3di");
00364 else return std::string("");
00365 }
00366
00367 #endif // __COMPILE_WITH_ANGELSCRIPT__
00368
00369