Beispiel #1
0
bool CompOnePolygon(RPolygon &p1, RPolygon &p2) {
    int xl1, xh1, yl1, yh1;
    int xl2, xh2, yl2, yh2;
    p1.get(&xl1, &yl1, &xh1, &yh1);
    p2.get(&xl2, &yl2, &xh2, &yh2);
    if(yl1>yl2) return true;
    if(yl1<yl2) return false;
    return (xl1 > xl2);
}
    /*!
    * @brief functor for columnar parallel version
    * @param[in] r range of map to be operated on
    */
    void operator()(const tbb::blocked_range<int> & r) const {
#ifdef _DEBUG
        // if we are debugging, serialize the method.  That way we can
        // see what is happening in each strip without the interleaving
        // confusing things.
        tbb::spin_mutex::scoped_lock lock(*m_rMutex);
        cout << unitbuf << "From " << r.begin() << " to " << r.end()-1 << std::endl;
#endif
        // instead of handing out subsets of polygons from map1 to intersect
        // with the polygons in map2, we are handed a strip of the map from
        // [(r.begin(),0)-(r.end()-1,yMapSize)].
        //
        // make a polygon with those values, and intersect with all the polygons
        // in map1 and map2, creating flagged polygon lists fmap1 and fmap2.
        // There are four possiblities:
        //
        //   1) a polygon is contained entirely within the strip.  We just
        //      add the polygon to our flagged map.
        //   2) the polygon will be partly contained in our strip, and partly
        //      in the strip to our right (higher x values).  Add the polygon
        //      to our flagged map.
        //   3) the polygon is partly contained in our map, and partly in the
        //      strip to our left.  Add the polygon to our map, but flag it as
        //      a duplicate.
        //   4) the polygons do not intersect. Don't add to flagged map.
        //

        // get yMapSize
        int r1, g1, b1, r2, g2, b2;
        int myr=-1;
        int myg=-1;
        int myb=-1;
        int i1, i2, i3, yMapSize;
        m_map1->at(0)->get(&i1, &i2, &i3, &yMapSize);
        RPolygon *slicePolygon = RPolygon::alloc_RPolygon(r.begin(), 0, r.end() - 1, yMapSize);

        Flagged_map_t *fmap1, *fmap2;
        fmap1 = new std::vector<RPolygon_flagged>;
        fmap1->reserve(m_map1->size());
        fmap2 = new Flagged_map_t;
        fmap2->reserve(m_map2->size());

        PRINT_DEBUG(std::endl << "Map1 -------------------");
        for(unsigned int i=1; i<m_map1->size(); i++) {
            int xl, yl, xh, yh;
            RPolygon *px = m_map1->at(i);
            if(PolygonsOverlap(slicePolygon, px, xl, yl, xh, yh)) {
                bool is_duplicate = false;
                int pxl, pyl, pxh, pyh;
                int indx = (int)(fmap1->size());
                fmap1->resize(indx+1);
                fmap1->at(indx).setp(px);
                px->get(&pxl, &pyl, &pxh, &pyh);
                if(pxl < xl) {
                    is_duplicate = true;
                }
                //fmap1->at(indx).setp(px);
                fmap1->at(indx).setDuplicate(is_duplicate);
                PRINT_DEBUG(" Polygon " << *px << " is in map, is_duplicate=" << is_duplicate);

            }
        }

        PRINT_DEBUG(std::endl << "Map2 -------------------");

        for(unsigned int i=1; i<m_map2->size(); i++) {
            int xl, yl, xh, yh;
            RPolygon *px = m_map2->at(i);

            if(PolygonsOverlap(slicePolygon, px, xl, yl, xh, yh)) {
                bool is_duplicate = false;
                int pxl, pyl, pxh, pyh;
                int indx = (int)(fmap2->size());
                fmap2->resize(indx+1);
                fmap2->at(indx).setp(px);
                px->get(&pxl, &pyl, &pxh, &pyh);
                if(pxl < xl) {
                    is_duplicate = true;
                }
                fmap2->at(indx).setDuplicate(is_duplicate);
                PRINT_DEBUG(" Polygon " << *px << " is in map, is_duplicate=" << is_duplicate);
            }
        }

        // When intersecting polygons from fmap1 and fmap2, if BOTH are flagged
        // as duplicate, don't add the result to the output map.  We can still
        // intersect them, because we are keeping track of how much of the polygon
        // is left over from intersecting, and quitting when the polygon is
        // used up.

        for(unsigned int ii=0; ii < fmap1->size(); ii++) {
            RPolygon *p1 = fmap1->at(ii).p();
            bool is_dup = fmap1->at(ii).isDuplicate();
            int parea = p1->area();
            p1->getColor(&r1, &g1, &b1);
            for(unsigned int jj=0;(jj < fmap2->size()) && (parea > 0); jj++) {
                int xl, yl, xh, yh;
                RPolygon *p2 = fmap2->at(jj).p();
                if(PolygonsOverlap(p1, p2, xl, yl, xh, yh)) {
                    if(!(is_dup && fmap2->at(jj).isDuplicate())) {
                        p2->getColor(&r2, &g2, &b2);
                        myr = r1 + r2;
                        myg = g1 + g2;
                        myb = b1 + b2;
                        RPolygon *pnew = RPolygon::alloc_RPolygon(xl, yl, xh, yh, myr, myg, myb);
#ifdef _DEBUG
#else
                        tbb::spin_mutex::scoped_lock lock(*m_rMutex);
#endif
                        (*m_resultMap).push_back(pnew);
                    }
                    parea -= (xh-xl+1)*(yh-yl+1);
                }
            }
        }

        delete fmap1;
        delete fmap2;
        RPolygon::free_RPolygon( slicePolygon );
    }