Example #1
0
int main()
{
    WFMath::Vector<3> east(1,0,0), north(0,1,0), west(-1,0,0);
    WFMath::Vector<3> up(0,0,1), down(0,0,-1);

    Quaternion rotation;

    // Normal 90 degree rotation
    rotation = quaternionFromTo(east, north);

    // No rotation to cover special case
    rotation = quaternionFromTo(east, east);

    // Exact 180 to cover special case
    rotation = quaternionFromTo(east, west);

    // Exact 180 to cover other part of same case
    rotation = quaternionFromTo(up, down);

    return 0;
}
Example #2
0
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;
}