PyObject *scriptVertexAdd(PyObject *self, PyObject *args) {
    PyObject *pyCoords=Py_None;
    if (!PyArg_ParseTuple(args, "|O", &pyCoords))
        return NULL;

    if (figureData.dim<0) {
        scriptThrowException("There is no space yet, use new or open");
        return NULL;
    }

    GLdouble coords[figureData.dim];
    if (!coordsFromPython(pyCoords, coords))
        return NULL;

    if (!safeCheckPos(coords, figureData.dim)) {
        scriptThrowException("Wrong position");
        return NULL;
    }

    int index=figureVertexAdd(coords);

    scriptEventsPerform(&scriptEventsModified);

    return Py_BuildValue("i", index);
}
Ejemplo n.º 2
0
struct figureData *figureFromPython(PyObject *pyFigure) {
	struct figureData *figure=safeCalloc(1, sizeof(struct figureData));

	if (pyFigure==Py_None) {
		figure->dim=-1;
		return figure;
	}
	figure->dim=PyList_Size(pyFigure)-1;

	if (PyErr_Occurred() || figure->dim<0)
		throw("Wrong figure");
	figure->count=safeMalloc(sizeof(GLint) * (figure->dim+1));

	PyObject *vertices=PyList_GET_ITEM(pyFigure, 0);
	figure->count[0]=PyList_Size(vertices);
	if (PyErr_Occurred())
		throw("Wrong list of vertices");
	figure->vertices=safeCalloc(figure->count[0], sizeof(GLdouble *));
	for (int i=0; i<figure->count[0]; i++) {
		PyObject *vertex=PyList_GET_ITEM(vertices, i);
		figure->vertices[i]=safeMalloc(sizeof(GLdouble) * figure->dim);
		if ((PyTuple_Size(vertex)!=figure->dim) || (PyErr_Occurred()))
			throw("Wrong list of vertices");
		for (int j=0; j<figure->dim; j++) {
			PyObject *value=PyTuple_GET_ITEM(vertex, j);
			figure->vertices[i][j]=PyFloat_AsDouble(value);
		}
		if (!safeCheckPos(figure->vertices[i], figure->dim))
			throw("Wrong vertex position")
	}
	if (PyErr_Occurred())
		throw("Wrong list of vertices");

	figure->boundary=safeCalloc(figure->dim+1, sizeof(GLint **));
	for (int i=1; i<=figure->dim; i++) {
		PyObject *faces=PyList_GET_ITEM(pyFigure, i);
		figure->count[i]=PyList_Size(faces);
		if (PyErr_Occurred())
			throw("Wrong topology");
		figure->boundary[i]=safeCalloc(figure->count[i], sizeof(GLint *));
		for (int j=0; j<figure->count[i]; j++) {
			PyObject *face=PyList_GET_ITEM(faces, j);
			int count=PyList_Size(face);
			if (PyErr_Occurred())
				throw("Wrong topology");
			figure->boundary[i][j]=safeMalloc(sizeof(GLint) * (count+1));
			figure->boundary[i][j][0]=count;
			for (int k=1; k<=count; k++) {
				PyObject *value=PyList_GET_ITEM(face, k-1);
				figure->boundary[i][j][k]=PyInt_AsLong(value);
			}
		}
	}
	if (PyErr_Occurred())
		throw("Wrong topology");

	return figure;
}
PyObject *scriptVertexSetPos(PyObject *self, PyObject *args) {
    PyObject *pyCoords;
    int index;
    if (!PyArg_ParseTuple(args, "iO", &index, &pyCoords))
        return NULL;

    if ((figureData.dim<0) || (index<0) || (index>=figureData.count[0])) {
        scriptThrowException("Out of bounds");
        return NULL;
    }

    GLdouble coords[figureData.dim];
    if (!coordsFromPython(pyCoords, coords))
        return NULL;

    if (!safeCheckPos(coords, figureData.dim)) {
        scriptThrowException("Wrong position");
        return NULL;
    }

    figureVertexMove(index, coords);
    scriptEventsPerform(&scriptEventsModified);
    return Py_None;
}