Line data Source code
1 8 : function readmesh(filename::AbstractString, template::Mesh;
2 : exceptions::Bool=false,
3 : position=Val(:x),
4 : normal=nothing,
5 : texcoord=Val(:u),
6 : color=Val(:c),
7 : real_type=Float64, index_type=Int32, color_type=RGBA,
8 : kargs...)
9 8 : mformat = meshformat(filename)
10 :
11 4 : try
12 4 : IOUtilities.open(filename) do f
13 4 : readmesh(f, mformat, template;
14 : position, normal, texcoord, color,
15 : real_type, index_type, color_type,
16 : kargs...)
17 : end
18 : catch e
19 0 : exceptions && rethrow()
20 0 : Base.showerror(stderr, e, Base.catch_backtrace())
21 :
22 : # NOTE: stay "type stable" and return empty mesh
23 0 : return similar(template)
24 : end
25 : end
26 :
27 8 : function writemesh(filename::AbstractString, mesh::Mesh;
28 : exceptions::Bool=false,
29 : position=Val(:x),
30 : normal=nothing,
31 : texcoord=Val(:u),
32 : color=Val(:c),
33 : format::String="%g",
34 : kargs...)
35 :
36 8 : mformat = meshformat(filename)
37 :
38 4 : try
39 4 : IOUtilities.open(filename, "w+") do f
40 4 : writemesh(f, mformat, mesh;
41 : position, normal, texcoord, color, format,
42 : kargs...)
43 : end
44 : catch e
45 0 : rm(filename; force=true)
46 0 : exceptions && rethrow()
47 0 : Base.showerror(stderr, e, Base.catch_backtrace())
48 :
49 0 : return false
50 : end
51 :
52 4 : return true
53 : end
54 :
55 : """
56 : mesh = readmesh(io, format, template=createmesh(format); ...)
57 :
58 : Read [`AbstractMeshFormat`](@ref) `format` from stream `io` in and
59 : construct `mesh` of same type as the `template`, which remains
60 : unchanged.
61 :
62 : mesh = readmesh(filename, template[; exceptions=false, ...])
63 :
64 : Read `mesh` from `filename`.
65 :
66 : !!! note
67 : When reading from a file, the format is determined "dynamically"
68 : from the file name. In this case, `readmesh` expects a `template`
69 : that should "statically" determine the `Mesh` type.
70 :
71 : Although you can use [`createmesh`](@ref), it is not recommended
72 : as dynamic typing may significantly deteriorate performance!
73 :
74 : Print an error message **on failure** and return empty mesh, or
75 : rethrow exception to caller if `exceptions=true`.
76 :
77 : # Optional keyword arguments
78 :
79 : | keyword argument | default name / value | attribute |
80 : |------------------|----------------------|-------------------------|
81 : | `positition` | `:x` | vertex position |
82 : | `normal` | `:n` / `nothing` | vertex or face normal |
83 : | `texcoord` | `:u` | texture coordinate |
84 : | `color` | `:c` | vertex or face color |
85 : | `real_type` | `Float64` | all floating point data |
86 : | `index_type` | `Int32` | *use default* |
87 : | `color_type` | `RGBA` | 32 bit RGBA type |
88 :
89 : See also [`writemesh`](@ref), [`createmesh`](@ref)
90 : """ readmesh
91 :
92 : """
93 : writemesh(io, format, mesh[; ...]) :: Bool
94 :
95 : Write `mesh` to stream `io` in [`AbstractMeshFormat`](@ref) `format`.
96 : Raise an error on failure, return `nothing` on success.
97 :
98 : mesh = writemesh(filename, mesh,[; exceptions=false...])
99 :
100 : Write `mesh` to file `filename`.
101 :
102 : Print an error message **on failure** and return `false`, or rethrow
103 : exception to caller if `exceptions=true`. Any contents of `filename`
104 : will be removed on failure.
105 :
106 : # Optional keyword arguments
107 :
108 : | keyword argument | default name / value | attribute |
109 : |------------------|----------------------|-------------------------|
110 : | `positition` | `:x` | vertex position |
111 : | `normal` | `:n` / `nothing` | vertex or face normal |
112 : | `texcoord` | `:u` | texture coordinate |
113 : | `color` | `:c` | vertex or face color |
114 : | `format` | `"%g"` | `sprintf` format string |
115 :
116 : See also [`readmesh`](@ref), [`createmesh`](@ref)
117 : """ writemesh
118 :
119 : """
120 : mesh = createmesh(format[; ...])
121 :
122 : Create an empty "template mesh" suitable for reading meshes in
123 : [`AbstractMeshFormat`](@ref) `format`.
124 :
125 : # Optional keyword arguments
126 :
127 : | keyword argument | default name / value | attribute |
128 : |------------------|----------------------|---------------------------------|
129 : | `real_type` | `Float64` | all floating point data |
130 : | `dimx` | `Val(3)` | dimension of vertex position |
131 : | `dimu` | `Val(2)` | dimension of texture coordinate |
132 :
133 : See also [`AbstractMeshFormat`](@ref)
134 : """ createmesh
|