- P1-1:VDE_BUILD_IO 开关;C API 拆分 vde_kernel_capi(不链接 vde_io)/vde_io_capi/旧 vde_capi 聚合;
kernel SDK 导出 vde::kernel_capi + vde::kernel::capi 别名;kernel-only 配置/构建/install/consumer 验证;
src/capi/vde_capi_io.cpp、tests/capi/test_capi_split.cpp 新增;
- P1-2:插件 ABI 1.1(VdePluginHost 尾部追加 body_from_mesh,旧宿主/旧插件按 struct_size 兼容);
官方格式插件 plugins/official/{stl,obj,ply}(纯 C ABI、不链接内核;STL 读写、OBJ/PLY 只读;与内置导入
面数/体积/bbox 一致到 1e-9);import_cad 插件回退(内置优先 + VDE_IMPORT_CAD_PREFER_PLUGIN 显式);
新增 tests/plugin/test_plugin_format_official.cpp 10 用例;
- P1-3:VDE_MODULES_MODE(static/hybrid 生效;shared/plugin 预留 WARNING);6 个业务模块独立开关;
vde::io/vde::app 聚合 + vde::engine deprecated;cmake/check_dependency_direction.cmake +
VDE_DependencyDirection_Guard CTest(六配置 PASS、负向可检出);
- 文档:plugin-development §10/§11、architecture §2.2–2.4/§7、design §7/§8 P1 进展;
- 版本 1.0.46(CMake/version.h/test_version/README/docs index/module-manual/CHANGELOG 同步)。
验证:全量构建 0 error;全量 ctest 7369 注册、7274/7274 PASS(0 failed,25 skipped,300.51s,-E Benchmark);
插件 ctest 54/54、官方插件 10/10、test_capi_split 10/10(kernel 7+io 3);kernel-only / no-cam / 业务全关 /
static / shared 配置均通过;依赖守卫 PASS。
342 lines
14 KiB
C++
342 lines
14 KiB
C++
// 官方 STL 格式插件(VDE 微内核 P1;纯 C ABI,不链接内核)。
|
|
//
|
|
// 读:ASCII / 二进制 STL → 三角网格 → host->body_from_mesh 创建实体
|
|
// (与内置 foundation::read_stl + mesh_to_brep_model 同源转换);
|
|
// 写:实体 → host->body_tessellate → 二进制 STL(每体三角面摊平)。
|
|
#include <cstdint>
|
|
#include <cstring>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include "../common/format_plugin_common.h"
|
|
|
|
namespace {
|
|
|
|
using vde_plugin_common::MeshAccumulator;
|
|
using vde_plugin_common::fail;
|
|
using vde_plugin_common::read_file_bytes;
|
|
using vde_plugin_common::validate_common_host;
|
|
using vde_plugin_common::validate_read_host;
|
|
using vde_plugin_common::validate_write_host;
|
|
using vde_plugin_common::write_file_bytes;
|
|
|
|
constexpr std::size_t kMaxTriangles = 50000000u;
|
|
|
|
struct StlTriangleF {
|
|
float normal[3] = {0.0f, 0.0f, 0.0f};
|
|
float v[3][3] = {{0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}};
|
|
};
|
|
|
|
bool looks_like_ascii_stl(const char* data, std::size_t size) {
|
|
return vde_plugin_common::first_token_is(data, size, "solid");
|
|
}
|
|
|
|
|
|
bool parse_ascii_stl(const char* data, std::size_t size, MeshAccumulator& mesh) {
|
|
const char* p = data;
|
|
const char* end = data + size;
|
|
std::size_t facet_start = 0;
|
|
bool in_facet = false;
|
|
while (p < end) {
|
|
const char* line_end = p;
|
|
while (line_end < end && *line_end != '\n') ++line_end;
|
|
const char* limit = line_end;
|
|
const char* cursor = vde_plugin_common::skip_spaces(p, limit);
|
|
const char* token_end = cursor;
|
|
while (token_end < limit && !vde_plugin_common::is_space_char(*token_end))
|
|
++token_end;
|
|
const std::string token =
|
|
vde_plugin_common::lower_copy(std::string(cursor, token_end));
|
|
if (token == "facet") {
|
|
in_facet = true;
|
|
facet_start = mesh.vertex_count();
|
|
} else if (token == "vertex" && in_facet) {
|
|
const char* c = token_end;
|
|
double values[3] = {0.0, 0.0, 0.0};
|
|
bool ok = true;
|
|
for (int i = 0; i < 3; ++i) {
|
|
c = vde_plugin_common::skip_spaces(c, limit);
|
|
if (c >= limit) { ok = false; break; }
|
|
std::string number(c, limit);
|
|
char* parse_end = nullptr;
|
|
values[i] = std::strtod(number.c_str(), &parse_end);
|
|
if (parse_end == number.c_str()) { ok = false; break; }
|
|
c += static_cast<std::size_t>(parse_end - number.c_str());
|
|
}
|
|
if (ok) mesh.add_vertex(values[0], values[1], values[2]);
|
|
} else if (token == "endfacet") {
|
|
if (in_facet && mesh.vertex_count() - facet_start == 3) {
|
|
const uint32_t a = static_cast<uint32_t>(facet_start);
|
|
mesh.add_triangle(a, a + 1, a + 2);
|
|
}
|
|
in_facet = false;
|
|
}
|
|
p = (line_end < end) ? line_end + 1 : end;
|
|
}
|
|
return !mesh.empty();
|
|
}
|
|
bool parse_binary_stl(const char* data, std::size_t size, MeshAccumulator& mesh) {
|
|
if (!data || size < 84) return false;
|
|
uint32_t count = 0;
|
|
std::memcpy(&count, data + 80, 4);
|
|
if (count == 0 || count > kMaxTriangles) return false;
|
|
const std::size_t expected = 84u + static_cast<std::size_t>(count) * 50u;
|
|
std::size_t read_count = count;
|
|
if (size < expected) {
|
|
// 截断容错:只读完整落盘的记录(与内置 read_stl_binary 一致)
|
|
const std::size_t available = (size - 84u) / 50u;
|
|
read_count = available < count ? available : count;
|
|
if (read_count == 0) return false;
|
|
}
|
|
const char* cursor = data + 84;
|
|
mesh.positions.reserve(read_count * 9);
|
|
mesh.indices.reserve(read_count * 3);
|
|
for (std::size_t i = 0; i < read_count; ++i, cursor += 50) {
|
|
float values[12];
|
|
std::memcpy(values, cursor, sizeof(values)); // normal[3] + v0/v1/v2
|
|
const uint32_t base = static_cast<uint32_t>(mesh.vertex_count());
|
|
mesh.add_vertex(values[3], values[4], values[5]);
|
|
mesh.add_vertex(values[6], values[7], values[8]);
|
|
mesh.add_vertex(values[9], values[10], values[11]);
|
|
mesh.add_triangle(base, base + 1, base + 2);
|
|
}
|
|
return !mesh.empty();
|
|
}
|
|
|
|
int load_stl(const VdePluginHost* host, const char* path, const void* memory_data,
|
|
std::size_t memory_size, VdeBody** bodies, std::size_t* count) {
|
|
if (bodies) *bodies = nullptr;
|
|
if (count) *count = 0;
|
|
if (!bodies || !count)
|
|
return fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "stl: bodies/count 为空");
|
|
const int host_rc = validate_read_host(host);
|
|
if (host_rc != VDE_PLUGIN_OK) return host_rc;
|
|
|
|
std::string bytes;
|
|
if (memory_data) {
|
|
bytes.assign(static_cast<const char*>(memory_data), memory_size);
|
|
} else if (!path || !read_file_bytes(path, &bytes)) {
|
|
return fail(host, VDE_PLUGIN_ERR_IO, "stl: 无法读取输入文件");
|
|
}
|
|
if (bytes.empty()) return fail(host, VDE_PLUGIN_ERR_PARSE, "stl: 文件为空");
|
|
|
|
MeshAccumulator mesh;
|
|
bool parsed = false;
|
|
if (looks_like_ascii_stl(bytes.data(), bytes.size())) {
|
|
parsed = parse_ascii_stl(bytes.data(), bytes.size(), mesh);
|
|
}
|
|
if (!parsed) {
|
|
mesh = MeshAccumulator{};
|
|
parsed = parse_binary_stl(bytes.data(), bytes.size(), mesh);
|
|
}
|
|
if (!parsed) return fail(host, VDE_PLUGIN_ERR_PARSE, "stl: 未解析出三角面(ASCII/二进制均失败)");
|
|
return vde_plugin_common::publish_single_body(host, mesh, bodies, count, "stl");
|
|
}
|
|
|
|
int stl_read_file(const VdePluginHost* host, const char* path, VdeBody** bodies,
|
|
std::size_t* count) {
|
|
return load_stl(host, path, nullptr, 0, bodies, count);
|
|
}
|
|
|
|
int stl_read_memory(const VdePluginHost* host, const void* data, std::size_t size,
|
|
VdeBody** bodies, std::size_t* count) {
|
|
if (!data || size == 0) {
|
|
if (bodies) *bodies = nullptr;
|
|
if (count) *count = 0;
|
|
return fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "stl: 内存数据为空");
|
|
}
|
|
return load_stl(host, nullptr, data, size, bodies, count);
|
|
}
|
|
|
|
/** 实体系列 → 二进制 STL 字节流(每体三角面摊平;法向按三角面重算) */
|
|
int build_stl_binary(const VdePluginHost* host, const VdeBody* bodies,
|
|
std::size_t count, std::vector<unsigned char>& out) {
|
|
out.clear();
|
|
const int host_rc = validate_write_host(host);
|
|
if (host_rc != VDE_PLUGIN_OK) return host_rc;
|
|
if (!bodies || count == 0)
|
|
return fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "stl: 没有实体可写");
|
|
|
|
std::vector<StlTriangleF> triangles;
|
|
for (std::size_t i = 0; i < count; ++i) {
|
|
if (!bodies[i]) return fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "stl: 实体句柄为空");
|
|
VdeMeshData mesh{};
|
|
mesh.struct_size = sizeof(VdeMeshData);
|
|
const int rc = host->body_tessellate(bodies[i], 0.01, &mesh);
|
|
if (rc != VDE_PLUGIN_OK) {
|
|
if (host->free_buffer) {
|
|
host->free_buffer(mesh.positions);
|
|
host->free_buffer(mesh.indices);
|
|
host->free_buffer(mesh.normals);
|
|
}
|
|
return fail(host, VDE_PLUGIN_ERR_INTERNAL, "stl: body_tessellate 失败");
|
|
}
|
|
if (mesh.positions && mesh.indices) {
|
|
for (std::size_t t = 0; t < mesh.num_triangles; ++t) {
|
|
StlTriangleF triangle{};
|
|
double p[3][3];
|
|
for (int k = 0; k < 3; ++k) {
|
|
const uint32_t index = mesh.indices[3 * t + static_cast<std::size_t>(k)];
|
|
if (static_cast<std::size_t>(index) >= mesh.num_vertices) continue;
|
|
p[k][0] = mesh.positions[3 * static_cast<std::size_t>(index) + 0];
|
|
p[k][1] = mesh.positions[3 * static_cast<std::size_t>(index) + 1];
|
|
p[k][2] = mesh.positions[3 * static_cast<std::size_t>(index) + 2];
|
|
triangle.v[k][0] = static_cast<float>(p[k][0]);
|
|
triangle.v[k][1] = static_cast<float>(p[k][1]);
|
|
triangle.v[k][2] = static_cast<float>(p[k][2]);
|
|
}
|
|
const double ux = p[1][0] - p[0][0];
|
|
const double uy = p[1][1] - p[0][1];
|
|
const double uz = p[1][2] - p[0][2];
|
|
const double vx = p[2][0] - p[0][0];
|
|
const double vy = p[2][1] - p[0][1];
|
|
const double vz = p[2][2] - p[0][2];
|
|
double nx = uy * vz - uz * vy;
|
|
double ny = uz * vx - ux * vz;
|
|
double nz = ux * vy - uy * vx;
|
|
const double length = std::sqrt(nx * nx + ny * ny + nz * nz);
|
|
if (length > 1e-30) {
|
|
nx /= length;
|
|
ny /= length;
|
|
nz /= length;
|
|
} else {
|
|
nx = ny = nz = 0.0;
|
|
}
|
|
triangle.normal[0] = static_cast<float>(nx);
|
|
triangle.normal[1] = static_cast<float>(ny);
|
|
triangle.normal[2] = static_cast<float>(nz);
|
|
triangles.push_back(triangle);
|
|
if (triangles.size() > kMaxTriangles) {
|
|
if (host->free_buffer) {
|
|
host->free_buffer(mesh.positions);
|
|
host->free_buffer(mesh.indices);
|
|
host->free_buffer(mesh.normals);
|
|
}
|
|
return fail(host, VDE_PLUGIN_ERR_BUFFER, "stl: 三角面数量超过上限");
|
|
}
|
|
}
|
|
}
|
|
if (host->free_buffer) {
|
|
host->free_buffer(mesh.positions);
|
|
host->free_buffer(mesh.indices);
|
|
host->free_buffer(mesh.normals);
|
|
}
|
|
}
|
|
if (triangles.empty())
|
|
return fail(host, VDE_PLUGIN_ERR_PARSE, "stl: 实体三角化后无三角面");
|
|
|
|
const std::size_t total = 84u + triangles.size() * 50u;
|
|
out.resize(total, 0);
|
|
char header[80] = {};
|
|
std::memcpy(&out[0], header, 80);
|
|
const uint32_t tri_count = static_cast<uint32_t>(triangles.size());
|
|
std::memcpy(&out[80], &tri_count, 4);
|
|
std::size_t offset = 84;
|
|
for (const auto& triangle : triangles) {
|
|
std::memcpy(&out[offset], triangle.normal, 12);
|
|
offset += 12;
|
|
for (int k = 0; k < 3; ++k) {
|
|
std::memcpy(&out[offset], triangle.v[k], 12);
|
|
offset += 12;
|
|
}
|
|
const uint16_t attribute = 0;
|
|
std::memcpy(&out[offset], &attribute, 2);
|
|
offset += 2;
|
|
}
|
|
return VDE_PLUGIN_OK;
|
|
}
|
|
|
|
int stl_write_file(const VdePluginHost* host, const char* path, const VdeBody* bodies,
|
|
std::size_t count) {
|
|
if (!path || !*path) return fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "stl: 路径为空");
|
|
std::vector<unsigned char> bytes;
|
|
const int rc = build_stl_binary(host, bodies, count, bytes);
|
|
if (rc != VDE_PLUGIN_OK) return rc;
|
|
if (!write_file_bytes(path, bytes.data(), bytes.size()))
|
|
return fail(host, VDE_PLUGIN_ERR_IO, "stl: 写出文件失败");
|
|
return VDE_PLUGIN_OK;
|
|
}
|
|
|
|
int stl_write_memory(const VdePluginHost* host, const VdeBody* bodies, std::size_t count,
|
|
VdeBuffer* out) {
|
|
if (out) {
|
|
out->data = nullptr;
|
|
out->size = 0;
|
|
}
|
|
if (!out) return fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "stl: out 为空");
|
|
std::vector<unsigned char> bytes;
|
|
const int rc = build_stl_binary(host, bodies, count, bytes);
|
|
if (rc != VDE_PLUGIN_OK) return rc;
|
|
void* buffer = host->alloc(bytes.size());
|
|
if (!buffer) return fail(host, VDE_PLUGIN_ERR_BUFFER, "stl: 输出缓冲分配失败");
|
|
std::memcpy(buffer, bytes.data(), bytes.size());
|
|
out->data = buffer;
|
|
out->size = bytes.size();
|
|
return VDE_PLUGIN_OK;
|
|
}
|
|
|
|
int stl_probe(const void* head, std::size_t head_size) {
|
|
if (!head || head_size == 0) return 0;
|
|
const char* data = static_cast<const char*>(head);
|
|
if (vde_plugin_common::first_token_is(data, head_size, "solid")) return 100;
|
|
// 二进制 STL 启发式:80 字节头 + uint32 三角形数
|
|
if (head_size >= 84) {
|
|
uint32_t count = 0;
|
|
std::memcpy(&count, data + 80, 4);
|
|
if (count > 0 && count <= 20000000u) return 60;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
const char* stl_describe() {
|
|
return "VDE official STL format (ASCII/binary tri mesh; read/write)";
|
|
}
|
|
|
|
const VdeFormatPlugin kFormat = [] {
|
|
VdeFormatPlugin format{};
|
|
format.struct_size = static_cast<uint32_t>(sizeof(VdeFormatPlugin));
|
|
format.format_id = "stl";
|
|
format.extensions = ".stl";
|
|
format.probe = &stl_probe;
|
|
format.read_file = &stl_read_file;
|
|
format.read_memory = &stl_read_memory;
|
|
format.write_file = &stl_write_file;
|
|
format.write_memory = &stl_write_memory;
|
|
format.describe = &stl_describe;
|
|
return format;
|
|
}();
|
|
|
|
} // namespace
|
|
|
|
extern "C" {
|
|
|
|
VDE_PLUGIN_EXPORT int vde_plugin_query(uint32_t host_abi_major, VdePluginInfo* info) {
|
|
if (!info || info->struct_size < sizeof(VdePluginInfo)) return VDE_PLUGIN_ERR_ABI;
|
|
if (host_abi_major != VDE_PLUGIN_ABI_MAJOR) return VDE_PLUGIN_ERR_ABI;
|
|
info->id = "com.vde.official.stl";
|
|
info->name = "VDE Official STL Format";
|
|
info->version = "1.0.0";
|
|
info->kind = VDE_PLUGIN_KIND_FORMAT;
|
|
info->abi_major = VDE_PLUGIN_ABI_MAJOR;
|
|
info->abi_minor = VDE_PLUGIN_ABI_MINOR;
|
|
info->capabilities = "format:stl.read;format:stl.write";
|
|
info->license = "Apache-2.0";
|
|
return VDE_PLUGIN_OK;
|
|
}
|
|
|
|
VDE_PLUGIN_EXPORT int vde_plugin_init(const VdePluginHost* host,
|
|
VdePluginRegistration* reg) {
|
|
if (!reg || reg->struct_size < sizeof(VdePluginRegistration))
|
|
return VDE_PLUGIN_ERR_ABI;
|
|
const int host_rc = validate_common_host(host);
|
|
if (host_rc != VDE_PLUGIN_OK) return host_rc;
|
|
reg->kind = VDE_PLUGIN_KIND_FORMAT;
|
|
reg->format = &kFormat;
|
|
reg->generic = nullptr;
|
|
return VDE_PLUGIN_OK;
|
|
}
|
|
|
|
VDE_PLUGIN_EXPORT void vde_plugin_shutdown() {}
|
|
|
|
} // extern "C"
|