Co-authored-by: Damien Guillotin <damguillotin@gmail.com>

This commit is contained in:
Laureηt 2023-01-30 12:11:52 +01:00
parent 9e41de60ad
commit f3292494bb
Signed by: Laurent
SSH key fingerprint: SHA256:kZEpW8cMJ54PDeCvOhzreNr4FSh6R13CMGH/POoO8DI

59
src/draw.py Normal file
View file

@ -0,0 +1,59 @@
import matplotlib.pyplot as plt
import numpy as np
def f(x, y):
return np.exp(-((x**2) + y**2) / 3)
x_vals = np.linspace(-5, 5, 100)
y_vals = np.linspace(-5, 5, 100)
X, Y = np.meshgrid(x_vals, y_vals)
Z = f(X - 2, Y) + f(X + 2, Y)
Z = (Z > 0.4).astype(np.float32)
Z *= np.random.rand(*Z.shape)
for i, x in enumerate(x_vals):
for j, y in enumerate(y_vals):
color = f"#{hex(int(Z[j, i] * 255))[2:]}{hex(int(Z[j, i] * 255))[2:]}{hex(int(Z[j, i] * 255))[2:]}"
plt.fill([x, x + 0.1, x + 0.1, x], [y, y, y + 0.1, y + 0.1], color=color)
nb_cams = 32
cam_poses = np.array(
[[6 * np.cos(theta), 6 * np.sin(theta)] for theta in np.linspace(0, 2 * np.pi, nb_cams, endpoint=False)]
)
cam_rots = np.linspace(np.pi, 3 * np.pi, nb_cams, endpoint=False)
cam2world_projs = np.array(
[
[[np.cos(theta), -np.sin(theta), cam_pose[0]], [np.sin(theta), np.cos(theta), cam_pose[1]], [0, 0, 1]]
for theta, cam_pose in zip(cam_rots, cam_poses)
]
)
for i in range(nb_cams):
plt.plot(cam_poses[i][0], cam_poses[i][1], "ro")
plt.text(cam_poses[i][0], cam_poses[i][1], str(i))
x = np.array([[0, 0, 1], [0.5, -0.2, 1], [0.5, 0.2, 1], [0, 0, 1]]).T
x = cam2world_projs[i] @ x
x /= x[2, :]
plt.plot(x[0, :], x[1, :], "r-")
# draw 1d image of the scene for each camera
for i in range(nb_cams):
# sort pixels by distance to camera
cam_pose = cam_poses[i]
pixels_dist = np.linalg.norm(np.array([X.flatten(), Y.flatten()]).T - cam_pose, axis=1)
X_ = X.flatten()[np.argsort(pixels_dist)]
Y_ = Y.flatten()[np.argsort(pixels_dist)]
plt.xlim(-7, 7)
plt.ylim(-7, 7)
plt.axis("equal")
plt.show()