#include <quadtree.h>

Collaboration diagram for Geom::QuadTree:

List of all members.

Public Member Functions

 QuadTree ()
Quadsearch (double x0, double y0, double x1, double y1)
void insert (double x0, double y0, double x1, double y1, int shape)
Quadsearch (Geom::Rect const &r)
void insert (Geom::Rect const &r, int shape)
void erase (Quad *q, int shape)

Public Attributes

Quadroot
double scale
double bx0
double bx1
double by0
double by1

Private Member Functions

bool clean_root ()

Detailed Description

Definition at line 70 of file quadtree.h.


Constructor & Destructor Documentation

Geom::QuadTree::QuadTree (  )  [inline]

Definition at line 77 of file quadtree.h.

: root(0), scale(1) {}


Member Function Documentation

bool Geom::QuadTree::clean_root (  )  [private]

Definition at line 253 of file quadtree.cpp.

References Geom::Quad::children, Geom::Quad::data, Barcode::Code39Ext::i, and root.

Referenced by insert().

                          { 
    assert(root);

    // false if root *has* rects assigned to it.
    bool all_clean = root->data.empty(); 

    // if root has children we get false
    for(unsigned i = 0; i < 4; i++)
    {
        if(root->children[i])
        {
            all_clean = false;
        }
    }

    if(all_clean)
    {
        delete root;
        root=0;
        return true;
    }
    return false;
}

void Geom::QuadTree::erase ( Quad q,
int  shape 
)

Definition at line 236 of file quadtree.cpp.

References Geom::Quad::data, and Barcode::Code39Ext::i.

                                       {
    for(Quad::iterator i = q->data.begin();  i != q->data.end(); i++) {
        if(*i == shape) {
            q->data.erase(i);
            if(q->data.empty()) {

            }
        }
    }
    return;
}

void Geom::QuadTree::insert ( Geom::Rect const &  r,
int  shape 
)

Definition at line 9 of file quadtree.cpp.

References insert(), Geom::max(), and Geom::min().

                                              {
    insert(r[0].min(), r[1].min(),
           r[0].max(), r[1].max(), shape);
}

void Geom::QuadTree::insert ( double  x0,
double  y0,
double  x1,
double  y1,
int  shape 
)

Definition at line 130 of file quadtree.cpp.

References bx0, bx1, by0, by1, Geom::Quad::children, clean_root(), Geom::Quad::data, Barcode::Code39Ext::i, samplify::q, and root.

Referenced by insert().

                                                                           {
    // loop until a quad would break the box.

    // empty root => empty QuadTree. Create initial bounding box (0,0), (1,1)
    if(root == 0) {
        root = new Quad;
            
        bx0 = 0;
        bx1 = 1;
        by0 = 0;
        by1 = 1;
    }
    Quad *q = root;

    //A temp bounding box. Same as root's bounting box (ie of the whole QuadTree)
    double bxx0 = bx0, bxx1 = bx1;
    double byy0 = by0, byy1 = by1;

    while((bxx0 > x0) ||
          (bxx1 < x1) ||
          (byy0 > y0) ||
          (byy1 < y1)) { 
        // QuadTree has small size, can't accomodate new rect. Double the size:
        unsigned i = 0;

        if(bxx0 > x0) {
            bxx0 = 2*bxx0 - bxx1;
            i += 1;
        } else {
            bxx1 = 2*bxx1 - bxx0;
        }
        if(byy0 > y0) {
            byy0 = 2*byy0 - byy1;
            i += 2;
        } else {
            byy1 = 2*byy1 - byy0;
        }
        q = new Quad;
        //check if root is empty (no rects, no quad children)
        if( clean_root() ){
            root = q;
        }
        else{
            q->children[i] = root;
            root = q;
        }
        bx0 = bxx0;
        bx1 = bxx1;
        by0 = byy0;
        by1 = byy1;
    }

    while(q) {
        // Find the center of the temp bounding box
        double cx = (bxx0 + bxx1)/2;
        double cy = (byy0 + byy1)/2;
        unsigned i = 0;
        assert(x0 >= bxx0);
        assert(x1 <= bxx1);
        assert(y0 >= byy0);
        assert(y1 <= byy1);

        if(x0 >= cx) {
            i += 1;
            bxx0 = cx; // zoom in a quad
        } else if(x1 <= cx) {
            bxx1 = cx;
        } else{
            // rect does not fit in one unique quarter (in X axis) of the temp bounding box
            break;
        }
        if(y0 >= cy) {
            i += 2;
            byy0 = cy;
        } else if(y1 <= cy) {
            byy1 = cy;
        } else{
            // rect does not fit in one unique quarter (in Y axis) of the temp bounding box
            break;
        }

        // check if rect's bounding box has size 1x1. This means that rect is defined by 2 points
        // that are in the same place.
        if( ( fabs(bxx0 - bxx1) < 1.0 ) && ( fabs(byy0 - byy1) < 1.0 )){
            bxx0 = floor(bxx0);
            bxx1 = floor(bxx1);
            byy0 = floor(byy0);
            byy1 = floor(byy1);
            break;
        }

        /*
        1 rect does fit in one unique quarter of the temp bounding box. And we have found which.
        2 temp bounding box = bounding box of this quarter. 
        3 "Go in" this quarter (create if doesn't exist)
        */
        assert(i < 4);
        Quad *qq = q->children[i];
        if(qq == 0) {
            qq = new Quad;
            q->children[i] = qq;
        }
        q = qq;
    }
    q->data.push_back(shape);
}

Quad * Geom::QuadTree::search ( Geom::Rect const &  r  ) 

Definition at line 4 of file quadtree.cpp.

References Geom::max(), Geom::min(), and search().

                                    {
    return search(r[0].min(), r[1].min(),
                  r[0].max(), r[1].max());
}

Quad * Geom::QuadTree::search ( double  x0,
double  y0,
double  x1,
double  y1 
)

Definition at line 15 of file quadtree.cpp.

References bx1, by1, Geom::Quad::children, Barcode::Code39Ext::i, samplify::q, and root.

Referenced by search().

                                                                 {
    Quad *q = root;
        
    double bxx0 = bx1, bxx1 = bx1;
    double byy0 = by1, byy1 = by1;
    while(q) {
        double cx = (bxx0 + bxx1)/2;
        double cy = (byy0 + byy1)/2;
        unsigned i = 0;
        if(x0 >= cx) {
            i += 1;
            bxx0 = cx; // zoom in a quad
        } else if(x1 <= cx) {
            bxx1 = cx;
        } else
            break;
        if(y0 >= cy) {
            i += 2;
            byy0 = cy;
        } else if(y1 <= cy) {
            byy1 = cy;
        } else
            break;
        
        assert(i < 4);
        Quad *qq = q->children[i];
        if(qq == 0) break; // last non-null
        q = qq;
    }
    return q;
}


Member Data Documentation

Definition at line 74 of file quadtree.h.

Referenced by insert().

Definition at line 74 of file quadtree.h.

Referenced by insert(), and search().

Definition at line 75 of file quadtree.h.

Referenced by insert().

Definition at line 75 of file quadtree.h.

Referenced by insert(), and search().

Definition at line 72 of file quadtree.h.

Referenced by clean_root(), insert(), and search().

Definition at line 73 of file quadtree.h.


The documentation for this class was generated from the following files: