// 示例 Mesher(网格生成)插件(VDE 微内核 P4 / 插件 ABI 1.4;纯 C ABI,不链接内核)。 // // 这是 VdeMesherPlugin 扩展点的 mock/演示实现:不依赖任何内核符号,仅使用 // VdePluginHost 的 alloc/free/set_error;用于验证 ABI 1.4 注册、调用与错误隔离。 // 真实 mesher 插件可把 model_data 经 host->body_from_serialized 还原为实体后 // 用 host->body_volume/body_bbox/body_tessellate 查询几何,再调用自有网格算法。 // // 本 mock 输出固定 8 节点 / 5 四面体的单位立方体网格(不读取模型几何), // 并在 JSON 中回显 model_bytes/element_size/options_tolerance,便于测试断言。 #include #include #include #include #include "vde/plugin/vde_plugin_api.h" namespace { int fail(const VdePluginHost* host, int code, const char* message) { if (host && host->set_error) host->set_error(message); return code; } bool valid_result(const VdeMesherResult* result) { return result != nullptr && result->struct_size >= sizeof(VdeMesherResult); } void free_buffer(const VdePluginHost* host, VdeBuffer* buffer) { if (buffer && buffer->data && host && host->free_buffer) host->free_buffer(buffer->data); if (buffer) { buffer->data = nullptr; buffer->size = 0; } } // 固定 tet4 单位立方体(8 节点 / 5 四面体;索引 0-based) const double kVertices[8][3] = { {0.0, 0.0, 0.0}, {1.0, 0.0, 0.0}, {1.0, 1.0, 0.0}, {0.0, 1.0, 0.0}, {0.0, 0.0, 1.0}, {1.0, 0.0, 1.0}, {1.0, 1.0, 1.0}, {0.0, 1.0, 1.0}}; const unsigned kTets[5][4] = {{0, 1, 2, 6}, {0, 2, 3, 6}, {0, 3, 7, 6}, {0, 7, 4, 6}, {0, 4, 5, 6}}; int mesh(const VdePluginHost* host, const void* model_data, size_t model_size, const VdeMesherOptions* options, VdeMesherResult* result) { if (!host || !host->alloc || !host->set_error || !host->free_buffer) return VDE_PLUGIN_ERR_INVALID_ARG; if (!model_data || model_size == 0) return fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "mesher: model_data is empty"); if (!valid_result(result)) return fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "mesher: result struct_size too small"); if (options && options->struct_size >= sizeof(VdeMesherOptions) && options->flags != 0) return fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "mesher: options.flags must be 0"); const double element_size = (options && options->element_size > 0.0) ? options->element_size : 1.0; const double tolerance = (options && options->tolerance > 0.0) ? options->tolerance : 1e-6; const std::uint32_t requested_type = options ? options->element_type : 0; if (requested_type > 2) return fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "mesher: unsupported element_type"); std::string json; char buffer[512]; std::snprintf(buffer, sizeof(buffer), "{\"mesher_id\":\"mesher.mock.tet4\",\"ok\":true," "\"element_type\":\"tet4\",\"nodes\":%zu,\"elements\":%zu," "\"model_bytes\":%zu,\"element_size\":%.12g,\"tolerance\":%.12g,", sizeof(kVertices) / sizeof(kVertices[0]), sizeof(kTets) / sizeof(kTets[0]), model_size, element_size, tolerance); json += buffer; json += "\"vertices\":["; for (std::size_t i = 0; i < sizeof(kVertices) / sizeof(kVertices[0]); ++i) { if (i != 0) json += ","; std::snprintf(buffer, sizeof(buffer), "[%.12g,%.12g,%.12g]", kVertices[i][0], kVertices[i][1], kVertices[i][2]); json += buffer; } json += "],\"tets\":["; for (std::size_t i = 0; i < sizeof(kTets) / sizeof(kTets[0]); ++i) { if (i != 0) json += ","; std::snprintf(buffer, sizeof(buffer), "[%u,%u,%u,%u]", kTets[i][0], kTets[i][1], kTets[i][2], kTets[i][3]); json += buffer; } json += "],\"diagnostics\":[\"mock tet4 mesher (unit cube template); not for production " "use\"]}"; void* out = host->alloc(json.size() + 1); if (!out) return fail(host, VDE_PLUGIN_ERR_BUFFER, "mesher: cannot allocate result buffer"); std::memcpy(out, json.c_str(), json.size() + 1); result->status = VDE_MESHER_OK; result->element_type = 1; // tet4 result->num_nodes = static_cast(sizeof(kVertices) / sizeof(kVertices[0])); result->num_elements = static_cast(sizeof(kTets) / sizeof(kTets[0])); result->mesh_json.data = out; result->mesh_json.size = json.size(); return VDE_PLUGIN_OK; } const char* describe() { return "VDE sample tet4 mesher (mock unit-cube template; ABI 1.4)"; } const VdeMesherPlugin kMesher = [] { VdeMesherPlugin plugin{}; plugin.struct_size = sizeof(VdeMesherPlugin); plugin.mesher_id = "mesher.mock.tet4"; plugin.capabilities_json = "{\"dimensions\":[3],\"element_types\":[\"tet4\"],\"mock\":true}"; plugin.mesh = &mesh; plugin.describe = &describe; return plugin; }(); } // namespace extern "C" { VDE_PLUGIN_EXPORT int vde_plugin_query(std::uint32_t host_abi_major, VdePluginInfo* info) { if (!info || info->struct_size < sizeof(VdePluginInfo)) return VDE_PLUGIN_ERR_INVALID_ARG; if (host_abi_major != VDE_PLUGIN_ABI_MAJOR) return VDE_PLUGIN_ERR_ABI; info->id = "com.vde.sample.mesher"; info->name = "VDE Sample Mesher (ABI 1.4)"; info->version = "0.1.0"; info->kind = VDE_PLUGIN_KIND_MESHER; info->abi_major = VDE_PLUGIN_ABI_MAJOR; info->abi_minor = VDE_PLUGIN_ABI_MINOR; info->capabilities = "mesher:tet"; info->license = "Apache-2.0"; return VDE_PLUGIN_OK; } VDE_PLUGIN_EXPORT int vde_plugin_init(const VdePluginHost* host, VdePluginRegistration* reg) { if (!host || !reg) return VDE_PLUGIN_ERR_INVALID_ARG; if (reg->struct_size < sizeof(VdePluginRegistration)) return VDE_PLUGIN_ERR_ABI; if (!host->alloc || !host->free_buffer || !host->set_error) return fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "mesher: host services incomplete"); reg->kind = VDE_PLUGIN_KIND_MESHER; reg->format = nullptr; reg->generic = nullptr; reg->cae_solver = nullptr; reg->cam_strategy = nullptr; reg->gcs_solver = nullptr; reg->mesher = &kMesher; return VDE_PLUGIN_OK; } VDE_PLUGIN_EXPORT void vde_plugin_shutdown() {} } // extern "C"