Skip to content

Commit

Permalink
Mesh base generation
Browse files Browse the repository at this point in the history
  • Loading branch information
connorslade committed Jun 12, 2024
1 parent dea9885 commit 6cb967a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ target
screenshots
states
*.wav
*.mp4
*.mp4
*.stl
43 changes: 39 additions & 4 deletions scripts/3d_mesh.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ STATE = "states/energy-4000.bin"
WIDTH = 1440
HEIGHT = 900

VALUE_SCALE = 350
BASE_HEIGHT = 14

raw_state = read(STATE)
state = reshape(reinterpret(Float32, raw_state[9:end]), (WIDTH, HEIGHT))

Expand All @@ -17,19 +20,51 @@ function index_from_coords(x, y)
return (y - 1) * WIDTH + x
end

function push_quad!(to, x1, y1, x2, y2)
push!(to, TriangleFace((index_from_coords(x1, y1), index_from_coords(x2, y1), index_from_coords(x1, y2))))
push!(to, TriangleFace((index_from_coords(x2, y1), index_from_coords(x2, y2), index_from_coords(x1, y2))))
end

for y in 1:HEIGHT
for x in 1:WIDTH
push!(points, GeometryBasics.Point3f((x, y, round(state[x, y] * 100))))
push!(points, GeometryBasics.Point3f((x, y, state[x, y] * VALUE_SCALE + BASE_HEIGHT)))

if x != WIDTH && y != HEIGHT
push!(connec, TriangleFace((index_from_coords(x, y), index_from_coords(x + 1, y), index_from_coords(x, y + 1))))
push!(connec, TriangleFace((index_from_coords(x + 1, y), index_from_coords(x + 1, y + 1), index_from_coords(x, y + 1))))
push_quad!(connec, x, y, x + 1, y + 1)
end
end
end

if BASE_HEIGHT != 0
last_index = index_from_coords(WIDTH, HEIGHT)
push!(points, GeometryBasics.Point3f((0, 0, 0)))
push!(points, GeometryBasics.Point3f((WIDTH, 0, 0)))
push!(points, GeometryBasics.Point3f((0, HEIGHT, 0)))
push!(points, GeometryBasics.Point3f((WIDTH, HEIGHT, 0)))

# Left wall
push!(connec, TriangleFace((index_from_coords(1, 1), last_index + 1, last_index + 3)))
push!(connec, TriangleFace((index_from_coords(1, 1), index_from_coords(1, HEIGHT), last_index + 3)))

# Right wall
push!(connec, TriangleFace((index_from_coords(WIDTH, 1), index_from_coords(WIDTH, HEIGHT), last_index + 2)))
push!(connec, TriangleFace((index_from_coords(WIDTH, HEIGHT), last_index + 2, last_index + 4)))

# Bottom wall
push!(connec, TriangleFace((index_from_coords(1, 1), index_from_coords(WIDTH, 1), last_index + 2)))
push!(connec, TriangleFace((index_from_coords(1, 1), last_index + 2, last_index + 1)))

# Top wall
push!(connec, TriangleFace((index_from_coords(1, HEIGHT), index_from_coords(WIDTH, HEIGHT), last_index + 3)))
push!(connec, TriangleFace((index_from_coords(WIDTH, HEIGHT), last_index + 4, last_index + 3)))

# Bottom cap
push!(connec, TriangleFace((last_index + 1, last_index + 3, last_index + 4)))
push!(connec, TriangleFace((last_index + 1, last_index + 4, last_index + 2)))
end

println("Points: $(length(points))")
println("Connections: $(length(connec))")

mesh = GeometryBasics.Mesh(points, connec)
save("energy-4000.stl", mesh)
save("energy.stl", mesh);

0 comments on commit 6cb967a

Please sign in to comment.