Skip to content
Closed
Show file tree
Hide file tree
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
2 changes: 0 additions & 2 deletions src/MeshIO.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ using FileIO: FileIO, @format_str, Stream, File, stream, skipmagic

import Base.show

include("util.jl")

include("io/off.jl")
include("io/ply.jl")
include("io/stl.jl")
Expand Down
63 changes: 26 additions & 37 deletions src/io/obj.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
##############################

function load(io::Stream{format"OBJ"}; facetype=GLTriangleFace,
pointtype=Point3f, normaltype=Vec3f, uvtype=Any)
pointtype=Point3f, normaltype=Vec3f, uvtype=Vec2f)

points, v_normals, uv, faces = pointtype[], normaltype[], uvtype[], facetype[]
f_uv_n_faces = (faces, facetype[], facetype[])
points, v_normals, uv, faces = pointtype[], normaltype[], uvtype[], Any[]
last_command = ""
attrib_type = nothing
for full_line in eachline(stream(io))
Expand Down Expand Up @@ -43,55 +42,45 @@ function load(io::Stream{format"OBJ"}; facetype=GLTriangleFace,
elseif "f" == command # mesh always has faces
if any(x-> occursin("//", x), lines)
fs = process_face_normal(lines)
pos_faces = triangulated_faces(facetype, getindex.(fs, 1))
normal_faces = triangulated_faces(facetype, getindex.(fs, 2))
append!(faces, GeometryBasics.NormalFace.(pos_faces, normal_faces))

elseif any(x-> occursin("/", x), lines)
fs = process_face_uv_or_normal(lines)
pos_faces = triangulated_faces(facetype, getindex.(fs, 1))
uv_faces = triangulated_faces(facetype, getindex.(fs, 2))
if length(fs[1]) == 2
append!(faces, GeometryBasics.UVFace.(pos_faces, uv_faces))
else
normal_faces = triangulated_faces(facetype, getindex.(fs, 3))
append!(faces, GeometryBasics.NormalUVFace.(pos_faces, normal_faces, uv_faces))
end
else
append!(faces, triangulated_faces(facetype, lines))
continue
end
for i = 1:length(first(fs))
append!(f_uv_n_faces[i], triangulated_faces(facetype, getindex.(fs, i)))
end
else
#TODO
end
end
end

point_attributes = Dict{Symbol, Any}()
non_empty_faces = filtertuple(!isempty, f_uv_n_faces)

# Do we have faces with different indices for positions and normals
# (and texture coordinates) per vertex?
if length(non_empty_faces) > 1

# map vertices with distinct indices for possition and normal (and uv)
# to new indices, updating faces along the way
faces, attrib_maps = merge_vertex_attribute_indices(non_empty_faces)

# Update order of vertex attributes
points = points[attrib_maps[1]]
counter = 2
if !isempty(uv)
point_attributes[:uv] = uv[attrib_maps[counter]]
counter += 1
end
if !isempty(v_normals)
point_attributes[:normals] = v_normals[attrib_maps[counter]]
end
vertex_attributes = Dict{Symbol, Any}()

else # we have vertex indexing - no need to remap
# TODO: add GeometryBasics convenience for dropping nothing vertex attributes?
if !isempty(v_normals)
vertex_attributes[:normal] = v_normals
end

if !isempty(v_normals)
point_attributes[:normals] = v_normals
end
if !isempty(uv)
point_attributes[:uv] = uv
end

if !isempty(uv)
vertex_attributes[:uv] = uv
end

return Mesh(meta(points; point_attributes...), faces)
# TODO: Can we avoid this conversion?
# Also, is it safe to do? Or can an obj file define different face types for different groups?
faces = convert(Vector{typeof(first(faces))}, faces)

return GeometryBasics.mesh(points, faces, facetype = facetype; vertex_attributes...)
end

# of form "faces v1 v2 v3 ....""
Expand Down
4 changes: 2 additions & 2 deletions src/io/stl.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ function load(fs::Stream{format"STL_BINARY"}; facetype=GLTriangleFace,
i += 1
end

return Mesh(meta(vertices; normals=normals), faces)
return Mesh(vertices, faces; normal = normals)
end


Expand Down Expand Up @@ -127,5 +127,5 @@ function load(fs::Stream{format"STL_ASCII"}; facetype=GLTriangleFace,
push!(faces, TriangleFace{Int}(vert_idx...))
end
end
return Mesh(meta(points; normals=normals), faces)
return Mesh(points, faces; normal = normals)
end
56 changes: 0 additions & 56 deletions src/util.jl

This file was deleted.