Update app.py
Browse files
app.py
CHANGED
|
@@ -9,319 +9,264 @@ import time
|
|
| 9 |
# ============================================================
|
| 10 |
|
| 11 |
def bernstein_poly(i, n, t):
|
| 12 |
-
"""Polinômio de Bernstein para curvas Bézier"""
|
| 13 |
return comb(n, i) * (t**i) * ((1-t)**(n-i))
|
| 14 |
|
| 15 |
def bezier_curve(points, n_times=20):
|
| 16 |
-
"""Gera curva de Bézier a partir de pontos de controle"""
|
| 17 |
points = np.array(points)
|
| 18 |
t = np.linspace(0, 1, n_times)
|
| 19 |
n = len(points) - 1
|
| 20 |
curve = np.zeros((n_times, 3))
|
| 21 |
for i, p in enumerate(points):
|
| 22 |
curve += np.outer(bernstein_poly(i, n, t), p)
|
| 23 |
-
return curve.T
|
| 24 |
|
| 25 |
def learn_distribution(history, n_bins=4):
|
| 26 |
-
"""
|
| 27 |
-
Aprende distribuição de probabilidade 3D do histórico.
|
| 28 |
-
"""
|
| 29 |
if len(history) < 10:
|
| 30 |
return None, None
|
| 31 |
-
|
| 32 |
history = np.array(history[-50:])
|
| 33 |
bins = [np.linspace(-30, 30, n_bins+1)] * 3
|
| 34 |
H, edges = np.histogramdd(history, bins=bins)
|
| 35 |
-
|
| 36 |
if H.sum() > 0:
|
| 37 |
P = H / H.sum()
|
| 38 |
else:
|
| 39 |
P = np.ones_like(H) / H.size
|
| 40 |
-
|
| 41 |
return P, edges
|
| 42 |
|
| 43 |
def sample_from_distribution(P, bins):
|
| 44 |
-
"""Sorteia um ponto 3D a partir da distribuição aprendida"""
|
| 45 |
if P is None:
|
| 46 |
return np.random.uniform(-25, 25, 3)
|
| 47 |
-
|
| 48 |
flat_idx = np.random.choice(len(P.ravel()), p=P.ravel())
|
| 49 |
idx = np.unravel_index(flat_idx, P.shape)
|
| 50 |
-
|
| 51 |
point = []
|
| 52 |
for i in range(3):
|
| 53 |
low = bins[i][idx[i]]
|
| 54 |
high = bins[i][idx[i]+1]
|
| 55 |
point.append(np.random.uniform(low, high))
|
| 56 |
-
|
| 57 |
return np.array(point)
|
| 58 |
|
| 59 |
# ============================================================
|
| 60 |
-
# SIMULAÇÃO
|
| 61 |
# ============================================================
|
| 62 |
|
| 63 |
def hybrid_simulation(camera_angle, auto_rotate):
|
| 64 |
-
"""Simulação com visual de cometa e layout otimizado para mobile"""
|
| 65 |
-
|
| 66 |
-
# Estado inicial
|
| 67 |
pos = np.array([0., 0., 0.])
|
| 68 |
-
inertia = np.array([
|
| 69 |
history = [pos.copy()]
|
| 70 |
|
| 71 |
-
#
|
| 72 |
-
fig = plt.figure(figsize=(
|
| 73 |
-
gs = fig.add_gridspec(4, 2, hspace=0.3, wspace=0.3
|
| 74 |
-
height_ratios=[2, 1, 1, 1])
|
| 75 |
-
|
| 76 |
-
# Painel 3D ocupa a parte superior (maior)
|
| 77 |
-
ax_3d = fig.add_subplot(gs[0, :], projection='3d')
|
| 78 |
|
| 79 |
-
|
| 80 |
-
ax_prob = fig.add_subplot(gs[
|
| 81 |
-
ax_hist = fig.add_subplot(gs[
|
| 82 |
-
ax_info = fig.add_subplot(gs[
|
| 83 |
-
ax_extra = fig.add_subplot(gs[3, :]) # espaço extra para estatísticas
|
| 84 |
|
| 85 |
-
#
|
| 86 |
bg_color = '#0a0a0a'
|
| 87 |
fig.patch.set_facecolor(bg_color)
|
| 88 |
-
for ax in [ax_3d, ax_prob, ax_hist, ax_info
|
| 89 |
ax.set_facecolor(bg_color)
|
| 90 |
|
| 91 |
-
# Cores do cometa (laranja para fogo, azul para rastro)
|
| 92 |
-
comet_color = '#ff4500' # laranja vibrante
|
| 93 |
-
trail_start = '#ffaa00' # amarelo
|
| 94 |
-
trail_end = '#ff4500' # laranja queimado
|
| 95 |
-
|
| 96 |
cycle = 0
|
| 97 |
frame_global = 0
|
| 98 |
|
| 99 |
while True:
|
| 100 |
cycle += 1
|
| 101 |
|
| 102 |
-
# Ângulo da câmera com rotação automática opcional
|
| 103 |
if auto_rotate:
|
| 104 |
-
current_angle = (camera_angle + frame_global * 0.
|
| 105 |
else:
|
| 106 |
current_angle = camera_angle
|
| 107 |
|
| 108 |
-
|
| 109 |
-
P, bins = learn_distribution(history, n_bins=4)
|
| 110 |
-
|
| 111 |
-
# Sorteia novo alvo
|
| 112 |
target = sample_from_distribution(P, bins)
|
| 113 |
-
|
| 114 |
-
# Curva de Bézier
|
| 115 |
control = pos + inertia
|
| 116 |
curve = bezier_curve([pos, control, target])
|
| 117 |
|
| 118 |
-
# Atualiza histórico
|
| 119 |
new_points = curve.T.tolist()
|
| 120 |
history.extend(new_points)
|
| 121 |
history = history[-200:]
|
| 122 |
|
| 123 |
-
# Aprende inércia
|
| 124 |
if len(history) > 10:
|
| 125 |
inertia = np.array(history[-1]) - np.array(history[-10])
|
| 126 |
-
inertia = np.clip(inertia, -
|
| 127 |
|
| 128 |
-
# Anima a curva
|
| 129 |
for frame_idx in range(curve.shape[1]):
|
| 130 |
frame_global += 1
|
|
|
|
| 131 |
|
| 132 |
-
# Limpa
|
| 133 |
ax_3d.cla()
|
| 134 |
ax_prob.cla()
|
| 135 |
ax_hist.cla()
|
| 136 |
ax_info.cla()
|
| 137 |
-
ax_extra.cla()
|
| 138 |
|
| 139 |
-
# ===== PAINEL 3D -
|
| 140 |
ax_3d.set_facecolor(bg_color)
|
| 141 |
ax_3d.grid(color='#333333', linestyle='--', alpha=0.2)
|
| 142 |
-
ax_3d.view_init(elev=
|
| 143 |
-
ax_3d.set_xlim(-
|
| 144 |
-
ax_3d.set_ylim(-
|
| 145 |
-
ax_3d.set_zlim(-
|
| 146 |
-
|
| 147 |
-
# Remove eixos para visual mais limpo
|
| 148 |
ax_3d.set_xticklabels([])
|
| 149 |
ax_3d.set_yticklabels([])
|
| 150 |
ax_3d.set_zticklabels([])
|
| 151 |
-
ax_3d.xaxis.pane.fill = False
|
| 152 |
-
ax_3d.yaxis.pane.fill = False
|
| 153 |
-
ax_3d.zaxis.pane.fill = False
|
| 154 |
|
| 155 |
-
#
|
| 156 |
-
|
| 157 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
|
| 159 |
-
# Alvo
|
| 160 |
-
ax_3d.scatter(*target, s=
|
| 161 |
-
alpha=0.
|
| 162 |
|
| 163 |
-
# =====
|
| 164 |
-
|
| 165 |
-
trail_points = np.array(history[-30:])
|
| 166 |
|
| 167 |
-
if len(
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
# Posição no rastro (0 = mais antigo, 1 = mais recente)
|
| 171 |
-
t_pos = i / (len(trail_points)-1)
|
| 172 |
|
| 173 |
-
#
|
| 174 |
-
line_width =
|
| 175 |
|
| 176 |
-
#
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
|
|
|
|
|
|
| 181 |
|
| 182 |
-
# Alpha
|
| 183 |
-
alpha = 0.
|
| 184 |
|
| 185 |
-
|
| 186 |
-
ax_3d.plot(trail_points[i:i+2, 0],
|
| 187 |
-
trail_points[i:i+2, 1],
|
| 188 |
-
trail_points[i:i+2, 2],
|
| 189 |
color=color, linewidth=line_width,
|
| 190 |
alpha=alpha, solid_capstyle='round')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
|
| 192 |
-
#
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
for i in range(0, len(trail_points)-1, 3):
|
| 196 |
-
ax_3d.scatter(trail_points[i, 0],
|
| 197 |
-
trail_points[i, 1],
|
| 198 |
-
trail_points[i, 2],
|
| 199 |
-
s=5, c='white', alpha=0.2)
|
| 200 |
-
|
| 201 |
-
# Agente (cometa) - ponto principal com brilho
|
| 202 |
-
current_pos = curve[:, frame_idx]
|
| 203 |
-
ax_3d.scatter(*current_pos, s=200, c=comet_color,
|
| 204 |
-
edgecolors='white', linewidth=2, zorder=10,
|
| 205 |
alpha=0.9)
|
| 206 |
|
| 207 |
-
#
|
| 208 |
-
ax_3d.scatter(*current_pos, s=
|
| 209 |
-
alpha=0.2, zorder=
|
| 210 |
|
| 211 |
-
#
|
| 212 |
-
|
| 213 |
-
|
|
|
|
|
|
|
| 214 |
|
|
|
|
|
|
|
| 215 |
if P is not None:
|
| 216 |
P_xy = P.sum(axis=2)
|
| 217 |
P_xy = P_xy / P_xy.max() if P_xy.max() > 0 else P_xy
|
| 218 |
-
|
| 219 |
ax_prob.imshow(P_xy, cmap='hot', origin='lower',
|
| 220 |
-
extent=[-30, 30, -30, 30], alpha=0.
|
| 221 |
-
|
| 222 |
-
# Marca posição atual
|
| 223 |
ax_prob.scatter(current_pos[0], current_pos[1],
|
| 224 |
-
c='cyan', s=
|
| 225 |
-
edgecolors='white', linewidth=
|
| 226 |
else:
|
| 227 |
-
ax_prob.text(0.5, 0.5, '
|
| 228 |
transform=ax_prob.transAxes, ha='center',
|
| 229 |
-
va='center', color='#
|
| 230 |
-
|
| 231 |
-
ax_prob.set_xlim(-30, 30)
|
| 232 |
-
ax_prob.set_ylim(-30, 30)
|
| 233 |
ax_prob.set_xticklabels([])
|
| 234 |
ax_prob.set_yticklabels([])
|
| 235 |
|
| 236 |
-
# =====
|
| 237 |
-
ax_hist.set_title('🧭
|
| 238 |
-
ax_hist.set_facecolor(bg_color)
|
| 239 |
-
|
| 240 |
if len(history) > 10:
|
| 241 |
dirs = np.diff(np.array(history[-50:]), axis=0)
|
| 242 |
angles = np.arctan2(dirs[:,1], dirs[:,0])
|
| 243 |
-
ax_hist.hist(angles, bins=12, color='#
|
| 244 |
edgecolor='white', density=True)
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
ax_hist.tick_params(colors='white', labelsize=8)
|
| 248 |
-
ax_hist.set_xticklabels([])
|
| 249 |
-
ax_hist.set_yticklabels([])
|
| 250 |
|
| 251 |
-
# =====
|
| 252 |
-
ax_info.set_title('📊 Estatísticas', color='white', fontsize=12)
|
| 253 |
ax_info.axis('off')
|
| 254 |
-
|
| 255 |
entropy = 0
|
| 256 |
if P is not None:
|
| 257 |
p_flat = P.ravel()
|
| 258 |
entropy = -np.sum(p_flat * np.log(p_flat + 1e-10))
|
| 259 |
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
transform=ax_info.transAxes,
|
| 264 |
-
color='white', fontsize=11,
|
| 265 |
-
horizontalalignment='center',
|
| 266 |
-
bbox=dict(boxstyle='round', facecolor='#222222', alpha=0.6))
|
| 267 |
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
ax_extra.text(0.3, 0.5, pos_text, transform=ax_extra.transAxes,
|
| 274 |
-
color='#88ccff', fontsize=10)
|
| 275 |
-
ax_extra.text(0.3, 0.2, vel_text, transform=ax_extra.transAxes,
|
| 276 |
-
color='#ffaa00', fontsize=10)
|
| 277 |
|
| 278 |
yield fig
|
| 279 |
-
time.sleep(0.
|
| 280 |
|
| 281 |
pos = target
|
| 282 |
|
| 283 |
# ============================================================
|
| 284 |
-
# INTERFACE
|
| 285 |
# ============================================================
|
| 286 |
|
| 287 |
with gr.Blocks(theme=gr.themes.Base(
|
| 288 |
primary_hue="orange",
|
| 289 |
-
secondary_hue="
|
| 290 |
-
font=gr.themes.GoogleFont('
|
| 291 |
)) as demo:
|
| 292 |
|
| 293 |
gr.HTML("""
|
| 294 |
-
<div style="text-align: center;
|
| 295 |
-
|
| 296 |
-
<
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 297 |
</div>
|
| 298 |
""")
|
| 299 |
|
| 300 |
-
with gr.Column():
|
| 301 |
with gr.Row():
|
| 302 |
-
camera = gr.Slider(-180, 180, value=45,
|
| 303 |
-
label="📐 Ângulo", step=5)
|
| 304 |
|
| 305 |
with gr.Row():
|
| 306 |
-
auto_rotate = gr.Checkbox(label="
|
| 307 |
|
| 308 |
with gr.Row():
|
| 309 |
-
btn = gr.Button("
|
| 310 |
size="lg", scale=2)
|
| 311 |
|
| 312 |
output = gr.Plot(label="")
|
| 313 |
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
<
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
""")
|
| 325 |
|
| 326 |
btn.click(
|
| 327 |
fn=hybrid_simulation,
|
|
|
|
| 9 |
# ============================================================
|
| 10 |
|
| 11 |
def bernstein_poly(i, n, t):
|
|
|
|
| 12 |
return comb(n, i) * (t**i) * ((1-t)**(n-i))
|
| 13 |
|
| 14 |
def bezier_curve(points, n_times=20):
|
|
|
|
| 15 |
points = np.array(points)
|
| 16 |
t = np.linspace(0, 1, n_times)
|
| 17 |
n = len(points) - 1
|
| 18 |
curve = np.zeros((n_times, 3))
|
| 19 |
for i, p in enumerate(points):
|
| 20 |
curve += np.outer(bernstein_poly(i, n, t), p)
|
| 21 |
+
return curve.T
|
| 22 |
|
| 23 |
def learn_distribution(history, n_bins=4):
|
|
|
|
|
|
|
|
|
|
| 24 |
if len(history) < 10:
|
| 25 |
return None, None
|
|
|
|
| 26 |
history = np.array(history[-50:])
|
| 27 |
bins = [np.linspace(-30, 30, n_bins+1)] * 3
|
| 28 |
H, edges = np.histogramdd(history, bins=bins)
|
|
|
|
| 29 |
if H.sum() > 0:
|
| 30 |
P = H / H.sum()
|
| 31 |
else:
|
| 32 |
P = np.ones_like(H) / H.size
|
|
|
|
| 33 |
return P, edges
|
| 34 |
|
| 35 |
def sample_from_distribution(P, bins):
|
|
|
|
| 36 |
if P is None:
|
| 37 |
return np.random.uniform(-25, 25, 3)
|
|
|
|
| 38 |
flat_idx = np.random.choice(len(P.ravel()), p=P.ravel())
|
| 39 |
idx = np.unravel_index(flat_idx, P.shape)
|
|
|
|
| 40 |
point = []
|
| 41 |
for i in range(3):
|
| 42 |
low = bins[i][idx[i]]
|
| 43 |
high = bins[i][idx[i]+1]
|
| 44 |
point.append(np.random.uniform(low, high))
|
|
|
|
| 45 |
return np.array(point)
|
| 46 |
|
| 47 |
# ============================================================
|
| 48 |
+
# SIMULAÇÃO - COMETA VISÍVEL
|
| 49 |
# ============================================================
|
| 50 |
|
| 51 |
def hybrid_simulation(camera_angle, auto_rotate):
|
|
|
|
|
|
|
|
|
|
| 52 |
pos = np.array([0., 0., 0.])
|
| 53 |
+
inertia = np.array([5., 5., 5.]) # inércia maior pra movimento mais visível
|
| 54 |
history = [pos.copy()]
|
| 55 |
|
| 56 |
+
# Layout otimizado
|
| 57 |
+
fig = plt.figure(figsize=(12, 10))
|
| 58 |
+
gs = fig.add_gridspec(4, 2, hspace=0.3, wspace=0.3)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
+
ax_3d = fig.add_subplot(gs[0:2, :], projection='3d') # 3D maior
|
| 61 |
+
ax_prob = fig.add_subplot(gs[2, 0])
|
| 62 |
+
ax_hist = fig.add_subplot(gs[2, 1])
|
| 63 |
+
ax_info = fig.add_subplot(gs[3, :])
|
|
|
|
| 64 |
|
| 65 |
+
# Cores vibrantes
|
| 66 |
bg_color = '#0a0a0a'
|
| 67 |
fig.patch.set_facecolor(bg_color)
|
| 68 |
+
for ax in [ax_3d, ax_prob, ax_hist, ax_info]:
|
| 69 |
ax.set_facecolor(bg_color)
|
| 70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
cycle = 0
|
| 72 |
frame_global = 0
|
| 73 |
|
| 74 |
while True:
|
| 75 |
cycle += 1
|
| 76 |
|
|
|
|
| 77 |
if auto_rotate:
|
| 78 |
+
current_angle = (camera_angle + frame_global * 0.8) % 360
|
| 79 |
else:
|
| 80 |
current_angle = camera_angle
|
| 81 |
|
| 82 |
+
P, bins = learn_distribution(history)
|
|
|
|
|
|
|
|
|
|
| 83 |
target = sample_from_distribution(P, bins)
|
|
|
|
|
|
|
| 84 |
control = pos + inertia
|
| 85 |
curve = bezier_curve([pos, control, target])
|
| 86 |
|
|
|
|
| 87 |
new_points = curve.T.tolist()
|
| 88 |
history.extend(new_points)
|
| 89 |
history = history[-200:]
|
| 90 |
|
|
|
|
| 91 |
if len(history) > 10:
|
| 92 |
inertia = np.array(history[-1]) - np.array(history[-10])
|
| 93 |
+
inertia = np.clip(inertia, -20, 20)
|
| 94 |
|
|
|
|
| 95 |
for frame_idx in range(curve.shape[1]):
|
| 96 |
frame_global += 1
|
| 97 |
+
current_pos = curve[:, frame_idx]
|
| 98 |
|
| 99 |
+
# Limpa
|
| 100 |
ax_3d.cla()
|
| 101 |
ax_prob.cla()
|
| 102 |
ax_hist.cla()
|
| 103 |
ax_info.cla()
|
|
|
|
| 104 |
|
| 105 |
+
# ===== PAINEL 3D - COMETA GIGANTE =====
|
| 106 |
ax_3d.set_facecolor(bg_color)
|
| 107 |
ax_3d.grid(color='#333333', linestyle='--', alpha=0.2)
|
| 108 |
+
ax_3d.view_init(elev=25, azim=current_angle)
|
| 109 |
+
ax_3d.set_xlim(-35, 35)
|
| 110 |
+
ax_3d.set_ylim(-35, 35)
|
| 111 |
+
ax_3d.set_zlim(-35, 35)
|
|
|
|
|
|
|
| 112 |
ax_3d.set_xticklabels([])
|
| 113 |
ax_3d.set_yticklabels([])
|
| 114 |
ax_3d.set_zticklabels([])
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
+
# Esfera transparente para referência
|
| 117 |
+
u = np.linspace(0, 2*np.pi, 20)
|
| 118 |
+
v = np.linspace(0, np.pi, 20)
|
| 119 |
+
x_sphere = 25 * np.outer(np.cos(u), np.sin(v))
|
| 120 |
+
y_sphere = 25 * np.outer(np.sin(u), np.sin(v))
|
| 121 |
+
z_sphere = 25 * np.outer(np.ones_like(u), np.cos(v))
|
| 122 |
+
ax_3d.plot_wireframe(x_sphere, y_sphere, z_sphere,
|
| 123 |
+
color='#333333', alpha=0.1, linewidth=0.5)
|
| 124 |
+
|
| 125 |
+
# Ponto inicial (sutil)
|
| 126 |
+
ax_3d.scatter(*pos, s=50, c='#4488ff', alpha=0.3)
|
| 127 |
|
| 128 |
+
# Alvo em X bem visível
|
| 129 |
+
ax_3d.scatter(*target, s=300, c='#ff0066', marker='X',
|
| 130 |
+
alpha=0.9, edgecolors='white', linewidth=2)
|
| 131 |
|
| 132 |
+
# ===== RASTRO DE FOGO (BEM GROSSO) =====
|
| 133 |
+
trail = np.array(history[-40:])
|
|
|
|
| 134 |
|
| 135 |
+
if len(trail) > 5:
|
| 136 |
+
for i in range(len(trail)-1):
|
| 137 |
+
t_pos = i / (len(trail)-1)
|
|
|
|
|
|
|
| 138 |
|
| 139 |
+
# LINHA GROSSA (10-20 pixels)
|
| 140 |
+
line_width = 10 + 30 * t_pos
|
| 141 |
|
| 142 |
+
# CORES VIBRANTES: amarelo -> laranja -> vermelho
|
| 143 |
+
if t_pos < 0.3:
|
| 144 |
+
color = '#ffff00' # amarelo
|
| 145 |
+
elif t_pos < 0.6:
|
| 146 |
+
color = '#ffaa00' # laranja
|
| 147 |
+
else:
|
| 148 |
+
color = '#ff4400' # vermelho
|
| 149 |
|
| 150 |
+
# Alpha alto pra ficar visível
|
| 151 |
+
alpha = 0.4 + 0.5 * t_pos
|
| 152 |
|
| 153 |
+
ax_3d.plot(trail[i:i+2, 0], trail[i:i+2, 1], trail[i:i+2, 2],
|
|
|
|
|
|
|
|
|
|
| 154 |
color=color, linewidth=line_width,
|
| 155 |
alpha=alpha, solid_capstyle='round')
|
| 156 |
+
|
| 157 |
+
# Bolinhas extras pra dar textura
|
| 158 |
+
if i % 3 == 0:
|
| 159 |
+
ax_3d.scatter(trail[i, 0], trail[i, 1], trail[i, 2],
|
| 160 |
+
s=30, c='white', alpha=0.3)
|
| 161 |
|
| 162 |
+
# ===== AGENTE (BOLA GIGANTE) =====
|
| 163 |
+
ax_3d.scatter(*current_pos, s=600, c='#ffaa00',
|
| 164 |
+
edgecolors='white', linewidth=3, zorder=20,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
alpha=0.9)
|
| 166 |
|
| 167 |
+
# Brilho
|
| 168 |
+
ax_3d.scatter(*current_pos, s=900, c='#ffaa00',
|
| 169 |
+
alpha=0.2, zorder=10)
|
| 170 |
|
| 171 |
+
# Chamas (partículas ao redor)
|
| 172 |
+
for _ in range(8):
|
| 173 |
+
offset = np.random.randn(3) * 3
|
| 174 |
+
ax_3d.scatter(*(current_pos + offset), s=20,
|
| 175 |
+
c='#ff5500', alpha=0.6)
|
| 176 |
|
| 177 |
+
# ===== MAPA DE CALOR =====
|
| 178 |
+
ax_prob.set_title('🔥 ONDE ANDOU', color='white', fontsize=14, fontweight='bold')
|
| 179 |
if P is not None:
|
| 180 |
P_xy = P.sum(axis=2)
|
| 181 |
P_xy = P_xy / P_xy.max() if P_xy.max() > 0 else P_xy
|
|
|
|
| 182 |
ax_prob.imshow(P_xy, cmap='hot', origin='lower',
|
| 183 |
+
extent=[-30, 30, -30, 30], alpha=0.9)
|
|
|
|
|
|
|
| 184 |
ax_prob.scatter(current_pos[0], current_pos[1],
|
| 185 |
+
c='cyan', s=100, marker='o',
|
| 186 |
+
edgecolors='white', linewidth=2)
|
| 187 |
else:
|
| 188 |
+
ax_prob.text(0.5, 0.5, 'EXPLORANDO',
|
| 189 |
transform=ax_prob.transAxes, ha='center',
|
| 190 |
+
va='center', color='#ffaa00', fontsize=14)
|
|
|
|
|
|
|
|
|
|
| 191 |
ax_prob.set_xticklabels([])
|
| 192 |
ax_prob.set_yticklabels([])
|
| 193 |
|
| 194 |
+
# ===== DIREÇÕES =====
|
| 195 |
+
ax_hist.set_title('🧭 DIREÇÕES', color='white', fontsize=14, fontweight='bold')
|
|
|
|
|
|
|
| 196 |
if len(history) > 10:
|
| 197 |
dirs = np.diff(np.array(history[-50:]), axis=0)
|
| 198 |
angles = np.arctan2(dirs[:,1], dirs[:,0])
|
| 199 |
+
ax_hist.hist(angles, bins=12, color='#ff8800', alpha=0.8,
|
| 200 |
edgecolor='white', density=True)
|
| 201 |
+
ax_hist.tick_params(colors='white')
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
|
| 203 |
+
# ===== INFO =====
|
|
|
|
| 204 |
ax_info.axis('off')
|
|
|
|
| 205 |
entropy = 0
|
| 206 |
if P is not None:
|
| 207 |
p_flat = P.ravel()
|
| 208 |
entropy = -np.sum(p_flat * np.log(p_flat + 1e-10))
|
| 209 |
|
| 210 |
+
info_text = f"""⚡ CICLO: {cycle} | HIST: {len(history)} | ENTROPIA: {entropy:.2f}
|
| 211 |
+
📍 POS: ({current_pos[0]:.0f}, {current_pos[1]:.0f}, {current_pos[2]:.0f})
|
| 212 |
+
➡️ INÉRCIA: ({inertia[0]:.0f}, {inertia[1]:.0f}, {inertia[2]:.0f})"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
|
| 214 |
+
ax_info.text(0.5, 0.5, info_text, transform=ax_info.transAxes,
|
| 215 |
+
color='white', fontsize=14, ha='center', va='center',
|
| 216 |
+
bbox=dict(boxstyle='round', facecolor='#222222',
|
| 217 |
+
edgecolor='#ffaa00', linewidth=2, alpha=0.9))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 218 |
|
| 219 |
yield fig
|
| 220 |
+
time.sleep(0.03)
|
| 221 |
|
| 222 |
pos = target
|
| 223 |
|
| 224 |
# ============================================================
|
| 225 |
+
# INTERFACE
|
| 226 |
# ============================================================
|
| 227 |
|
| 228 |
with gr.Blocks(theme=gr.themes.Base(
|
| 229 |
primary_hue="orange",
|
| 230 |
+
secondary_hue="red",
|
| 231 |
+
font=gr.themes.GoogleFont('Montserrat')
|
| 232 |
)) as demo:
|
| 233 |
|
| 234 |
gr.HTML("""
|
| 235 |
+
<div style="text-align: center; background: linear-gradient(45deg, #ff4400, #ffaa00);
|
| 236 |
+
padding: 20px; border-radius: 15px; margin-bottom: 20px;">
|
| 237 |
+
<h1 style="color: white; font-size: 3rem; text-shadow: 3px 3px 0px #aa2200;
|
| 238 |
+
margin: 0; letter-spacing: 4px;">
|
| 239 |
+
🔥 COMETA DE FOGO 🔥
|
| 240 |
+
</h1>
|
| 241 |
+
<p style="color: #ffffaa; font-size: 1.2rem; margin: 5px 0 0 0;">
|
| 242 |
+
aprendizado + inércia + rastro explosivo
|
| 243 |
+
</p>
|
| 244 |
</div>
|
| 245 |
""")
|
| 246 |
|
| 247 |
+
with gr.Column():
|
| 248 |
with gr.Row():
|
| 249 |
+
camera = gr.Slider(-180, 180, value=45, label="🎥 ÂNGULO", step=5)
|
|
|
|
| 250 |
|
| 251 |
with gr.Row():
|
| 252 |
+
auto_rotate = gr.Checkbox(label="🌀 GIRAR AUTOMÁTICO", value=True)
|
| 253 |
|
| 254 |
with gr.Row():
|
| 255 |
+
btn = gr.Button("💥 LANÇAR COMETA", variant="primary",
|
| 256 |
size="lg", scale=2)
|
| 257 |
|
| 258 |
output = gr.Plot(label="")
|
| 259 |
|
| 260 |
+
gr.HTML("""
|
| 261 |
+
<div style="display: flex; gap: 20px; justify-content: center;
|
| 262 |
+
color: #ffaa00; font-size: 0.9rem; margin-top: 10px;">
|
| 263 |
+
<span>🔥 rastro de 40px</span>
|
| 264 |
+
<span>•</span>
|
| 265 |
+
<span>⚡ bola de 600px</span>
|
| 266 |
+
<span>•</span>
|
| 267 |
+
<span>🌈 gradiente explosivo</span>
|
| 268 |
+
</div>
|
| 269 |
+
""")
|
|
|
|
| 270 |
|
| 271 |
btn.click(
|
| 272 |
fn=hybrid_simulation,
|