// 官方 STL 格式插件(VDE 微内核 P1;纯 C ABI,不链接内核)。 // // 读:ASCII / 二进制 STL → 三角网格 → host->body_from_mesh 创建实体 // (与内置 foundation::read_stl + mesh_to_brep_model 同源转换); // 写:实体 → host->body_tessellate → 二进制 STL(每体三角面摊平)。 #include #include #include #include #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(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(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(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(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(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& 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 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(k)]; if (static_cast(index) >= mesh.num_vertices) continue; p[k][0] = mesh.positions[3 * static_cast(index) + 0]; p[k][1] = mesh.positions[3 * static_cast(index) + 1]; p[k][2] = mesh.positions[3 * static_cast(index) + 2]; triangle.v[k][0] = static_cast(p[k][0]); triangle.v[k][1] = static_cast(p[k][1]); triangle.v[k][2] = static_cast(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(nx); triangle.normal[1] = static_cast(ny); triangle.normal[2] = static_cast(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(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 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 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(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(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"