ddiakopoulos / tinyply

:earth_africa: C++11 ply 3d mesh format importer & exporter
598 stars 118 forks source link

Catch Exception on Ascii Ply #59

Open kzfile opened 2 years ago

kzfile commented 2 years ago

Hello,Is there anythong wrong in the new commit? I just clone the repository and run the example but got exception when reading example_cube-ascii.ply:

........................................................................
Now Reading: example_cube-ascii.ply
        [ply_header] Type: ascii
        [ply_header] Comment: generated by tinyply 2.3
        [ply_header] element: vertex (24)
        [ply_header]    property: x (type=float)
        [ply_header]    property: y (type=float)
        [ply_header]    property: z (type=float)
        [ply_header]    property: nx (type=float)
        [ply_header]    property: ny (type=float)
        [ply_header]    property: nz (type=float)
        [ply_header]    property: u (type=float)
        [ply_header]    property: v (type=float)
        [ply_header] element: face (12)
        [ply_header]    property: vertex_indices (type=uint) (list_type=uchar)
tinyply exception: the following property keys were not found in the header: red, green, blue, alpha,
tinyply exception: the following property keys were not found in the header: r, g, b, a,
tinyply exception: the element key was not found in the header: tristrips
Caught tinyply exception: unexpected EOF. malformed file?
........................................................................
Now Reading: example_cube-binary.ply
        [ply_header] Type: binary
        [ply_header] Comment: generated by tinyply 2.3
        [ply_header] element: vertex (24)
        [ply_header]    property: x (type=float)
        [ply_header]    property: y (type=float)
        [ply_header]    property: z (type=float)
        [ply_header]    property: nx (type=float)
        [ply_header]    property: ny (type=float)
        [ply_header]    property: nz (type=float)
        [ply_header]    property: u (type=float)
        [ply_header]    property: v (type=float)
        [ply_header] element: face (12)
        [ply_header]    property: vertex_indices (type=uint) (list_type=uchar)
tinyply exception: the following property keys were not found in the header: red, green, blue, alpha,
tinyply exception: the following property keys were not found in the header: r, g, b, a,
tinyply exception: the element key was not found in the header: tristrips
        parsing 0.001217mb in 4.04e-05 seconds [30.1238 MBps]
        Read 24 total vertices
        Read 24 total vertex normals
        Read 24 total vertex texcoords
        Read 12 total faces (triangles)

Same exception occured when I try another ascii ply. I compiled the code on windows msvc/wsl2 and no one was spared.

guban commented 1 year ago

The "unexpected EOF. malformed file?" exception happens when opening a well-formed ASCII file, such as this file: https://people.sc.fsu.edu/~jburkardt/data/ply/airplane.ply. The exception happens in tinyply 2.3.4, but not in tinyply 2.3.2. It is possible that the problem was introduced in 2.3.4 by the code that avoids crashes when reading incomplete files (the ones with less data than declared in the header).

3Descape commented 1 year ago

Hello. I stil get the exception "Caught tinyply exception: unexpected EOF. malformed file?" e.g. when reading files exported with Blender. Intrestingly enough it works if the mesh is triangles only(e.g. applying a triangulate modifier before exporting). The same mesh with quads does not work. I the testfiles.zip file you'll find the same mesh, once triangulated and once with quads. The one with quads should trigger the exception.

testfiles.zip

3Descape commented 1 year ago

okay, I have some more information: It seems to be caused by a wrong user list hint. e.g. faces = file.request_properties_from_element("face", { "vertex_indices" }, 3); when the list actually has 4 elements per list. Setting faces = file.request_properties_from_element("face", { "vertex_indices" }, 0); works and the error disappears. However, since it is called only "hint"(as to "try this value first and fall back to finding it out ourselfs it that didn't work"), shouldn't there be some validation/fallback, if the user provided hint does not correspond with the size of the actual list in the .ply file? E.g. something along the lines of

try {
    parse_data(is, false);
} catch(Exception e) {
    parse_data(is, true);
}

in tinyply.h line 544...