// P4-23 官方 VRML 2.0(IndexedFaceSet)插件测试。 // // 覆盖: // 1) 发现/加载:id=com.vde.official.vrml、format_id=vrml、extensions .wrl/.vrml、 // capabilities format:vrml.read/write; // 2) 单 Shape 读取(含 Appearance/Material 忽略)+ 四边形扇形三角化 + 写回再导入; // 3) 多 Shape(多个 IndexedFaceSet)合并读取; // 4) 负例:空文件/VRML 1.0 头/缺失 coordIndex/索引越界/面顶点数<3/花括号不匹配/无几何。 // // 注:shared CMake 由 Lead 注册;本文件按现有官方插件测试模式编写。 #include #include #include "vde/fs_compat.h" #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_OFFICIAL_DIR #define VDE_PLUGIN_OFFICIAL_DIR "" #endif #ifndef VDE_PLUGIN_SAMPLE_DIR #define VDE_PLUGIN_SAMPLE_DIR "" #endif #ifndef VDE_TEST_TEMP_DIR #define VDE_TEST_TEMP_DIR "" #endif namespace { using vde::plugin::FormatImportResult; using vde::plugin::PluginManager; constexpr const char* kVrmlPluginId = "com.vde.official.vrml"; std::string temp_path(const char* name) { const std::string dir = VDE_TEST_TEMP_DIR[0] != '\0' ? std::string(VDE_TEST_TEMP_DIR) : (vde::fs::temp_directory_path().string() + "/"); return dir + name; } void write_text(const std::string& path, const std::string& text) { std::ofstream out(path, std::ios::binary | std::ios::trunc); ASSERT_TRUE(out.good()) << path; out << text; } std::string read_text(const std::string& path) { std::ifstream in(path, std::ios::binary); return std::string((std::istreambuf_iterator(in)), std::istreambuf_iterator()); } std::size_t count_substring(const std::string& text, const std::string& needle) { std::size_t count = 0; std::size_t pos = 0; while ((pos = text.find(needle, pos)) != std::string::npos) { ++count; pos += needle.size(); } return count; } const char* kSingleShape = "#VRML V2.0 utf8\n" "# single quad shape\n" "Shape {\n" " appearance Appearance { material Material { diffuseColor 0.8 0.2 0.2 } }\n" " geometry IndexedFaceSet {\n" " solid FALSE\n" " coord Coordinate { point [ 0 0 0, 1 0 0, 1 1 0, 0 1 0 ] }\n" " coordIndex [ 0, 1, 2, 3, -1 ]\n" " }\n" "}\n"; class VrmlPluginFixture : 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(); if (VDE_PLUGIN_OFFICIAL_DIR[0] != '\0') manager.add_search_path(VDE_PLUGIN_OFFICIAL_DIR); if (VDE_PLUGIN_SAMPLE_DIR[0] != '\0') manager.add_search_path(VDE_PLUGIN_SAMPLE_DIR); manager.scan_search_paths(); } void TearDown() override { PluginManager::instance().clear(); vde::plugin::release_geometry_host_services(&host_); for (const auto& path : files_) vde::fs::remove(path); } void remember(const std::string& path) { files_.push_back(path); } bool load_vrml(std::string* error) { PluginManager& manager = PluginManager::instance(); if (manager.is_loaded(kVrmlPluginId)) return true; return manager.load(kVrmlPluginId, error); } bool has_vrml_format() { for (const auto& format : PluginManager::instance().formats()) { if (format.plugin_id == kVrmlPluginId && format.format_id == "vrml") return true; } return false; } VdePluginHost host_{}; std::vector files_; }; TEST_F(VrmlPluginFixture, DiscoveredAndLoaded) { ASSERT_TRUE(PluginManager::instance().load(kVrmlPluginId, nullptr)) << "VRML 插件未发现/加载(检查 VDE_PLUGIN_OFFICIAL_DIR 与 CMake 注册)"; EXPECT_TRUE(PluginManager::instance().is_loaded(kVrmlPluginId)); EXPECT_TRUE(has_vrml_format()); bool read_capability = false; bool write_capability = false; const auto manifest = PluginManager::instance().manifest(kVrmlPluginId); ASSERT_TRUE(manifest.has_value()); for (const auto& capability : manifest->capabilities) { if (capability == "format:vrml.read") read_capability = true; if (capability == "format:vrml.write") write_capability = true; } EXPECT_TRUE(read_capability); EXPECT_TRUE(write_capability); } TEST_F(VrmlPluginFixture, ReadWriteSingleShapeRoundTrip) { ASSERT_TRUE(load_vrml(nullptr)); const std::string input = temp_path("vde_vrml_input.wrl"); const std::string output = temp_path("vde_vrml_output.wrl"); remember(input); remember(output); write_text(input, kSingleShape); FormatImportResult imported; ASSERT_TRUE(PluginManager::instance().import_file(input, &imported)) << imported.error; EXPECT_TRUE(imported.ok); EXPECT_EQ(imported.format_id, "vrml"); ASSERT_EQ(imported.bodies.size(), 1u); ASSERT_TRUE(PluginManager::instance().export_file(output, imported.bodies, "vrml")); const std::string exported = read_text(output); EXPECT_NE(exported.find("#VRML V2.0 utf8"), std::string::npos); EXPECT_NE(exported.find("IndexedFaceSet"), std::string::npos); EXPECT_GE(count_substring(exported, "-1"), 2u); // 四边形至少 2 三角面 FormatImportResult reimported; ASSERT_TRUE(PluginManager::instance().import_file(output, &reimported)) << reimported.error; EXPECT_EQ(reimported.format_id, "vrml"); EXPECT_EQ(reimported.bodies.size(), 1u); } TEST_F(VrmlPluginFixture, ReadsMultipleShapes) { ASSERT_TRUE(load_vrml(nullptr)); const std::string input = temp_path("vde_vrml_multi.wrl"); remember(input); write_text(input, "#VRML V2.0 utf8\n" "Shape { geometry IndexedFaceSet {\n" " coord Coordinate { point [ 0 0 0, 1 0 0, 0 1 0 ] }\n" " coordIndex [ 0, 1, 2, -1 ] } }\n" "Shape { geometry IndexedFaceSet {\n" " coord Coordinate { point [ 2 0 0, 3 0 0, 2 1 0 ] }\n" " coordIndex [ 0, 1, 2, -1 ] } }\n" "Shape { geometry IndexedFaceSet {\n" " coord Coordinate { point [ 4 0 0, 5 0 0, 4 1 0 ] }\n" " coordIndex [ 0, 1, 2, -1 ] } }\n"); FormatImportResult imported; ASSERT_TRUE(PluginManager::instance().import_file(input, &imported)) << imported.error; EXPECT_TRUE(imported.ok); EXPECT_EQ(imported.format_id, "vrml"); ASSERT_EQ(imported.bodies.size(), 1u); // 合并网格应包含三个三角形 → 写回后 coordIndex 至少 3 个 -1 const std::string output = temp_path("vde_vrml_multi_out.wrl"); remember(output); ASSERT_TRUE(PluginManager::instance().export_file(output, imported.bodies, "vrml")); EXPECT_GE(count_substring(read_text(output), "-1"), 3u); } TEST_F(VrmlPluginFixture, RejectsMalformed) { ASSERT_TRUE(load_vrml(nullptr)); const std::vector> cases = { {"empty", ""}, {"vrml1", "#VRML V1.0 ascii\nShape { geometry IndexedFaceSet { } }\n"}, {"missing_geometry", "#VRML V2.0 utf8\nShape { }\n"}, {"missing_coord_index", "#VRML V2.0 utf8\nShape { geometry IndexedFaceSet {\n" "coord Coordinate { point [ 0 0 0, 1 0 0, 0 1 0 ] } } }\n"}, {"index_out_of_range", "#VRML V2.0 utf8\nShape { geometry IndexedFaceSet {\n" "coord Coordinate { point [ 0 0 0, 1 0 0, 0 1 0 ] }\n" "coordIndex [ 0, 1, 9, -1 ] } }\n"}, {"short_face", "#VRML V2.0 utf8\nShape { geometry IndexedFaceSet {\n" "coord Coordinate { point [ 0 0 0, 1 0 0, 0 1 0 ] }\n" "coordIndex [ 0, 1, -1 ] } }\n"}, {"unbalanced_braces", "#VRML V2.0 utf8\nShape { geometry IndexedFaceSet {\n" "coord Coordinate { point [ 0 0 0, 1 0 0, 0 1 0 ] }\n"}, {"non_numeric_point", "#VRML V2.0 utf8\nShape { geometry IndexedFaceSet {\n" "coord Coordinate { point [ 0 0 X, 1 0 0, 0 1 0 ] }\n" "coordIndex [ 0, 1, 2, -1 ] } }\n"}, }; for (const auto& item : cases) { const std::string path = temp_path((std::string("vde_vrml_bad_") + item.first + ".wrl").c_str()); remember(path); write_text(path, item.second); FormatImportResult result; EXPECT_FALSE(PluginManager::instance().import_file(path, &result)) << "case=" << item.first; EXPECT_FALSE(result.error.empty()) << "case=" << item.first; } } // ── P4-36:Transform 层级 / DEF-USE / Material-Color / 未定义与循环 ── namespace { struct Box { double min[3] = {0, 0, 0}; double max[3] = {0, 0, 0}; }; Box body_box(VdePluginHost& host, VdeBody body) { Box box; host.body_bbox(body, box.min, box.max); return box; } std::size_t body_triangles(VdePluginHost& host, VdeBody body) { VdeMeshData mesh{}; mesh.struct_size = static_cast(sizeof(VdeMeshData)); if (host.body_tessellate(body, 0.01, &mesh) != VDE_PLUGIN_OK) return 0; const std::size_t count = mesh.num_triangles; if (mesh.positions) host.free_buffer(mesh.positions); if (mesh.indices) host.free_buffer(mesh.indices); if (mesh.normals) host.free_buffer(mesh.normals); return count; } std::string vrml_doc(const std::string& body) { return std::string("#VRML V2.0 utf8\n") + body; } } // namespace TEST_F(VrmlPluginFixture, TransformTranslationApplies) { ASSERT_TRUE(load_vrml(nullptr)); const std::string input = temp_path("vde_vrml_translate.wrl"); remember(input); write_text(input, vrml_doc( "Transform {\n" " translation [10 20 30]\n" " children [ Shape { geometry IndexedFaceSet {\n" " coord Coordinate { point [0 0 0, 1 0 0, 0 1 0] }\n" " coordIndex [0 1 2 -1] } } ]\n" "}\n")); FormatImportResult imported; ASSERT_TRUE(PluginManager::instance().import_file(input, &imported)) << PluginManager::instance().last_error(); ASSERT_EQ(imported.bodies.size(), 1u); const Box box = body_box(host_, imported.bodies[0]); EXPECT_NEAR(box.min[0], 10.0, 1e-9); EXPECT_NEAR(box.min[1], 20.0, 1e-9); EXPECT_NEAR(box.min[2], 30.0, 1e-9); EXPECT_NEAR(box.max[0], 11.0, 1e-9); EXPECT_NEAR(box.max[1], 21.0, 1e-9); EXPECT_NEAR(box.max[2], 30.0, 1e-9); EXPECT_EQ(body_triangles(host_, imported.bodies[0]), 1u); for (VdeBody body : imported.bodies) host_.body_destroy(body); } TEST_F(VrmlPluginFixture, TransformHierarchyScaleCenter) { ASSERT_TRUE(load_vrml(nullptr)); const std::string input = temp_path("vde_vrml_scale.wrl"); remember(input); write_text(input, vrml_doc( "Transform {\n" " translation [5 0 0]\n" " children [ Transform {\n" " scale [2 2 2]\n" " center [1 1 0]\n" " children [ Shape { geometry IndexedFaceSet {\n" " coord Coordinate { point [0 0 0, 1 0 0, 1 1 0, 0 1 0] }\n" " coordIndex [0 1 2 3 -1] } } ]\n" " } ]\n" "}\n")); FormatImportResult imported; ASSERT_TRUE(PluginManager::instance().import_file(input, &imported)) << PluginManager::instance().last_error(); ASSERT_EQ(imported.bodies.size(), 1u); const Box box = body_box(host_, imported.bodies[0]); EXPECT_NEAR(box.min[0], 4.0, 1e-9); // (0-1)*2+1+5 EXPECT_NEAR(box.max[0], 6.0, 1e-9); // (1-1)*2+1+5 EXPECT_NEAR(box.min[1], -1.0, 1e-9); EXPECT_NEAR(box.max[1], 1.0, 1e-9); EXPECT_NEAR(box.min[2], 0.0, 1e-9); EXPECT_NEAR(box.max[2], 0.0, 1e-9); EXPECT_EQ(body_triangles(host_, imported.bodies[0]), 2u); for (VdeBody body : imported.bodies) host_.body_destroy(body); } TEST_F(VrmlPluginFixture, TransformRotationApplies) { ASSERT_TRUE(load_vrml(nullptr)); const std::string input = temp_path("vde_vrml_rotate.wrl"); remember(input); write_text(input, vrml_doc( "Transform {\n" " rotation [0 0 1 1.5707963267948966]\n" " children [ Shape { geometry IndexedFaceSet {\n" " coord Coordinate { point [0 0 0, 1 0 0, 0 1 0] }\n" " coordIndex [0 1 2 -1] } } ]\n" "}\n")); FormatImportResult imported; ASSERT_TRUE(PluginManager::instance().import_file(input, &imported)) << PluginManager::instance().last_error(); ASSERT_EQ(imported.bodies.size(), 1u); const Box box = body_box(host_, imported.bodies[0]); EXPECT_NEAR(box.min[0], -1.0, 1e-6); EXPECT_NEAR(box.max[0], 0.0, 1e-6); EXPECT_NEAR(box.min[1], 0.0, 1e-6); EXPECT_NEAR(box.max[1], 1.0, 1e-6); for (VdeBody body : imported.bodies) host_.body_destroy(body); } TEST_F(VrmlPluginFixture, DefUseGeometryMergedWithTransforms) { ASSERT_TRUE(load_vrml(nullptr)); const std::string input = temp_path("vde_vrml_defuse.wrl"); remember(input); write_text(input, vrml_doc( "DEF Q IndexedFaceSet {\n" " coord Coordinate { point [0 0 0, 1 0 0, 1 1 0, 0 1 0] }\n" " coordIndex [0 1 2 3 -1]\n" "}\n" "Shape { geometry USE Q }\n" "Transform { translation [10 0 0] children [ Shape { geometry USE Q } ] }\n")); FormatImportResult imported; ASSERT_TRUE(PluginManager::instance().import_file(input, &imported)) << PluginManager::instance().last_error(); ASSERT_EQ(imported.bodies.size(), 1u); const Box box = body_box(host_, imported.bodies[0]); EXPECT_NEAR(box.min[0], 0.0, 1e-9); EXPECT_NEAR(box.max[0], 11.0, 1e-9); EXPECT_EQ(body_triangles(host_, imported.bodies[0]), 6u); // DEF 定义 + 2 次 USE = 3 个四边形 for (VdeBody body : imported.bodies) host_.body_destroy(body); } TEST_F(VrmlPluginFixture, MaterialAndColorAcceptedAndIgnored) { ASSERT_TRUE(load_vrml(nullptr)); const std::string input = temp_path("vde_vrml_material.wrl"); remember(input); write_text(input, vrml_doc( "Shape {\n" " appearance Appearance {\n" " material Material { diffuseColor 1 0 0 emissiveColor 0 0 0 " "shininess 0.5 transparency 0.1 }\n" " }\n" " geometry IndexedFaceSet {\n" " coord Coordinate { point [0 0 0, 1 0 0, 1 1 0, 0 1 0] }\n" " coordIndex [0 1 2 3 -1]\n" " color Color { color [1 0 0, 0 1 0, 0 0 1, 1 1 0] }\n" " colorIndex [0 1 2 3]\n" " }\n" "}\n")); FormatImportResult imported; ASSERT_TRUE(PluginManager::instance().import_file(input, &imported)) << PluginManager::instance().last_error(); ASSERT_EQ(imported.bodies.size(), 1u); const Box box = body_box(host_, imported.bodies[0]); EXPECT_NEAR(box.min[0], 0.0, 1e-9); EXPECT_NEAR(box.max[0], 1.0, 1e-9); EXPECT_NEAR(box.max[1], 1.0, 1e-9); for (VdeBody body : imported.bodies) host_.body_destroy(body); } TEST_F(VrmlPluginFixture, UndefinedUseRejectedAndCycleRejected) { ASSERT_TRUE(load_vrml(nullptr)); const std::string undefined_use = temp_path("vde_vrml_undefined_use.wrl"); remember(undefined_use); write_text(undefined_use, vrml_doc( "Shape { geometry USE Missing }\n")); FormatImportResult result; EXPECT_FALSE(PluginManager::instance().import_file(undefined_use, &result)); EXPECT_FALSE(result.error.empty()); EXPECT_NE(result.error.find("read failed"), std::string::npos) << result.error; const std::string cycle = temp_path("vde_vrml_cycle.wrl"); remember(cycle); write_text(cycle, vrml_doc( "DEF T Transform { children [ DEF T2 Transform { children [ USE T2 ] } ] }\n")); FormatImportResult cycle_result; EXPECT_FALSE(PluginManager::instance().import_file(cycle, &cycle_result)); EXPECT_FALSE(cycle_result.error.empty()); EXPECT_NE(cycle_result.error.find("read failed"), std::string::npos) << cycle_result.error; } } // namespace