Skip to content

intersects_id can return duplicate hits when mesh.ray._scale is very small #2504

@hoyeong-heo

Description

@hoyeong-heo

Bug: Duplicate intersections due to too-small ray offset (self._scale)

I encountered an issue where trimesh.ray.intersects_id returns massive duplicate intersection points when multiple_hits=True.

Symptoms

  • A single ray produces a very large number of duplicate hit locations.
  • Increasing max_hits results in proportionally more identical points.
  • All returned intersection points are effectively the same (within floating-point precision).

Root cause (suspected)

The problem appears to be caused by an extremely small self._scale value, which makes the computed ray offset:

distance = np.clip(_ray_offset_factor * self._scale, _ray_offset_floor, np.inf)

too small relative to the scene scale.

As a result:

  • The updated ray origin (new_origins + ray_offsets) does not move far enough.
  • The next Embree query hits the same triangle again.
  • This repeats until max_hits is reached, producing duplicate intersections.

In short: ray origins are not effectively advancing between iterations.

Expected behavior

Ray origins should advance enough to avoid immediately re-hitting the same triangle (or duplicates should be filtered).

Environment

  • trimesh version: 4.11.2
  • Backend: Embree
  • OS: (fill in)

Reproduction

I will provide:

  • Minimal test script
import trimesh


mesh = trimesh.load_mesh("combined.stl")

x = -164.70464393956098
y = 275.5539451575655
z = 10000

origin = [[x, y, z]]
direction = [[0, 0, -1]]

# mesh.ray._scale_to_box = False

index_tri, index_ray, locations = mesh.ray.intersects_id(
    origin, direction, multiple_hits=True, return_locations=True
)

print(f"{index_tri=}")
print(f"{index_ray=}")
print(f"{locations=}")
  • combined.stl mesh that reproduces the issue

combined.zip

Notes

This seems related to scale-dependent robustness in the multi-hit ray loop.

Potential fixes might include:

  • A configurable option for minimum offset
  • Triangle ID re-hit prevention

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions