klampt.model.collide module

Functions and classes for managing collision tests between multiple objects.

This module defines the WorldCollider class, which makes it easy to ignore various collision pairs in a WorldModel.

For groups of objects, the self_collision_iter() and group_collision_iter() functions perform broad-phase collision detection to speed up collision testing.

The ray_cast() function is a convenient way to return the first point of intersection for a ray and a group of objects.

Classes:

WorldCollider(world[, ignore])

Used in planning routines to mask out objects in the world to check / ignore when doing collision detection.

Functions:

bb_contains(bb, x)

Returns true if x is inside the bounding box bb

bb_create(*ptlist)

Creates a bounding box from an optional set of points.

bb_empty(bb)

Returns True if the bounding box is empty

bb_intersect(a, b)

Returns true if the bounding boxes (a[0]->a[1]) and (b[0]->b[1]) intersect

bb_intersection(*bbs)

Returns the bounding box representing the intersection the given bboxes.

bb_union(*bbs)

Returns the smallest bounding box containing the given bboxes

group_collision_iter(geomlist1, geomlist2[, …])

Tests whether two sets of geometries collide.

group_subset_collision_iter(geomlist, alist, …)

Tests whether two subsets of geometries collide.

ray_cast(geomlist, s, d)

Finds the first collision among the geometries in geomlist with the ray at source s and direction d.

self_collision_iter(geomlist[, pairs])

Performs efficient self collision testing for a list of geometries.

class klampt.model.collide.WorldCollider(world, ignore=[])[source]

Bases: object

Used in planning routines to mask out objects in the world to check / ignore when doing collision detection.

You should not need to interact directly with this object’s attributes. Instead, use the methods provided.

geomList

a list of (object,geom) pairs for all non-empty objects in the world.

Type

list

mask

indicating which items are activated for collision detection. Basically, a sparse, symmetric boolean matrix over len(geomList)*len(geomList) possible collision pairs.

Type

list of sets of ints

terrains

contains the geomList indices of each terrain in the world.

Type

list of ints

rigidObjects

contains the geomList indices of each object in the world

Type

list of ints

robots

contains the geomList indices of each robot in the world.

Type

list of list of ints

Args: world (WorldModel): the world to use ignore (list, optional): a list of items to pass to ignoreCollision

Methods:

collisionTests([filter1, filter2, bb_reject])

Returns an iterator over potential colliding pairs, which should be tested for collisions.

collisions([filter1, filter2])

Returns an iterator over the colliding pairs of objects, optionally that satisfies the filter(s).

ignoreCollision(ign)

Permanently removes an object or a pair of objects from consideration.

isCollisionEnabled(obj_or_pair)

Returns true if the object or pair of objects are considered for collision.

objectObjectCollisions(object, object2)

Yields an iterator over object-terrain collision pairs.

objectTerrainCollisions(object[, terrain])

Yields an iterator over object-terrain collision pairs.

rayCast(s, d[, indices])

Finds the first collision between a ray and objects in the world.

rayCastRobot(robot, s, d)

Finds the first collision between a ray and a robot.

robotObjectCollisions(robot[, object])

Yields an iterator over robot-object collision pairs.

robotSelfCollisions([robot])

Yields an iterator over robot self collisions.

robotTerrainCollisions(robot[, terrain])

Yields an iterator over robot-terrain collision pairs.

collisionTests(filter1=None, filter2=None, bb_reject=True)[source]

Returns an iterator over potential colliding pairs, which should be tested for collisions.

Usage:

To test collisions, you call:

for i,j in worldCollider.collisionTests():
    if i[1].collides(j[1]):
        print("Object",i[0].getName(),"collides with",j[0].getName())

(Note that for this purpose is easier to just call collisions(); however you may want to use collisionTests to perform other queries like proximity detection.)

Parameters
  • filter1 (function, optional) – has form f(object) -> bool

  • filter2 (function, optional) – has form f(object) -> bool

  • bb_reject (bool, optional) – True if we should quick reject objects whose bounding boxes are not overlapping (broad phase collision detection). If false, all non-ignored collision pairs are returned.

See collisions() for an explanation of how filter1 and filter2 are interpreted

Returns

Iterates over ((object1,geom1),(object2,geom2)) pairs indicating which objects should be tested for collision. They have type:

  • object1, object2: a RobotModelLink, RigidObjectModel, or TerrainModel

  • geom1, geom2: Geometry3D corresponding to those objects.

Return type

iterator of tuple

collisions(filter1=None, filter2=None)[source]

Returns an iterator over the colliding pairs of objects, optionally that satisfies the filter(s).

Parameters
  • filter1 (function, optional) – has form f(object) -> bool

  • filter2 (function, optional) – has form f(object) -> bool

filter1 and filter2 are predicates to allow subsets of objects to collide. The argument can be a RobotModelLink, RigidObjectModel or TerrainModel.

If neither filter1 nor filter2 are provided, then all pairs are checked.

If filter1 is provided but filter2 is not, then objects in the set filter1 will be collided against each other.

If filter1 and filter2 are provided, then objects that satisfy filter1 will be collided against objects that satisfy filter2. (Note: in this case there is no checking of duplicates, i.e., the sets should be disjoint to avoid duplicating work).

ignoreCollision(ign)[source]

Permanently removes an object or a pair of objects from consideration.

Parameters

ign – either a single body (RobotModelLink, RigidObjectModel, TerrainModel) in the world, or a pair of bodies. In the former case all collisions with that body will be ignored.

isCollisionEnabled(obj_or_pair)[source]

Returns true if the object or pair of objects are considered for collision.

Parameters

obj_or_pair – either a single body (RobotModelLink, RigidObjectModel, TerrainModel) in the world, or a pair of bodies. In the former case, True is returned if the body is checked with anything.

