From c67ec5a4831135fbc92e4cdd4f4b031d0f96b41c Mon Sep 17 00:00:00 2001 From: Daniel Loos Date: Tue, 13 Feb 2024 11:47:15 +0100 Subject: [PATCH 1/3] Fix uvw coordinates in obj files --- src/io/obj.jl | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/io/obj.jl b/src/io/obj.jl index a427777..0ebf193 100644 --- a/src/io/obj.jl +++ b/src/io/obj.jl @@ -5,7 +5,7 @@ ############################## function load(io::Stream{format"OBJ"}; facetype=GLTriangleFace, - pointtype=Point3f, normaltype=Vec3f, uvtype=Vec2f) + pointtype=Point3f, normaltype=Vec3f, uvtype=Any) points, v_normals, uv, faces = pointtype[], normaltype[], uvtype[], facetype[] f_uv_n_faces = (faces, facetype[], facetype[]) @@ -26,9 +26,17 @@ function load(io::Stream{format"OBJ"}; facetype=GLTriangleFace, push!(v_normals, normaltype(parse.(eltype(normaltype), lines))) elseif "vt" == command if length(lines) == 2 - push!(uv, Vec{2, eltype(uvtype)}(parse.(eltype(uvtype), lines))) + if uvtype == Any + uvtype = Vec2f + uv = uvtype[] + end + push!(uv, Vec{2,eltype(uvtype)}(parse.(eltype(uvtype), lines))) elseif length(lines) == 3 - push!(uv, Vec{3, eltype(uvtype)}(parse.(eltype(uvtype), lines))) + if uvtype == Any + uvtype = Vec3f + uv = uvtype[] + end + push!(uv, Vec{3,eltype(uvtype)}(parse.(eltype(uvtype), lines))) else error("Unknown UVW coordinate: $lines") end @@ -155,7 +163,7 @@ function save(f::Stream{format"OBJ"}, mesh::AbstractMesh) println(io, "vn ", n[1], " ", n[2], " ", n[3]) end end - + F = eltype(faces(mesh)) for f in decompose(F, mesh) println(io, "f ", join(convert.(Int, f), " ")) From 2ad732919b8db4cef5a5efc99037b8a70e19b80d Mon Sep 17 00:00:00 2001 From: Daniel Loos Date: Tue, 13 Feb 2024 11:52:56 +0100 Subject: [PATCH 2/3] Add tests for uvw --- test/runtests.jl | 8 ++++++-- test/testfiles/cube_uvw.obj | 28 ++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 test/testfiles/cube_uvw.obj diff --git a/test/runtests.jl b/test/runtests.jl index 89103fd..5263802 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -14,7 +14,7 @@ function test_face_indices(mesh) end @testset "MeshIO" begin - dirlen = 1f0 + dirlen = 1.0f0 baselen = 0.02f0 mesh = [ Rect3f(Vec3f(baselen), Vec3f(dirlen, baselen, baselen)), @@ -104,7 +104,7 @@ end @test test_face_indices(msh) @test length(coordinates(msh)) == 72 - msh = load(joinpath(tf, "binary.ply")) + msh = load(joinpath(tf, "binary.ply")) @test length(faces(msh)) == 36 @test test_face_indices(msh) @test length(coordinates(msh)) == 72 @@ -142,6 +142,10 @@ end @test length(coordinates(msh)) == 8 @test test_face_indices(msh) + msh = load(joinpath(tf, "cube_uvw.obj")) + @test typeof(msh.uv) == Vector{Vec{3,Float32}} + @test length(msh.uv) == 8 + msh = load(joinpath(tf, "polygonal_face.obj")) @test length(faces(msh)) == 4 @test length(coordinates(msh)) == 6 diff --git a/test/testfiles/cube_uvw.obj b/test/testfiles/cube_uvw.obj new file mode 100644 index 0000000..e71298f --- /dev/null +++ b/test/testfiles/cube_uvw.obj @@ -0,0 +1,28 @@ +# Blender v2.71 (sub 0) OBJ File: '' +# www.blender.org +mtllib cube.mtl +o Cube +v 1.000000 -1.000000 -1.000000 +v 1.000000 -1.000000 1.000000 +v -1.000000 -1.000000 1.000000 +v -1.000000 -1.000000 -1.000000 +v 1.000000 1.000000 -0.999999 +v 0.999999 1.000000 1.000001 +v -1.000000 1.000000 1.000000 +v -1.000000 1.000000 -1.000000 +vt 0 0 0 +vt 1 0 0 +vt 0 1 0 +vt 0 0 1 +vt 1 1 0 +vt 0 1 1 +vt 1 0 1 +vt 1 1 1 +usemtl Material +s off +f 1 2 3 4 +f 5 8 7 6 +f 1 5 6 2 +f 2 6 7 3 +f 3 7 8 4 +f 5 1 4 8 From ada98fc61d2e060e1f153a12f5a90cfdb34f9354 Mon Sep 17 00:00:00 2001 From: Daniel Loos Date: Tue, 13 Feb 2024 11:57:21 +0100 Subject: [PATCH 3/3] Add tests for uv --- test/runtests.jl | 4 ++++ test/testfiles/cube_uv.obj | 28 ++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 test/testfiles/cube_uv.obj diff --git a/test/runtests.jl b/test/runtests.jl index 5263802..d9a7edd 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -142,6 +142,10 @@ end @test length(coordinates(msh)) == 8 @test test_face_indices(msh) + msh = load(joinpath(tf, "cube_uv.obj")) + @test typeof(msh.uv) == Vector{Vec{2,Float32}} + @test length(msh.uv) == 8 + msh = load(joinpath(tf, "cube_uvw.obj")) @test typeof(msh.uv) == Vector{Vec{3,Float32}} @test length(msh.uv) == 8 diff --git a/test/testfiles/cube_uv.obj b/test/testfiles/cube_uv.obj new file mode 100644 index 0000000..9d061c2 --- /dev/null +++ b/test/testfiles/cube_uv.obj @@ -0,0 +1,28 @@ +# Blender v2.71 (sub 0) OBJ File: '' +# www.blender.org +mtllib cube.mtl +o Cube +v 1.000000 -1.000000 -1.000000 +v 1.000000 -1.000000 1.000000 +v -1.000000 -1.000000 1.000000 +v -1.000000 -1.000000 -1.000000 +v 1.000000 1.000000 -0.999999 +v 0.999999 1.000000 1.000001 +v -1.000000 1.000000 1.000000 +v -1.000000 1.000000 -1.000000 +vt 0 0.0 +vt 0 0.1 +vt 0 0.2 +vt 0 0.3 +vt 0 0.4 +vt 0 0.5 +vt 0 0.6 +vt 0 0.7 +usemtl Material +s off +f 1 2 3 4 +f 5 8 7 6 +f 1 5 6 2 +f 2 6 7 3 +f 3 7 8 4 +f 5 1 4 8