Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save STL #588

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/preprocessing/geometries/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,41 @@ function trixi2vtk(geometry::TriangleMesh; output_directory="out", prefix="",
return trixi2vtk(stack(geometry.vertices); output_directory, filename, prefix,
vertex_normals=vertex_normals, custom_quantities...)
end

function save(filename, mesh; faces=eachface(mesh))
save(FileIO.File{FileIO.format"STL_BINARY"}(filename), mesh; faces)
end

function save(fn::FileIO.File{FileIO.format"STL_BINARY"},
mesh::TriangleMesh; faces=eachface(mesh))
open(fn, "w") do s
save(s, mesh; faces)
end
end

function save(f::FileIO.Stream{FileIO.format"STL_BINARY"},
mesh::TriangleMesh; faces)
io = FileIO.stream(f)
points = mesh.face_vertices
normals = mesh.face_normals

# Implementation made according to https://en.wikipedia.org/wiki/STL_%28file_format%29#Binary_STL
for i in 1:80 # Write empty header
write(io, 0x00)
end

write(io, UInt32(length(faces))) # Write triangle count
for i in faces
n = SVector{3, Float32}(normals[i])
triangle = points[i]

for j in 1:3
write(io, n[j])
end

for point in triangle, p in point
write(io, Float32(p))
end
write(io, 0x0000) # 16 empty bits
end
end
Loading