/*!The method uses #_shape1 and #_shape2 structure as an input data and produces one or more polygons representing the result of the logical OR between the input polygons. Method returns false if no output shapes are generated, and true otherwise*/ bool logicop::logic::OR(pcollection& plycol) { bool result = false; VPoint* centinel = NULL; if (0 == _crossp) { // If there are no crossing points found, this still does not mean // that the operation will fail. Polygons might be fully overlapping... // Check that a random point from poly1 is inside poly2 ... if (_shape1->inside(_poly2)) centinel = _shape2; // ... if not, check that a random point from poly2 is inside poly1 ... else if (_shape2->inside(_poly1)) centinel = _shape1; // ... if not - polygons does not have any common area else return false; // If we've got here means that one of the polygons is completely // overlapped by the other one. So we need to return the outer one pointlist *shgen = new pointlist(); VPoint* vpnt = centinel; do { shgen->push_back(TP(vpnt->cp()->x(), vpnt->cp()->y())); vpnt = vpnt->next(); }while (centinel != vpnt); plycol.push_back(shgen); return true; } pcollection lclcol; // local collection of the resulting shapes // get first external and non crossing point centinel = getFirstOutside(_poly2, _shape1); if (NULL == centinel) centinel = getFirstOutside(_poly1, _shape2); assert(centinel); VPoint* collector = centinel; bool direction = true; /*next*/ do { if (0 == collector->visited()) { pointlist *shgen = new pointlist(); VPoint* pickup = collector; direction = (0 == lclcol.size()); do { pickup = pickup->follower(direction); shgen->push_back(TP(pickup->cp()->x(), pickup->cp()->y())); } while (pickup != collector); direction = true; lclcol.push_back(shgen); result = true; } collector = collector->next(); } while (collector != centinel); if (!result) return result; // Convert all collected shapes to a single normalized polygon pointlist* respoly = lclcol.front();lclcol.pop_front(); while (0 < lclcol.size()) { respoly = hole2simple(*respoly, *(lclcol.front())); lclcol.pop_front(); } plycol.push_back(respoly); return result; }
/*!If more than one logical operatoin has to be executed over the input shapes the raw data #_shape1 and #_shape2 can be reused, but has to be recycled beforehand This method is traversing both fields and invokes VPoint::reset_visited() in order to reinitialize the CPoint::_visited fields*/ void logicop::logic::reset_visited() { VPoint* centinel = _shape1; VPoint* looper = centinel; do { looper->reset_visited(); looper = looper->next(); } while (centinel != looper); centinel = _shape2; looper = centinel; do { looper->reset_visited(); looper = looper->next(); } while (centinel != looper); }
/*!The method uses #_shape1 and #_shape2 structure as an input data and produces one or more polygons representing the result of the logical AND between the input polygons. Method returns false if no output shapes are generated, and true otherwise*/ bool logicop::logic::AND(pcollection& plycol) { bool result = false; VPoint* centinel = NULL; if (0 == _crossp) { // If there are no crossing points found, this still does not mean // that the operation will fail. Polygons might be fully overlapping... // Check that a random point from poly1 is inside poly2 ... if (_shape1->inside(_poly2)) centinel = _shape1; // ... if not, check that a random point from poly2 is inside poly1 ... else if (_shape2->inside(_poly1)) centinel = _shape2; // ... if not - polygons does not have any common area else return false; // If we've got here means that one of the polygons is completely // overlapped by the other one. So we need to return the inner one pointlist *shgen = new pointlist(); VPoint* vpnt = centinel; do { shgen->push_back(TP(vpnt->cp()->x(), vpnt->cp()->y())); vpnt = vpnt->next(); }while (centinel != vpnt); plycol.push_back(shgen); return true; } bool direction = true; /*next*/ //if crossing points exists, get first external and non crossing point centinel = getFirstOutside(_poly2, _shape1); if (NULL == centinel) centinel = getFirstOutside(_poly1, _shape2); assert(centinel); VPoint* collector = centinel; do { if (0 == collector->visited()) { pointlist *shgen = new pointlist(); VPoint* pickup = collector; do { pickup = pickup->follower(direction); shgen->push_back(TP(pickup->cp()->x(), pickup->cp()->y())); } while (pickup != collector); plycol.push_back(shgen); result = true; } collector = collector->prev(); } while (collector != centinel); return result; }
/*! This method returns properly sorted dual linked list of all vertices (including crossing ones) of this segment collection. The method should be called after normalize(). The list created here is used as a source data when the new polygons are generated. All logic operations are using this data. This is effectively the input polygon vertices and the crossing points lined-up conterclockwise*/ logicop::VPoint* logicop::segmentlist::dump_points() { logicop::VPoint* vlist = NULL; for (unsigned i = 0; i < _segs.size(); i++) _segs[i]->dump_points(vlist); logicop::VPoint* lastV = vlist; VPoint* centinel = NULL; while (vlist->prev()) { if (-1 == vlist->visited()) centinel = vlist; vlist = vlist->prev(); } lastV->set_next(vlist); vlist->set_prev(lastV); if (NULL != centinel) { VPoint* vwork = centinel; do { if (-1 == vwork->visited()) { //here visited == 0 means only that the object is Cpoint. VPoint* tbdel = NULL; if ((*vwork->cp()) == (*vwork->prev()->cp())) { tbdel = vwork->prev(); vwork->set_prev(vwork->prev()->prev()); vwork->prev()->set_next(vwork); } else if ((*vwork->cp()) == (*vwork->next()->cp())) { tbdel = vwork->next(); vwork->set_next(vwork->next()->next()); vwork->next()->set_prev(vwork); } vwork = vwork->next(); if (tbdel) delete tbdel; } else vwork = vwork->next(); } while (centinel != vwork); } return vlist; }