diff --git a/src/MeshIO.jl b/src/MeshIO.jl index 01d0c24..5de7c83 100644 --- a/src/MeshIO.jl +++ b/src/MeshIO.jl @@ -17,6 +17,7 @@ include("io/2dm.jl") include("io/msh.jl") include("io/gts.jl") include("io/ifs.jl") +include("io/inp.jl") """ load(fn::File{MeshFormat}; pointtype=Point3f, uvtype=Vec2f, diff --git a/src/io/inp.jl b/src/io/inp.jl new file mode 100644 index 0000000..637e2bd --- /dev/null +++ b/src/io/inp.jl @@ -0,0 +1,36 @@ +# read .inp files +function load(fs::Stream{format"INP"}; facetype=GLTriangleFace, pointtype=Point3f) + #INP file format + io = stream(fs) + + points = pointtype[] + faces = facetype[] + node_idx = Int[] + + # read the first 3 lines if there is the "*heading" keyword + line = readline(io) + contains(line,"*heading") && (line = readline(io)) + BlockType = contains(line,"*NODE") ? Val{:NodeBlock}() : Val{:DataBlock}() + + # read the file + while !eof(io) + line = readline(io) + BlockType, line = parse_blocktype!(BlockType, io, line) + if BlockType == Val{:NodeBlock}() + push!(node_idx, parse(Int,split(line,",")[1])) # keep track of the node index of the inp file + push!(points, pointtype(parse.(eltype(pointtype),split(line,",")[2:4]))) + elseif BlockType == Val{:ElementBlock}() + nodes = parse.(Int,split(line,",")[2:end]) + push!(faces, TriangleFace{Int}(facetype([findfirst(==(node),node_idx) for node in nodes])...)) # parse the face + else + continue + end + end + + return Mesh(points, faces) +end +function parse_blocktype!(block, io, line) + contains(line,"*NODE") && return block=Val{:NodeBlock}(),readline(io) + contains(line,"*ELEMENT") && return block=Val{:ElementBlock}(),readline(io) + return block, line +end \ No newline at end of file diff --git a/src/precompile.jl b/src/precompile.jl index 67c9c9e..2b54dc3 100644 --- a/src/precompile.jl +++ b/src/precompile.jl @@ -21,6 +21,7 @@ function _precompile_() @warnpcfail precompile(load, (File{format"PLY_BINARY",IOStream},)) @warnpcfail precompile(load, (File{format"STL_ASCII",IOStream},)) @warnpcfail precompile(load, (File{format"STL_BINARY",IOStream},)) + @warnpcfail precompile(load, (File{format"INP",IOStream},)) else @warnpcfail precompile(load, (File{format"2DM"},)) @warnpcfail precompile(load, (File{format"MSH"},)) @@ -30,6 +31,7 @@ function _precompile_() @warnpcfail precompile(load, (File{format"PLY_BINARY"},)) @warnpcfail precompile(load, (File{format"STL_ASCII"},)) @warnpcfail precompile(load, (File{format"STL_BINARY"},)) + @warnpcfail precompile(load, (File{format"INP"},)) end end diff --git a/test/runtests.jl b/test/runtests.jl index b719e27..28eaefe 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -190,9 +190,8 @@ end msh2 = expand_faceviews(Mesh(msh)) @test !(normals(msh2) isa FaceView) @test length(faces(msh2)) == 1 - @test coordinates(coordinates(msh2)[faces(msh2)[1]]) == (Vec3f(0), Vec3f(0.062805, 0.591207, 0.902102), Vec3f(0.058382, 0.577691, 0.904429)) - @test normals(msh2)[faces(msh2)[1]] == (Vec3f(0.9134, 0.104, 0.3934), Vec3f(0.8079, 0.4428, 0.3887), Vec3f(0.8943, 0.4474, 0.0)) - + @test all(coordinates(coordinates(msh2)[faces(msh2)[1]]) .==[Point3f(0), Point3f(0.062805, 0.591207, 0.902102), Point3f(0.058382, 0.577691, 0.904429)]) + @test all(normals(msh2)[faces(msh2)[1]] .== [Vec3f(0.9134, 0.104, 0.3934), Vec3f(0.8079, 0.4428, 0.3887), Vec3f(0.8943, 0.4474, 0.0)]) # test that save works with FaceViews mktempdir() do tmpdir save(joinpath(tmpdir, "test.obj"), msh) @@ -264,5 +263,14 @@ end @test material["diffuse map"] isa Dict{String, Any} @test material["diffuse map"]["filename"] == replace(joinpath(tf, "mini sponza/SP_LUK.JPG"), '\\' => '/') end + + @testset "INP" begin + msh = load(joinpath(tf, "cube.inp")) + + @test length(faces(msh)) == 24 + @test length(coordinates(msh)) == 14 + @test msh.views == [] + @test test_face_indices(msh) + end end end diff --git a/test/testfiles/cube.inp b/test/testfiles/cube.inp new file mode 100644 index 0000000..f45335b --- /dev/null +++ b/test/testfiles/cube.inp @@ -0,0 +1,40 @@ +*NODE +1, -0.5, -0.5, 0.5 +2, -0.5, -0.5, -0.5 +3, -0.5, 0.5, 0.5 +4, -0.5, 0.5, -0.5 +5, 0.5, -0.5, 0.5 +6, 0.5, -0.5, -0.5 +7, 0.5, 0.5, 0.5 +8, 0.5, 0.5, -0.5 +9, -0.5, 0.0, 0.0 +10, 0.5, 0.0, 0.0 +11, 0.0, -0.5, 0.0 +12, 0.0, 0.5, 0.0 +13, 0.0, 0.0, -0.5 +14, 0.0, 0.0, 0.5 +*ELEMENT, type=CPS3, ELSET=Surface1 +21, 2, 1, 9 +22, 1, 3, 9 +23, 4, 2, 9 +24, 3, 4, 9 +25, 6, 10, 5 +26, 5, 10, 7 +27, 8, 10, 6 +28, 7, 10, 8 +29, 1, 2, 11 +30, 5, 1, 11 +31, 2, 6, 11 +32, 6, 5, 11 +33, 3, 12, 4 +34, 7, 12, 3 +35, 4, 12, 8 +36, 8, 12, 7 +37, 2, 4, 13 +38, 6, 2, 13 +39, 4, 8, 13 +40, 8, 6, 13 +41, 1, 14, 3 +42, 5, 14, 1 +43, 3, 14, 7 +44, 7, 14, 5