Beispiel #1
0
static PyObject*
nb_subtract (PyObject* o1, PyObject* o2) {
    float o1w = 0, o1h = 0, o1d = 0;
    float o2w = 0, o2h = 0, o2d = 0;
    PyObject *args, *result;
    PySoy_atoms_Size_Object *so1, *so2;

    //* parse object attributes
    if (!PySoy_atoms_Size_Check(o1) || !PySoy_atoms_Size_Check(o2)) {
        PyErr_SetString(PyExc_TypeError, "unsupported operand type(s)");
        return NULL;
    }

    so1 = (PySoy_atoms_Size_Object*)o1;
    o1w = soy_atoms_size_get_width(so1->g);
    o1h = soy_atoms_size_get_height(so1->g);
    o1d = soy_atoms_size_get_depth(so1->g);

    so2 = (PySoy_atoms_Size_Object*)o2;
    o2w = soy_atoms_size_get_width(so2->g);
    o2h = soy_atoms_size_get_height(so2->g);
    o2d = soy_atoms_size_get_depth(so2->g);

    // build args with calculated values
    args = Py_BuildValue("((fff))", o1w - o2w, o1h - o2h, o1d - o2d);

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

    // decref args tuple and tmp
    Py_DECREF(args);

    // return calculated result
    return result;
}
Beispiel #2
0
static PyObject*
nb_multiply (PyObject* o1, PyObject* o2) {
    float o1w = 0, o1h = 0, o1d = 0;
    float o2w = 0, o2h = 0, o2d = 0;
    PyObject *args, *result;
    PySoy_atoms_Size_Object *so1, *so2;

    // parse object 1
    if (PyLong_Check(o1)) {
        o1w = o1h = o1d = PyLong_AsDouble(o1);
    }
    else if (PyFloat_Check(o1)) {
        o1w = o1h = o1d = PyFloat_AsDouble(o1);
    }
    else if (PySoy_atoms_Size_Check(o1)) {
        so1 = (PySoy_atoms_Size_Object*)o1;
        o1w = soy_atoms_size_get_width(so1->g);
        o1h = soy_atoms_size_get_height(so1->g);
        o1d = soy_atoms_size_get_depth(so1->g);
    }
    else {
        PyErr_SetString(PyExc_TypeError, "unsupported operand type(s)");
        return NULL;
    }

    // parse object 2
    if (PyLong_Check(o2)) {
        o2w = o2h = o2d = PyLong_AsDouble(o2);
    }
    else if (PyFloat_Check(o2)) {
        o2w = o2h = o2d = PyFloat_AsDouble(o2);
    }
    else if (PySoy_atoms_Size_Check(o2)) {
        so2 = (PySoy_atoms_Size_Object*)o2;
        o2w = soy_atoms_size_get_width(so2->g);
        o2h = soy_atoms_size_get_height(so2->g);
        o2d = soy_atoms_size_get_depth(so2->g);
    } else {
        PyErr_SetString(PyExc_TypeError, "unsupported operand type(s)");
        return NULL;
    }

    // build args with calculated values
    args = Py_BuildValue("((fff))", o1w * o2w, o1h * o2h, o1d * o2d);

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

    // decref args tuple and tmp
    Py_DECREF(args);

    // return calculated result
    return result;
}
Beispiel #3
0
static PyObject*
tp_new (PyTypeObject *type, PyObject *args, PyObject *kwds) {
    SELF self;
    PySoy_scenes_Scene_Object*   scene;
    PySoy_bodies_Body_Object*    controlled;
    PySoy_atoms_Position_Object* dest;
    float radius, speed, granularity = -1.0f, fuzziness = -1.0f;
    int updates = FALSE, paused = FALSE;
    PyObject* bounds = Py_None;

    static char *kw[] = {"scene", "controlled", "dest", "speed", "granularity", "fuzziness", "bounds", "updates", "paused", NULL};

    if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!O!O!ff|fOii",   kw,
                                     &PySoy_scenes_Scene_Type,      &scene,
                                     &PySoy_bodies_Body_Type,       &controlled,
                                     &PySoy_atoms_Position_Type,    &dest,
                                                                    &speed,
                                                                    &granularity,
                                                                    &fuzziness,
                                                                    &bounds,
                                                                    &updates,
                                                                    &paused))
        return NULL;

    if (fuzziness == -1.0f) {
        fuzziness = speed/10000.0f;
    } else if (fuzziness < 0.0f) {
        PyErr_SetString(PyExc_ValueError, "'fuzziness' must be a number greater than 0");
        return NULL;
    }

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

    if (bounds == Py_None) {
        self->g = soy_controllers_space_navigator_new(scene->g, controlled->g, speed, fuzziness, granularity, dest->g, updates, paused);
    } else if (PySoy_atoms_Size_Check(bounds)) {
        soycontrollersgraphSpace* graph = soy_controllers_graph_space_new_with_size(scene->g, granularity, ((PySoy_atoms_Size_Object*)bounds)->g,NULL);
        self->g = soy_controllers_space_navigator_new_with_graph(scene->g, controlled->g, speed, fuzziness, (soycontrollersgraphIGraph*) graph, dest->g, updates, paused);
    } else if (PyNumber_Check(bounds)) {
        PyObject* flt = PyNumber_Float(bounds);
        if (flt == NULL) 
            return NULL;
        radius = (float)PyFloat_AsDouble(flt);
        Py_DECREF(flt);
        if (PyErr_Occurred()) { return NULL; }

        soycontrollersgraphSpace* graph = soy_controllers_graph_space_new_with_radius(scene->g, granularity, radius,NULL);
        self->g = soy_controllers_space_navigator_new_with_graph(scene->g, controlled->g, speed, fuzziness, (soycontrollersgraphIGraph*) graph, dest->g, updates, paused);
    } else {
        PyErr_SetString(PyExc_ValueError, "'bounds' must be either a number greater then 0 or a soy.atoms.Size");
        return NULL;
    }

    return (PyObject*) self;
}
Beispiel #4
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;
}