#include #include #include #include #include "vde/foundation/io_collada.h" #include "vde/foundation/utf8_path.h" #include "vde/brep/modeling.h" #include #include #include using namespace vde::foundation; // ── Test 1: Import tetrahedron COLLADA ── TEST(IoColladaTest, ImportTetrahedron) { const char* path = VDE_TEST_TEMP_DIR "test_collada_1.dae"; std::remove(path); std::ofstream out(path); out << "\n"; out << "\n"; out << " \n"; out << " \n"; out << " \n"; out << " \n"; out << " "; out << "0 0 0 1 0 0 0.5 0.866 0 0.5 0.289 0.816\n"; out << " \n"; out << " \n"; out << " \n"; out << " \n"; out << " \n"; out << " \n"; out << " \n"; out << " \n"; out << " \n"; out << " \n"; out << " \n"; out << " \n"; out << " \n"; out << "

0 1 2 0 3 1 1 3 2 0 2 3

\n"; out << "
\n"; out << "
\n"; out << "
\n"; out << "
\n"; out << " \n"; out << " \n"; out << " \n"; out << " \n"; out << "
\n"; out.close(); auto models = import_collada(path); ASSERT_GE(models.size(), 1u); EXPECT_GT(models[0].num_vertices(), 0u); EXPECT_GT(models[0].num_faces(), 0u); std::remove(path); } // ── Test 2: Import single triangle ── TEST(IoColladaTest, ImportSingleTriangle) { const char* path = VDE_TEST_TEMP_DIR "test_collada_2.dae"; std::remove(path); std::ofstream out(path); out << "\n"; out << "\n"; out << " \n"; out << " \n"; out << " \n"; out << " \n"; out << " 1 0 0 2 0 0 1.5 0.866 0\n"; out << " \n"; out << " \n"; out << " \n"; out << " \n"; out << " \n"; out << " \n"; out << " \n"; out << " \n"; out << " \n"; out << " \n"; out << " \n"; out << " \n"; out << " \n"; out << "

0 1 2

