Skip to content

Commit

Permalink
Merge pull request #45 from goretkin/fix-stl-nonormal
Browse files Browse the repository at this point in the history
Fix loading Binary STL into mesh without normals
  • Loading branch information
SimonDanisch authored Sep 13, 2018
2 parents 553daea + aea9368 commit 4a3f364
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/io/stl.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,29 +64,40 @@ function load(fs::Stream{format"STL_BINARY"}, MeshType=GLNormalMesh)
#https://en.wikipedia.org/wiki/STL_%28file_format%29#Binary_STL
io = stream(fs)
read(io, 80) # throw out header

triangle_count = read(io, UInt32)
FaceType = facetype(MeshType)
VertexType = vertextype(MeshType)
NormalType = normaltype(MeshType)

faces = Array{FaceType}(undef, triangle_count)
vertices = Array{VertexType}(undef, triangle_count*3)
normals = Array{NormalType}(undef, triangle_count*3)
if hasnormals(MeshType)
normals = Array{NormalType}(undef, triangle_count*3)
end
i = 0
while !eof(io)
faces[i+1] = Face{3, ZeroIndex{Int}}(i*3+1, i*3+2, i*3+3)
normals[i*3+1] = NormalType(read(io, Float32), read(io, Float32), read(io, Float32))
normals[i*3+2] = normals[i*3+1] # hurts, but we need per vertex normals
normals[i*3+3] = normals[i*3+1]
normal = (read(io, Float32), read(io, Float32), read(io, Float32))

if hasnormals(MeshType)
normals[i*3+1] = NormalType(normal...)
normals[i*3+2] = normals[i*3+1] # hurts, but we need per vertex normals
normals[i*3+3] = normals[i*3+1]
end

vertices[i*3+1] = VertexType(read(io, Float32), read(io, Float32), read(io, Float32))
vertices[i*3+2] = VertexType(read(io, Float32), read(io, Float32), read(io, Float32))
vertices[i*3+3] = VertexType(read(io, Float32), read(io, Float32), read(io, Float32))

skip(io, 2) # throwout 16bit attribute
i += 1
end
return MeshType(vertices=vertices, faces=faces, normals=normals)

if hasnormals(MeshType)
return MeshType(vertices=vertices, faces=faces, normals=normals)
else
return MeshType(vertices=vertices, faces=faces)
end
end


Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ end
save(File(format"STL_BINARY", joinpath(tmpdir, "test.stl")), mesh)
mesh_loaded = load(joinpath(tmpdir, "test.stl"), GLNormalMesh)
#@test mesh_loaded == mesh
mesh_loaded = load(joinpath(tmpdir, "test.stl"), GLPlainMesh)
end
end
@testset "Real world files" begin
Expand Down

0 comments on commit 4a3f364

Please sign in to comment.