// 官方 OBJ 格式插件(VDE 微内核 P1;纯 C ABI,不链接内核)。 // // 读:Wavefront OBJ(v 顶点 + f 多边形面,支持 v、v/vt、v/vt/vn、v//vn)→ 三角网格 // → host->body_from_mesh;与内置 foundation::read_obj + mesh_to_brep_model // 同源转换(同样以扇形三角化 f 多边形)。 // 写:P1 只读(能力声明 format:obj.read)。 #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; constexpr double kWriteDeflection = 0.01; /** 解析 "v/vt/vn" 中的首个顶点索引(1-based → 0-based;负值为相对索引) */ bool parse_obj_vertex_ref(const std::string& token, std::size_t vertex_count, std::int64_t* out_index) { if (!out_index) return false; const std::size_t slash = token.find('/'); const std::string head = (slash == std::string::npos) ? token : token.substr(0, slash); if (head.empty()) return false; char* parse_end = nullptr; const long long value = std::strtoll(head.c_str(), &parse_end, 10); if (parse_end == head.c_str()) return false; std::int64_t index = static_cast(value) - 1; if (index < 0) index += static_cast(vertex_count); if (index < 0 || static_cast(index) >= vertex_count) return false; *out_index = index; return true; } bool parse_obj(const char* data, std::size_t size, MeshAccumulator& mesh) { std::vector positions; // 3×N const char* p = data; const char* end = data + size; while (p < end) { const char* line_end = p; while (line_end < end && *line_end != '\n') ++line_end; const char* cursor = p; const char* limit = line_end; cursor = vde_plugin_common::skip_spaces(cursor, limit); // 取首个 token const char* token_end = cursor; while (token_end < limit && !vde_plugin_common::is_space_char(*token_end)) ++token_end; const std::string token(cursor, token_end); if (token == "v") { 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) return false; // 顶点行损坏:与内置 read_obj 的抛错等价 positions.push_back(values[0]); positions.push_back(values[1]); positions.push_back(values[2]); } else if (token == "f") { const std::size_t vertex_count = positions.size() / 3; std::vector face; const char* c = token_end; while (true) { c = vde_plugin_common::skip_spaces(c, limit); if (c >= limit) break; const char* ref_end = c; while (ref_end < limit && !vde_plugin_common::is_space_char(*ref_end)) ++ref_end; const std::string reference(c, ref_end); std::int64_t index = 0; if (!parse_obj_vertex_ref(reference, vertex_count, &index)) { face.clear(); break; } face.push_back(index); c = ref_end; } if (face.size() < 3) { // 坏面/退化面:跳过(内置实现会建失败面) p = (line_end < end) ? line_end + 1 : end; continue; } for (std::size_t i = 2; i < face.size(); ++i) { mesh.indices.push_back(static_cast(face[0])); mesh.indices.push_back(static_cast(face[i - 1])); mesh.indices.push_back(static_cast(face[i])); if (mesh.triangle_count() > kMaxTriangles) return false; } } p = (line_end < end) ? line_end + 1 : end; } if (mesh.indices.empty()) return false; mesh.positions = std::move(positions); // 去掉未被任何面引用的顶点会导致索引重映射;这里保留原顶点表(与内置 // read_obj 直接把全部 v 顶点交给 build_from_triangles 的口径一致)。 return true; } int obj_read_file(const VdePluginHost* host, const char* path, 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, "obj: bodies/count 为空"); const int host_rc = validate_read_host(host); if (host_rc != VDE_PLUGIN_OK) return host_rc; std::string bytes; if (!path || !read_file_bytes(path, &bytes)) return fail(host, VDE_PLUGIN_ERR_IO, "obj: 无法读取输入文件"); if (bytes.empty()) return fail(host, VDE_PLUGIN_ERR_PARSE, "obj: 文件为空"); MeshAccumulator mesh; if (!parse_obj(bytes.data(), bytes.size(), mesh)) return fail(host, VDE_PLUGIN_ERR_PARSE, "obj: 未解析出三角面"); return vde_plugin_common::publish_single_body(host, mesh, bodies, count, "obj"); } int obj_read_memory(const VdePluginHost* host, const void* data, std::size_t 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, "obj: bodies/count 为空"); const int host_rc = validate_read_host(host); if (host_rc != VDE_PLUGIN_OK) return host_rc; if (!data || size == 0) return fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "obj: 内存数据为空"); MeshAccumulator mesh; if (!parse_obj(static_cast(data), size, mesh)) return fail(host, VDE_PLUGIN_ERR_PARSE, "obj: 未解析出三角面"); return vde_plugin_common::publish_single_body(host, mesh, bodies, count, "obj"); } /// 把实体三角化后累加为 OBJ 文本(v/f;每个 body 一个 o 组)。 int build_obj_text(const VdePluginHost* host, const VdeBody* bodies, std::size_t count, std::string* out) { if (!out) return VDE_PLUGIN_ERR_INVALID_ARG; out->clear(); std::string text; text.reserve(1024 + count * 4096); char line[256]; std::size_t vertex_offset = 0; bool wrote_geometry = false; for (std::size_t i = 0; i < count; ++i) { VdeMeshData mesh{}; mesh.struct_size = static_cast(sizeof(VdeMeshData)); const int rc = host->body_tessellate(bodies[i], kWriteDeflection, &mesh); const auto release = [&]() { if (mesh.positions) host->free_buffer(mesh.positions); if (mesh.indices) host->free_buffer(mesh.indices); if (mesh.normals) host->free_buffer(mesh.normals); }; if (rc != VDE_PLUGIN_OK) { release(); return fail(host, rc, "obj: 实体三角化失败"); } if (!mesh.positions || !mesh.indices || mesh.num_vertices == 0 || mesh.num_triangles == 0) { release(); continue; } if (mesh.num_triangles > kMaxTriangles) { release(); return fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "obj: 三角面数超过上限"); } if (vertex_offset + mesh.num_vertices > 50000000u) { release(); return fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "obj: 顶点数超过上限"); } std::snprintf(line, sizeof(line), "o body_%zu\n", i); text += line; for (std::size_t v = 0; v < mesh.num_vertices; ++v) { std::snprintf(line, sizeof(line), "v %.17g %.17g %.17g\n", mesh.positions[v * 3], mesh.positions[v * 3 + 1], mesh.positions[v * 3 + 2]); text += line; } for (std::size_t t = 0; t < mesh.num_triangles; ++t) { const std::uint32_t a = mesh.indices[t * 3]; const std::uint32_t b = mesh.indices[t * 3 + 1]; const std::uint32_t c = mesh.indices[t * 3 + 2]; if (a >= mesh.num_vertices || b >= mesh.num_vertices || c >= mesh.num_vertices) { release(); return fail(host, VDE_PLUGIN_ERR_INTERNAL, "obj: 三角化索引越界"); } std::snprintf(line, sizeof(line), "f %zu %zu %zu\n", vertex_offset + a + 1, vertex_offset + b + 1, vertex_offset + c + 1); text += line; } vertex_offset += mesh.num_vertices; wrote_geometry = true; release(); } if (!wrote_geometry) return fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "obj: 没有可导出的三角面"); *out = std::move(text); return VDE_PLUGIN_OK; } int obj_write_file(const VdePluginHost* host, const char* path, const VdeBody* bodies, std::size_t count) { if (!path) return fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "obj: 输出路径为空"); const int host_rc = validate_write_host(host); if (host_rc != VDE_PLUGIN_OK) return host_rc; if (count > 0 && !bodies) return fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "obj: bodies 为空"); std::string text; const int rc = build_obj_text(host, bodies, count, &text); if (rc != VDE_PLUGIN_OK) return rc; if (!write_file_bytes(path, text.data(), text.size())) return fail(host, VDE_PLUGIN_ERR_IO, "obj: 无法写入输出文件"); return VDE_PLUGIN_OK; } int obj_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, "obj: 输出缓冲为空"); const int host_rc = validate_write_host(host); if (host_rc != VDE_PLUGIN_OK) return host_rc; if (count > 0 && !bodies) return fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "obj: bodies 为空"); std::string text; const int rc = build_obj_text(host, bodies, count, &text); if (rc != VDE_PLUGIN_OK) return rc; void* data = host->alloc(text.size() + 1); if (!data) return fail(host, VDE_PLUGIN_ERR_BUFFER, "obj: 输出缓冲分配失败"); std::memcpy(data, text.c_str(), text.size() + 1); out->data = data; out->size = text.size(); return VDE_PLUGIN_OK; } int obj_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, "ply")) return 0; if (vde_plugin_common::first_token_is(data, head_size, "solid")) return 0; // 扫描前若干行:以 "v " 开头的顶点行是最强 OBJ 特征;注释中出现 // "wavefront"/"blender" 提高置信度。 const char* p = data; const char* end = data + head_size; int score = 0; int lines = 0; while (p < end && lines < 64) { const char* line_end = p; while (line_end < end && *line_end != '\n') ++line_end; const char* cursor = vde_plugin_common::skip_spaces(p, line_end); if (cursor + 1 < line_end && cursor[0] == 'v' && vde_plugin_common::is_space_char(cursor[1])) { score = 70; break; } const std::string line = vde_plugin_common::lower_copy(std::string(p, line_end)); if (line.find("wavefront") != std::string::npos || line.find("blender") != std::string::npos) { score = 85; break; } if (cursor < line_end && cursor[0] == '#') score = (score > 20) ? score : 20; p = (line_end < end) ? line_end + 1 : end; ++lines; } return score; } const char* obj_describe() { return "VDE official Wavefront OBJ format (ASCII tri/quad read + tri write)"; } const VdeFormatPlugin kFormat = [] { VdeFormatPlugin format{}; format.struct_size = static_cast(sizeof(VdeFormatPlugin)); format.format_id = "obj"; format.extensions = ".obj"; format.probe = &obj_probe; format.read_file = &obj_read_file; format.read_memory = &obj_read_memory; format.write_file = &obj_write_file; format.write_memory = &obj_write_memory; format.describe = &obj_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.obj"; info->name = "VDE Official Wavefront OBJ 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:obj.read;format:obj.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"