\n"; out << "
\n"; out << "
\n"; out << "
\n"; out << "
\n"; out << " \n"; out << " \n"; out << " \n"; out << " \n"; out << "
\n"; out.close(); auto models = import_collada(path); ASSERT_GE(models.size(), 1u); EXPECT_EQ(models[0].num_vertices(), 3u); EXPECT_EQ(models[0].num_faces(), 1u); std::remove(path); } // ── Test 3: Empty export returns false ── TEST(IoColladaTest, ExportEmptyReturnsFalse) { std::vector empty; EXPECT_FALSE(export_collada(empty, VDE_TEST_TEMP_DIR "test_collada_empty.dae")); } // ── Test 4: Import nonexistent file ── TEST(IoColladaTest, ImportNonExistentReturnsEmpty) { auto result = import_collada(VDE_TEST_TEMP_DIR "nonexistent_collada_xyz.dae"); EXPECT_TRUE(result.empty()); } // ── VDE-1264:自适应导出(demo box+sphere)────────────────────────────── namespace { std::uintmax_t file_size_bytes(const char* path) { std::ifstream in(path, std::ios::binary | std::ios::ate); if (!in.good()) return 0; return static_cast(in.tellg()); } std::size_t imported_face_count(const std::vector& models) { std::size_t faces = 0; for (const auto& model : models) faces += model.num_faces(); return faces; } } // namespace TEST(IoColladaVde1264, DemoBoxSphereAdaptiveSizeTimeAndRoundtrip) { const char* path = VDE_TEST_TEMP_DIR "vde1264_demo.dae"; std::remove(path); std::vector bodies; bodies.push_back(vde::brep::make_box(5, 5, 5)); bodies.push_back(vde::brep::make_sphere(2.5)); const auto begin = std::chrono::steady_clock::now(); ASSERT_TRUE(export_collada(bodies, path)) << "默认自适应导出失败"; const auto end = std::chrono::steady_clock::now(); const auto elapsed_ms = std::chrono::duration_cast(end - begin).count(); const std::uintmax_t size = file_size_bytes(path); EXPECT_GT(size, 0u); EXPECT_LT(size, 1024u * 1024u) << "VDE-1264:demo 导出应 <1MB,实际 " << size; EXPECT_LT(elapsed_ms, 1000) << "VDE-1264:demo 导出应 <1s,实际 " << elapsed_ms << "ms"; const auto imported = import_collada(path); std::remove(path); EXPECT_EQ(imported.size(), 2u) << "导出后回读应得到 2 个 geometry"; const std::size_t faces = imported_face_count(imported); EXPECT_GT(faces, 0u); EXPECT_LE(faces, 80000u) << "默认三角预算 80k 不应被突破"; std::cout << "[vde1264] demo size=" << size << " bytes time=" << elapsed_ms << "ms imported_geometries=" << imported.size() << " triangles=" << faces << std::endl; } TEST(IoColladaVde1264, ExplicitDeflectionIsPrioritizedOverBudget) { std::vector bodies; bodies.push_back(vde::brep::make_box(3, 3, 3)); const char* path_a = VDE_TEST_TEMP_DIR "vde1264_explicit_a.dae"; const char* path_b = VDE_TEST_TEMP_DIR "vde1264_explicit_b.dae"; std::remove(path_a); std::remove(path_b); // 显式 deflection>0:即使 max_triangles=1 也不得触发预算自适应(两次输出应一致)。 ColladaExportOptions tiny_budget; tiny_budget.deflection = 0.5; tiny_budget.max_triangles = 1; tiny_budget.adaptive = true; ASSERT_TRUE(export_collada(bodies, path_a, {}, tiny_budget)); ColladaExportOptions normal_budget; normal_budget.deflection = 0.5; normal_budget.max_triangles = 80000; normal_budget.adaptive = true; ASSERT_TRUE(export_collada(bodies, path_b, {}, normal_budget)); const std::uintmax_t size_a = file_size_bytes(path_a); const std::uintmax_t size_b = file_size_bytes(path_b); ASSERT_GT(size_a, 0u); EXPECT_EQ(size_a, size_b) << "显式 deflection 应优先,不被 max_triangles 改写"; const auto imported = import_collada(path_a); std::remove(path_a); std::remove(path_b); ASSERT_EQ(imported.size(), 1u); EXPECT_GT(imported[0].num_faces(), 0u); } TEST(IoColladaVde1264, AdaptiveBudgetIsHonored) { std::vector bodies; bodies.push_back(vde::brep::make_box(5, 5, 5)); const char* path = VDE_TEST_TEMP_DIR "vde1264_budget.dae"; std::remove(path); ColladaExportOptions options; options.max_triangles = 100; // box 最小网格远低于该预算,验证预算参数生效 ASSERT_TRUE(export_collada(bodies, path, {}, options)); const auto imported = import_collada(path); std::remove(path); ASSERT_EQ(imported.size(), 1u); EXPECT_LE(imported_face_count(imported), 100u) << "预算 100 三角应被遵守"; } TEST(IoColladaVde1264, SingleBodyAndOldApiStillWork) { const char* path = VDE_TEST_TEMP_DIR "vde1264_single.dae"; std::remove(path); std::vector bodies; bodies.push_back(vde::brep::make_box(2, 2, 2)); ASSERT_TRUE(export_collada(bodies, path)); // 旧 3 参 API(默认自适应) EXPECT_LT(file_size_bytes(path), 1024u * 1024u); const auto imported = import_collada(path); std::remove(path); ASSERT_EQ(imported.size(), 1u); EXPECT_GT(imported[0].num_faces(), 0u); } // ── FMT-11:COLLADA 材质 + 多 primitive/多 geometry ───────────────────── namespace { const char* kMaterialCollada = "\n" "\n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " 0 0 0 1 0 0 0 1 0 2 0 0 3 0 0 2 1 0\n" " \n" " \n" " \n" " \n" " \n" " \n" "

0 1 2

\n" "
\n" " \n" "

3 4 5

\n" "
\n" "
\n" "
\n" " \n" " \n" " \n" " 0 0 0 2 0 0 2 2 0 0 2 0\n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " 4

0 1 2 3