objectObjectCollisions(object, object2)[source]

Yields an iterator over object-terrain collision pairs.

Parameters
  • object (RigidObjectModel or int) – the object to test

  • terrain (TerrainModel or int, optional) – the terrain to test, or None to all terrains.

Returns

Iterates over colliding (RigidObjectModel,TerrainModel) pairs.

Return type

iterator over tuple

objectTerrainCollisions(object, terrain=None)[source]

Yields an iterator over object-terrain collision pairs.

Parameters
  • object (RigidObjectModel or int) – the object to test

  • terrain (TerrainModel or int, optional) – the terrain to test, or None to all terrains.

Returns

Iterates over colliding (RigidObjectModel,TerrainModel) pairs.

Return type

iterator over tuple

rayCast(s, d, indices=None)[source]

Finds the first collision between a ray and objects in the world.

Parameters
  • s (list of 3 floats) – the ray source

  • d (list of 3 floats) – the ray direction

  • indices (list of ints, optional) – if given, the indices of geometries in geomList to test.

Returns

The (object,point) pair or None if no collision is found.

Return type

tuple

rayCastRobot(robot, s, d)[source]

Finds the first collision between a ray and a robot.

Parameters
  • robot (RobotModel or int) – the robot

  • s (list of 3 floats) – the ray source

  • d (list of 3 floats) – the ray direction

Returns

The (object,point) pair or None if no collision is found.

Return type

tuple

robotObjectCollisions(robot, object=None)[source]

Yields an iterator over robot-object collision pairs.

Parameters
  • robot (RobotModel or int) – the robot to test

  • object (RigidObjectModel or int, optional) – the object to test, or None to all objects.

Returns

Iterates over colliding (RobotModelLink,RigidObjectModel) pairs.

Return type

iterator over tuple

robotSelfCollisions(robot=None)[source]

Yields an iterator over robot self collisions.

Parameters
  • robot (RobotModel or int, optional) – If None (default), all

  • are tested. If an index or a RobotModel object only (robots) –

  • for that robot are tested (collisions) –

Returns

Iterates over colliding (RobotModelLink,RobotModelLink) pairs.

Return type

iterator over tuple

robotTerrainCollisions(robot, terrain=None)[source]

Yields an iterator over robot-terrain collision pairs.

Parameters
  • robot (RobotModel or int) – the robot to test

  • terrain (TerrainModel or int, optional) – the terrain to test, or None to all terrains.

Returns

Iterates over colliding (RobotModelLink,TerrainModel) pairs.

Return type

iterator over tuple

klampt.model.collide.bb_contains(bb, x)[source]

Returns true if x is inside the bounding box bb

klampt.model.collide.bb_create(*ptlist)[source]

Creates a bounding box from an optional set of points. If no points are provided, creates an empty bounding box.

klampt.model.collide.bb_empty(bb)[source]

Returns True if the bounding box is empty

klampt.model.collide.bb_intersect(a, b)[source]

Returns true if the bounding boxes (a[0]->a[1]) and (b[0]->b[1]) intersect

klampt.model.collide.bb_intersection(*bbs)[source]

Returns the bounding box representing the intersection the given bboxes. The result may be empty.

klampt.model.collide.bb_union(*bbs)[source]

Returns the smallest bounding box containing the given bboxes

klampt.model.collide.group_collision_iter(geomlist1, geomlist2, pairs='all')[source]

Tests whether two sets of geometries collide.

Parameters
  • geomlist1 (list of Geometry3D) – set 1

  • geomlist2 (list of Geometry3D) – set 2

  • pairs

    can be:

    • ’all’: all pairs are tested.

    • a function test(i,j) -> bool, taking geomlist1 index i and geomlist2 index j, and returning true if they should be tested

    • list of pairs (i,j) of collision indices.

Uses a quick bounding box reject test.

Returns

Iterator over colliding pairs (i,j) where i is an index into geomlist1 and j is an index into geomlist.

Return type

iterator over tuple

klampt.model.collide.group_subset_collision_iter(geomlist, alist, blist, pairs='all')[source]

Tests whether two subsets of geometries collide. Can be slightly faster than group_collision_iter if alist and blist overlap.

Parameters
  • geomlist (list of Geometry3D) – a list of all possible geometries

  • alist (list of int) – collision set 1, containing indices into geomlist

  • blist (list of int) – collision set 2, containing indices into geomlist

  • pairs

    can be:

    • ’all’: all pairs are tested.

    • a function test(i,j) -> bool, taking geomlist1 index i and geomlist2 index j, and returning true if they should be tested

    • list of pairs (i,j) of collision indices. In this case, alist and blist are ignored and can be set to None.

Uses a quick bounding box reject test.

Returns

Iterator over colliding pairs (i,j) where i is an index into alist and j is an index into glist.

Return type

iterator over tuple

klampt.model.collide.ray_cast(geomlist, s, d)[source]

Finds the first collision among the geometries in geomlist with the ray at source s and direction d.

Returns

A pair (index,point) if a collision is found, where:

  • index is the index of the geometry in geomlist

  • point is the collision point in world coordinates.

Returns None if no collision is found.

Return type

tuple

klampt.model.collide.self_collision_iter(geomlist, pairs='all')[source]

Performs efficient self collision testing for a list of geometries.

Parameters
  • geomlist (list of Geometry3D) – the list of geometries

  • pairs

    can be:

    • ’all’: all pairs are tested.

    • a function test(i,j) -> bool taking geometry indices and returning true if they should be tested

    • list of pairs (i,j) of collision indices.

Uses a quick bounding box reject test.

Returns

Iterator over colliding pairs (i,j) where i and j are indices into geomlist.

Return type

iterator over tuple