-
Notifications
You must be signed in to change notification settings - Fork 81
Add ball-table resolver strategies #297
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| from typing import cast | ||
|
|
||
| import attrs | ||
|
|
||
| from pooltool.physics.resolve.ball_table.core import BallTableCollisionStrategy | ||
| from pooltool.physics.resolve.ball_table.frictional_inelastic import ( | ||
| FrictionalInelasticTable, | ||
| ) | ||
| from pooltool.physics.resolve.ball_table.frictionless_inelastic import ( | ||
| FrictionlessInelasticTable, | ||
| ) | ||
| from pooltool.physics.resolve.models import BallTableModel | ||
|
|
||
| _ball_table_model_registry: tuple[type[BallTableCollisionStrategy], ...] = ( | ||
| FrictionlessInelasticTable, | ||
| FrictionalInelasticTable, | ||
| ) | ||
|
|
||
| ball_table_models: dict[BallTableModel, type[BallTableCollisionStrategy]] = { | ||
| cast(BallTableModel, attrs.fields_dict(cls)["model"].default): cls | ||
| for cls in _ball_table_model_registry | ||
| } | ||
|
|
||
| __all__ = [ | ||
| "BallTableCollisionStrategy", | ||
| "BallTableModel", | ||
| "FrictionalInelasticTable", | ||
| "FrictionlessInelasticTable", | ||
| "ball_table_models", | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| from abc import ABC, abstractmethod | ||
| from typing import Protocol | ||
|
|
||
| import numpy as np | ||
| from numpy.typing import NDArray | ||
|
|
||
| import pooltool.constants as const | ||
| from pooltool.objects.ball.datatypes import Ball | ||
| from pooltool.physics.dimensionality import Dim | ||
| from pooltool.physics.utils import on_table, rel_velocity | ||
| from pooltool.ptmath.utils import norm2d, norm3d | ||
|
|
||
|
|
||
| def bounce_height(vz: float, g: float) -> float: | ||
| """Return how high a ball with outgoing positive z-velocity will bounce. | ||
|
|
||
| Measured as distance from table to bottom of ball. | ||
| """ | ||
| return 0.5 * vz**2 / g | ||
|
|
||
|
|
||
| def final_ball_motion_state(rvw: NDArray[np.float64], R: float) -> int: | ||
| """Return the final (post-collision) motion state label for a ball.""" | ||
| if rvw[0, 2] < 0: | ||
| return const.pocketed | ||
|
|
||
| if rvw[1, 2] != 0.0 or not on_table(rvw, R): | ||
| return const.airborne | ||
|
|
||
| if norm3d(rel_velocity(rvw, R)) > const.EPS: | ||
| return const.sliding | ||
|
|
||
| if norm2d(rvw[1]) > const.EPS: | ||
| return const.rolling | ||
|
|
||
| if rvw[2, 2] != 0.0: | ||
| return const.spinning | ||
|
|
||
| return const.stationary | ||
|
|
||
|
|
||
| class _BaseStrategy(Protocol): | ||
| dim: Dim | ||
|
|
||
| def resolve(self, ball: Ball, inplace: bool = False) -> Ball: ... | ||
|
|
||
| def make_kiss(self, ball: Ball) -> Ball: ... | ||
|
|
||
|
|
||
| class BallTableCollisionStrategy(_BaseStrategy, Protocol): | ||
| """Ball-table collision models must satisfy this protocol""" | ||
|
|
||
| def solve(self, ball: Ball) -> Ball: | ||
| """Resolves a ball-table collision""" | ||
| ... | ||
|
|
||
|
|
||
| class CoreBallTableCollision(ABC): | ||
| """Operations used by every ball-table collision resolver""" | ||
|
|
||
| def make_kiss(self, ball: Ball) -> Ball: | ||
| """Translate the ball so its height is exactly its radius. | ||
|
|
||
| If the ball is not at a height R, it is moved vertically such that it is. | ||
| """ | ||
| ball.state.rvw[0, 2] = ball.params.R | ||
| return ball | ||
|
|
||
| def resolve(self, ball: Ball, inplace: bool = False) -> Ball: | ||
| if not inplace: | ||
| ball = ball.copy() | ||
|
|
||
| ball = self.make_kiss(ball) | ||
| return self.solve(ball) | ||
|
|
||
| @abstractmethod | ||
| def solve(self, ball: Ball) -> Ball: | ||
| pass | ||
92 changes: 92 additions & 0 deletions
92
pooltool/physics/resolve/ball_table/frictional_inelastic/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| import attrs | ||
| import numpy as np | ||
| from numba import jit | ||
| from numpy.typing import NDArray | ||
|
|
||
| import pooltool.constants as const | ||
| import pooltool.physics as physics | ||
| import pooltool.ptmath as ptmath | ||
| from pooltool.objects.ball.datatypes import Ball, BallState | ||
| from pooltool.physics.dimensionality import Dim | ||
| from pooltool.physics.resolve.ball_table.core import ( | ||
| CoreBallTableCollision, | ||
| bounce_height, | ||
| final_ball_motion_state, | ||
| ) | ||
| from pooltool.physics.resolve.models import BallTableModel | ||
|
|
||
|
|
||
| @jit(nopython=True, cache=const.use_numba_cache) | ||
| def _resolve_ball_table( | ||
| rvw: NDArray[np.float64], R: float, u: float, e: float | ||
| ) -> NDArray[np.float64]: | ||
| rvw_i = rvw.copy() | ||
| v_i = rvw_i[1] | ||
| w_i = rvw_i[2] | ||
| if v_i[2] >= 0: | ||
| raise ValueError( | ||
| "Ball with non-negative z-velocity can't collide with table surface." | ||
| ) | ||
|
|
||
| unit_z = np.array([0.0, 0.0, 1.0]) | ||
|
|
||
| D_v_perpendicular_magnitude = (1 + e) * -v_i[2] | ||
| D_v_perpendicular = D_v_perpendicular_magnitude * unit_z | ||
|
|
||
| v_i[2] = 0 | ||
|
|
||
| v_c_i = physics.surface_velocity(rvw_i, -unit_z, R) | ||
| has_relative_velocity = ptmath.squared_norm3d(v_c_i) > const.EPS**2 | ||
|
|
||
| if has_relative_velocity: | ||
| v_hat_c_i = ptmath.unit_vector(v_c_i) | ||
| D_v_parallel_slip = u * D_v_perpendicular_magnitude * -v_hat_c_i | ||
| else: | ||
| v_hat_c_i = np.zeros(3) | ||
| D_v_parallel_slip = np.zeros(3) | ||
|
|
||
| D_v_parallel_no_slip = (2.0 / 7.0) * (R * ptmath.cross(w_i, unit_z) - v_i) | ||
|
|
||
| if not has_relative_velocity or ptmath.squared_norm3d( | ||
| D_v_parallel_no_slip | ||
| ) <= ptmath.squared_norm3d(D_v_parallel_slip): | ||
| rvw[1] = rvw[1] + D_v_perpendicular + D_v_parallel_no_slip | ||
| rvw[2] = rvw[2] + (5.0 / 7.0) * (-w_i + ptmath.cross(unit_z, v_i) / R) | ||
| else: | ||
| rvw[1] = rvw[1] + D_v_perpendicular + D_v_parallel_slip | ||
| rvw[2] = rvw[2] + (2.5 / R) * ptmath.norm3d(D_v_parallel_slip) * ptmath.cross( | ||
| unit_z, v_hat_c_i | ||
| ) | ||
|
|
||
| return rvw | ||
|
|
||
|
|
||
| @attrs.define | ||
| class FrictionalInelasticTable(CoreBallTableCollision): | ||
| """A frictional, inelastic ball-table collision. | ||
|
|
||
| Reference: | ||
| https://billiards.colostate.edu/technical_proofs/new/TP_A-14.pdf | ||
| """ | ||
|
|
||
| min_bounce_height: float = 0.005 | ||
|
|
||
| model: BallTableModel = attrs.field( | ||
| default=BallTableModel.FRICTIONAL_INELASTIC, init=False, repr=False | ||
| ) | ||
| dim: Dim = attrs.field(default=Dim.THREE, init=False, repr=False) | ||
|
|
||
| def solve(self, ball: Ball) -> Ball: | ||
| """Resolves the collision.""" | ||
| rvw = _resolve_ball_table( | ||
| ball.state.rvw.copy(), ball.params.R, ball.params.u_s, ball.params.e_t | ||
| ) | ||
|
|
||
| if bounce_height(rvw[1, 2], ball.params.g) < self.min_bounce_height: | ||
| rvw[1, 2] = 0 | ||
|
|
||
| state = final_ball_motion_state(rvw, ball.params.R) | ||
|
|
||
| ball.state = BallState(rvw, state) | ||
|
|
||
| return ball |
54 changes: 54 additions & 0 deletions
54
pooltool/physics/resolve/ball_table/frictionless_inelastic/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import attrs | ||
|
|
||
| from pooltool.objects.ball.datatypes import Ball, BallState | ||
| from pooltool.physics.dimensionality import Dim | ||
| from pooltool.physics.resolve.ball_table.core import ( | ||
| CoreBallTableCollision, | ||
| bounce_height, | ||
| final_ball_motion_state, | ||
| ) | ||
| from pooltool.physics.resolve.models import BallTableModel | ||
|
|
||
|
|
||
| def _resolve_ball_table(vz0: float, e_t: float) -> float: | ||
| if vz0 >= 0: | ||
| raise ValueError( | ||
| "Ball with non-negative z-velocity can't collide with table surface." | ||
| ) | ||
|
|
||
| return -vz0 * e_t | ||
|
|
||
|
|
||
| @attrs.define | ||
| class FrictionlessInelasticTable(CoreBallTableCollision): | ||
| """A frictionless, inelastic collision. | ||
|
|
||
| The ball bounces on the table with a coefficient of restitution. There is no | ||
| influence of friction, so only the z-component of the velocity is affected. | ||
|
|
||
| To avoid infinite bouncing (the dichotomy paradox), the projected bounce height | ||
| is calculated; if it is less than ``min_bounce_height``, the z-component of the | ||
| velocity is zeroed and the outgoing ball state is set accordingly. | ||
| """ | ||
|
|
||
| min_bounce_height: float = 0.005 | ||
|
|
||
| model: BallTableModel = attrs.field( | ||
| default=BallTableModel.FRICTIONLESS_INELASTIC, init=False, repr=False | ||
| ) | ||
| dim: Dim = attrs.field(default=Dim.THREE, init=False, repr=False) | ||
|
|
||
| def solve(self, ball: Ball) -> Ball: | ||
| """Resolves the collision.""" | ||
| vz = _resolve_ball_table(ball.state.rvw[1, 2], ball.params.e_t) | ||
|
|
||
| if bounce_height(vz, ball.params.g) < self.min_bounce_height: | ||
| vz = 0.0 | ||
|
|
||
| ball.state.rvw[1, 2] = vz | ||
|
|
||
| state = final_ball_motion_state(ball.state.rvw, ball.params.R) | ||
|
|
||
| ball.state = BallState(ball.state.rvw, state) | ||
|
|
||
| return ball |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Guard
bounce_heightagainst non-positive gravity.bounce_heightdivides bygdirectly;g == 0crashes andg < 0yields non-physical output. Add an explicit validation guard.Suggested fix
📝 Committable suggestion
🤖 Prompt for AI Agents