wolph / numpy-stl

Simple library to make working with STL files (and 3D objects in general) fast and easy.
http://numpy-stl.readthedocs.org/
BSD 3-Clause "New" or "Revised" License
624 stars 105 forks source link

ASCII/binary detection issues #156

Closed hakostra closed 3 years ago

hakostra commented 3 years ago

I just came across some STL files generated by the computational fluid dynamics software Star-CCM+, where numpy-stl fail to load the ASCII STL's.

This old bug in OpenFOAM have two simple examples attached: https://bugs.openfoam.org/view.php?id=516 see the file "cubes.zip". I attach the same file here as a reference: cubes.zip

The ASCII STL's start with " solid", i.e. a whitespace before the word "solid". This puzzles the automatic ASCII/binary detection in numpy-stl and it is attempted to read the file as binary, which fails.

The best descriptions I have found for the STL file format(s) are Wikipedia [1] and a page on fabbers.com [2]. Neither of them are perfectly clear on the validity of this file(s) coming from Star-CCM+... These resources (Wikipedia, Fabbers) are also sometimes in conflict, Wikipedia list tabs as examples of allowed whitespace characters and Fabbers explicitly write that this is not allowed. Fabbers, being an old resource, write that all coordinates must be non-negative, which is rarely enforced today. Wikipedia write that the file must start with the line "solid name", but later they state that whitespace may be used anywhere in the file except within numbers or words...

I then in the end conclude that whitespace are also allowed before the first "solid", and that both of the STL's in the attached file are valid STL's (even though both are really stupid in their own ways).

First I would like to know your thoughts on the validity of the attached STL's.

Then, secondly I do not like the current method of reading ASCII/binary STL's. I can force the reading of a binary STL with mode=Mode.BINARY, but the same is not true for Mode.ASCII, since by code design in stl.py:70 it nevertheless will jump to binary reading, since the first five characters of the file is not "solid"...

What would be the opinions on really having three modes: ASCII, binary and automatic? In ASCII mode it would never fall back to binary, in binary mode it would never fall back to ASCII and in automatic it would make an educated guess on the mode based on the file structure, and possibly fall back to the other mode if the first is failing. Opinions?

It can be possible for me to provide some pull request solving some of this, but I would like to hear your opinion first.

[1] https://en.wikipedia.org/wiki/STL_(file_format) [2] http://www.fabbers.com/tech/STL_Format

wolph commented 3 years ago

The STL files might be slightly different from what I've come across so far, but as long as it won't break other scenarios, I am perfectly ok with accepting these as valid as well. I believe the fix would be fairly trivial as well. I think most of the code already supports whitespace but solid is the one exception.

As for the modes, I think you're right. That if statement is wrong. It should end up at line 76 instead. https://github.com/WoLpH/numpy-stl/blob/9bd9594354f1191f46c17341700d14b2df56afed/stl/stl.py#L70-L77

If you're willing to make a pull request that would be greatly appreciated. If not than I'll fix it myself as soon as I have a bit of time. It shouldn't be all that much work I guess

hakostra commented 3 years ago

Thanks. I will see if I manage to make a PR next week.