\n" "
\n" "
\n" "
\n" "
\n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" "
\n"; } // namespace TEST(IoColladaFmt11, ImportMaterialsMultiPrimitiveAndPolylist) { const std::string path = std::string(VDE_TEST_TEMP_DIR) + "fmt11_materials.dae"; { std::ofstream out(path, std::ios::binary); out << kMaterialCollada; } const ColladaImportResult result = import_collada_with_materials(path); EXPECT_TRUE(result.success) << result.error; EXPECT_TRUE(result.error.empty()) << result.error; EXPECT_EQ(result.bodies.size(), 2u) << "两个 geometry 应各自一个 body"; EXPECT_EQ(result.meshes.size(), 3u) << "2 triangles + 1 polylist primitive"; ASSERT_EQ(result.material_names.size(), 2u); EXPECT_EQ(result.material_names[0], "RedMat"); EXPECT_EQ(result.material_names[1], "BlueMat"); ASSERT_EQ(result.materials.size(), 2u); EXPECT_EQ(result.materials[0].effect_id, "effectA"); EXPECT_EQ(result.meshes[0].material_name, "RedMat"); EXPECT_EQ(result.meshes[1].material_name, "BlueMat"); EXPECT_EQ(result.meshes[2].material_name, "BlueMat"); // polylist via symbol mapping EXPECT_EQ(result.meshes[2].mesh.num_faces(), 2u); // quad → 2 triangles EXPECT_GT(result.meshes[0].mesh.num_faces(), 0u); // 旧 API 兼容:仅返回 bodies,数量与 with_materials 一致 const auto legacy_bodies = import_collada(path); EXPECT_EQ(legacy_bodies.size(), result.bodies.size()); std::remove(path.c_str()); } TEST(IoColladaFmt11, NoMaterialDefaultsToDefaultName) { std::string step = kMaterialCollada; const std::string with_materials = ""; const size_t mat_begin = step.find(with_materials); ASSERT_NE(mat_begin, std::string::npos); const size_t mat_end = step.find("", mat_begin); ASSERT_NE(mat_end, std::string::npos); step.erase(mat_begin, mat_end + std::string("").size() - mat_begin); // 去掉符号映射,primitive 的 MAT_A/MAT_B 无法解析 → 保留原始 material 名 const std::string path = std::string(VDE_TEST_TEMP_DIR) + "fmt11_nomat.dae"; { std::ofstream out(path, std::ios::binary); out << step; } const ColladaImportResult result = import_collada_with_materials(path); std::remove(path.c_str()); EXPECT_TRUE(result.success) << result.error; EXPECT_TRUE(result.material_names.empty()); for (const auto& mesh : result.meshes) { EXPECT_FALSE(mesh.material_name.empty()); } } TEST(IoColladaFmt11, UnsupportedImagesAnimationsWarn) { std::string collada = kMaterialCollada; const std::string extra = " tex.png\n" " \n"; const size_t insert = collada.find(" "); ASSERT_NE(insert, std::string::npos); collada.insert(insert, extra); const std::string path = std::string(VDE_TEST_TEMP_DIR) + "fmt11_warn.dae"; { std::ofstream out(path, std::ios::binary); out << collada; } const ColladaImportResult result = import_collada_with_materials(path); std::remove(path.c_str()); EXPECT_TRUE(result.success) << result.error; bool image_warning = false; bool animation_warning = false; for (const auto& w : result.warnings) { if (w.find("图像") != std::string::npos) image_warning = true; if (w.find("动画") != std::string::npos) animation_warning = true; } EXPECT_TRUE(image_warning) << "图像不支持必须 warning"; EXPECT_TRUE(animation_warning) << "动画不支持必须 warning"; } // ═══════════════════════════════════════════════════════════ // Wave21 A54:UTF-8(中文/非 ASCII)路径导出/导入往返 // ═══════════════════════════════════════════════════════════ TEST(IoColladaVde1264, Utf8PathRoundtrip) { const std::vector bodies = {vde::brep::make_box(2.0, 2.0, 2.0)}; const std::string path = std::string(VDE_TEST_TEMP_DIR) + "test_collada_模型_①.dae"; std::error_code ec; std::filesystem::remove(path_from_utf8(path), ec); ASSERT_TRUE(export_collada(bodies, path)); EXPECT_TRUE(std::filesystem::exists(path_from_utf8(path))); const auto imported = import_collada(path); ASSERT_GE(imported.size(), 1u); EXPECT_GT(imported_face_count(imported), 0u); std::filesystem::remove(path_from_utf8(path), ec); EXPECT_FALSE(std::filesystem::exists(path_from_utf8(path))); }