The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Algorithm::QuadTree - A QuadTree Algorithm class in pure Perl.

SYNOPSIS

    use Algorithm::QuadTree;

    # create a quadtree object
    my $qt = Algorithm::QuadTree->new(-xmin  => 0,
                                      -xmax  => 1000,
                                      -ymin  => 0,
                                      -ymax  => 1000,
                                      -depth => 6);

    # add objects randomly
    my $x = my $tag = 1;
    while ($x < 1000) {
      my $y = 1;
      while ($y < 1000) {
        $qt->add($tag++, $x, $y, $x, $y);

        $y += int rand 200;
      }
      $x += int rand 100;
    }

    # find the objects enclosed in a given region
    my $r_list = $qt->getEnclosedObjects(400, 300,
                                         689, 799);

DESCRIPTION

Algorithm::QuadTree implements a quadtree algorithm (QTA) in pure Perl. Essentially, a QTA is used to access a particular area of a map very quickly. This is especially useful in finding objects enclosed in a given region, or in detecting intersection among objects. In fact, I wrote this module to rapidly search through objects in a Tk::Canvas widget, but have since used it in other non-Tk programs successfully. It is a classic memory/speed trade-off.

Lots of information about QTAs can be found on the web. But, very briefly, a quadtree is a hierarchical data model that recursively decomposes a map into smaller regions. Each node in the tree has 4 children nodes, each of which represents one quarter of the area that the parent represents. So, the root node represents the complete map. This map is then split into 4 equal quarters, each of which is represented by one child node. Each of these children is now treated as a parent, and its area is recursively split up into 4 equal areas, and so on up to a desired depth.

Here is a somewhat crude diagram:

                   ------------------------------
                  |AAA|AAB|       |              |
                  |___AA__|  AB   |              |
                  |AAC|AAD|       |              |
                  |___|___A_______|      B       |
                  |       |       |              |
                  |       |       |              |
                  |   AC  |   AD  |              |
                  |       |       |              |
                   -------------ROOT-------------
                  |               |              |
                  |               |              |
                  |               |              |
                  |      C        |      D       |
                  |               |              |
                  |               |              |
                  |               |              |
                   ------------------------------

Which corresponds to the following quadtree:

                        __ROOT_
                       /  / \  \
                      /  /   \  \
                _____A_  B   C   D
               /  / \  \
              /  /   \  \
        _____AA  AB  AC  AD
       /  / \  \
      /  /   \  \
    AAA AAB AAC AAD

In the above diagrams I show only the nodes through the first branch of each level. The same structure exists under each node. This quadtree has a depth of 4.

Each object in the map is assigned to the nodes that it intersects. For example, if we have an object that overlaps regions AAA and AAC, it will be assigned to the nodes ROOT, A, AA, AAA and AAC. Now, suppose we want to find all the objects that intersect a given area. Instead of checking all objects, we check to see which children of the ROOT node intersect the area. For each of those nodes, we recursively check their children nodes, and so on until we reach the leaves of the tree. Finally, we find all the objects that are assigned to those leaf nodes and check them for overlap with the initial area.

CLASS METHODS

The following methods are public:

Algorithm::QuadTree->new(options)

This is the constructor. It expects the following options (all mandatory) and returns an Algorithm::QuadTree object:

-xmin

This is the X-coordinate of the bottom left corner of the area associated with the quadtree.

-ymin

This is the Y-coordinate of the bottom left corner of the area associated with the quadtree.

-xmax

This is the X-coordinate of the top right corner of the area associated with the quadtree.

-ymax

This is the Y-coordinate of the top right corner of the area associated with the quadtree.

-depth

The depth of the quadtree.

$qt->add(object, x0, y0, x1, y1)

This method is used to add objects in shape of rectangles to the tree. It has to be called for every object in the map so that it can properly assigned to the correct tree nodes. The first argument is an object reference or an unique ID for the object. The remaining 4 arguments define the outline of the object (edge coordinates: left, bottom, right, top). This method will traverse the tree and add the object to the nodes that it overlaps with.

NOTE: The method does NOT check if the object references or IDs passed are unique or not. It is up to you to make sure they are.

$qt->add(object, x, y, radius)

Same as above, but for circular objects. x and y are coordinates of object's center.

NOTE: this method called with three coordinates treats the object as a circle, and with four coordinates as a rectangle. You don't have to worry about potential empty / undef values in your coordinates, as long as the number of arguments is right. It will never treat ->add('obj', 1, 2, 3, undef) as a call to the circular version, and instead produce warnings about undef being treated as a number, hinting you about the problem.

$qt->getEnclosedObjects(x0, y0, x1, y1)

This method returns an array reference of all the objects that are assigned to the nodes that overlap the given rectangular area. The objects will be returned in the exact form they were passed to ->add.

$qt->getEnclosedObjects(x, y, radius)

Same as above, but for circular areas. x and y are coordinates of area's center.

NOTE: this method called with three coordinates treats the object as a circle, and with four coordinates as a rectangle. You don't have to worry about potential empty / undef values in your coordinates, as long as the number of arguments is right. It will never treat ->getEnclosedObjects(1, 2, 3, undef) as a call to the circular version, and instead produce warnings about undef being treated as a number, hinting you about the problem.

$qt->delete(object)

This method deletes the object from the tree.

$qt->clear()

This method deletes all the objects from the tree. It allows to reuse the (expensive to compute) tree structure whenever the tree needs to be repopulated.

$qt->setWindow(x0, y0, scale)

This method is useful when you zoom your display to a certain segment of the map. It sets the window to the given region such that any calls to add or getEnclosedObjects will have its coordinates properly adjusted before running. The first two coordinates specify the lower left coordinates of the new window. The third coordinate specifies the new zoom scale.

NOTE: You are free, of course, to make the coordinate transformation yourself.

$qt->resetWindow()

This method resets the window region to the full map.

Backends

By default, the module uses Algorithm::QuadTree::PP, which is the pure perl backend.

If you install Algorithm::QuadTree::XS, the module will load and use that instead. This behavior can be controlled by setting ALGORITHM_QUADTREE_BACKEND environmental variable to the package name, which should be used as the backend.

Note: the environmental variable must be present before loading Algorithm::QuadTree for the first time

        # will load pure perl backend regardless of XS availability
        BEGIN { $ENV{ALGORITHM_QUADTREE_BACKEND} = 'Algorithm::QuadTree::PP' };

        use Algorithm::QuadTree;

AUTHORS

Ala Qumsieh aqumsieh@cpan.org

Currently maintained by Bartosz Jarzyna bbrtj.pro@gmail.com

COPYRIGHTS

This module is distributed under the same terms as Perl itself.