- 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。
195 lines
7.5 KiB
C++
195 lines
7.5 KiB
C++
#pragma once
|
||
// 官方格式插件公共工具(VDE 微内核 P1;纯 C ABI,不链接内核)。
|
||
//
|
||
// 只允许 include vde/plugin/vde_plugin_api.h + C/C++ 标准库;所有内核几何能力
|
||
// 通过 VdePluginHost 服务表获得(P1 的 body_from_mesh 用于把三角网格变成实体)。
|
||
#include <cmath>
|
||
#include <cstddef>
|
||
#include <cstdint>
|
||
#include <cstdio>
|
||
#include <cstring>
|
||
#include <string>
|
||
#include <vector>
|
||
|
||
#include "vde/plugin/vde_plugin_api.h"
|
||
|
||
namespace vde_plugin_common {
|
||
|
||
inline int fail(const VdePluginHost* host, int code, const char* message) {
|
||
if (host && host->set_error) host->set_error(message);
|
||
return code;
|
||
}
|
||
|
||
/// 宿主是否提供 P1 body_from_mesh(按 struct_size 判字段存在,兼容旧宿主 ABI 1.0)
|
||
inline bool host_has_body_from_mesh(const VdePluginHost* host) {
|
||
if (!host) return false;
|
||
const std::size_t required =
|
||
offsetof(VdePluginHost, body_from_mesh) + sizeof(host->body_from_mesh);
|
||
return host->struct_size >= required && host->body_from_mesh != nullptr;
|
||
}
|
||
|
||
/// 所有官方插件共用的宿主表检查(内存/诊断/ABI)
|
||
inline int validate_common_host(const VdePluginHost* host) {
|
||
if (!host) return VDE_PLUGIN_ERR_INVALID_ARG;
|
||
if (host->abi_major != VDE_PLUGIN_ABI_MAJOR)
|
||
return fail(host, VDE_PLUGIN_ERR_ABI, "official plugin: 宿主 ABI major 不兼容");
|
||
if (!host->alloc || !host->free || !host->set_error || !host->body_destroy)
|
||
return fail(host, VDE_PLUGIN_ERR_UNSUPPORTED,
|
||
"official plugin: 宿主缺少 alloc/free/set_error/body_destroy");
|
||
return VDE_PLUGIN_OK;
|
||
}
|
||
|
||
/// 读插件读取路径所需宿主能力(含 P1 body_from_mesh)
|
||
inline int validate_read_host(const VdePluginHost* host) {
|
||
const int rc = validate_common_host(host);
|
||
if (rc != VDE_PLUGIN_OK) return rc;
|
||
if (!host_has_body_from_mesh(host))
|
||
return fail(host, VDE_PLUGIN_ERR_UNSUPPORTED,
|
||
"official plugin: 宿主未提供 body_from_mesh(需要插件 ABI minor >= 1)");
|
||
return VDE_PLUGIN_OK;
|
||
}
|
||
|
||
/// 写插件写出路径所需宿主能力(三角化 + bbox + 缓冲区释放)
|
||
inline int validate_write_host(const VdePluginHost* host) {
|
||
const int rc = validate_common_host(host);
|
||
if (rc != VDE_PLUGIN_OK) return rc;
|
||
if (!host->body_tessellate || !host->free_buffer)
|
||
return fail(host, VDE_PLUGIN_ERR_UNSUPPORTED,
|
||
"official plugin: 宿主缺少 body_tessellate/free_buffer");
|
||
return VDE_PLUGIN_OK;
|
||
}
|
||
|
||
/// 整文件读取(二进制;失败返回 false)
|
||
inline bool read_file_bytes(const char* path, std::string* out) {
|
||
if (!path || !out) return false;
|
||
out->clear();
|
||
std::FILE* file = std::fopen(path, "rb");
|
||
if (!file) return false;
|
||
if (std::fseek(file, 0, SEEK_END) != 0) {
|
||
std::fclose(file);
|
||
return false;
|
||
}
|
||
const long length = std::ftell(file);
|
||
std::rewind(file);
|
||
if (length < 0) {
|
||
std::fclose(file);
|
||
return false;
|
||
}
|
||
out->resize(static_cast<std::size_t>(length));
|
||
const std::size_t read_bytes =
|
||
out->empty() ? 0u : std::fread(&(*out)[0], 1, out->size(), file);
|
||
std::fclose(file);
|
||
if (read_bytes != out->size()) {
|
||
out->clear();
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
/// 整文件写出
|
||
inline bool write_file_bytes(const char* path, const void* data, std::size_t size) {
|
||
if (!path || (!data && size > 0)) return false;
|
||
std::FILE* file = std::fopen(path, "wb");
|
||
if (!file) return false;
|
||
const std::size_t written =
|
||
size == 0 ? 0u : std::fwrite(data, 1, size, file);
|
||
std::fclose(file);
|
||
return written == size;
|
||
}
|
||
|
||
/// 三角网格累加器(positions 3×N、indices 3×M,0-based)
|
||
struct MeshAccumulator {
|
||
std::vector<double> positions;
|
||
std::vector<uint32_t> indices;
|
||
|
||
[[nodiscard]] std::size_t vertex_count() const { return positions.size() / 3; }
|
||
[[nodiscard]] std::size_t triangle_count() const { return indices.size() / 3; }
|
||
[[nodiscard]] bool empty() const { return indices.empty(); }
|
||
|
||
uint32_t add_vertex(double x, double y, double z) {
|
||
const uint32_t index = static_cast<uint32_t>(vertex_count());
|
||
positions.push_back(x);
|
||
positions.push_back(y);
|
||
positions.push_back(z);
|
||
return index;
|
||
}
|
||
|
||
bool add_triangle(uint32_t a, uint32_t b, uint32_t c) {
|
||
if (a == b || b == c || a == c) return false;
|
||
const uint32_t count = static_cast<uint32_t>(vertex_count());
|
||
if (a >= count || b >= count || c >= count) return false;
|
||
indices.push_back(a);
|
||
indices.push_back(b);
|
||
indices.push_back(c);
|
||
return true;
|
||
}
|
||
};
|
||
|
||
/// 把已解析网格经宿主 body_from_mesh 变成实体 + host->alloc 句柄数组交付。
|
||
/// 成功时 *out_bodies 由宿主 alloc 分配(调用方/宿主负责 free),实体所有权移交宿主。
|
||
inline int publish_single_body(const VdePluginHost* host, const MeshAccumulator& mesh,
|
||
VdeBody** out_bodies, std::size_t* out_count,
|
||
const char* format_name) {
|
||
if (out_bodies) *out_bodies = nullptr;
|
||
if (out_count) *out_count = 0;
|
||
if (!out_bodies || !out_count)
|
||
return fail(host, VDE_PLUGIN_ERR_INVALID_ARG,
|
||
"official plugin: bodies/count 输出指针为空");
|
||
if (mesh.empty())
|
||
return fail(host, VDE_PLUGIN_ERR_PARSE,
|
||
"official plugin: 文件不含可用三角面");
|
||
if (!host_has_body_from_mesh(host))
|
||
return fail(host, VDE_PLUGIN_ERR_UNSUPPORTED,
|
||
"official plugin: 宿主未提供 body_from_mesh(需要插件 ABI minor >= 1)");
|
||
VdeBody body = host->body_from_mesh(mesh.positions.data(), mesh.vertex_count(),
|
||
mesh.indices.data(), mesh.triangle_count());
|
||
if (!body)
|
||
return fail(host, VDE_PLUGIN_ERR_INTERNAL,
|
||
"official plugin: 宿主由网格创建实体失败(见 host 错误信息)");
|
||
VdeBody* array = static_cast<VdeBody*>(host->alloc(sizeof(VdeBody)));
|
||
if (!array) {
|
||
host->body_destroy(body);
|
||
return fail(host, VDE_PLUGIN_ERR_BUFFER,
|
||
"official plugin: 句柄数组分配失败");
|
||
}
|
||
array[0] = body;
|
||
*out_bodies = array;
|
||
*out_count = 1;
|
||
(void)format_name;
|
||
return VDE_PLUGIN_OK;
|
||
}
|
||
|
||
// ── 轻量文本工具 ──────────────────────────────────────────
|
||
inline bool is_space_char(char c) {
|
||
return c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == '\f' || c == '\v';
|
||
}
|
||
|
||
/// 跳过空白后返回首个 token 起始位置(到 end 或空白为止),找不到返回 data+size
|
||
inline const char* skip_spaces(const char* begin, const char* end) {
|
||
const char* p = begin;
|
||
while (p < end && is_space_char(*p)) ++p;
|
||
return p;
|
||
}
|
||
|
||
inline std::string lower_copy(const std::string& text) {
|
||
std::string out = text;
|
||
for (char& c : out) {
|
||
if (c >= 'A' && c <= 'Z') c = static_cast<char>(c - 'A' + 'a');
|
||
}
|
||
return out;
|
||
}
|
||
|
||
/// 首 token 是否等于 word(大小写不敏感)
|
||
inline bool first_token_is(const char* data, std::size_t size, const char* word) {
|
||
if (!data || !word) return false;
|
||
const char* begin = data;
|
||
const char* end = data + size;
|
||
const char* p = skip_spaces(begin, end);
|
||
const char* token_end = p;
|
||
while (token_end < end && !is_space_char(*token_end)) ++token_end;
|
||
const std::string token = lower_copy(std::string(p, token_end));
|
||
return token == lower_copy(std::string(word));
|
||
}
|
||
|
||
} // namespace vde_plugin_common
|