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.1-pp310-pypy310_pp73-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0cbd2bab8d676a8708d2303204e29eae9b7a38d8dfdb84c15d67c5558e478f73 |
|
MD5 | 7d104491c701d639427c1472d77697d2 |
|
BLAKE2b-256 | 2f5e0f3c37c17b3f386642e22154fb151a6e5e1ca7bae089a3158bd98b04900b |
Hashes for openstl-1.0.1-pp39-pypy39_pp73-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 01265a3e8329ac54610e40f6e45737e447069568f2b6575a53e7a21baaf34f79 |
|
MD5 | 024be5b9f8fc289a32ab5301692353a2 |
|
BLAKE2b-256 | 0e89aa053174ee21027f892e5380ee59bd02f337eca933cc27491a2f52bdb7ce |
Hashes for openstl-1.0.1-pp38-pypy38_pp73-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cda9c80556f7c087559261c5a19829a5e606a5b7ffe3ac70e9cd23c4d8252888 |
|
MD5 | 3c7fe49189d9852b7ae3433419c0f8c1 |
|
BLAKE2b-256 | a493f302ac51bd407751c23018d2b46625d21c4a7549990273bbdffcd1ca0ef2 |
Hashes for openstl-1.0.1-pp37-pypy37_pp73-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0ca8ccef6bf6c1fcb316c9827a7718cd266e20aa10d3237a38f728ce56a18649 |
|
MD5 | 04128e8aee0ca84d3351349120fd4899 |
|
BLAKE2b-256 | 4e0ac2ba13d34ddea72a57b3c10bd4fdb2ea392c667a4dd6c05b1e43388ff300 |
Hashes for openstl-1.0.1-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3eac06694e6aa2d6552e982299eb810baa92306d3276d30f5dc35c5b21045527 |
|
MD5 | 9200da8c7e9d1796a121b89f76209618 |
|
BLAKE2b-256 | de30cc57c5f9f587f05c006cb8d0466e5d10340c7d1dfdfb89067698f22bcdfb |
Hashes for openstl-1.0.1-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f562990f77d68c8a552ddaf08e70744b15e93b1b14a8116742985b7f2a9a5883 |
|
MD5 | 0b35f61b132b35db2204b1644ff09359 |
|
BLAKE2b-256 | e26e5ea2fb9274ead7369cdab8d4e61fd7ebf7329d54473348999a3af592446c |
Hashes for openstl-1.0.1-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bf05a859c70b74677889c934954f9031827507262850633c3e3ac8395dcd3856 |
|
MD5 | 3be94e6dfa1e82ee8ef66d543e09d21b |
|
BLAKE2b-256 | 219ff29c72e7e0948d1fcc6836b143fb97b71279ce9e4751e5a0026bdac42d45 |
Hashes for openstl-1.0.1-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 112746e3f190080bc7a200bbde2fef859debc8859ef4d341ecea76af34866972 |
|
MD5 | 279db7f571335a7325e93ec4322c4b47 |
|
BLAKE2b-256 | 8ddff6aa29d968f387f94acec696fa956c004cbf7f02be8acf115a8a8e044880 |
Hashes for openstl-1.0.1-cp38-cp38-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bb5347cc55dcd055747ecfa6f325e1262608dc8c7195ce8979d80c09a97c92f7 |
|
MD5 | 2ba364d5c2cb37aa30914de1fce339b6 |
|
BLAKE2b-256 | 7a6597cf5f562ce1163c7ba373af340f2734a5de2ac7e9a21cf8c69ec62b2542 |
Hashes for openstl-1.0.1-cp37-cp37m-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 96e038e59636e3cea7a1c1f297cf2d2ec7d9b5f361ec42df6474f51939302631 |
|
MD5 | f63f3894a3b04def0a65ee42f8ab0683 |
|
BLAKE2b-256 | eff7e2bb8226766d5b101074c0c5a49ceda6f3cb6e42e11b5204909baf8b5c56 |
Hashes for openstl-1.0.1-cp36-cp36m-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4d4a85911a89f77e74998da566c0b747dabfc33f191cd51c5cb3167bfe7b3038 |
|
MD5 | cc7dc566220ac20fa7246c480e9e647d |
|
BLAKE2b-256 | 97185759363ef29cc496cab4cc004be1a756f5ae1e5dd7241051a57dc3db1dd8 |