-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathilastik_objids_to_meshes.jl
47 lines (42 loc) · 1.42 KB
/
ilastik_objids_to_meshes.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using LinearAlgebra, GeometryBasics, MarchingCubes, FileIO, MeshIO, HDF5
mesh_and_save_hole!(M::Array{UInt32, 3}, i::UInt32, pos::Point3f, filename::String) = begin
divs = 8
samples = Float32(divs^3)
sx, sy, sz = div.(size(M), divs)
vol = zeros(Float32, (sx, sy, sz))
for iz = 1:sz, iy = 1:sy, ix = 1:sx
val = 0f0
for kz = 1:divs, ky = 1:divs, kx = 1:divs
inobj = M[divs*(ix-1)+kx, divs*(iy-1)+ky, divs*(iz-1)+kz] == i
val += Float32(inobj) / samples
end
vol[ix, iy, iz] = val - 0.5f0
end
mc = MC(vol)
# mc = MC(Float32(0.5) .- Float32.(M .== i))
march(mc)
println("Meshing ($(length(mc.vertices)) vertices)...")
if length(mc.vertices) < 3
println("not enough vertices")
else
msh = MarchingCubes.makemesh(GeometryBasics, mc)
msh.position .*= Float32(divs)
msh.position .+= pos
save(filename, msh)
end
nothing
end
hole_ids_to_meshes(hole_ids_file::String, file_prefix::String, pos::Point3f) = begin
f = h5open(hole_ids_file)
M = f["exported_data"][1, :, :, :, 1]
n = maximum(M)
close(f)
for id = 1:n
println("Building mesh for hole $id / $n ... ")
mesh_and_save_hole!(M, UInt32(id), pos, "$(file_prefix)$(id).stl")
end
end
hole_ids_to_meshes(hole_ids_file::String, file_prefix::String, jy::Int, jx::Int, jz::Int) = begin
cell_pos = Point3f(500f0 * (jx-1), 500f0 * (jy-1), 500f0 * (jz-1))
hole_ids_to_meshes(hole_ids_file, file_prefix, cell_pos)
end