Line data Source code
1 : """ 2 : writeoffmesh(io, mesh[; position=Val(:x), format="%g"]) 3 : 4 : Write `mesh` to `io` in [OFF format](https://paulbourke.net/dataformats/oogl/#OFF) 5 : using attributes 6 : 7 : - `position` 8 : 9 : !!! note 10 : The current version of `writeoffmesh` supports only the vertex 11 : `position` attribute. 12 : 13 : Pass `nothing` to ignore an *optional* attribute. Attributes the are 14 : disabled are also ignored, i.e., not written. 15 : 16 : The optional `format` argument specifies the output format of a 17 : floating point number as a `String` using `sprintf`-syntax. 18 : 19 : !!! note 20 : This method is called by [`writemesh`](@ref). 21 : 22 : See also [`writemesh`](@ref), [`readoffmesh`](@ref) 23 : """ writeoffmesh 24 : 25 2 : function writeoffmesh(io::IO, mesh::Mesh{V, F, H, E}; 26 : position=Val(:x), 27 : texcoord=nothing, 28 : normal=nothing, 29 : color=nothing, 30 : format::String="%g") where {V, F, H, E} 31 : 32 1 : isnothing(normal) || @warn "'normal' is not supported by writeoffmesh" 33 1 : isnothing(texcoord) || @warn "'texcoord' is not supported by writeoffmesh" 34 1 : isnothing(color) || @warn "'color' is not supported by writeoffmesh" 35 : 36 : # TODO: improve efficiency 37 : 38 1 : print(io, "OFF\n$(PMesh.nv(mesh)) $(PMesh.nf(mesh)) 0\n") 39 34835 : vidx = zeros(Int, PMesh.n_total(vattr(mesh))) 40 : 41 1 : ax = vattr(mesh, position) 42 1 : isenabled(ax) || error("require vertex position") 43 : 44 3 : x = zeros(3) 45 1 : d = min(3, length(zeros(eltype(ax)))) # TODO: prefer using helpers 46 : 47 2 : for (i, v) in enumerate(vertices(mesh)) 48 34835 : vidx[Int(v)] = i 49 : # Note: ax is not necessarily a 3-vector 50 34835 : for j in 1:d 51 104505 : x[j] = ax[v][j] 52 174175 : end 53 34835 : print(io, "$(x[1]) $(x[2]) $(x[3])\n") 54 69669 : end 55 : 56 2 : for f in faces(mesh) 57 69666 : vtx = collect(vertices(mesh, f)) 58 69666 : print(io, length(vtx)) 59 69666 : for v in vtx 60 208998 : idx = vidx[Int(v)] 61 208998 : @assert idx > 0 62 208998 : print(io, " $(idx-1)") 63 208998 : end 64 69666 : print(io, "\n") 65 139331 : end 66 : 67 1 : nothing 68 : end