12#include "stl_reader.h"
46 std::vector<float> coords, normals;
47 std::vector<unsigned int> tris, solids;
50 stl_reader::ReadStlFile(path.c_str(), coords, normals, tris, solids);
51 }
catch (
const std::exception &e) {
52 throw std::runtime_error(std::string(
"Failed to read STL file '") +
53 path +
"': " + e.what());
58 int nv =
static_cast<int>(coords.size()) / 3;
59 for (
int i = 0; i < nv; i++) {
61 coords[i * 3 + 0], coords[i * 3 + 1], coords[i * 3 + 2]));
64 int nt =
static_cast<int>(tris.size()) / 3;
65 for (
int i = 0; i < nt; i++) {
67 tris[i * 3 + 0], tris[i * 3 + 1], tris[i * 3 + 2]});
70 int nn =
static_cast<int>(normals.size()) / 3;
71 for (
int i = 0; i < nn; i++) {
73 normals[i * 3 + 0], normals[i * 3 + 1], normals[i * 3 + 2]));
91 std::ofstream out(path, std::ios::binary);
93 throw std::runtime_error(
"Failed to open file for writing: " + path);
97 std::memcpy(header,
"scimesh STL export", 18);
98 out.write(header, 80);
100 uint32_t ntri =
static_cast<uint32_t
>(mesh.
triangles.size());
101 out.write(
reinterpret_cast<const char *
>(&ntri), 4);
108 float nx = 0.0f, ny = 0.0f, nz = 1.0f;
115 float normal[3] = {nx, ny, nz};
116 out.write(
reinterpret_cast<const char *
>(normal), 12);
118 float vert_data[9] = {
122 out.write(
reinterpret_cast<const char *
>(vert_data), 36);
125 out.write(
reinterpret_cast<const char *
>(&attr), 2);
141 std::ofstream out(path);
143 throw std::runtime_error(
"Failed to open file for writing: " + path);
146 out <<
"solid scimesh\n";
152 out <<
" facet normal 0 0 1\n";
153 out <<
" outer loop\n";
154 out <<
" vertex " << v0.x <<
' ' << v0.y <<
' ' << v0.z <<
'\n';
155 out <<
" vertex " << v1.x <<
' ' << v1.y <<
' ' << v1.z <<
'\n';
156 out <<
" vertex " << v2.x <<
' ' << v2.y <<
' ' << v2.z <<
'\n';
158 out <<
" endfacet\n";
160 out <<
"endsolid scimesh\n";
180 const std::string &format) {
181 if (format ==
"binary") {
The Mesh — the central data structure for 3D geometry in scimesh.
void write_stl(const std::string &path, const Mesh &mesh, const std::string &format)
Write a mesh to an STL file, choosing the format.
void write_stl_binary(const std::string &path, const Mesh &mesh)
Write a mesh to a binary STL file.
Mesh read_stl(const std::string &path)
Load a mesh from an STL file (ASCII or binary).
void write_stl_ascii(const std::string &path, const Mesh &mesh)
Write a mesh to an ASCII STL file.
glm::vec3 Vec3
3-component floating-point vector (xyz).
A 3D triangle mesh using an indexed face set representation.
std::vector< Vec3 > normals
Per-vertex surface normals (unit-length direction vectors).
bool has_normals() const
Does the mesh have per-vertex normals?
std::vector< Vec3 > vertices
3D vertex positions.
std::vector< Triangle > triangles
Triangle index triplets.
A triangle defined by three vertex indices.