# Scene « Saturne » (reprise compacte de 3D-01) : sphere + anneau incline, densites analytiques.
INCLINAISON = math.radians(25.0)
def densite_analytique(p):
# p : (..., 3). Renvoie la densite sigma (..., 1) : sphere pleine + anneau plat incline.
x, y, z = p[..., 0], p[..., 1], p[..., 2]
r_axial = torch.sqrt(x**2 + y**2)
# Sphere pleine de rayon 0.55 centree a l'origine.
d_sphere = torch.where(x**2 + y**2 + z**2 < 0.55**2, 8.0 * torch.ones_like(x), torch.zeros_like(x))
# Anneau : plan z=0 tourne de INCLINAISON autour de x, rayons 0.85..1.55, epaisseur 0.05.
zp = z * math.cos(INCLINAISON) - y * math.sin(INCLINAISON)
yp = z * math.sin(INCLINAISON) + y * math.cos(INCLINAISON)
dans_anneau = (r_axial > 0.85) & (r_axial < 1.55) & (zp.abs() < 0.05)
d_anneau = torch.where(dans_anneau, 6.0 * torch.ones_like(x), torch.zeros_like(x))
return torch.clamp(d_sphere + d_anneau, max=10.0)
def couleur_analytique(p):
# Couleur diffuse simple : coeur chaud, anneau gris-bleute.
x, y, z = p[..., 0], p[..., 1], p[..., 2]
r = torch.sqrt(x**2 + y**2 + z**2 + 1e-9)
r_axial = torch.sqrt(x**2 + y**2 + 1e-9)
c_sphere = torch.stack([0.92 - 0.35 * z, 0.45 + 0.15 * x, 0.22 + 0.10 * y], dim=-1)
c_anneau = torch.stack([0.55 + 0.15 * torch.sin(3 * r_axial), torch.full_like(x, 0.58), torch.full_like(x, 0.66)], dim=-1)
dans_anneau = (r_axial > 0.85) & (r_axial < 1.55)
return torch.where(dans_anneau.unsqueeze(-1), c_anneau, c_sphere).clamp(0, 1)
def pose_spherique(theta, r=3.2, phi=math.radians(70)):
# Pose c2w (look-at origine) sur l'orbite, meme esprit que 3D-01.
ct, st, cp, sp = math.cos(theta), math.sin(theta), math.cos(phi), math.sin(phi)
c2w = np.array([
[-st, -ct * cp, ct * sp, r * ct * sp],
[ ct, -st * cp, st * sp, r * st * sp],
[0.0, sp, cp, r * cp],
[0.0, 0.0, 0.0, 1.0],
], dtype=np.float32)
return c2w
def rendu_volumique_analytique(theta, H=80, W=80, N=64, near=1.8, far=4.8, focal=90.0):
# Ray-marching direct de la densite analytique (le "continu", sans reseau) : c'est la cible.
c2w = torch.tensor(pose_spherique(theta))
ii, jj = torch.meshgrid(torch.arange(W, dtype=torch.float32), torch.arange(H, dtype=torch.float32), indexing='xy')
dirs = torch.stack([(ii - W / 2) / focal, -(jj - H / 2) / focal, -torch.ones_like(ii)], dim=-1)
rays_d = dirs @ c2w[:3, :3].T
rays_o = c2w[:3, -1].expand(rays_d.shape)
ts = near + (far - near) * torch.linspace(0, 1, N)
pts = rays_o.unsqueeze(-2) + rays_d.unsqueeze(-2) * ts.view(1, 1, N, 1)
sigma = densite_analytique(pts)
rgb = couleur_analytique(pts)
delta = (far - near) / N
alpha = 1 - torch.exp(-sigma * delta)
w = alpha * torch.cumprod(torch.cat([torch.ones_like(alpha[..., :1]), 1 - alpha + 1e-10], dim=-1)[..., :-1], dim=-1)
img = torch.sum(w.unsqueeze(-1) * rgb, dim=-2)
return img.clamp(0, 1)
ANGLES_CIBLES = [math.radians(a) for a in (20, 110, 200)]
vues_cibles = torch.stack([rendu_volumique_analytique(t) for t in ANGLES_CIBLES])
fig, axes = plt.subplots(1, 3, figsize=(11, 3.6))
for ax, img, a in zip(axes, vues_cibles, (20, 110, 200)):
ax.imshow(img.numpy()); ax.set_title(f"vue cible (volumique) — azimuth {a} deg"); ax.axis('off')
plt.tight_layout(); plt.show()