JuliaEarth / GeoIO.jl

Load/save geospatial data compatible with the GeoStats.jl framework
https://github.com/JuliaEarth/GeoStats.jl
MIT License
21 stars 6 forks source link

Opening a .stl file #86

Closed roflmaostc closed 3 months ago

roflmaostc commented 3 months ago

Hi,

with Julia 1.10.3 and 1.13.0 I get the following error for opening a .stl file:

using GeoIO
using Meshes

fpath = "/home/user/3DBenchy.stl"
GeoIO.load(fpath)
Base.InvalidCharError{Char}('\xd7')
Stack trace

Here is what happened, the most recent locations are first:

    throw_invalid_char(c::Char) @ [char.jl:86](https://github.com/JuliaLang/julia/tree/0b4590a5507d3f3046e5bafc007cacbbfc9b310b/base/char.jl#L86)
    UInt32 @ char.jl:133
    convert @ char.jl:185
    cconvert @ essentials.jl:543
    lowercase(c::Char) @ [unicode.jl:290](https://github.com/JuliaLang/julia/tree/0b4590a5507d3f3046e5bafc007cacbbfc9b310b/base/strings/unicode.jl#L290)
    map(f::typeof(lowercase), s::String) @ [basic.jl:654](https://github.com/JuliaLang/julia/tree/0b4590a5507d3f3046e5bafc007cacbbfc9b310b/base/strings/basic.jl#L650)
    lowercase @ unicode.jl:623
    _splitline @ stl.jl:160
    (::GeoIO.var"#21#23"{Vector{Tuple{Tuple{Float64, Float64, Float64}, Tuple{Float64, Float64, Float64}, Tuple{Float64, Float64, Float64}}}, Vector{Tuple{Float64, Float64, Float64}}})(io::IOStream) @ stl.jl:37
    open(f::GeoIO.var"#21#23"{Vector{Tuple{Tuple{Float64, Float64, Float64}, Tuple{Float64, Float64, Float64}, Tuple{Float64, Float64, Float64}}}, Vector{Tuple{Float64, Float64, Float64}}}, args::String; kwargs::@Kwargs{}) @ [io.jl:396](https://github.com/JuliaLang/julia/tree/0b4590a5507d3f3046e5bafc007cacbbfc9b310b/base/io.jl#L393)
    open @ io.jl:393
    stlasciiread @ stl.jl:33
    stlraed(fname::String) @ stl.jl:11
    load(fname::String; layer::Int64, fix::Bool, kwargs::@Kwargs{}) @ load.jl:50
    load(fname::String) @ load.jl:21
    [This cell: line 1](http://localhost:1234/edit?id=647d274c-2274-11ef-263e-e155516d9bef#6eb5497e-201a-4905-91df-bceb9f8fa3d4)

    [mesh_object2 = GeoIO.load(fpath)](http://localhost:1234/edit?id=647d274c-2274-11ef-263e-e155516d9bef#6eb5497e-201a-4905-91df-bceb9f8fa3d4)

I wanted to open the common Benchy STL.

Best,

Felix

roflmaostc commented 3 months ago

The file I used in the standard Benchy, so nothing very fancy: https://www.3dbenchy.com/download/

eliascarv commented 3 months ago

You file is an "edge case", because according to the description of the STL format, binary files cannot begin with the word "solid" in their header:

Captura de tela de 2024-06-04 14-25-22

Reading the file we can see that its header starts with the word "solid":

julia> io = open("3DBenchy.stl");

julia> String(read(io, 80))
"solid Shape0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"

julia> close(io)

I think we can support this edge case in future versions of GeoIO.jl. However, for now, you can run this code to generate a new, valid version of your file:

open("3DBenchyFIXED.stl", write=true) do io_new
    foreach(_ -> write(io_new, 0x00), 1:80) # empty header
    open("3DBenchy.stl") do io_old
        skip(io_old, 80) # skip header
        write(io_new, read(io_old))
    end
end
julia> GeoIO.load("3DBenchyFIXED.stl")
225706×2 GeoTable over 225706 SimpleMesh
┌────────────────────────────────────────────┬──────────────────────────────────
│                   NORMAL                   │                                 ⋯
│                  Unknown                   │                                 ⋯
│                 [NoUnits]                  │                                 ⋯
├────────────────────────────────────────────┼──────────────────────────────────
│     (-0.996917 m, 0.0 m, 0.0784591 m)      │ Triangle((x: 6.622 m, y: -1.25  ⋯
│     (-0.996917 m, 0.0 m, 0.0784591 m)      │  Triangle((x: 6.622 m, y: -1.25 ⋯
│ (-0.996917 m, 1.34498f-15 m, 0.0784591 m)  │  Triangle((x: 6.597 m, y: -1.20 ⋯
│ (-0.996917 m, -1.1773f-15 m, 0.0784591 m)  │ Triangle((x: 6.605 m, y: -1.23  ⋯
│ (-0.996917 m, 2.22926f-16 m, 0.0784591 m)  │  Triangle((x: 6.622 m, y: 0.0 m ⋯
│ (-0.996917 m, -1.61398f-15 m, 0.0784591 m) │ Triangle((x: 6.575 m, y: -1.099 ⋯
│ (-0.996917 m, -1.75022f-15 m, 0.0784591 m) │ Triangle((x: 6.582 m, y: -1.143 ⋯
│ (-0.996917 m, 1.87249f-15 m, 0.0784591 m)  │  Triangle((x: 6.622 m, y: 0.0 m ⋯
│ (-0.996917 m, -4.26202f-15 m, 0.0784591 m) │ Triangle((x: 6.556 m, y: -0.927 ⋯
│ (-0.996917 m, -2.16594f-16 m, 0.0784591 m) │  Triangle((x: 6.562 m, y: -0.99 ⋯
│  (-0.996917 m, 2.3476f-15 m, 0.0784591 m)  │ Triangle((x: 6.622 m, y: 0.0 m, ⋯
│ (-0.996917 m, -2.44542f-16 m, 0.0784591 m) │ Triangle((x: 6.541 m, y: -0.702 ⋯
│ (-0.996917 m, 1.18079f-15 m, 0.0784591 m)  │ Triangle((x: 6.546 m, y: -0.782 ⋯
│ (-0.996917 m, 4.91879f-15 m, 0.0784591 m)  │ Triangle((x: 6.622 m, y: 0.0 m, ⋯
│ (-0.996917 m, -6.60613f-15 m, 0.0784591 m) │  Triangle((x: 6.53 m, y: -0.438 ⋯
│ (-0.996917 m, -1.04105f-15 m, 0.0784591 m) │  Triangle((x: 6.533 m, y: -0.53 ⋯
│                     ⋮                      │                                 ⋱
└────────────────────────────────────────────┴──────────────────────────────────
                                                1 column and 225690 rows omitted
eliascarv commented 3 months ago

@roflmaostc, in v1.13.4, you can load the original file without modifications.

roflmaostc commented 3 months ago

Cool, great :)!