Pair Discovery#

Pair discovery finds which objects of one target touch which objects of another, by their published world bounding boxes. Use it to learn which moving objects are near which obstacles, or which objects cross from one region into another, without querying every object.

Pair discovery provides two tools:

  • A pair snapshot answers one instant: every pair at the versions you name. It keeps nothing.

  • A pair reporter answers change. It keeps one accepted set of pairs, and each time you advance it to newer versions, it reports which pairs entered and which exited.

The following figure shows a reporter advancing while two robots move past a fixed scene:

The reporter compares its baseline with the new endpoints only. A contact that starts and ends between two advances is never reported.

Snapshot and Reporter Example#

The following program puts a shelf and a cone in one population and a robot in another. It takes one snapshot and then advances one reporter twice:

import warp as wp

from ovspatialquery import (
    ExplicitUpdateKind,
    GeometryType,
    Library,
    PairReporterConfig,
)

wp.config.log_level = wp.LOG_WARNING  # skip Warp's start-up banner
CPU = "cpu"  # this program keeps its input arrays on the CPU
IDENTITY = [0.0, 0, 0, 1]


def advance(reporter):
    with reporter.advance()[0] as operation:  # both endpoints at their head
        operation.wait()
        result = operation.fetch_pair_reporter().to_numpy()
    print(
        f"advance ({result.first_source_ordinal}, {result.second_source_ordinal}):",
        f"entered {result.entered_pairs.tolist()}",
        f"exited {result.exited_pairs.tolist()}",
        f"active {result.accepted_active_pair_count}",
    )


with Library.open() as library, library.instance() as instance:
    adapter = instance.explicit()
    adapter.register_boxes(
        definition_values=wp.array([1], dtype=wp.uint64, device=CPU),
        half_extents=wp.full((1, 3), 0.5, dtype=wp.float64, device=CPU),
    ).finish()

    def publish(population, ordinal, ids, positions):
        population.enqueue_set(
            ordinal,
            object_values=wp.array(ids, dtype=wp.uint64, device=CPU),
            geometry_types=wp.full(
                len(ids), int(GeometryType.BOX), dtype=wp.int32, device=CPU
            ),
            geometry_values=wp.ones(len(ids), dtype=wp.uint64, device=CPU),
            translations=wp.array(positions, dtype=wp.float64, device=CPU),
            rotations_xyzw=wp.array([IDENTITY] * len(ids), dtype=wp.float32, device=CPU),
        ).finish()

    # Two populations on their own clocks:
    # a shelf (1) and a cone (2), and one robot (7).
    scene = adapter.create_population()
    publish(scene, 20, [1, 2], [[0, 0, 0], [5, 0, 0]])
    robots = adapter.create_population()
    publish(robots, 100, [7], [[0.8, 0, 0]])

    # A snapshot answers one instant and keeps nothing.
    snapshot = instance.query_pairs(robots.target, scene.target, capacity=8)
    ordinals = (snapshot.first_source_ordinal, snapshot.second_source_ordinal)
    print("snapshot", list(snapshot.rows), "at", ordinals)

    # A reporter keeps one baseline and reports what changed at each advance.
    reporter = instance.create_pair_reporter(
        PairReporterConfig(
            first=robots.target.reference,
            second=scene.target.reference,
            active_pair_capacity=8,
            delta_pair_capacity=8,
        )
    )
    advance(reporter)

    # The robot moves next to the cone: publish only its pose as robots ordinal 104.
    robots.enqueue_update(
        100,
        104,
        kind=ExplicitUpdateKind.POSES,
        object_values=wp.array([7], dtype=wp.uint64, device=CPU),
        translations=wp.array([[4.2, 0, 0]], dtype=wp.float64, device=CPU),
        rotations_xyzw=wp.array([IDENTITY], dtype=wp.float32, device=CPU),
    ).finish()
    advance(reporter)

The snapshot finds the robot at the shelf. The reporter reports that pair entering, and then, after a pose update, the cone entering and the shelf exiting. The program prints this output:

Output#
snapshot [(7, 1)] at (100, 20)
advance (100, 20): entered [[7, 1]] exited [] active 1
advance (104, 20): entered [[7, 2]] exited [[7, 1]] active 1

Pair Rules#

Pair snapshots and pair reporters follow these rules:

  • Bounding boxes only. Two objects form a pair when their published world AABBs overlap, grown by contact_tolerance. Pair discovery makes no claim about exact shapes. If you need one, run an exact query on the pair.

  • Two endpoints, two clocks. Each side is a population or view with its own ordinal. Ordinals of different populations have no relation to each other. Two endpoints in the same population must select the same ordinal, or the call returns ORDINAL_MISMATCH.

  • Roles. Rows are (first, second) object IDs. Two different targets keep their roles. When both sides are the same target, ordinal, and bits, each unordered pair appears once with the smaller ID first, and an object never pairs with itself.

  • Forward only. An advance must move at least one endpoint forward and neither backward. An older ordinal returns BACKWARD_ORDINAL, and no progress returns STALE_ORDINAL. Either way, the reporter keeps its baseline. The first advance reports every current pair as entered. To start over, destroy the reporter and create a new one.

  • Capacity. A reporter has a capacity for active pairs and for each delta. An advance that exceeds either reports ACTIVE_CAPACITY or DELTA_CAPACITY with empty deltas and keeps the old baseline. Grow the capacity with ovspatialquery_grow_pair_reporter_capacity() and advance again.

Note

Pair discovery lives in its own header, ovspatialquery_pairs.h. Unlike the core header, it does not promise backward compatibility between releases.