static PyObject * Vector3D_dot(PyVector3D * self, PyVector3D * other) { if (!PyVector3D_Check(other)) { PyErr_SetString(PyExc_TypeError, "Can only dot with Vector3D"); return NULL; } return PyFloat_FromDouble(Dot(self->coords, other->coords)); }
static PyObject * Vector3D_angle(PyVector3D * self, PyVector3D * other) { if (!PyVector3D_Check(other)) { PyErr_SetString(PyExc_TypeError, "Can get angle to Vector3D"); return NULL; } return PyFloat_FromDouble(Angle(self->coords, other->coords)); }
static PyObject * Vector3D_cross(PyVector3D * self, PyVector3D * other) { if (!PyVector3D_Check(other)) { PyErr_SetString(PyExc_TypeError, "Can only cross with Vector3D"); return NULL; } PyVector3D * ret = newPyVector3D(); if (ret != NULL) { ret->coords = Cross(self->coords, other->coords); } return (PyObject *)ret; }
static PyVector3D*Vector3D_num_sub(PyVector3D*self,PyVector3D*other) { if (!PyVector3D_Check(other)) { PyErr_SetString(PyExc_TypeError, "Can only sub Vector3D from Vector3D"); return NULL; } PyVector3D * ret = newPyVector3D(); if (ret != NULL) { ret->coords = (self->coords - other->coords); } return ret; }
static PyPoint3D * Point3D_num_add(PyPoint3D * self, PyVector3D*other) { if (!PyVector3D_Check(other)) { PyErr_SetString(PyExc_TypeError, "Can only add Vector3D to Point3D"); return NULL; } PyPoint3D * ret = newPyPoint3D(); if (ret != NULL) { ret->coords = (self->coords + other->coords); } return ret; }
static PyObject * Quaternion_rotation(PyQuaternion * self, PyObject * args) { PyObject * axis_arg; double angle; if (!PyArg_ParseTuple(args, "Od", &axis_arg, &angle)) { return NULL; } if (!PyVector3D_Check(axis_arg)) { PyErr_SetString(PyExc_TypeError, "Argument must be a Vector3D"); return NULL; } PyVector3D * axis = (PyVector3D *)axis_arg; self->rotation.rotation(axis->coords, angle); Py_INCREF(Py_None); return Py_None; }
static PyObject *Vector3D_unit_vector_to(PyVector3D * self, PyVector3D * other) { if (!PyVector3D_Check(other)) { PyErr_SetString(PyExc_TypeError, "Argument must be a Vector3D"); return NULL; } PyVector3D * ret = newPyVector3D(); if (ret == NULL) { return NULL; } ret->coords = (other->coords - self->coords); WFMath::CoordType the_mag = ret->coords.mag(); if (!the_mag > 0) { PyErr_SetString(PyExc_ZeroDivisionError, "Attempt to normalize a vector with zero magnitude"); return NULL; } ret->coords /= the_mag; return (PyObject *)ret; }
static PyObject * Point3D_num_sub(PyPoint3D * self, PyObject * other) { if (PyVector3D_Check(other)) { PyVector3D * ovec = (PyVector3D *)other; PyPoint3D * ret = newPyPoint3D(); if (ret != NULL) { ret->coords = (self->coords - ovec->coords); } return (PyObject *)ret; } else if (PyPoint3D_Check(other)) { PyPoint3D * opoint = (PyPoint3D *)other; PyVector3D * ret = newPyVector3D(); if (ret != NULL) { ret->coords = (self->coords - opoint->coords); } return (PyObject *)ret; } else { PyErr_SetString(PyExc_TypeError, "Can only subtract Vector3D or Point3D from Point3D"); return NULL; } }
static int Quaternion_init(PyQuaternion * self, PyObject * args, PyObject * kwds) { PyObject * clist; switch (PyTuple_Size(args)) { case 0: break; case 1: clist = PyTuple_GetItem(args, 0); if (!PyList_Check(clist) || PyList_Size(clist) != 4) { PyErr_SetString(PyExc_TypeError, "Quaternion() from single value must a list 4 long"); return -1; } { float quaternion[4]; for(int i = 0; i < 4; i++) { PyObject * item = PyList_GetItem(clist, i); if (PyInt_Check(item)) { quaternion[i] = (WFMath::CoordType)PyInt_AsLong(item); } else if (PyFloat_Check(item)) { quaternion[i] = PyFloat_AsDouble(item); } else { PyErr_SetString(PyExc_TypeError, "Quaternion() must take list of floats, or ints"); return -1; } } self->rotation = Quaternion(quaternion[3], quaternion[0], quaternion[1], quaternion[2]); } break; case 2: { PyObject * v1 = PyTuple_GetItem(args, 0); PyObject * v2 = PyTuple_GetItem(args, 1); if (!PyVector3D_Check(v1)) { PyErr_SetString(PyExc_TypeError, "Quaternion(a,b) must take a vector"); return -1; } PyVector3D * arg1 = (PyVector3D *)v1; if (PyVector3D_Check(v2)) { PyVector3D * to = (PyVector3D *)v2; self->rotation = quaternionFromTo(arg1->coords, to->coords); } else if (PyFloat_Check(v2)) { float angle = PyFloat_AsDouble(v2); self->rotation.rotation(arg1->coords, angle); } else { PyErr_SetString(PyExc_TypeError, "Quaternion(a,b) must take a vector"); return -1; } } break; case 4: { float quaternion[4]; for(int i = 0; i < 4; i++) { PyObject * item = PyTuple_GetItem(args, i); if (PyInt_Check(item)) { quaternion[i] = (WFMath::CoordType)PyInt_AsLong(item); } else if (PyFloat_Check(item)) { quaternion[i] = PyFloat_AsDouble(item); } else { PyErr_SetString(PyExc_TypeError, "Quaternion() must take list of floats, or ints"); return -1; } } self->rotation = Quaternion(quaternion[3], quaternion[0], quaternion[1], quaternion[2]); } break; default: PyErr_SetString(PyExc_TypeError, "Quaternion must take list of floats, or ints, 4 ints or 4 floats"); return -1; break; } return 0; }