Skip to content

Commit

Permalink
Add ASCII reader (#22)
Browse files Browse the repository at this point in the history
* add ASCII reader

* improve performance of ASCII STL reader; update README
  • Loading branch information
akaszynski authored Nov 26, 2024
1 parent c764dcd commit 95cc552
Show file tree
Hide file tree
Showing 6 changed files with 380 additions and 20 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ CMakeCache.txt
CMakeFiles/
Makefile
cmake_install.cmake
compile_commands.json
compile_commands.json
.cache/
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ extensions.
If using emacs and helm, generate the project configuration files using `-DCMAKE_EXPORT_COMPILE_COMMANDS=ON`. Here's a sample configuration for C++11 on Linux:

```
pip install nanobind
pip install nanobind -U
export NANOBIND_INCLUDE=$(python -c "import nanobind, os; print(os.path.join(os.path.dirname(nanobind.__file__), 'cmake'))")
cmake -Dnanobind_DIR=$NANOBIND_INCLUDE -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_CXX_STANDARD_INCLUDE_DIRECTORIES="/usr/include/c++/11;/usr/include/x86_64-linux-gnu/c++/11/;/usr/lib/gcc/x86_64-linux-gnu/11/include/"
```
Expand Down
38 changes: 36 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
.. |MIT| image:: https://img.shields.io/badge/License-MIT-yellow.svg
:target: https://opensource.org/licenses/MIT

``stl-reader`` is a Python library for raipidly reading binary STL
files. It wraps a `nanobind
``stl-reader`` is a Python library for rapidly reading binary and ASCII
STL files. It wraps a `nanobind
<https://nanobind.readthedocs.io/en/latest/>`_ interface to the fast STL
library provided by `libstl <https://github.com/aki5/libstl>`_. Thanks
@aki5!
Expand Down Expand Up @@ -169,6 +169,40 @@ Here's an additional benchmark comparing VTK with ``stl-reader``:

.. image:: https://github.com/pyvista/stl-reader/raw/main/bench1.png

Read in ASCII Meshes
====================

The `stl-reader` also supports ASCII files and is around 2.4 times
faster than VTK at reading ASCII files.

.. code:: python
import time
import stl_reader
import pyvista as pv
import numpy as np
# create and save a ASCII file
n = 1000
mesh = pv.Plane(i_resolution=n, j_resolution=n).triangulate()
mesh.save("/tmp/tmp-ascii.stl", binary=False)
# stl reader
tstart = time.time()
mesh = stl_reader.read_as_mesh("/tmp/tmp-ascii.stl")
print("stl-reader ", time.time() - tstart)
tstart = time.time()
pv_mesh = pv.read("/tmp/tmp-ascii.stl")
print("pyvista reader", time.time() - tstart)
# verify meshes are identical
assert np.allclose(mesh.points, pv_mesh.points)
# approximate time to read in the 1M point file:
# stl-reader 0.80303955078125
# pyvista reader 1.916085958480835
*****************************
License and Acknowledgments
*****************************
Expand Down
9 changes: 0 additions & 9 deletions src/stl_reader/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@
from pyvista.core.pointset import PolyData


def _check_stl_ascii(filename: str) -> bool:
"""Check if a STL is ASCII."""
with open(filename, "rb") as fid:
return fid.read(5) == b"solid"


def _polydata_from_faces(points: npt.NDArray[float], faces: npt.NDArray[int]) -> "PolyData":
"""Generate a polydata from a faces array containing no padding and all triangles.
Expand Down Expand Up @@ -99,9 +93,6 @@ def read(filename: str) -> Tuple[npt.NDArray[np.float32], npt.NDArray[np.uint32]
[9005998, 9005999, 9005995]], dtype=uint32)
"""
if _check_stl_ascii(filename):
raise RuntimeError("stl-reader only supports binary STL files")

return _stlfile_wrapper.get_stl_data(filename)


Expand Down
Loading

0 comments on commit 95cc552

Please sign in to comment.