Skip to content

Intersections¤

compas_model.geometry.intersections ¤

Functions:¤

intersection_ray_triangle ¤

intersection_ray_triangle(line: Line, triangle: list[Point]) -> Point | None

Compute the intersection between a ray and a triangle.

Parameters:

  • line (Line) –

    The ray.

  • triangle (list[Point]) –

    The triangle as a list of three points.

Returns:

  • Point or None –

    The intersection point if one exists.

Notes

The function is an implementation of the Möller-Trumbore intersection algorithm 1.


  1. https://en.wikipedia.org/wiki/M%C3%B6ller%E2%80%93Trumbore_intersection_algorithm ↩

intersections_line_aabb ¤

intersections_line_aabb(line: Line, box: Box) -> tuple[int, list[Point]]

Find the intersections between a line and an axis aligned box.

Parameters:

  • line (Line) –

    The line.

  • box (Box) –

    An axis aligned box.

Returns:

  • tuple[bool, list[Point]] –

    The number of intersections, and a list of intersection points. If the number of intersections is 0 (zero), the list is empty. If the number of intersections is 1 (one), the list contains one point. If the number of intersections is 2 (two), the list contains two distinct points.

intersections_line_box ¤

intersections_line_box(line: Line, box: Box) -> tuple[int, list[Point]]

Find the intersections between a line and a box.

Parameters:

  • line (Line) –

    The line.

  • box (Box) –

    An oriented box.

Returns:

  • tuple[bool, list[Point]] –

    The number of intersections, and a list of intersection points. If the number of intersections is 0 (zero), the list is empty. If the number of intersections is 1 (one), the list contains one point. If the number of intersections is 2 (two), the list contains two distinct points.

intersections_ray_aabb ¤

intersections_ray_aabb(ray: Line, box: Box) -> tuple[int, list[Point]]

Find the intersections between a ray and an axis aligned box.

Parameters:

  • ray (Line) –

    The ray.

  • box (Box) –

    An axis aligned box.

Returns:

  • tuple[bool, list[Point]] –

    The number of intersections, and a list of intersection points. If the number of intersections is 0 (zero), the list is empty. If the number of intersections is 1 (one), the list contains one point. If the number of intersections is 2 (two), the list contains two distinct points.

intersections_ray_box ¤

intersections_ray_box(ray: Line, box: Box) -> tuple[int, list[Point]]

Find the intersections between a ray and a box.

Parameters:

  • ray (Line) –

    The line.

  • box (Box) –

    An oriented box.

Returns:

  • tuple[bool, list[Point]] –

    The number of intersections, and a list of intersection points. If the number of intersections is 0 (zero), the list is empty. If the number of intersections is 1 (one), the list contains one point. If the number of intersections is 2 (two), the list contains two distinct points.

is_intersection_aabb_aabb ¤

is_intersection_aabb_aabb(
    a: Box, b: Box, tolerance: float = 0.0, include_touching: bool = True
) -> bool

Determine whether two axis-aligned boxes intersect.

Parameters:

  • a (Box) –

    The first axis-aligned box.

  • b (Box) –

    The second axis-aligned box.

  • tolerance (float, default: 0.0 ) –

    The maximum separation at which the boxes are still considered intersecting.

  • include_touching (bool, default: True ) –

    If True, boxes that only touch are considered intersecting.

Returns:

  • bool –

    True if the boxes intersect, and False otherwise.

Raises:

Notes

If touching is excluded, the boxes must overlap by more than tolerance along every coordinate axis.

Examples:

>>> from compas.geometry import Box
>>> from compas.geometry import Translation
>>> from compas_model.geometry import is_intersection_aabb_aabb
>>> a = Box(1, 1, 1)
>>> b = Box(1, 1, 1)
>>> b.transform(Translation.from_vector([1, 0, 0]))
>>> is_intersection_aabb_aabb(a, b)
True
>>> is_intersection_aabb_aabb(a, b, include_touching=False)
False

is_intersection_box_box ¤

is_intersection_box_box(a: Box, b: Box) -> bool

Determine whether a box intersects another box.

Parameters:

  • a (Box) –

    The first box.

  • b (Box) –

    The second box.

Returns:

  • bool –

    True if the boxes intersect. False otherwise.

Notes

The algorithm uses the method of separating axes, which states, for two convex objects: If there exists a line for which the intervals of projection of the two objects onto that line do not intersect, then the objects do not intersect.

The underlying theorem is described here 1.

For two oriented (bounding) boxes, this can be formulated in the form of 15 axis checks, based on the coordinate frames of the boxes, and the box coordinate extents.


  1. https://en.wikipedia.org/wiki/Hyperplane_separation_theorem ↩

Examples:

>>> from compas.geometry import Box, Frame
>>> from compas_model.geometry import is_intersection_box_box
>>> A = Box(2, 2, 2)
>>> B = Box(1, 1, 1, frame=Frame(point=[1, 1, 1], xaxis=[1, 1, 0], yaxis=[-1, 1, 0]))
>>> is_intersection_box_box(A, B)
True

is_intersection_line_aabb ¤

is_intersection_line_aabb(line: Line, box: Box) -> bool

Determine whether a line intersects with an aligned box.

Parameters:

  • line (Line) –

    The line.

  • box (Box) –

    The test box.

