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

Gts #48

Merged
merged 3 commits into from
Dec 4, 2021
Merged

Gts #48

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
1 change: 1 addition & 0 deletions src/MeshIO.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ include("io/ply.jl")
include("io/stl.jl")
include("io/obj.jl")
include("io/2dm.jl")
include("io/gts.jl")

load(fn::File{format}, MeshType=GLNormalMesh) where {format} = open(fn) do s
skipmagic(s)
Expand Down
64 changes: 64 additions & 0 deletions src/io/gts.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
function parseGtsLine( s::AbstractString, C, T=eltype(C) )
firstInd = findfirst( isequal(' '), s )
secondInd = findnext( isequal(' '), s, firstInd+1 )
firstNum = parse( T, s[1:firstInd] )
if secondInd != nothing
secondNum = parse( T, s[firstInd:secondInd] )
thirdNum = parse( T, s[secondInd:end] )
return C([firstNum, secondNum, thirdNum])
else
secondNum = parse( T, s[firstInd:end] )
return C([firstNum, secondNum])
end
end

function load( st::Stream{format"GTS"}, MeshType=GLNormalMesh )
io = stream(st)
head = readline( io )
FT = facetype(MeshType)
VT = vertextype(MeshType)

nVertices, nEdges, nFacets = parseGtsLine( head, Tuple{Int,Int,Int} )
iV = iE = iF = 1
vertices = Vector{VT}(undef, nVertices)
edges = Vector{Vector{Int}}(undef, nEdges)
facets = Vector{Vector{Int}}(undef, nFacets)
for full_line::String in eachline(io)
# read a line, remove newline and leading/trailing whitespaces
line = strip(chomp(full_line))
!isascii(line) && error("non valid ascii in obj")

if !startswith(line, "#") && !isempty(line) && !all(iscntrl, line) #ignore comments
if iV <= nVertices
vertices[iV] = parseGtsLine( line, VT )
iV += 1
elseif iV > nVertices && iE <= nEdges
edges[iE] = parseGtsLine( line, Array{Int} )
iE += 1
elseif iE > nEdges && iF <= nFacets
facets[iF] = parseGtsLine( line, Array{Int} )
iF += 1
end # if
end # if
end # for
faces = [ FT( union( edges[facets[i][1]], edges[facets[i][2]], edges[facets[i][3]] ) ) for i in 1:length(facets) ] # orientation not guaranteed
return MeshType( vertices, faces )
end

function save( st::Stream{format"GTS"}, mesh::AbstractMesh )
# convert faces to edges and facets
edges = [[ 0, 0 ]] # TODO
facets = [[ 0, 0 ]] # TODO
# write to file
io = stream( st )
println( io, length(mesh.vertices), legth(edges), length(mesh.faces) )
for v in mesh.vertices
println( io, v[1], v[2], v[3] )
end
for e in edges
println( io, e[1], e[2] )
end
for f in facets
println( io, f[1], f[2], f[3] )
end
end
5 changes: 5 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@ end
#@test length(vertices(msh)) == 2248
#@test length(normals(msh)) == 2248
end
@testset "GTS" begin
msh = load(joinpath(tf, "sphere5.gts"))
@test typeof(msh) == GLNormalMesh
test_face_indices(msh)
end
end
end

Expand Down
Loading