pooltool.objects.ball.datatypes#
Module that holds Ball and all of its constituents#
Overview#
Stores a ball’s rendered BallOrientation |
|
Holds a ball’s state |
|
A container of BallState objects |
|
A billiards ball |
Classes#
- class pooltool.objects.ball.datatypes.BallOrientation(pos: Tuple[float, float, float, float], sphere: Tuple[float, float, float, float])[source]#
Stores a ball’s rendered BallOrientation
From a practical standpoint, what needs to be understood about this class is that its attributes uniquely specify a ball’s rendered orientation. Less practically, but more specifically, these attributes correspond to the nodes, ‘pos’ and ‘sphere’, that make up a ball’s visual rendering.
Methods:
- static random() BallOrientation[source]#
Generate a random BallOrientation
This generates a ball orientation from a uniform sampling of possible orientations.
- Returns:
A randomized ball orientation.
- Return type:
- copy() BallOrientation[source]#
Create a copy
Note
Since the class is frozen and its attributes are immutable, this just returns
self.
- Return type:
- class pooltool.objects.ball.datatypes.BallState(rvw: ndarray[Any, dtype[float64]], s, t=0)[source]#
Holds a ball’s state
The ball’s state is defined (1) the kinematic state of the ball, (2) a label specifying the ball’s motion state, and (3) the point in time that the ball exists in.
- rvw#
The kinematic state of the ball.
rvwis a \(3\times3\) matrix that stores the 3 vectors that characterize a ball’s kinematic state:\(r\): The displacement (from origin) vector (accessed with
rvw[0])\(v\): The velocity vector (accessed with
rvw[1])\(w\): The angular velocity vector (accessed with
rvw[2])
- Type:
numpy.ndarray[Any, numpy.dtype[numpy.float64]]
- s#
The motion state label of the ball.
sis an integer corresponding to the following motion state labels:0 = stationary 1 = spinning 2 = sliding 3 = rolling 4 = pocketed
- Type:
Methods:
- class pooltool.objects.ball.datatypes.BallHistory(states: List[BallState] = UNKNOWN)[source]#
A container of BallState objects
- states#
A list of time-increasing BallState objects (default =
[]).- Type:
Methods:
- add(state: BallState) None[source]#
Append a state to the history
- Raises:
AssertionError -- If
state.t < self.states[-1]
Notes
This appends
statetostatesstateis not copied before appending to the history, so they share the same memory address.
- copy() BallHistory[source]#
Create a copy
- Return type:
- vectorize() Tuple[numpy.typing.NDArray[numpy.float64], numpy.typing.NDArray[numpy.float64], numpy.typing.NDArray[numpy.float64]] | None[source]#
Compile the attribute from each ball state into arrays
This method unzips each
BallStateinstates, resulting in an array ofBallState.rvwvalues, an array ofBallState.svalues, and an array ofBallState.tvalues.The vectors have the following properties:
>>> import pooltool as pt >>> history = pt.simulate(pt.System.example(), continuous=True).balls["cue"].history_cts >>> rvws, ss, ts = history.vectorize() >>> # Their lengths are equal to the BallHistory >>> len(rvws) == len(ss) == len(ts) == len(history) True >>> # The indices of the arrays match the values of the history >>> pt.objects.BallState(rvws[26], ss[26], ts[26]) == history[26] True
- Returns:
A length 3 tuple (
rvws,ssandts). Returns None ifselfhas no length.- Return type:
Optional[Tuple[numpy.typing.NDArray[numpy.float64], numpy.typing.NDArray[numpy.float64], numpy.typing.NDArray[numpy.float64]]]
Example
vectorizecan be useful for plotting trajectories.import pooltool as pt import matplotlib.pyplot as plt system = pt.System.example() pt.simulate(system, continuous=True, inplace=True) for ball in system.balls.values(): rvw, ss, ts = ball.history_cts.vectorize() plt.plot(rvw[:, 0, 0], rvw[:, 0, 1], color=ss) plt.show()
See also
- static from_vectorization(vectorization: Tuple[numpy.typing.NDArray[numpy.float64], numpy.typing.NDArray[numpy.float64], numpy.typing.NDArray[numpy.float64]] | None) BallHistory[source]#
Zips a vectorization into a BallHistory
An inverse method of
vectorize().- Returns:
A BallHistory constructed from the input vectors.
- Return type:
Example
This illustrates a round-trip with
vectorize()andfrom_vectorization().First create history
>>> import pooltool as pt >>> history = pt.simulate(pt.System.example(), continuous=True).balls["cue"].history_cts
Illustrate a lossless round trip:
>>> pt.objects.BallHistory.from_vectorization(history.vectorize()) == history True
See also
- class pooltool.objects.ball.datatypes.Ball(id: str, state: BallState = BallState.default, params: BallParams = BallParams.default, ballset: BallSet | None = None, initial_orientation: BallOrientation = BallOrientation.random, history: BallHistory = BallHistory.factory, history_cts: BallHistory = BallHistory.factory)[source]#
A billiards ball
This class represents a billiards ball. It stores its parameters (mass, radius, etc.), it’s state (coordinates, velocity, spin, etc), its history (a time-resolved trajectory of its state), amongst other things.
- state#
The ball’s state.
This is the current state of the ball.
See also
See the Important section in
Ballfor a description of the role ofstatesduring simulation.
- params#
The ball’s physical parameters.
The physical parameters of the ball.
- ballset#
The ball set that the ball belongs to.
Important if rendering the ball in a scene.
See also
See
Ball.set_ballset()for details
- Type:
- initial_orientation#
The initial rendered orientation of the ball.
Important if rendering the ball in a scene.
This is the orientation of the ball at \(t = 0\).
- history#
The ball’s state history
The historical states of the ball from \(t_{initial}\) to \(t_{final}\).
See also
See the Important section in
Ballfor a description of the role ofhistoryduring simulation.
- history_cts#
The ball’s continuous state history
The historical states of the ball from \(t_{initial}\) to \(t_{final}\) densely sampled with respect to time.
See also
See
pooltool.evolution.event_based.continuize.continuize()for a details about continuizing a simulated system.See the Important section in
Ballfor a description of the role ofhistory_ctsduring simulation.
Important
To instantiate this class, consider using the
create()constructor. Or, use functions withinpooltool.layoutsto generate entire collection of balls. Or, of course, construct as normal with__init__.Important
The following explains how a
Ballobject is modified when its parent system is simulated (pooltool.evolution.event_based.simulate.simulate()).At the start of the simulation process,
staterepresents the ball state at \(t = 0\). A copy ofstateis appended tohistory.For each timestep of the simulation,
stateis used to inform how the system should advance forward in time. Once determined,stateis updated to reflect the ball’s new state. A copy ofstateis appended tohistory.When the simulation is finished,
staterepresents the final resting state of the ball. So too doeshistory[-1].Finally, if the system is continuized (see
pooltool.evolution.continuize.continuize()),history_ctsis populated. Otherwise it remains empty.- property xyz#
The displacement (from origin) vector of the ball.
A shortcut for
self.state.rvw[0].
- property vel#
The velocity vector of the ball.
A shortcut for
self.state.rvw[1].
- property avel#
The angular velocity vector of the ball.
A shortcut for
self.state.rvw[2].
Methods:
- set_ballset(ballset: pooltool.objects.ball.sets.BallSet) None[source]#
Update the ballset
- Raises:
ValueError -- If the ball ID doesn’t match to a model name of the ballset.
See also
See
pooltool.objects.ball.setsfor details about ball sets.See
pooltool.system.datatypes.System.set_ballset()for setting the ballset for all the balls in a system.
- copy(drop_history: bool = False) Ball[source]#
Create a copy
- Parameters:
drop_history (bool) -- If True, the returned copy
historyandhistory_ctsattributes are both set to emptyBallHistoryobjects.- Return type:
- static create(id: str, *, xy: Sequence[float] | None = None, ballset: pooltool.objects.ball.sets.BallSet | None = None, **kwargs) Ball[source]#
Create a ball using keyword arguments.
This constructor flattens the tunable parameter space, allowing one to construct a
Ballwithout directly instancing objects like likepooltool.objects.balls.params.BallParamsandBallState.- Parameters:
xy (Optional[Sequence[float]]) -- The x and y coordinates of the ball position.
ballset (Optional[pooltool.objects.ball.sets.BallSet]) -- A ballset.
**kwargs -- Arguments accepted by
pooltool.objects.balls.params.BallParams
- Return type: