Guangdong Juyao Intelligent Technology Co., Ltd. · Software & Algorithm Engineering
23°07′N 113°15′E · GUANGZHOU · PURE STATIC MONOGRAPH
Back to Open Source

boostorg/geometry · 1.88.0 · BSL-1.0 · C++17

How does Boost.Geometry find paths in a selection box?

Use an R-tree to filter bounding boxes, then test which polylines actually intersect the selection.

Example result Boost 1.88.0 returns two candidates. The horizontal line hits, while the diagonal intersects through its bounding box alone.

Which paths actually touch the selection box?

Suppose you drag a box around the upper left of an engraving design. Checking every segment on every drag update gets more expensive as the drawing grows. Boost.Geometry is Boost's C++ geometry library. In 1.88.0, its R-tree can filter candidates by axis-aligned bounding box, leaving a smaller set for polyline intersection tests. This example implements crossing selection.

Boost 1.88.0 release · Boost.Geometry source at the pinned tag · Boost Software License 1.0

Filter boxes, then test the paths

The selection box, four paths and their bounding boxes
A bounding-box query narrows four paths to two candidates. Testing the paths themselves leaves only the horizontal line.

The demo puts each path's bounding box and ID into the R-tree. A node's box encloses all its children. During a query, the spatial_query visitor uses a bounds_tag check at internal nodes, skipping any subtree whose box is disjoint from the selection rectangle. At leaves it applies a value_tag check to each entry. Since our entries carry bounding boxes, that pass reports box intersections. The final call to bg::intersects(paths[id], marquee) tests the actual polyline. See both checks in the R-tree visitor

The selection rectangle is [0,2] × [8,10]. The bounding box of the diagonal from (1,1) to (9,9) overlaps the rectangle. The diagonal itself misses because y is at most 2 wherever x is at most 2. The horizontal path at y = 9 crosses the rectangle. The two distant paths are excluded by their boxes. The demo uses a small quadratic<2> node capacity to exercise node splits in this tiny dataset.

If many long diagonals have overlapping boxes, the candidate count can approach the full path count. An editor also needs to update the index when objects move, map screen coordinates to drawing coordinates, and decide how stroke width, curves, and boundary contact affect selection. This example tests zero-width polylines in the drawing plane.

Build and run locally

Use a fresh directory on macOS or Linux with curl, tar, and a C++17 compiler. This demo uses the template headers from the complete Boost 1.88.0 source archive. It was run on Apple Silicon macOS.

Download the C++17 example · Build commands in plain text

View the example source
#include <algorithm>
#include <iostream>
#include <iterator>
#include <string_view>
#include <utility>
#include <vector>

#include <boost/geometry.hpp>
#include <boost/geometry/index/rtree.hpp>
#include <boost/version.hpp>

namespace bg = boost::geometry;
namespace bgi = boost::geometry::index;

using Point = bg::model::d2::point_xy<double>;
using Path = bg::model::linestring<Point>;
using Box = bg::model::box<Point>;
using Entry = std::pair<Box, std::size_t>;

int main() {
    static_assert(BOOST_VERSION == 108800, "This demo pins Boost 1.88.0");
    const std::vector<Path> paths{
        {{1, 1}, {9, 9}},
        {{0, 9}, {9, 9}},
        {{12, 12}, {16, 12}},
        {{18, 14}, {21, 15}},
    };
    const std::vector<std::string_view> labels{
        "diagonal", "horizontal", "far-a", "far-b",
    };

    // A small node capacity makes the tree split even for this tiny fixture.
    bgi::rtree<Entry, bgi::quadratic<2>> index;
    for (std::size_t id = 0; id < paths.size(); ++id) {
        index.insert({bg::return_envelope<Box>(paths[id]), id});
    }

    const Box marquee{{0, 8}, {2, 10}};
    std::vector<Entry> candidates;
    index.query(bgi::intersects(marquee), std::back_inserter(candidates));
    std::sort(candidates.begin(), candidates.end(),
              [](const Entry& a, const Entry& b) { return a.second < b.second; });

    std::cout << "Boost " << BOOST_VERSION / 100000 << '.'
              << BOOST_VERSION / 100 % 1000 << '.' << BOOST_VERSION % 100 << '\n';
    for (const auto& entry : candidates) {
        const auto id = entry.second;
        std::cout << labels[id] << ": "
                  << (bg::intersects(paths[id], marquee) ? "hit" : "box only")
                  << '\n';
    }
}
mkdir boost-geometry-study && cd boost-geometry-study
curl -fsSL https://archives.boost.io/release/1.88.0/source/boost_1_88_0.tar.bz2 -o boost.tar.bz2
tar -xjf boost.tar.bz2
curl -fsSLO https://www.juyaoai.com/examples/boost-geometry-path-selection/demo.cpp
c++ -std=c++17 -I boost_1_88_0 demo.cpp -o boost-geometry-demo
./boost-geometry-demo

Run output

Boost 1.88.0
diagonal: box only
horizontal: hit

box only means the bounding box intersects and the path itself misses. Change the rectangle to [0,2] × [0,2] and the diagonal should become the only hit. Both cases make small regression fixtures for an editor's crossing-selection logic.