Ejemplo n.º 1
0
static PyObject *Polygon_simplify(Polygon *self, PyObject *args) {
    gpc_polygon *ret, *lop, *rop, *tmp, *p = self->p;
    int i;
    if (p->num_contours <= 0) {
        Py_INCREF(Py_None);
        return Py_None;
    }
    if (! (lop = poly_p_new())) return Polygon_Raise(ERR_MEM);
    if (! (rop = poly_p_new())) return Polygon_Raise(ERR_MEM);
    if (! (ret = poly_p_new())) return Polygon_Raise(ERR_MEM);
    /* find first contour which is not a hole */
    i = 0;
    while ((i < p->num_contours) && (p->hole[i] == 1))
        i++;
    if (i < p->num_contours)
        gpc_add_contour(lop, p->contour+i, 1);
    /* then try to add other contours */
    for (i++; i < p->num_contours; i++) {
        if (p->hole[i] == 0) {
            gpc_free_polygon(rop);
            gpc_free_polygon(ret);
            gpc_add_contour(rop, (p->contour+i), 0);
            gpc_polygon_clip(GPC_UNION, lop, rop, ret);
            tmp = lop;
            lop = ret;
            ret = tmp;
        }
    }
    /* then try to cut out holes */
    for (i = 0; i < p->num_contours; i++) {
        if (p->hole[i] == 1) {
            gpc_free_polygon(rop);
            gpc_free_polygon(ret);
            gpc_add_contour(rop, (p->contour+i), 0);
            gpc_polygon_clip(GPC_DIFF, lop, rop, ret);
            tmp = lop;
            lop = ret;
            ret = tmp;
        }
    }
    gpc_free_polygon(self->p);
    free(self->p);
    self->p = lop;
    gpc_free_polygon(ret);
    free(ret);
    gpc_free_polygon(rop);
    free(rop);
    self->bbValid = 0;
    return Py_BuildValue("O", Py_None);
}
Ejemplo n.º 2
0
static PyObject *Polygon_opUnion(Polygon *self, Polygon *other) {
    gpc_polygon *ret;
    if (! Polygon_Check(other)) return Polygon_Raise(ERR_TYP);
    if (! (ret = poly_p_new())) return Polygon_Raise(ERR_MEM);
    gpc_polygon_clip(GPC_UNION, self->p, other->p, ret);
    return (PyObject *)Polygon_NEW(ret);
}
Ejemplo n.º 3
0
static PyObject *Polygon_new(PyTypeObject *type, PyObject *args, PyObject *kwds) {
    Polygon *self;
    self = (Polygon *)type->tp_alloc(type, 0);
    if (self != NULL) {
        self->bbValid = 0;
        if (! (self->gpc_p = poly_p_new())) {
            Py_DECREF(self);
            return NULL;
        }
    }
    return (PyObject *)self;
}
Ejemplo n.º 4
0
/* make a new Polygon, using gpc_p if not NULL */
static Polygon *Polygon_NEW(gpc_polygon *gpc_p) {
    Polygon *obj = PyObject_NEW(Polygon, &Polygon_Type);
    if (gpc_p != NULL)
        obj->gpc_p = gpc_p;
    else {
        if (! (obj->gpc_p = poly_p_new()))
            return (Polygon *)Polygon_Raise(ERR_MEM);
    }
    obj->bbValid = 0;
    obj->attr = NULL;
    return obj;
}
Ejemplo n.º 5
0
static Polygon *Polygon_NEW(void *ptr) {
    Polygon *obj = PyObject_NEW(Polygon, &Polygon_Type);
    if (ptr != NULL)
        /* create from existing gpc_polygon struct */
        obj->p = (gpc_polygon *)ptr;
    else {
        /* create a new struct */
        if (! (obj->p = poly_p_new()))
            return (Polygon *)Polygon_Raise(ERR_MEM);
    }
    obj->bbValid = 0;
    obj->attr = NULL;
    return obj;
}
Ejemplo n.º 6
0
static PyObject *Polygon_overlaps(Polygon *self, Polygon *other) {
    double x0, x1, y0, y1, X0, X1, Y0, Y1;
    gpc_polygon * pres;
    int r;
    if (! Polygon_Check(other))
        return Polygon_Raise(ERR_ARG);
    Polygon_getBoundingBox(self,  &x0, &x1, &y0, &y1);
    Polygon_getBoundingBox(other, &X0, &X1, &Y0, &Y1);
    /* first test if bounding box overlaps other boundingbox */
    if ((X0 > x1) || (x0 > X1) || (Y0 > y1) || (y0 > Y1))
        return Py_BuildValue("i",  0);
    /* still there? Let's do the full test... */
    if (! (pres = poly_p_new())) return Polygon_Raise(ERR_MEM);
    gpc_polygon_clip(GPC_INT, other->p, self->p, pres);
    r = pres->num_contours;
    gpc_free_polygon(pres);
    free(pres);
    return Py_BuildValue("i",  ((r > 0) ? 1 : 0));
}
Ejemplo n.º 7
0
static PyObject *Polygon_covers(Polygon *self, Polygon *other) {
    double x0, x1, y0, y1, X0, X1, Y0, Y1;
    gpc_polygon * pres;
    int r;
    if (! Polygon_Check(other))
        return Polygon_Raise(ERR_ARG);
    Polygon_getBoundingBox(self,  &x0, &x1, &y0, &y1);
    Polygon_getBoundingBox(other, &X0, &X1, &Y0, &Y1);
    /* first test if bounding box covers other boundingbox */ 
    if ((X0 < x0) || (X1 > x1) || (Y0 < y0) || (Y1 > y1))
        Py_RETURN_FALSE;
    /* still there? Let's do the full test... */  
    if (! (pres = poly_p_new())) return Polygon_Raise(ERR_MEM);
    gpc_polygon_clip(GPC_DIFF, other->gpc_p, self->gpc_p, pres);
    r = pres->num_contours;
    gpc_free_polygon(pres);
    free(pres);
    if (r > 0)
        Py_RETURN_FALSE;
    else
        Py_RETURN_TRUE;
}