Skip to content

Commit 35bf575

Browse files
committed
Pretty print for space
1 parent d9d6eaa commit 35bf575

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

src/phyjax2d/impl.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -854,7 +854,6 @@ def _polygon_to_polygon(
854854
}
855855

856856

857-
# TODO: pretty print
858857
@chex.dataclass
859858
class Space:
860859
gravity: jax.Array
@@ -998,6 +997,27 @@ def init_solver(self) -> VelocitySolver:
998997
def zeros_state(self) -> StateDict:
999998
return self.shaped.zeros_state()
1000999

1000+
def __repr__(self) -> str:
1001+
field_info = dataclasses.fields(self)
1002+
# Skip _field
1003+
attrs = [
1004+
f"{f.name}={getattr(self, f.name)!r}"
1005+
for f in field_info
1006+
if not f.name.startswith("_")
1007+
]
1008+
return f"Space({', '.join(attrs)})"
1009+
1010+
def __str__(self) -> str:
1011+
lines = [
1012+
"Physics Space Configuration",
1013+
f" Gravity: {self.gravity}",
1014+
f" Timestep (dt): {self.dt}",
1015+
f" Damping: Linear={self.linear_damping}, Angular={self.angular_damping}",
1016+
f" Solver Iterations: Velocity={self.n_velocity_iter}, Position={self.n_position_iter}",
1017+
f" Safety: Linear Slop={self.linear_slop}, Max Velocity={self.max_velocity}"
1018+
]
1019+
return "\n".join(lines)
1020+
10011021

10021022
def update_velocity(space: Space, shape: Shape, state: State) -> State:
10031023
# Expand (N, ) to (N, 1) because xy has a shape (N, 2)

0 commit comments

Comments
 (0)