Returns:

  • bool –

    True if the line intersects the box.

is_intersection_line_box ¤

is_intersection_line_box(line: Line, box: Box) -> bool

Determine whether a line intersects an oriented box.

Parameters:

  • line (Line) –

    The line.

  • box (Box) –

    The box.

Returns:

  • bool –

    True if the line intersects the box. False otherwise.

is_intersection_ray_aabb ¤

is_intersection_ray_aabb(ray: Line, box: Box) -> bool

Determine whether a ray intersects an axis aligned box.

Parameters:

  • ray (Line) –

    The ray.

  • box (Box) –

    The box.

Returns:

  • bool –

    True if the ray intersects the box. False otherwise.

is_intersection_ray_box ¤

is_intersection_ray_box(ray: Line, box: Box) -> bool

Determine whether a ray intersects a box.

Parameters:

  • ray (Line) –

    The ray.

  • box (Box) –

    The box.

Returns:

  • bool –

    True if the ray intersects the box. False otherwise.

is_intersection_segment_aabb ¤

is_intersection_segment_aabb(segment: Line, box: Box) -> bool

Determine whether a segment intersects an axis aligned box.

Parameters:

  • segment (Line) –

    The segment.

  • box (Box) –

    The box.

Returns:

  • bool –

    True if the segmnet intersects the box. False otherwise.

Warnings

The name of this function can be misleading, since it returns True not only when the segment intersects the box boundary, but also when the segment is contained inside the box.

This makes sense if you think of the box as a "solid", but is less intuitive when you think of it as a "shell".

See Also

Examples:

Note that Line can be used as an infinite line, a ray, and as a segment between the two points at t=0 and t=1.

>>> from compas.geometry import Line
>>> from compas.geometry import Box
>>> from compas_model.geometry import is_intersection_segment_aabb

Create a box centered at the origin.

>>> box = Box(1, 1, 1)

A segment crossing the box boundary intersects the box.

>>> line = Line([0, 0, 0], [1, 0, 0])
>>> is_intersection_segment_aabb(line, box)
True

A segment contained inside the box interiro intersects the box.

>>> line = Line([0, 0, 0], [0.1, 0, 0])
>>> is_intersection_segment_aabb(line, box)
True

A segment outside the box but with one point on the box boundary intersects the box.

>>> line = Line([0.5, 0, 0], [1.5, 0, 0])
>>> is_intersection_segment_aabb(line, box)
True

A segment outside the box doesn't intersect the box.

>>> line = Line([1.0, 0, 0], [2.0, 0, 0])
>>> is_intersection_segment_aabb(line, box)
False

is_intersection_segment_box ¤

is_intersection_segment_box(segment: Line, box: Box) -> bool

Determine whether a segment intersects a box.

Parameters:

  • segment (Line) –

    The segment.

  • box (Box) –

    The box.

Returns:

  • bool –

    True if the segment intersects the box. False otherwise.

is_intersection_sphere_aabb ¤

is_intersection_sphere_aabb(sphere: Sphere, box: Box) -> bool

Determine whether a sphere intersects an axis-aligned box.

Parameters:

  • sphere (Sphere) –

    The sphere.

  • box (Box) –

    The box.

Returns:

  • bool –

    True if the sphere intersects the aabb. False otherwise.

is_intersection_sphere_box ¤

is_intersection_sphere_box(sphere: Sphere, box: Box) -> bool

Determine whether a sphere intersects an oriented box.

Parameters:

  • sphere (Sphere) –

    The sphere.

  • box (Box) –

    The box.

Returns:

  • bool –

    True if the sphere intersects the box. False otherwise.

is_line_contained_locally ¤

is_line_contained_locally(
    point: Point | Vector, direction: Vector, dx: float, dy: float, dz: float
) -> bool

Determine whether the direction vector from a specific point is contained within the specified coordinate extents.

Parameters:

  • point (Point | Vector) –

    The base point of the direction.

  • direction (Vector) –

    The direction vector.

  • dx (float) –

    Extent of X coordinate.

  • dy (float) –

    Extent of Y coordinate.

  • dz (float) –

    Extent of Z coordinate.

Returns:

  • bool –

    True if the direction is contained. False otherwise.

is_ray_contained_locally ¤

is_ray_contained_locally(
    point: Point | Vector, direction: Vector, extents: list[float]
) -> bool

Determine whether a ray is contained within the given extents along local axes.

Parameters:

  • point (Point | Vector) –

    The base point of the ray.

  • direction (Vector) –

    The direction of the ray.

  • extents (list[float]) –

    The coordinate extents along local axes.

Returns:

  • bool –

    True if the ray is contained. False otherwise.

is_segment_contained_locally ¤

is_segment_contained_locally(
    point: Point | Vector,
    direction: Vector,
    box_extents: list[float],
    segment_extent: float,
) -> bool

Determine whether a segment is contained within the given extents along local axes.

Parameters:

  • point (Point | Vector) –

    The midpoint of the segment.

  • direction (Vector) –

    The direction of the segment.

  • box_extents (list[float]) –

    The coordinate extents of the box along local axes.

  • segment_extent (float) –

    Coordinate extent of the segment along its direction, wrt its midpoint.

Returns:

  • bool –

    True if the segment is contained. False otherwise.