des dessins + geoly

This commit is contained in:
gdamms 2023-01-30 10:23:34 +01:00
parent d8591efbae
commit 705b610d68

View file

@ -94,19 +94,42 @@ def fast_voxel_intersect(start_, end_, origin_, step_, shape_) -> tuple[list, li
if __name__ == "__main__": if __name__ == "__main__":
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
from perlin_noise import PerlinNoise
def update_figure(): def update_figure():
positions, voxels, voxels_idx = fast_voxel_intersect(start, end, origin, step, shape) positions, voxels, voxels_idx = fast_voxel_intersect(start, end, origin, step, shape)
plt.clf() plt.clf()
# Plot black background based on levelset
for i in range(shape[0]):
for j in range(shape[1]):
if V[i, j] > 0.5:
continue
plt.fill(
[
origin[0] + i * step[0],
origin[0] + (i + 1) * step[0],
origin[0] + (i + 1) * step[0],
origin[0] + i * step[0],
],
[
origin[1] + j * step[1],
origin[1] + j * step[1],
origin[1] + (j + 1) * step[1],
origin[1] + (j + 1) * step[1],
],
color="#000",
alpha=0.8,
)
# Plot hitted voxels # Plot hitted voxels
for voxel in voxels: for voxel in voxels:
plt.fill( plt.fill(
[voxel[0], voxel[0] + step[0], voxel[0] + step[0], voxel[0]], [voxel[0], voxel[0] + step[0], voxel[0] + step[0], voxel[0]],
[voxel[1], voxel[1], voxel[1] + step[1], voxel[1] + step[1]], [voxel[1], voxel[1], voxel[1] + step[1], voxel[1] + step[1]],
color="#e25", color="#e25",
alpha=0.5, alpha=0.3,
) )
for voxel_id in voxels_idx: for voxel_id in voxels_idx:
@ -124,7 +147,7 @@ if __name__ == "__main__":
origin[1] + (voxel_id[1] + 1) * step[1], origin[1] + (voxel_id[1] + 1) * step[1],
], ],
color="#2e3", color="#2e3",
alpha=0.5, alpha=0.3,
) )
# Plot line segment # Plot line segment
@ -139,31 +162,51 @@ if __name__ == "__main__":
plt.plot(end[0], end[1], 'ro') plt.plot(end[0], end[1], 'ro')
# Plot voxel grid # Plot voxel grid
plt.axis("equal")
plt.xlim((-10, 10)) plt.xlim((-10, 10))
plt.ylim((-10, 10)) plt.ylim((-10, 10))
plt.xticks(origin[0] + step[0] * np.arange(shape[0] + 1)) plt.xticks(origin[0] + step[0] * np.arange(shape[0] + 1))
plt.yticks(origin[1] + step[1] * np.arange(shape[1] + 1)) plt.yticks(origin[1] + step[1] * np.arange(shape[1] + 1))
plt.grid() plt.grid()
ax = plt.gca()
ax.set_aspect('equal', adjustable='box')
plt.draw() plt.draw()
def onkey(event): def onkey(event):
global start, end global start, end
if event.key == " ": if event.key == " ":
start = np.random.rand(2) * 20 - 10 start = random_start()
end = np.random.rand(2) * 20 - 10 end = random_end()
update_figure() update_figure()
elif event.key == "enter":
plt.savefig("fvi.png", dpi=300, bbox_inches="tight", pad_inches=0, transparent=True)
def random_start():
x = (np.random.randint(0, shape[0]) + 0.5) * step[0] + origin[0]
y = (np.random.randint(0, shape[1]) + 0.5) * step[1] + origin[1]
return (x, y)
def random_end():
teta = np.random.rand() * 2 * np.pi
x = np.cos(teta) * 10
y = np.sin(teta) * 10
return (x, y)
# Define voxel grid # Define voxel grid
origin = np.array([-5., -5.]) origin = np.array([-5., -5.])
step = np.array([1.4, 1.4]) step = np.array([1.0, 1.0])
shape = (10, 10) shape = (10, 10)
# Define levelset
noise = PerlinNoise(octaves=6, seed=1)
X, Y = np.mgrid[:shape[0], :shape[1]]
V = [[2 * noise([x/shape[0], y/shape[1]]) + np.sqrt((x-shape[0]/2)**2 + (y-shape[1]/2)**2) for y in range(shape[1])] for x in range(shape[0])]
V = np.array(V)
V = (V - V.min()) / (V.max() - V.min())
# Define segment # Define segment
# start = np.random.rand(2) * 20 - 10 start = random_start()
# end = np.random.rand(2) * 20 - 10 end = random_end()
start = np.array([2.5, -3.5])
end = np.array([7.0, 3.0])
# Plot # Plot
fig = plt.figure() fig = plt.figure()