| """ |
| Runtime monkey-patch that adds `RigidEntity.detect_collision_parallel` and |
| `RigidLink.detect_collision` to upstream Genesis. |
| |
| The methods are pure-Python — they only read fields already exposed by the |
| upstream `RigidEntity` / `RigidLink` / collider state — so we can attach them |
| at runtime instead of forking Genesis. |
| |
| IMPORTANT: `apply()` must be called AFTER `gs.init()`. Importing |
| `genesis.engine.entities.rigid_entity.rigid_entity` before `gs.init()` raises |
| `Genesis hasn't been initialized.` from `genesis.utils.array_class`, so all |
| genesis-internal imports are deferred until inside `apply()`. |
| |
| Usage: |
| |
| import genesis as gs |
| import genesis_patch |
| gs.init(...) |
| genesis_patch.apply() |
| # now RigidEntity.detect_collision_parallel is available |
| """ |
|
|
| import numpy as np |
|
|
|
|
| _APPLIED = False |
|
|
|
|
| def apply(): |
| """Install detect_collision_parallel / detect_collision on Genesis classes. |
| |
| Idempotent. Must be called after `gs.init()`. |
| """ |
| global _APPLIED |
| if _APPLIED: |
| return |
|
|
| import genesis as gs |
| from genesis.engine.entities.rigid_entity.rigid_entity import RigidEntity |
| from genesis.engine.entities.rigid_entity.rigid_link import RigidLink |
|
|
| @gs.assert_built |
| def detect_collision_parallel( |
| self, |
| with_entity=None, |
| exclude_self_contact=False, |
| envs_idx=None, |
| *, |
| unsafe=False, |
| ): |
| """GPU-parallel collision query against the current contact buffers. |
| |
| Parameters |
| ---------- |
| with_entity : Entity, optional |
| If given, only count contacts between `self` and this entity. |
| exclude_self_contact : bool |
| Ignore self-self contacts (only when `with_entity is None`). |
| envs_idx : list[int], optional |
| Indices of environments to report. Defaults to all envs. |
| unsafe : bool |
| If False (default), re-runs collision detection. Set True when |
| buffers are already current. |
| |
| Returns |
| ------- |
| numpy.ndarray |
| bool array, one entry per requested env. |
| """ |
| if not unsafe: |
| self.solver.collider.clear() |
| self.solver.collider.detection() |
|
|
| n_collision = self.solver.collider._collider_state.n_contacts.to_numpy() |
| geom_A = self.solver.collider._collider_state.contact_data.geom_a.to_numpy() |
| geom_B = self.solver.collider._collider_state.contact_data.geom_b.to_numpy() |
|
|
| n_collision = np.asarray(n_collision).reshape(-1) |
| geom_A = np.asarray(geom_A) |
| geom_B = np.asarray(geom_B) |
| max_contacts, B = geom_A.shape |
|
|
| if envs_idx is None: |
| envs_idx = np.arange(B, dtype=int) |
| else: |
| envs_idx = np.asarray(envs_idx, dtype=int) |
|
|
| valid_rows = (np.arange(max_contacts)[:, None] < n_collision[None, :]) |
|
|
| selfA = (geom_A >= self.geom_start) & (geom_A < self.geom_end) |
| selfB = (geom_B >= self.geom_start) & (geom_B < self.geom_end) |
|
|
| if with_entity is not None: |
| otherA = (geom_A >= with_entity.geom_start) & (geom_A < with_entity.geom_end) |
| otherB = (geom_B >= with_entity.geom_start) & (geom_B < with_entity.geom_end) |
| hits = ((selfA & otherB) | (selfB & otherA)) & valid_rows |
| else: |
| hits = (selfA | selfB) & valid_rows |
| if exclude_self_contact: |
| hits &= ~(selfA & selfB) |
|
|
| collided = np.any(hits, axis=0) |
| return collided[envs_idx] |
|
|
| @gs.assert_built |
| def link_detect_collision(self, env_idx=0): |
| """Single-env collision query for one rigid link. Re-detects in real |
| time (does not rely on `scene.step()`).""" |
| all_collision_pairs = self._solver.detect_collision(env_idx) |
| collision_pairs = all_collision_pairs[ |
| np.logical_and( |
| all_collision_pairs >= self.geom_start, |
| all_collision_pairs < self.geom_end, |
| ).any(axis=1) |
| ] |
| return collision_pairs |
|
|
| if not hasattr(RigidEntity, "detect_collision_parallel"): |
| RigidEntity.detect_collision_parallel = detect_collision_parallel |
| if not hasattr(RigidLink, "detect_collision"): |
| RigidLink.detect_collision = link_detect_collision |
|
|
| _APPLIED = True |
|
|