From 98db892260cd21872db1eaf4cdafed00daeef65c Mon Sep 17 00:00:00 2001 From: Jean-Christophe Date: Fri, 22 Mar 2024 14:20:49 -0400 Subject: [PATCH] docs: add c++ examples --- README.md | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 78e587e..b9f31a7 100644 --- a/README.md +++ b/README.md @@ -74,7 +74,7 @@ scale = 1000.0 quad[:,1:4,:] *= scale # Avoid scaling normals ``` -### Converting Triangles --> Vertices and Faces +### Converting Triangles -> Vertices and Faces ```python import openstl @@ -89,7 +89,7 @@ triangles = [ vertices, faces = openstl.convert.verticesandfaces(triangles) ``` -### Converting Vertices and Faces --> Triangles +### Converting Vertices and Faces -> Triangles ```python import openstl @@ -113,6 +113,8 @@ triangles = openstl.convert.triangles(vertices, faces) # C++ Usage ### Read STL from file ```c++ +#include + std::ifstream file(filename, std::ios::binary); if (!file.is_open()) { std::cerr << "Error: Unable to open file '" << filename << "'" << std::endl; @@ -149,6 +151,33 @@ std::vector originalTriangles{}; // User triangles openstl::serialize(originalTriangles, ss, openstl::StlFormat::Binary); // Or StlFormat::ASCII ``` +### Converting Triangles -> Vertices and Faces +```c++ +using namespace openstl + +std::vector triangles = { + // normal, vertices 0, vertices 1, vertices 2 + Triangle{{0.0f, 0.0f, 1.0f}, {1.0f, 1.0f, 1.0f}, {2.0f, 2.0f, 2.0f}, {3.0f, 3.0f, 3.0f}}, + Triangle{{0.0f, 0.0f, 1.0f}, {2.0f, 2.0f, 2.0f}, {3.0f, 3.0f, 3.0f}, {4.0f, 4.0f, 4.0f}} +}; + +const auto& [vertices, faces] = convertToVerticesAndFaces(triangles); +``` + +### Converting Vertices and Faces -> Triangles +```c++ +using namespace openstl + +std::vector vertices = { + Vec3{0.0f, 0.0f, 0.0f}, Vec3{1.0f, 1.0f, 1.0f}, Vec3{2.0f, 2.0f, 2.0f}, Vec3{3.0f, 3.0f, 3.0f} +}; +std::vector faces = { + {0, 1, 2}, {3, 1, 2} +}; + +const auto& triangles = convertToTriangles(vertices, faces); +``` + # Integrate to your codebase ### Smart method Include this repository with CMAKE Fetchcontent and link your executable/library to `openstl::core` library.