An intuitive and fast header-only library to read/write, serialize/deserialize STL (stereolithography) files and data for C++ and Python.
Discover the staggering performance of OpenSTL in comparison to numpy-stl, meshio and stl-reader, thanks to its powerful C++ backend. See benchmark.py. Benchmark performed on an Intel i5-9600KF CPU @ 3.70GHz.
Performance gains over numpy-stl, meshio and stl-reader
Write: 1.3 to 4+ times faster
Read: 1 to 2.3+ times faster
Rotate: 1 to 12+ times faster
Note: meshio has no straightfoward way of rotating vertices, so it was not benchmarked.
pip install openstl
or pip install -U git+https://github.com/Innoptech/OpenSTL@main
import openstl
import numpy as np
# Define an array of triangles
# Following the STL standard, each triangle is defined with : normal, v0, v1, v2
quad = np.array([
[[0.0, 0.0, 1.0], [0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [1.0, 1.0, 0.0]],
[[0.0, 0.0, 1.0], [0.0, 0.0, 0.0], [0.0, 1.0, 0.0], [1.0, 1.0, 0.0]],
])
# Serialize the triangles to a file
success = openstl.write("quad.stl", quad, openstl.format.binary) # Or openstl.format.ascii (slower but human readable)
if not success:
raise Exception("Error: Failed to write to the specified file.")
# Deserialize triangles from a file
deserialized_quad = openstl.read("quad.stl")
# Print the deserialized triangles
print("Deserialized Triangles:", deserialized_quad)
import openstl
import numpy as np
quad = openstl.read("quad.stl")
# Rotation
rotation_matrix = np.array([
[0,-1, 0],
[1, 0, 0],
[0, 0, 1]
])
rotated_quad = np.matmul(rotation_matrix, quad.reshape(-1,3).T).T.reshape()
# Translation
translation_vector = np.array([1,1,1])
quad[:,1:4,:] += translation_vector # Avoid translating normals
std::ifstream file(filename, std::ios::binary);
if (!file.is_open()) {
std::cerr << "Error: Unable to open file '" << filename << "'" << std::endl;
}
// Deserialize the triangles in either binary or ASCII format
std::vector<openstl::Triangle> triangles = openstl::deserializeStl(file);
file.close();
std::ofstream file(filename, std::ios::binary);
if (!file.is_open()) {
std::cerr << "Error: Unable to open file '" << filename << "'" << std::endl;
}
std::vector<openstl::Triangle> originalTriangles{}; // User triangles
openstl::serializeStl(originalTriangles, file, openstl::StlFormat::Binary); // Or StlFormat::ASCII
if (file.fail()) {
std::cerr << "Error: Failed to write to file " << filename << std::endl;
} else {
std::cout << "File " << filename << " has been successfully written." << std::endl;
}
file.close();
std::stringstream ss;
std::vector<openstl::Triangle> originalTriangles{}; // User triangles
openstl::serializeStl(originalTriangles, ss, openstl::StlFormat::Binary); // Or StlFormat::ASCII
Include this repository with CMAKE Fetchcontent and link your executable/library to openstl::core
library.
Choose weither you want to fetch a specific branch or tag using GIT_TAG
. Use the main
branch to keep updated with the latest improvements.
include(FetchContent)
FetchContent_Declare(
openstl
GIT_REPOSITORY https://github.com/Innoptech/OpenSTL.git
GIT_TAG main
GIT_SHALLOW TRUE
GIT_PROGRESS TRUE
)
FetchContent_MakeAvailable(openstl)
Simply add stl.h to your codebase.
git clone https://github.com/Innoptech/OpenSTL
mkdir OpenSTL/build && cd OpenSTL/build
cmake -DOPENSTL_BUILD_TESTS=ON .. && cmake --build .
ctest .
C++11 or higher.