//------------------------------------------------------------------------------------- int ScriptVector3::py_nonzero(PyObject *self) { ScriptVector3* sv = static_cast<ScriptVector3*>(self); // 点乘 Vector3 v = sv->getVector(); float val = v.x * v.x + v.y * v.y + v.z * v.z; return val > 0.f; }
//------------------------------------------------------------------------------------- PyObject* ScriptVector3::py_inplace_divide(PyObject *self, PyObject *b) { ScriptVector3* sv = static_cast<ScriptVector3*>(self); Vector3& v = sv->getVector(); float f = float(PyFloat_AsDouble(b)); v /= f; Py_INCREF(sv); return sv; }
//------------------------------------------------------------------------------------- PyObject* ScriptVector3::seq_item(PyObject* self, Py_ssize_t index) { if (index < 0 || VECTOR_SIZE <= index) { PyErr_SetString(PyExc_IndexError, "Vector3 index out of range"); //PyErr_PrintEx(0); return NULL; } ScriptVector3* sv = static_cast<ScriptVector3*>(self); return PyFloat_FromDouble(sv->getVector()[static_cast<int>(index)]); }
//------------------------------------------------------------------------------------- PyObject* ScriptVector3::__py_pyDot(PyObject* self, PyObject* args) { ScriptVector3* v = new ScriptVector3(0,0,0); PyObject* pyResult = v->__py_pySet((PyObject*)v, args); if(pyResult) Py_DECREF(pyResult); ScriptVector3* sv = static_cast<ScriptVector3*>(self); float result = KBEVec3Dot(const_cast<Vector3*>(&sv->getVector()), const_cast<Vector3*>(&v->getVector())); Py_DECREF(v); return PyFloat_FromDouble(result); }
//------------------------------------------------------------------------------------- PyObject* ScriptVector3::__py_pyScale(PyObject* self, PyObject* args) { if(PyTuple_Size(args) == 1) { ScriptVector3* sv = static_cast<ScriptVector3*>(self); Vector3& v = sv->getVector(); PyObject* pItem = PyTuple_GetItem(args, 0); return new ScriptVector3(v * float(PyFloat_AsDouble(pItem))); } PyErr_SetString(PyExc_TypeError, "Vector.scale expects a float argument"); PyErr_PrintEx(0); return NULL; }
//------------------------------------------------------------------------------------- PyObject* ScriptVector3::__py_pyNormalise(PyObject* self, PyObject* args) { if (PyTuple_Size(args) != 0) { PyErr_SetString(PyExc_TypeError, "Vector.normalise takes no arguments(nor does it brook any dissent :)"); PyErr_PrintEx(0); return NULL; } ScriptVector3* sv = static_cast<ScriptVector3*>(self); Vector3& v = sv->getVector(); KBEVec3Normalize(&v, &v); S_Return; }
//------------------------------------------------------------------------------------- int ScriptVector3::seq_ass_item(PyObject* self, Py_ssize_t index, PyObject* value) { ScriptVector3* sv = static_cast<ScriptVector3*>(self); if (index < 0 || VECTOR_SIZE <= index) { PyErr_SetString(PyExc_IndexError, "Vector assignment index out of range"); PyErr_PrintEx(0); return -1; } Vector3& v = sv->getVector(); v[static_cast<int>(index)] = float(PyFloat_AsDouble(value)); return 0; }
//------------------------------------------------------------------------------------- PyObject* ScriptVector3::__py_pyCross2D(PyObject* self, PyObject* args) { ScriptVector3* v = new ScriptVector3(0,0,0); ScriptVector3* sv = static_cast<ScriptVector3*>(self); PyObject* pyResult = v->__py_pySet((PyObject*)v, args); if(pyResult) Py_DECREF(pyResult); const Vector3 otherV = v->getVector(); const Vector3 thisV = sv->getVector(); float result = thisV[0] * otherV[2] - thisV[2] * otherV[0]; Py_DECREF(v); return PyFloat_FromDouble(result); }
//------------------------------------------------------------------------------------- PyObject* ScriptVector3::__py_pyFlatDistTo(PyObject* self, PyObject* args) { if (PyTuple_Size(args) != 1) { PyErr_SetString(PyExc_TypeError, "args > 1 is error!\n"); PyErr_PrintEx(0); S_Return; } ScriptVector3* sv = static_cast<ScriptVector3*>(self); Vector3& v = sv->getVector(); Vector3 v1; convertPyObjectToVector3(v1, PyTuple_GET_ITEM(args, 0)); return PyFloat_FromDouble(KBEVec3CalcVec2Length(v, v1)); }
//------------------------------------------------------------------------------------- PyObject* ScriptVector3::py_inplace_subtract(PyObject *self, PyObject *b) { if(!check(b)) { PyErr_Clear(); Py_INCREF(Py_NotImplemented); return Py_NotImplemented; } Vector3 bv; convertPyObjectToVector3(bv, b); ScriptVector3* sv = static_cast<ScriptVector3*>(self); Vector3& v = sv->getVector(); v -= bv; Py_INCREF(sv); return sv; }
//------------------------------------------------------------------------------------- PyObject* ScriptVector3::__py_pyList(PyObject* self, PyObject* args) { if (PyTuple_Size(args) != 0) { PyErr_SetString(PyExc_TypeError, "Vector.tuple takes no arguments"); PyErr_PrintEx(0); return NULL; } PyObject* pyList = PyList_New(VECTOR_SIZE); ScriptVector3* sv = static_cast<ScriptVector3*>(self); Vector3& v = sv->getVector(); for (int i=0; i < VECTOR_SIZE; ++i) PyList_SetItem(pyList, i, PyFloat_FromDouble(v[i])); return pyList; }
//------------------------------------------------------------------------------------- PyObject* ScriptVector3::__py_pyFlatDistSqrTo(PyObject* self, PyObject* args) { if (PyTuple_Size(args) != 1) { PyErr_SetString(PyExc_TypeError, "args > 1 is error!\n"); PyErr_PrintEx(0); S_Return; } ScriptVector3* sv = static_cast<ScriptVector3*>(self); Vector3& v = sv->getVector(); Vector3 v1; convertPyObjectToVector3(v1, PyTuple_GET_ITEM(args, 0)); float x = v.x - v1.x; float z = v.z - v1.z; return PyFloat_FromDouble(x*x + z*z); }
//------------------------------------------------------------------------------------- PyObject* ScriptVector3::__py_pySet(PyObject* self, PyObject* args) { ScriptVector3* sv = static_cast<ScriptVector3*>(self); bool good = false; Vector3 v; // 如果参数只有1个元素 int tupleSize = (int)PyTuple_Size(args); if(tupleSize == 1) { PyObject* pyItem = PyTuple_GetItem(args, 0); if(ScriptVector3::check(pyItem, false)) { convertPyObjectToVector3(v, pyItem); good = true; } else { float f = float(PyFloat_AsDouble(pyItem)); for (int i=0; i < VECTOR_SIZE; ++i) { v[i] = f; } good = true; } } else if(tupleSize >= VECTOR_SIZE) { convertPyObjectToVector3(v, args); good = true; } if(!good) { PyErr_Format(PyExc_TypeError, "Vector.set must be set to a tuple of %d floats, or one float", VECTOR_SIZE); PyErr_PrintEx(0); return NULL; } sv->setVector(v); S_Return; }
//------------------------------------------------------------------------------------- PyObject* ScriptVector3::__reduce_ex__(PyObject* self, PyObject* protocol) { ScriptVector3* sv = static_cast<ScriptVector3*>(self); Vector3& v = sv->getVector(); PyObject* args = PyTuple_New(2); PyObject* unpickleMethod = script::Pickler::getUnpickleFunc("Vector3"); PyTuple_SET_ITEM(args, 0, unpickleMethod); PyObject* args1 = PyTuple_New(VECTOR_SIZE); PyTuple_SET_ITEM(args1, 0, PyFloat_FromDouble(v.x)); PyTuple_SET_ITEM(args1, 1, PyFloat_FromDouble(v.y)); PyTuple_SET_ITEM(args1, 2, PyFloat_FromDouble(v.z)); PyTuple_SET_ITEM(args, 1, args1); if(unpickleMethod == NULL){ Py_DECREF(args); return NULL; } return args; }
//------------------------------------------------------------------------------------- PyObject* ScriptVector3::py_inplace_multiply(PyObject *self, PyObject *b) { ScriptVector3* sv = static_cast<ScriptVector3*>(self); Vector3& v = sv->getVector(); if (check(b, false)) { Vector3 bv; convertPyObjectToVector3(bv, b); // 叉乘 v.x = (v.y * bv.z) - (v.z * bv.y); v.y = (v.z * bv.x) - (v.x * bv.z); v.z = (v.x * bv.y) - (v.y * bv.x); } else { float f = float(PyFloat_AsDouble(b)); v *= f; } Py_INCREF(sv); return sv; }
//------------------------------------------------------------------------------------- PyObject* ScriptVector3::__py_pyDistSqrTo(PyObject* self, PyObject* args) { if (PyTuple_Size(args) != 1) { PyErr_SetString(PyExc_TypeError, "args > 1 is error!\n"); PyErr_PrintEx(0); S_Return; } PyObject* pyVal = PyTuple_GET_ITEM(args, 0); if(!check(pyVal)) { S_Return; } ScriptVector3* sv = static_cast<ScriptVector3*>(self); Vector3& v = sv->getVector(); Vector3 v1; convertPyObjectToVector3(v1, pyVal); Vector3 rv = (v - v1); return PyFloat_FromDouble(KBEVec3LengthSq(&rv)); //计算点乘并返回 }
//------------------------------------------------------------------------------------- Py_ssize_t ScriptVector3::seq_length(PyObject* self) { ScriptVector3* seq = static_cast<ScriptVector3*>(self); return seq->length(); }
//------------------------------------------------------------------------------------- PyObject* ScriptVector3::seq_slice(PyObject* self, Py_ssize_t startIndex, Py_ssize_t endIndex) { if(startIndex < 0) startIndex = 0; if(endIndex > VECTOR_SIZE) endIndex = VECTOR_SIZE; if(endIndex < startIndex) endIndex = startIndex; ScriptVector3* sv = static_cast<ScriptVector3*>(self); Vector3& my_v = sv->getVector(); PyObject* pyResult = NULL; int length = (int)(endIndex - startIndex); if (length == VECTOR_SIZE) { pyResult = sv; Py_INCREF(pyResult); } else switch(length) { case 0: pyResult = PyTuple_New(0); break; case 1: pyResult = PyTuple_New(1); PyTuple_SET_ITEM(pyResult, 0, PyFloat_FromDouble(sv->getVector()[static_cast<int>(startIndex)])); break; case 2: { Vector2 v; for (int i = (int)startIndex; i < (int)endIndex; ++i){ v[i - static_cast<int>(startIndex)] = my_v[i]; } pyResult = new ScriptVector2(v); break; } case 3: { Vector3 v; for (int i = (int)startIndex; i < (int)endIndex; ++i){ v[i - static_cast<int>(startIndex)] = my_v[i]; } pyResult = new ScriptVector3(v); break; } default: PyErr_Format(PyExc_IndexError, "Bad slice indexes [%d, %d] for Vector%d", startIndex, endIndex, VECTOR_SIZE); PyErr_PrintEx(0); break; } return pyResult; }
//------------------------------------------------------------------------------------- PyObject* ScriptVector3::py_positive(PyObject *self) { ScriptVector3* sv = static_cast<ScriptVector3*>(self); return new ScriptVector3(sv->getVector() * -1.f); }