Spaces:
Sleeping
Sleeping
Create visualization_utils.py
Browse files
gully_drs_core/visualization_utils.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import plotly.graph_objects as go
|
| 2 |
+
import numpy as np
|
| 3 |
+
|
| 4 |
+
def plot_3d_trajectory(ball_path_3d, stump_zone):
|
| 5 |
+
# Extract X, Y, Z coordinates
|
| 6 |
+
x = [point[0] for point in ball_path_3d]
|
| 7 |
+
y = [point[1] for point in ball_path_3d]
|
| 8 |
+
z = [point[2] for point in ball_path_3d]
|
| 9 |
+
|
| 10 |
+
# Create the 3D trajectory line
|
| 11 |
+
trajectory = go.Scatter3d(
|
| 12 |
+
x=x, y=y, z=z,
|
| 13 |
+
mode='lines+markers',
|
| 14 |
+
line=dict(color='limegreen', width=4),
|
| 15 |
+
marker=dict(size=5, color='orange'),
|
| 16 |
+
name='Ball Trajectory'
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
# Define the virtual pitch (a flat plane at z=0)
|
| 20 |
+
pitch_x = [0, 640, 640, 0]
|
| 21 |
+
pitch_y = [0, 0, 480, 480]
|
| 22 |
+
pitch_z = [0, 0, 0, 0]
|
| 23 |
+
pitch = go.Mesh3d(
|
| 24 |
+
x=pitch_x, y=pitch_y, z=pitch_z,
|
| 25 |
+
color='green', opacity=0.5,
|
| 26 |
+
name='Pitch'
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
# Define the stumps (a simple vertical rectangle)
|
| 30 |
+
stump_x1, stump_y1, stump_x2, stump_y2 = stump_zone
|
| 31 |
+
stumps = go.Mesh3d(
|
| 32 |
+
x=[stump_x1, stump_x2, stump_x2, stump_x1],
|
| 33 |
+
y=[stump_y1, stump_y1, stump_y2, stump_y2],
|
| 34 |
+
z=[0, 0, 50, 50], # Stumps height
|
| 35 |
+
color='white', opacity=0.8,
|
| 36 |
+
name='Stumps'
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
# Combine all elements into a 3D plot
|
| 40 |
+
fig = go.Figure(data=[trajectory, pitch, stumps])
|
| 41 |
+
|
| 42 |
+
# Customize the layout
|
| 43 |
+
fig.update_layout(
|
| 44 |
+
scene=dict(
|
| 45 |
+
xaxis_title='X (Width)',
|
| 46 |
+
yaxis_title='Y (Height)',
|
| 47 |
+
zaxis_title='Z (Bounce)',
|
| 48 |
+
xaxis=dict(range=[0, 640]),
|
| 49 |
+
yaxis=dict(range=[0, 480]),
|
| 50 |
+
zaxis=dict(range=[0, 60]),
|
| 51 |
+
aspectmode='manual',
|
| 52 |
+
aspectratio=dict(x=1, y=0.75, z=0.2) # Mimic cricket pitch proportions
|
| 53 |
+
),
|
| 54 |
+
title="3D Ball Trajectory Analysis",
|
| 55 |
+
showlegend=True
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
return fig
|