A simple STL serializer and deserializer
Project description
OpenSTL
An intuitive and fast header-only library to read/write, serialize/deserialize STL (stereolithography) files and data for C++ and Python.
Performances benchmark
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.
Python Usage
Install
pip install openstl
or pip install -U git+https://github.com/Innoptech/OpenSTL@main
Read and write from a STL file
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)
Rotate and translate a mesh
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
C++ Usage
Read STL from file
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();
Write STL to a file
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::serialize(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();
Serialize STL to a stream
std::stringstream ss;
std::vector<openstl::Triangle> originalTriangles{}; // User triangles
openstl::serialize(originalTriangles, ss, openstl::StlFormat::Binary); // Or StlFormat::ASCII
Integrate to your codebase
Smart method
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)
Naïve method
Simply add stl.h to your codebase.
Test
git clone https://github.com/Innoptech/OpenSTL
mkdir OpenSTL/build && cd OpenSTL/build
cmake -DOPENSTL_BUILD_TESTS=ON .. && cmake --build .
ctest .
Requirements
C++11 or higher.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
Hashes for openstl-1.0.2-pp310-pypy310_pp73-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 509359854fcc102197c8135dd3f66a3994e123eddd4d86c53c2d4246054951c1 |
|
MD5 | 8b762184d5ac208df7cfbe5e52efef77 |
|
BLAKE2b-256 | a775fff9a3163e5c8bd1cf83c12c83850c9b95ea8cf61599fd48f437d4576a2d |
Hashes for openstl-1.0.2-pp39-pypy39_pp73-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d59fa3f5ded0e66ac16e4ba8633db8cbf8e9c2a22bde3f9114061e3aba6da9f6 |
|
MD5 | ad352ad864b60dbaf45107a0f9dce4dd |
|
BLAKE2b-256 | 11108ad63d9f2d1fe59ebca8eabc95832056530a90e0ce8e1092792dc19be48e |
Hashes for openstl-1.0.2-pp38-pypy38_pp73-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ab56383fba1ed10f5fac207cb0d8b2c1f44192fdd335696a109d8842e20caba1 |
|
MD5 | 878596d27ceaad5f9dc655aeb1671e89 |
|
BLAKE2b-256 | 65139ea81f3cadc63222c36c031d8a335c75c023ed6ef662973d5d8ba32b6d1f |
Hashes for openstl-1.0.2-pp37-pypy37_pp73-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 12e1698e2961256a55e8b1daf725440cc6bc7b5d99d99dd7821de582a3914045 |
|
MD5 | 027cdc4665beb2263431861c7b0cd34e |
|
BLAKE2b-256 | d824702f501325d5092cdda758bddeec4d66564b8d65b77de4858425d5430d75 |
Hashes for openstl-1.0.2-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e89bfb11bc52ec042d94c212daed202ad3ba60f79d35162ac2705f5c55273998 |
|
MD5 | cdd2d390901d757b4194fa5be57be42d |
|
BLAKE2b-256 | 01b4328e28943436273fd7e22f2453580c520a344abe4ac95bfeaa3788ba2249 |
Hashes for openstl-1.0.2-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7d331421aa654c954da5519814cd66478001c7608e8a3d3e787168b79178083e |
|
MD5 | d08dc3e8a60048d78ea51d7d6bc09f21 |
|
BLAKE2b-256 | 1e2f3b1aca38ce05d2b021d2b63307fdacba611c7441e7e7569d2a7b11431915 |
Hashes for openstl-1.0.2-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 711f92506d4f80d574ba7a8b9b1616d299c40c36f29f4d1d1552d0851389f737 |
|
MD5 | a11086e6b15fd2e921a16ac81012919c |
|
BLAKE2b-256 | aeefa57f512f0db38259d77e8f2d067492ef5821aee7f95e5e138dfde438e24b |
Hashes for openstl-1.0.2-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e4e48b5a0cf925529d3922d9142280e2476214643da90f304b835962bdde4bf9 |
|
MD5 | 46cc0df75e371ff246ad25961bee5de5 |
|
BLAKE2b-256 | 83b2bdadc0ba104cb4fd120b64bd1a3a71104f1660213017c758c68b15bfe42f |
Hashes for openstl-1.0.2-cp38-cp38-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b5525cdaa4ec94885c1363376618c72a40ab118e1c7ea70f9ad4267d8096a2de |
|
MD5 | bb3ebee2b8533eec063a3be9763928d1 |
|
BLAKE2b-256 | dfbb8a4a37e9b1e02c3e89d58ec93daac2048f70c88123ec76e61858644b0014 |
Hashes for openstl-1.0.2-cp37-cp37m-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8d02b653c3ffcd6f23cd0bec76054868067eb607a1c09520979e57cb48498ee5 |
|
MD5 | 30baa44eb6af7e9eebfd9321c3a34585 |
|
BLAKE2b-256 | 8afdf729144286f4f3bc1ccdd48b62cb502303bed6b368f60176db51064d14cd |
Hashes for openstl-1.0.2-cp36-cp36m-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1392ce07272c49523e9efb9982b98979f87ed89bddc1e930b6711d0b1ac6e5c7 |
|
MD5 | b85aeb088ba64e48164e3113f1a7577b |
|
BLAKE2b-256 | 5634533ebfb1aa6af55f2d75e8489e8e65473266d93eeb39f6b73777ba29efa8 |