// P4-40 官方 GEOM_OP geom_analyze 插件测试(PluginManager + 真实 C ABI 动态加载)。 #include #include #include #include #include #include #include #include #include #include "vde/plugin/geometry_host_services.h" #include "vde/plugin/plugin_manager.h" #include "vde/plugin/vde_plugin_api.h" #ifndef VDE_PLUGIN_DIRS #define VDE_PLUGIN_DIRS "" #endif namespace { using vde::plugin::PluginManager; constexpr const char* kPluginId = "com.vde.official.geom_analyze"; constexpr const char* kOpId = "geom_op.analyze"; bool contains(const std::string& text, const std::string& needle) { return text.find(needle) != std::string::npos; } double extract_number(const std::string& json, const std::string& key) { const std::string pattern = "\"" + key + "\":"; const std::size_t pos = json.find(pattern); if (pos == std::string::npos) return std::nan(""); return std::strtod(json.c_str() + pos + pattern.size(), nullptr); } std::vector extract_numbers(const std::string& json, const std::string& marker, std::size_t count) { std::vector values; const std::size_t pos = json.find(marker); if (pos == std::string::npos) return values; const char* cursor = json.c_str() + pos + marker.size(); for (std::size_t i = 0; i < count; ++i) { while (*cursor != '\0' && !std::isdigit(static_cast(*cursor)) && *cursor != '-' && *cursor != '+' && *cursor != '.') { ++cursor; } if (*cursor == '\0') break; char* end = nullptr; const double value = std::strtod(cursor, &end); if (end == cursor) break; values.push_back(value); cursor = end; } return values; } void expect_near(double actual, double expected, double tolerance = 1e-6) { EXPECT_NEAR(actual, expected, tolerance * std::max(1.0, std::fabs(expected))); } class GeomAnalyzeFixture : public ::testing::Test { protected: void SetUp() override { host_ = vde::plugin::make_geometry_host_services(); PluginManager& manager = PluginManager::instance(); manager.clear(); manager.set_host_services(host_); manager.clear_search_paths(); manager.add_search_path(VDE_PLUGIN_DIRS); manager.scan_search_paths(); body_ = host_.body_create_box(2.0, 3.0, 4.0); ASSERT_NE(body_, nullptr); ASSERT_EQ(host_.body_serialize(body_, &model_), VDE_PLUGIN_OK); std::string error; ASSERT_TRUE(manager.load(kPluginId, &error)) << error; } void TearDown() override { if (model_.data) host_.free_buffer(model_.data); if (body_) host_.body_destroy(body_); PluginManager::instance().clear(); vde::plugin::release_geometry_host_services(&host_); } VdePluginHost host_{}; VdeBody body_ = nullptr; VdeBuffer model_{}; }; TEST_F(GeomAnalyzeFixture, Metadata) { const auto manifest = PluginManager::instance().manifest(kPluginId); ASSERT_TRUE(manifest.has_value()); EXPECT_EQ(manifest->abi_minor, 7); EXPECT_EQ(manifest->kind, vde::plugin::PluginKind::GeomOp); EXPECT_EQ(PluginManager::instance().geom_ops().size(), 1u); EXPECT_EQ(PluginManager::instance().geom_ops()[0].op_id, kOpId); } TEST_F(GeomAnalyzeFixture, OpsReturnExpectedMetrics) { PluginManager& manager = PluginManager::instance(); std::string error; std::string result; ASSERT_TRUE(manager.invoke_geometry_op(kOpId, model_.data, model_.size, "{\"op\":\"volume\"}", &result, &error)) << error; EXPECT_TRUE(contains(result, "\"volume\":24")) << result; result.clear(); ASSERT_TRUE(manager.invoke_geometry_op(kOpId, model_.data, model_.size, "{\"op\":\"bbox\"}", &result, &error)) << error; EXPECT_TRUE(contains(result, "\"bbox\"")) << result; result.clear(); ASSERT_TRUE(manager.invoke_geometry_op(kOpId, model_.data, model_.size, "{\"op\":\"area\"}", &result, &error)) << error; EXPECT_TRUE(contains(result, "\"surface_area\":52")) << result; result.clear(); ASSERT_TRUE(manager.invoke_geometry_op(kOpId, model_.data, model_.size, "{\"op\":\"centroid\"}", &result, &error)) << error; EXPECT_TRUE(contains(result, "\"centroid\"")) << result; } TEST_F(GeomAnalyzeFixture, InvalidInputFails) { PluginManager& manager = PluginManager::instance(); std::string error; std::string result; EXPECT_FALSE(manager.invoke_geometry_op(kOpId, nullptr, 0, "{}", &result, &error)); EXPECT_FALSE(error.empty()); error.clear(); const unsigned char trash[] = {1, 2, 3, 4}; EXPECT_FALSE(manager.invoke_geometry_op(kOpId, trash, sizeof(trash), "{}", &result, &error)); EXPECT_FALSE(error.empty()); } TEST_F(GeomAnalyzeFixture, MassPropertiesBoxAnalytic) { PluginManager& manager = PluginManager::instance(); std::string error; std::string result; ASSERT_TRUE(manager.invoke_geometry_op(kOpId, model_.data, model_.size, "{\"op\":\"mass_properties\"}", &result, &error)) << error; expect_near(extract_number(result, "volume"), 24.0); expect_near(extract_number(result, "area"), 52.0); const std::vector centroid = extract_numbers(result, "\"centroid\":[", 3); ASSERT_EQ(centroid.size(), 3u) << result; expect_near(centroid[0], 0.0); expect_near(centroid[1], 0.0); expect_near(centroid[2], 0.0); const std::vector inertia = extract_numbers(result, "\"inertia\":[[", 9); ASSERT_EQ(inertia.size(), 9u) << result; // box 2x3x4,质量=体积=24:Ixx=50, Iyy=40, Izz=26;质心在原点。 expect_near(inertia[0], 50.0); expect_near(inertia[1], 0.0); expect_near(inertia[2], 0.0); expect_near(inertia[3], 0.0); expect_near(inertia[4], 40.0); expect_near(inertia[5], 0.0); expect_near(inertia[6], 0.0); expect_near(inertia[7], 0.0); expect_near(inertia[8], 26.0); const std::vector inertia_centroid = extract_numbers(result, "\"inertia_centroid\":[[", 9); ASSERT_EQ(inertia_centroid.size(), 9u) << result; for (std::size_t i = 0; i < 9; ++i) expect_near(inertia_centroid[i], inertia[i]); EXPECT_TRUE(contains(result, "\"triangles\":")) << result; EXPECT_TRUE(contains(result, "\"skipped_degenerate\":0")) << result; } TEST_F(GeomAnalyzeFixture, MassPropertiesCylinderAnalytic) { PluginManager& manager = PluginManager::instance(); VdeBody cylinder = host_.body_create_cylinder(2.0, 5.0, 256); ASSERT_NE(cylinder, nullptr); VdeBuffer cylinder_model{}; ASSERT_EQ(host_.body_serialize(cylinder, &cylinder_model), VDE_PLUGIN_OK); std::string error; std::string result; const bool invoked = manager.invoke_geometry_op( kOpId, cylinder_model.data, cylinder_model.size, "{\"op\":\"mass_properties\"}", &result, &error); host_.free_buffer(cylinder_model.data); host_.body_destroy(cylinder); ASSERT_TRUE(invoked) << error; const double volume = std::acos(-1.0) * 4.0 * 5.0; const double area = 2.0 * std::acos(-1.0) * 2.0 * (2.0 + 5.0); expect_near(extract_number(result, "volume"), volume, 0.002); expect_near(extract_number(result, "area"), area, 0.002); const std::vector centroid = extract_numbers(result, "\"centroid\":[", 3); ASSERT_EQ(centroid.size(), 3u) << result; expect_near(centroid[0], 0.0, 0.01); expect_near(centroid[1], 0.0, 0.01); expect_near(centroid[2], 0.0, 0.01); const std::vector inertia = extract_numbers(result, "\"inertia\":[[", 9); ASSERT_EQ(inertia.size(), 9u) << result; const double i_axis = 0.5 * volume * 4.0; const double i_radial = volume * (3.0 * 4.0 + 25.0) / 12.0; expect_near(inertia[0], i_radial, 0.005); expect_near(inertia[4], i_radial, 0.005); expect_near(inertia[8], i_axis, 0.005); } TEST_F(GeomAnalyzeFixture, MassPropertiesDeterministicAndDegenerate) { PluginManager& manager = PluginManager::instance(); std::string error; std::string first; std::string second; ASSERT_TRUE(manager.invoke_geometry_op(kOpId, model_.data, model_.size, "{\"op\":\"mass_properties\"}", &first, &error)) << error; ASSERT_TRUE(manager.invoke_geometry_op(kOpId, model_.data, model_.size, "{\"op\":\"mass_properties\"}", &second, &error)) << error; EXPECT_EQ(first, second); const double positions[] = {0.0, 0.0, 0.0, 2.0, 0.0, 0.0, 2.0, 3.0, 0.0, 0.0, 3.0, 0.0}; const std::uint32_t indices[] = {0, 1, 2, 0, 2, 3}; VdeBody sheet = host_.body_from_mesh(positions, 4, indices, 2); if (!sheet) GTEST_SKIP() << "host rejected planar sheet mesh"; VdeBuffer sheet_model{}; ASSERT_EQ(host_.body_serialize(sheet, &sheet_model), VDE_PLUGIN_OK); std::string sheet_result; error.clear(); const bool sheet_ok = manager.invoke_geometry_op( kOpId, sheet_model.data, sheet_model.size, "{\"op\":\"mass_properties\"}", &sheet_result, &error); host_.free_buffer(sheet_model.data); host_.body_destroy(sheet); EXPECT_FALSE(sheet_ok); EXPECT_FALSE(error.empty()); } } // 编译期依赖:测试由 CMake 提供 VDE_PLUGIN_DIRS。 #ifndef VDE_PLUGIN_DIRS #define VDE_PLUGIN_DIRS "" #endif