363 lines
16 KiB
C++
363 lines
16 KiB
C++
#include <gtest/gtest.h>
|
||
#include <cstdio>
|
||
#include <fstream>
|
||
#include "vde/foundation/io_collada.h"
|
||
#include "vde/brep/modeling.h"
|
||
|
||
#include <chrono>
|
||
#include <iostream>
|
||
#include <cstdint>
|
||
|
||
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 << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
|
||
out << "<COLLADA xmlns=\"http://www.collada.org/2005/11/COLLADASchema\" version=\"1.4.1\">\n";
|
||
out << " <library_geometries>\n";
|
||
out << " <geometry id=\"geom0\" name=\"Tet\">\n";
|
||
out << " <mesh>\n";
|
||
out << " <source id=\"geom0-positions\">\n";
|
||
out << " <float_array id=\"fa0\" count=\"12\">";
|
||
out << "0 0 0 1 0 0 0.5 0.866 0 0.5 0.289 0.816</float_array>\n";
|
||
out << " <technique_common>\n";
|
||
out << " <accessor source=\"#fa0\" count=\"4\" stride=\"3\">\n";
|
||
out << " <param name=\"X\" type=\"float\"/>\n";
|
||
out << " <param name=\"Y\" type=\"float\"/>\n";
|
||
out << " <param name=\"Z\" type=\"float\"/>\n";
|
||
out << " </accessor>\n";
|
||
out << " </technique_common>\n";
|
||
out << " </source>\n";
|
||
out << " <vertices id=\"v0\">\n";
|
||
out << " <input semantic=\"POSITION\" source=\"#geom0-positions\"/>\n";
|
||
out << " </vertices>\n";
|
||
out << " <triangles count=\"4\">\n";
|
||
out << " <input semantic=\"VERTEX\" source=\"#v0\" offset=\"0\"/>\n";
|
||
out << " <p>0 1 2 0 3 1 1 3 2 0 2 3</p>\n";
|
||
out << " </triangles>\n";
|
||
out << " </mesh>\n";
|
||
out << " </geometry>\n";
|
||
out << " </library_geometries>\n";
|
||
out << " <library_visual_scenes>\n";
|
||
out << " <visual_scene id=\"S\"><node name=\"Tet\"><instance_geometry url=\"#geom0\"/></node></visual_scene>\n";
|
||
out << " </library_visual_scenes>\n";
|
||
out << " <scene><instance_visual_scene url=\"#S\"/></scene>\n";
|
||
out << "</COLLADA>\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 << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
|
||
out << "<COLLADA xmlns=\"http://www.collada.org/2005/11/COLLADASchema\" version=\"1.4.1\">\n";
|
||
out << " <library_geometries>\n";
|
||
out << " <geometry id=\"g1\" name=\"Tri\">\n";
|
||
out << " <mesh>\n";
|
||
out << " <source id=\"s1\">\n";
|
||
out << " <float_array id=\"fa1\" count=\"9\">1 0 0 2 0 0 1.5 0.866 0</float_array>\n";
|
||
out << " <technique_common>\n";
|
||
out << " <accessor source=\"#fa1\" count=\"3\" stride=\"3\">\n";
|
||
out << " <param name=\"X\" type=\"float\"/>\n";
|
||
out << " <param name=\"Y\" type=\"float\"/>\n";
|
||
out << " <param name=\"Z\" type=\"float\"/>\n";
|
||
out << " </accessor>\n";
|
||
out << " </technique_common>\n";
|
||
out << " </source>\n";
|
||
out << " <vertices id=\"v1\">\n";
|
||
out << " <input semantic=\"POSITION\" source=\"#s1\"/>\n";
|
||
out << " </vertices>\n";
|
||
out << " <triangles count=\"1\">\n";
|
||
out << " <input semantic=\"VERTEX\" source=\"#v1\" offset=\"0\"/>\n";
|
||
out << " <p>0 1 2</p>\n";
|
||
out << " </triangles>\n";
|
||
out << " </mesh>\n";
|
||
out << " </geometry>\n";
|
||
out << " </library_geometries>\n";
|
||
out << " <library_visual_scenes>\n";
|
||
out << " <visual_scene id=\"S\"><node name=\"Tri\"><instance_geometry url=\"#g1\"/></node></visual_scene>\n";
|
||
out << " </library_visual_scenes>\n";
|
||
out << " <scene><instance_visual_scene url=\"#S\"/></scene>\n";
|
||
out << "</COLLADA>\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<vde::brep::BrepModel> 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<std::uintmax_t>(in.tellg());
|
||
}
|
||
|
||
std::size_t imported_face_count(const std::vector<vde::brep::BrepModel>& 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<vde::brep::BrepModel> 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<std::chrono::milliseconds>(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<vde::brep::BrepModel> 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<vde::brep::BrepModel> 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<vde::brep::BrepModel> 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 =
|
||
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
|
||
"<COLLADA xmlns=\"http://www.collada.org/2005/11/COLLADASchema\" version=\"1.4.1\">\n"
|
||
" <library_materials>\n"
|
||
" <material id=\"matA-material\" name=\"RedMat\">\n"
|
||
" <instance_effect url=\"#effectA\"/>\n"
|
||
" </material>\n"
|
||
" <material id=\"matB-material\" name=\"BlueMat\">\n"
|
||
" <instance_effect url=\"#effectB\"/>\n"
|
||
" </material>\n"
|
||
" </library_materials>\n"
|
||
" <library_geometries>\n"
|
||
" <geometry id=\"g1\" name=\"TwoPrims\">\n"
|
||
" <mesh>\n"
|
||
" <source id=\"s1\">\n"
|
||
" <float_array id=\"fa1\" count=\"18\">0 0 0 1 0 0 0 1 0 2 0 0 3 0 0 2 1 0</float_array>\n"
|
||
" <technique_common><accessor source=\"#fa1\" count=\"6\" stride=\"3\">\n"
|
||
" <param name=\"X\" type=\"float\"/><param name=\"Y\" type=\"float\"/><param name=\"Z\" type=\"float\"/>\n"
|
||
" </accessor></technique_common>\n"
|
||
" </source>\n"
|
||
" <vertices id=\"v1\"><input semantic=\"POSITION\" source=\"#s1\"/></vertices>\n"
|
||
" <triangles count=\"1\" material=\"MAT_A\">\n"
|
||
" <input semantic=\"VERTEX\" source=\"#v1\" offset=\"0\"/><p>0 1 2</p>\n"
|
||
" </triangles>\n"
|
||
" <triangles count=\"1\" material=\"MAT_B\">\n"
|
||
" <input semantic=\"VERTEX\" source=\"#v1\" offset=\"0\"/><p>3 4 5</p>\n"
|
||
" </triangles>\n"
|
||
" </mesh>\n"
|
||
" </geometry>\n"
|
||
" <geometry id=\"g2\" name=\"QuadPoly\">\n"
|
||
" <mesh>\n"
|
||
" <source id=\"s2\">\n"
|
||
" <float_array id=\"fa2\" count=\"12\">0 0 0 2 0 0 2 2 0 0 2 0</float_array>\n"
|
||
" <technique_common><accessor source=\"#fa2\" count=\"4\" stride=\"3\">\n"
|
||
" <param name=\"X\" type=\"float\"/><param name=\"Y\" type=\"float\"/><param name=\"Z\" type=\"float\"/>\n"
|
||
" </accessor></technique_common>\n"
|
||
" </source>\n"
|
||
" <vertices id=\"v2\"><input semantic=\"POSITION\" source=\"#s2\"/></vertices>\n"
|
||
" <polylist count=\"1\" material=\"MAT_B\">\n"
|
||
" <input semantic=\"VERTEX\" source=\"#v2\" offset=\"0\"/>\n"
|
||
" <vcount>4</vcount><p>0 1 2 3</p>\n"
|
||
" </polylist>\n"
|
||
" </mesh>\n"
|
||
" </geometry>\n"
|
||
" </library_geometries>\n"
|
||
" <library_visual_scenes>\n"
|
||
" <visual_scene id=\"Scene\">\n"
|
||
" <node id=\"n1\">\n"
|
||
" <instance_geometry url=\"#g1\">\n"
|
||
" <bind_material><technique_common>\n"
|
||
" <instance_material symbol=\"MAT_A\" target=\"#matA-material\"/>\n"
|
||
" <instance_material symbol=\"MAT_B\" target=\"#matB-material\"/>\n"
|
||
" </technique_common></bind_material>\n"
|
||
" </instance_geometry>\n"
|
||
" </node>\n"
|
||
" </visual_scene>\n"
|
||
" </library_visual_scenes>\n"
|
||
" <scene><instance_visual_scene url=\"#Scene\"/></scene>\n"
|
||
"</COLLADA>\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 = "<library_materials>";
|
||
const size_t mat_begin = step.find(with_materials);
|
||
ASSERT_NE(mat_begin, std::string::npos);
|
||
const size_t mat_end = step.find("</library_materials>", mat_begin);
|
||
ASSERT_NE(mat_end, std::string::npos);
|
||
step.erase(mat_begin, mat_end + std::string("</library_materials>").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 =
|
||
" <library_images><image id=\"img0\"><init_from>tex.png</init_from></image></library_images>\n"
|
||
" <library_animations><animation id=\"a0\"></animation></library_animations>\n";
|
||
const size_t insert = collada.find(" <library_materials>");
|
||
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";
|
||
}
|
||
|