Beispiel #1
0
static PyObject*
nb_multiply (PyObject* o1, PyObject* o2) {
    double o1x, o1y, o1z = 0;
    double o2x, o2y, o2z = 0;
    PyObject *args, *result;
    PySoy_atoms_Position_Object *so1, *so2;

    // parse object 1
    if (PyLong_Check(o1)) {
        o1x = o1y = o1z = PyLong_AsDouble(o1);
    } else if (PyFloat_Check(o1)) {
        o1x = o1y = o1z = PyFloat_AsDouble(o1);
    } else if (PySoy_atoms_Position_Check(o1)) {
        so1 = (PySoy_atoms_Position_Object*)o1;
        o1x = soy_atoms_position_get_x(so1->g);
        o1y = soy_atoms_position_get_y(so1->g);
        o1z = soy_atoms_position_get_z(so1->g);
    } else {
        PyErr_SetString(PyExc_TypeError, "unsupported operand type(s)");
        return NULL;
    }

    // parse object 2
    if (PyLong_Check(o2)) {
        o2x = o2y = o2z = PyLong_AsDouble(o2);
    } else if (PyFloat_Check(o2)) {
        o2x = o2y = o2z = PyFloat_AsDouble(o2);
    } else if (PySoy_atoms_Position_Check(o2) && !PySoy_atoms_Position_Check(o1)) {
        so2 = (PySoy_atoms_Position_Object*)o2;
        o2x = soy_atoms_position_get_x(so2->g);
        o2y = soy_atoms_position_get_y(so2->g);
        o2z = soy_atoms_position_get_z(so2->g);
    } else {
        PyErr_SetString(PyExc_TypeError, "unsupported operand type(s)");
        return NULL;
    }

    // build args with calculated values
    args = Py_BuildValue("((fff))", o1x * o2x, o1y * o2y, o1z * o2z);

    // create result object
    result = tp_new(&PySoy_atoms_Position_Type, args, NULL);

    // decref args tuple
    Py_DECREF(args);

    // return calculated result
    return result;
}
Beispiel #2
0
static PyObject*
tp_new (PyTypeObject* type, PyObject* args, PyObject* kwds) {
    SELF self;
    PyObject* py_position = NULL;
    PyObject* py_size = NULL;
    PySoy_materials_Material_Object* py_material = NULL;
    soyatomsPosition* position = NULL;
    soyatomsSize* size = NULL;
    soymaterialsMaterial* material = NULL;
    float x, y, z;

    static char* kw[] = {"position", "size", "material", NULL};

    // Parse arguments
    if (!PyArg_ParseTupleAndKeywords(
            args, kwds, "|OOO!", kw,
            &py_position,
            &py_size,
            &PySoy_materials_Material_Type, &py_material))
        return NULL;

    // Parse py_position if its neither NULL or a Position type
    if (py_position) {
        if (PySoy_atoms_Position_Check(py_position)) {
            position = (soyatomsPosition*) ((PySoy__G_Object*) py_position)->g;
            g_object_ref(position);
        }
        else {
            if (!PyArg_ParseTuple(py_position, "fff", &x, &y, &z)) {
                PyErr_SetString(PyExc_ValueError,
                                "Argument 1 must be either a soy.atoms.Position or a sequence of 3 floats");
                return NULL;
            }
            position = soy_atoms_position_new(x, y, z);
        }
    }

    // Parse py_size if its neither NULL or a Size type
    if (py_size) {
        if (PySoy_atoms_Size_Check(py_size)) {
            size = (soyatomsSize*) ((PySoy__G_Object*) py_size)->g;
            g_object_ref(size);
        }
        else {
            if (!PyArg_ParseTuple(py_size, "fff", &x, &y, &z)) {
                PyErr_SetString(PyExc_ValueError,
                                "Argument 2 must be either a soy.atoms.Size or a sequence of 3 floats");
                return NULL;
            }
            size = soy_atoms_size_new(x, y, z);
        }
    }

    // Grab material's gobject
    material = (py_material) ? py_material->g : NULL;

    // inherit base type
    self = (SELF) PyType_GenericNew(type, args, kwds);
    if (!self)
      return NULL;

    // new gobject
    self->g = soy_bodies_box_new(position, size, material);

    // unref position and size
    if (position) g_object_unref(position);
    if (size) g_object_unref(size);

    // return self
    return (PyObject*) self;
}