Files
ViewDesignEngine/plugins/official/obj/obj_plugin.cpp
T
hm 88653eff2f feat(plugin): P1 微内核 — 官方格式插件 + C API 拆分 + 模块模式/依赖守卫(1.0.46)
- 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。
2026-10-28 14:46:31 +08:00

232 lines
9.5 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.
// 官方 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 <cstdint>
#include <cstdlib>
#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;
constexpr std::size_t kMaxTriangles = 50000000u;
/** 解析 "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<std::int64_t>(value) - 1;
if (index < 0) index += static_cast<std::int64_t>(vertex_count);
if (index < 0 || static_cast<std::size_t>(index) >= vertex_count) return false;
*out_index = index;
return true;
}
bool parse_obj(const char* data, std::size_t size, MeshAccumulator& mesh) {
std::vector<double> 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<std::size_t>(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<std::int64_t> 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<uint32_t>(face[0]));
mesh.indices.push_back(static_cast<uint32_t>(face[i - 1]));
mesh.indices.push_back(static_cast<uint32_t>(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<const char*>(data), size, mesh))
return fail(host, VDE_PLUGIN_ERR_PARSE, "obj: 未解析出三角面");
return vde_plugin_common::publish_single_body(host, mesh, bodies, count, "obj");
}
int obj_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, "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 mesh; read-only in P1)";
}
const VdeFormatPlugin kFormat = [] {
VdeFormatPlugin format{};
format.struct_size = static_cast<uint32_t>(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 = nullptr;
format.write_memory = nullptr;
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";
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"