Skip to content

Commit

Permalink
[Format] Add writeobj support (Wavefront OBJ)
Browse files Browse the repository at this point in the history
  • Loading branch information
tkemmer committed Jul 12, 2022
1 parent e5ca897 commit 3a27de1
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 0 deletions.
6 changes: 6 additions & 0 deletions docs/src/formats/output.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Currently supported output file formats with different models:
| File type | Point cloud | Surface model | Volume model | Charges included |
|--------------------|:-----------:|:-------------:|:------------:|:----------------:|
| [HMO](@ref hmoout) | || ||
| [OBJ](@ref) | || | |
| [SKEL](@ref) | ||| |
| [STL](@ref stlout) | || | |
| [VTK](@ref) | ||| |
Expand All @@ -20,6 +21,11 @@ Currently supported output file formats with different models:
writehmo
```

## OBJ
```@docs
writeobj
```

## SKEL
```@docs
writeskel
Expand Down
3 changes: 3 additions & 0 deletions src/Format.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ export readmcsf
include("format/msms.jl")
export readmsms

include("format/obj.jl")
export writeobj

include("format/off.jl")
export readoff

Expand Down
51 changes: 51 additions & 0 deletions src/format/obj.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# =========================================================================================
"""
function writeobj{T}(
stream::IOStream,
model ::Model{T, Triangle{T}}
)
Creates a Wavefront OBJ file from a given surface model.
# Specification
<https://www.loc.gov/preservation/digital/formats/fdd/fdd000507.shtml>
<http://paulbourke.net/dataformats/obj/>
# Return type
`Void`
# Alias
writeobj{T}(
fname::String,
model::Model{T, Triangle{T}}
)
Creates the OBJ file by name rather than `IOStream` object.
"""
function writeobj(
stream::IOStream,
model ::Model{T, Triangle{T}}
) where T
# nodes
for node in model.nodes
println(stream, "v ", node[1], " ", node[2], " ", node[3])
end
# vertex normals
vns = vertexnormals(model)
for vn in vns
println(stream, "vn ", vn[1], " ", vn[2], " ", vn[3])
end
# elements
ridx = reverseindex(model.nodes)
for elm in model.elements
print(stream, "f ")
print(stream, ridx[objectid(elm.v1)], "//", ridx[objectid(elm.v1)], " ")
print(stream, ridx[objectid(elm.v2)], "//", ridx[objectid(elm.v2)], " ")
println(stream, ridx[objectid(elm.v3)], "//", ridx[objectid(elm.v3)])
end
end

function writeobj(fname::String, model::Model{T, Triangle{T}}) where T
open(fh -> writeobj(fh, model), fname, "w")
end
3 changes: 3 additions & 0 deletions test/format/obj.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@testset "writeobj" begin
@test_skip writeobj
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const tests = [
"format/hmo.jl",
"format/mcsf.jl",
"format/msms.jl",
"format/obj.jl",
"format/off.jl",
"format/pqr.jl",
"format/skel.jl",
Expand Down

0 comments on commit 3a27de1

Please sign in to comment.