Files
ViewDesignEngine/include/vde/foundation/io_stl.h
T
茂之钳 c021855597
CI / Build & Test (push) Failing after 36s
CI / Release Build (push) Failing after 36s
Build & Test / build-and-test (push) Has been cancelled
Build & Test / python-bindings (push) Has been cancelled
fix(v1.0.1): VDE-011/012/013 — write_stl return bool, StlTrianglef, module options
VDE-011 (Medium): write_stl / write_stl_ascii return bool
- Return false on file open failure, true on success

VDE-012 (Low): StlTrianglef for float precision
- New StlTrianglef struct with Vector3F/Point3F
- to_stl_trianglef() / from_stl_trianglef() conversions

VDE-013 (Medium): Per-module build options
- VDE_BUILD_CURVES/MESH/BREP/SPATIAL/BOOLEAN/COLLISION/SKETCH/SDF/CAPI
- All default ON, parent project can set OFF
2026-07-27 22:51:03 +08:00

137 lines
4.2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#pragma once
#include <string>
#include <vector>
#include "vde/foundation/math_types.h"
namespace vde::foundation {
/**
* @brief STL 三角形面片
*
* 表示一个 STL 三角形,包含法向量和三个顶点。
* 法向量应为单位向量,方向朝外(右手法则)。
*
* 除直接访问成员(v0, v1, v2)外,提供便捷分量访问方法,
* 无需通过 Point3D 的 .x()/.y()/.z() 间接获取。
*
* @ingroup foundation
*/
struct StlTriangle {
/// 面法向量(单位向量)
Vector3D normal;
/// 三个顶点(右手法则 CCW 顺序)
Point3D v0, v1, v2;
// ── 便捷分量访问:顶点 v0 ──
/// 顶点 v0 的 X 分量
[[nodiscard]] double v0x() const { return v0.x(); }
/// 顶点 v0 的 Y 分量
[[nodiscard]] double v0y() const { return v0.y(); }
/// 顶点 v0 的 Z 分量
[[nodiscard]] double v0z() const { return v0.z(); }
// ── 便捷分量访问:顶点 v1 ──
/// 顶点 v1 的 X 分量
[[nodiscard]] double v1x() const { return v1.x(); }
/// 顶点 v1 的 Y 分量
[[nodiscard]] double v1y() const { return v1.y(); }
/// 顶点 v1 的 Z 分量
[[nodiscard]] double v1z() const { return v1.z(); }
// ── 便捷分量访问:顶点 v2 ──
/// 顶点 v2 的 X 分量
[[nodiscard]] double v2x() const { return v2.x(); }
/// 顶点 v2 的 Y 分量
[[nodiscard]] double v2y() const { return v2.y(); }
/// 顶点 v2 的 Z 分量
[[nodiscard]] double v2z() const { return v2.z(); }
};
/**
* @brief 读取 STL 文件(自动检测格式)
*
* 通过检查文件头部的 80 字节判断二进制(含 triangle count
* 还是 ASCII(以 "solid" 开头)格式,然后选择对应解析器。
*
* @param filepath STL 文件路径
* @return 三角形面片列表
*
* @throws std::runtime_error 若文件无法打开或格式无效
*
* @note 自动格式检测:二进制 STL 的 80 字节头后跟 4 字节三角形数量
* @note ASCII STL 以 "solid name" 开头
*
* @code{.cpp}
* auto tris = read_stl("part.stl");
* std::cout << "Triangles: " << tris.size() << "\n";
* @endcode
*
* @see write_stl, write_stl_ascii
* @ingroup foundation
*/
std::vector<StlTriangle> read_stl(const std::string& filepath);
/**
* @brief 写入二进制 STL 文件
*
* 以紧凑的二进制格式写出 STL 三角形列表。
* 二进制 STL 为业界标准交换格式,文件体积小、读写快。
*
* @param filepath 输出文件路径
* @param tris 三角形面片列表
*
* @note 二进制 STL 格式:80 字节头 + 4 字节三角形数 + 每三角形 50 字节
* @note 法向量在写入时自动归一化
*
* @code{.cpp}
* std::vector<StlTriangle> tris = // ...;
* write_stl("output.stl", tris); // 二进制,体积小
* @endcode
*
* @see read_stl, write_stl_ascii
* @ingroup foundation
*/
void write_stl(const std::string& filepath, const std::vector<StlTriangle>& tris);
bool write_stl(const std::string& filepath, const std::vector<StlTriangle>& tris);
/**
* @brief 写入 ASCII STL 文件
*
* 以人类可读的 ASCII 文本格式写出 STL,便于调试和手动编辑。
*
* @param filepath 输出文件路径
* @param tris 三角形面片列表
* @return true 写入成功,false 文件创建失败
*
* @warning ASCII STL 文件体积约为二进制 STL 的 5-10 倍,不适合大规模网格
*
* @code{.cpp}
* std::vector<StlTriangle> tris = // ...;
* if (!write_stl_ascii("debug.stl", tris)) {
* std::cerr << "Failed to write STL\n";
* }
* @endcode
*
* @see read_stl, write_stl
* @ingroup foundation
*/
void write_stl_ascii(const std::string& filepath, const std::vector<StlTriangle>& tris);
bool write_stl_ascii(const std::string& filepath, const std::vector<StlTriangle>& tris);
/**
* @brief float 精度 STL 三角形
*
* 用于与 float 网格数据直接交互,避免 double↔float 显式转换。
*/
struct StlTrianglef {
Vector3F normal;
Point3F v0, v1, v2;
};
/// 转换 double → float 精度
StlTrianglef to_stl_trianglef(const StlTriangle& t);
/// 转换 float → double 精度
StlTriangle from_stl_trianglef(const StlTrianglef& t);
} // namespace vde::foundation