### P2-5b(VDE-1249 线框残余收口) - 共形网格支持单侧极点退化 patch:极点行/列塌缩为同一极点(零面积三角形过滤),其余三侧用 DisplaySharedEdges 共享采样 → Sphere patch 与相邻 Cylinder 逐位一致;仅对 min(nu,nv)≥8 的球面 patch 启用;小球面 patch 保留 HEAD; - 实测:26 面圆角盒 open 960→96(Plane24/Cylinder72/Sphere0,目标≤100),tris 6500→6348(97.7%,≥95%); box/box+1hole/box+4holes/box+16holes/chain open 保持 0;box+16holes display 130.3ms; - test_display_mesh 13/13、test_capi_display 17/17、test_vde1239_display_quality 13/13、test_vde1238 20/20; RoundedBox 断言按新语义更新(open≤100 + 密度≥6175 + Draft.open≤Normal.open); ### P3-3 PLY 二进制读写 - plugins/official/ply 重写(C ABI/ABI 1.2 不变):读 ascii/binary LE/binary BE(全标量类型、按属性名识别 x/y/z、 额外属性跳过、face list 索引、n>4 支持);写默认 ASCII、.plyb 或 VDE_PLY_WRITE_FORMAT 选择 binary LE; - capabilities format:ply.read;format:ply.write、extensions .ply;.plyb;test_plugin_ply_binary 8/8、format_official 10/10、 插件/PLY 子集 105/105;已知限制(BE 只读、写出顶点不共享等)已写入文档 §11.2; ### VDE-1249 内核侧响应 - docs/feedback/VDE-1249.md 更新 partially fixed:三阻塞修复记录、线框三项来源与建议(tight wireframe/legacy 仅 B-Rep 边)、 save→load 未复现说明与临时规避、display 三角预算参考、宿主 N80 复测指引; ### 工程 - 版本 1.0.67(CMake/version.h/test_version/README/docs index/module-manual); - 全量构建 0 error(345 targets);全量 ctest 7452 注册、7357/7357 PASS(0 failed,25 skipped,193.90s)。
420 lines
16 KiB
C++
420 lines
16 KiB
C++
// P3-3 官方 PLY 插件二进制格式测试(读 ASCII/binary LE/BE、写 ASCII/binary LE)。
|
||
//
|
||
// 覆盖:
|
||
// 1) ASCII / binary_little_endian / binary_big_endian 三种编码读取结果一致;
|
||
// 2) binary 顶点额外属性(normal/颜色)与多属性跳过;
|
||
// 3) 写出 ASCII(.ply)/ binary_little_endian(.plyb 或 VDE_PLY_WRITE_FORMAT)后重读一致;
|
||
// 4) 坏文件失败隔离(截断/非法编码/索引越界)后仍可继续导入;
|
||
// 5) manifest 能力含 format:ply.read;format:ply.write 且 extensions 含 .plyb。
|
||
#include <gtest/gtest.h>
|
||
|
||
#include <algorithm>
|
||
#include <array>
|
||
#include <cmath>
|
||
#include <cstdint>
|
||
#include <cstdio>
|
||
#include <cstdlib>
|
||
#include <cstring>
|
||
#include <fstream>
|
||
#include <string>
|
||
#include <vector>
|
||
|
||
#ifndef VDE_PLUGIN_OFFICIAL_DIR
|
||
#define VDE_PLUGIN_OFFICIAL_DIR ""
|
||
#endif
|
||
|
||
#include "vde/plugin/geometry_host_services.h"
|
||
#include "vde/plugin/plugin_manager.h"
|
||
|
||
namespace {
|
||
|
||
using vde::plugin::FormatImportResult;
|
||
using vde::plugin::PluginManager;
|
||
|
||
constexpr const char* kPlyPluginId = "com.vde.official.ply";
|
||
constexpr double kHalfX = 10.0;
|
||
constexpr double kHalfY = 15.0;
|
||
constexpr double kHalfZ = 20.0;
|
||
constexpr double kVolume = 20.0 * 30.0 * 40.0;
|
||
|
||
std::string temp_path(const char* name) { return std::string(VDE_TEST_TEMP_DIR) + name; }
|
||
|
||
bool write_bytes_file(const std::string& path, const std::string& bytes) {
|
||
std::ofstream out(path, std::ios::binary | std::ios::trunc);
|
||
if (!out.good()) return false;
|
||
out.write(bytes.data(), static_cast<std::streamsize>(bytes.size()));
|
||
out.close();
|
||
return out.good();
|
||
}
|
||
|
||
std::string read_bytes_file(const std::string& path) {
|
||
std::ifstream in(path, std::ios::binary);
|
||
if (!in.good()) return {};
|
||
return std::string((std::istreambuf_iterator<char>(in)),
|
||
std::istreambuf_iterator<char>());
|
||
}
|
||
|
||
bool set_process_env(const char* name, const char* value) {
|
||
#ifdef _WIN32
|
||
return _putenv_s(name, value ? value : "") == 0;
|
||
#else
|
||
return value ? (setenv(name, value, 1) == 0) : (unsetenv(name) == 0);
|
||
#endif
|
||
}
|
||
|
||
// ── 立方体几何夹具(20×30×40,8 顶点 / 12 三角面)──
|
||
struct Cube {
|
||
std::vector<std::array<double, 3>> points;
|
||
std::vector<std::array<int, 3>> triangles;
|
||
};
|
||
|
||
Cube make_cube() {
|
||
Cube cube;
|
||
cube.points = {{{-kHalfX, -kHalfY, -kHalfZ},
|
||
{kHalfX, -kHalfY, -kHalfZ},
|
||
{kHalfX, kHalfY, -kHalfZ},
|
||
{-kHalfX, kHalfY, -kHalfZ},
|
||
{-kHalfX, -kHalfY, kHalfZ},
|
||
{kHalfX, -kHalfY, kHalfZ},
|
||
{kHalfX, kHalfY, kHalfZ},
|
||
{-kHalfX, kHalfY, kHalfZ}}};
|
||
cube.triangles = {{{0, 2, 1}, {0, 3, 2}, {4, 5, 6}, {4, 6, 7}, {0, 1, 5}, {0, 5, 4},
|
||
{1, 2, 6}, {1, 6, 5}, {2, 3, 7}, {2, 7, 6}, {3, 0, 4}, {3, 4, 7}}};
|
||
return cube;
|
||
}
|
||
|
||
std::string ply_ascii(const Cube& cube, bool with_normals) {
|
||
std::string out = "ply\nformat ascii 1.0\n";
|
||
out += "element vertex " + std::to_string(cube.points.size()) + "\n";
|
||
out += "property float x\nproperty float y\nproperty float z\n";
|
||
if (with_normals) out += "property float nx\nproperty float ny\nproperty float nz\n";
|
||
out += "element face " + std::to_string(cube.triangles.size()) + "\n";
|
||
out += "property list uchar int vertex_indices\nend_header\n";
|
||
char line[256];
|
||
for (const auto& point : cube.points) {
|
||
std::snprintf(line, sizeof(line), "%.9g %.9g %.9g", point[0], point[1], point[2]);
|
||
out += line;
|
||
if (with_normals) out += " 0 0 1";
|
||
out += "\n";
|
||
}
|
||
for (const auto& tri : cube.triangles) {
|
||
std::snprintf(line, sizeof(line), "3 %d %d %d\n", tri[0], tri[1], tri[2]);
|
||
out += line;
|
||
}
|
||
return out;
|
||
}
|
||
|
||
void append_float(std::string* out, float value, bool little_endian) {
|
||
unsigned char bytes[4];
|
||
std::memcpy(bytes, &value, 4);
|
||
if (!little_endian) std::swap(bytes[0], bytes[3]), std::swap(bytes[1], bytes[2]);
|
||
out->append(reinterpret_cast<const char*>(bytes), 4);
|
||
}
|
||
|
||
void append_u32(std::string* out, uint32_t value, bool little_endian) {
|
||
unsigned char bytes[4] = {static_cast<unsigned char>(value & 0xFFu),
|
||
static_cast<unsigned char>((value >> 8) & 0xFFu),
|
||
static_cast<unsigned char>((value >> 16) & 0xFFu),
|
||
static_cast<unsigned char>((value >> 24) & 0xFFu)};
|
||
if (!little_endian) std::swap(bytes[0], bytes[3]), std::swap(bytes[1], bytes[2]);
|
||
out->append(reinterpret_cast<const char*>(bytes), 4);
|
||
}
|
||
|
||
std::string ply_binary(const Cube& cube, bool little_endian, bool with_normals,
|
||
bool with_color) {
|
||
std::string out = "ply\nformat ";
|
||
out += little_endian ? "binary_little_endian 1.0\n" : "binary_big_endian 1.0\n";
|
||
out += "element vertex " + std::to_string(cube.points.size()) + "\n";
|
||
out += "property float x\nproperty float y\nproperty float z\n";
|
||
if (with_normals) out += "property float nx\nproperty float ny\nproperty float nz\n";
|
||
if (with_color) out += "property uchar red\nproperty uchar green\nproperty uchar blue\n";
|
||
out += "element face " + std::to_string(cube.triangles.size()) + "\n";
|
||
out += "property list uchar int vertex_indices\nend_header\n";
|
||
for (const auto& point : cube.points) {
|
||
append_float(&out, static_cast<float>(point[0]), little_endian);
|
||
append_float(&out, static_cast<float>(point[1]), little_endian);
|
||
append_float(&out, static_cast<float>(point[2]), little_endian);
|
||
if (with_normals) {
|
||
append_float(&out, 0.0f, little_endian);
|
||
append_float(&out, 0.0f, little_endian);
|
||
append_float(&out, 1.0f, little_endian);
|
||
}
|
||
if (with_color) {
|
||
out.push_back(static_cast<char>(255));
|
||
out.push_back(static_cast<char>(128));
|
||
out.push_back(static_cast<char>(0));
|
||
}
|
||
}
|
||
for (const auto& tri : cube.triangles) {
|
||
out.push_back(static_cast<char>(3));
|
||
append_u32(&out, static_cast<uint32_t>(tri[0]), little_endian);
|
||
append_u32(&out, static_cast<uint32_t>(tri[1]), little_endian);
|
||
append_u32(&out, static_cast<uint32_t>(tri[2]), little_endian);
|
||
}
|
||
return out;
|
||
}
|
||
|
||
struct Metrics {
|
||
bool valid = false;
|
||
double volume = 0.0;
|
||
int faces = 0;
|
||
double bmin[3] = {0.0, 0.0, 0.0};
|
||
double bmax[3] = {0.0, 0.0, 0.0};
|
||
};
|
||
|
||
class PlyBinaryFixture : 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);
|
||
manager.scan_search_paths();
|
||
}
|
||
void TearDown() override {
|
||
set_process_env("VDE_PLY_WRITE_FORMAT", nullptr);
|
||
PluginManager::instance().clear();
|
||
vde::plugin::release_geometry_host_services(&host_);
|
||
for (const auto& path : cleanup_paths_) std::remove(path.c_str());
|
||
cleanup_paths_.clear();
|
||
}
|
||
|
||
bool load_ply(std::string* error) {
|
||
PluginManager& manager = PluginManager::instance();
|
||
if (manager.is_loaded(kPlyPluginId)) return true;
|
||
return manager.load(kPlyPluginId, error);
|
||
}
|
||
|
||
std::string save_fixture(const char* name, const std::string& bytes) {
|
||
const std::string path = temp_path(name);
|
||
EXPECT_TRUE(write_bytes_file(path, bytes)) << path;
|
||
cleanup_paths_.push_back(path);
|
||
return path;
|
||
}
|
||
|
||
/// 经 PluginManager 导入并汇总指标;句柄由宿主销毁
|
||
Metrics import_metrics(const std::string& path, std::string* error) {
|
||
Metrics metrics;
|
||
FormatImportResult result;
|
||
PluginManager& manager = PluginManager::instance();
|
||
if (!manager.import_file(path, &result)) {
|
||
if (error) *error = manager.last_error();
|
||
return metrics;
|
||
}
|
||
if (result.bodies.empty()) {
|
||
if (error) *error = "import_file returned no bodies";
|
||
return metrics;
|
||
}
|
||
bool have_bbox = false;
|
||
for (VdeBody body : result.bodies) {
|
||
VdeBodyInfo info{};
|
||
info.struct_size = sizeof(VdeBodyInfo);
|
||
if (host_.body_query(body, &info) == VDE_PLUGIN_OK) {
|
||
metrics.valid = true;
|
||
metrics.volume += info.volume;
|
||
metrics.faces += static_cast<int>(info.num_faces);
|
||
for (int i = 0; i < 3; ++i) {
|
||
if (!have_bbox || info.bbox_min[i] < metrics.bmin[i])
|
||
metrics.bmin[i] = info.bbox_min[i];
|
||
if (!have_bbox || info.bbox_max[i] > metrics.bmax[i])
|
||
metrics.bmax[i] = info.bbox_max[i];
|
||
}
|
||
have_bbox = true;
|
||
}
|
||
host_.body_destroy(body);
|
||
}
|
||
result.bodies.clear();
|
||
return metrics;
|
||
}
|
||
|
||
void expect_cube_metrics(const Metrics& metrics, double volume_tolerance) {
|
||
ASSERT_TRUE(metrics.valid);
|
||
EXPECT_NEAR(std::abs(metrics.volume), kVolume, volume_tolerance);
|
||
EXPECT_EQ(metrics.faces, 12);
|
||
EXPECT_NEAR(metrics.bmin[0], -kHalfX, 1e-6);
|
||
EXPECT_NEAR(metrics.bmax[0], kHalfX, 1e-6);
|
||
EXPECT_NEAR(metrics.bmin[1], -kHalfY, 1e-6);
|
||
EXPECT_NEAR(metrics.bmax[1], kHalfY, 1e-6);
|
||
EXPECT_NEAR(metrics.bmin[2], -kHalfZ, 1e-6);
|
||
EXPECT_NEAR(metrics.bmax[2], kHalfZ, 1e-6);
|
||
}
|
||
|
||
std::string export_body(VdeBody body, const char* file_name) {
|
||
const std::string path = temp_path(file_name);
|
||
cleanup_paths_.push_back(path);
|
||
std::vector<VdeBody> bodies{body};
|
||
PluginManager& manager = PluginManager::instance();
|
||
EXPECT_TRUE(manager.export_file(path, bodies, "ply")) << manager.last_error();
|
||
return path;
|
||
}
|
||
|
||
VdePluginHost host_{};
|
||
std::vector<std::string> cleanup_paths_;
|
||
};
|
||
|
||
} // namespace
|
||
|
||
TEST_F(PlyBinaryFixture, ReadAsciiAndBinaryEncodingsAreConsistent) {
|
||
std::string error;
|
||
ASSERT_TRUE(load_ply(&error)) << error;
|
||
const Cube cube = make_cube();
|
||
const std::string ascii_path = save_fixture("p33_cube_ascii.ply", ply_ascii(cube, false));
|
||
const std::string le_path = save_fixture("p33_cube_le.plyb", ply_binary(cube, true, false, false));
|
||
const std::string be_path = save_fixture("p33_cube_be.plyb", ply_binary(cube, false, false, false));
|
||
|
||
const Metrics ascii = import_metrics(ascii_path, &error);
|
||
ASSERT_TRUE(ascii.valid) << error;
|
||
expect_cube_metrics(ascii, 1e-6);
|
||
|
||
const Metrics little = import_metrics(le_path, &error);
|
||
ASSERT_TRUE(little.valid) << error;
|
||
expect_cube_metrics(little, 1e-6);
|
||
|
||
const Metrics big = import_metrics(be_path, &error);
|
||
ASSERT_TRUE(big.valid) << error;
|
||
expect_cube_metrics(big, 1e-6);
|
||
|
||
for (int i = 0; i < 3; ++i) {
|
||
EXPECT_NEAR(ascii.bmin[i], little.bmin[i], 1e-6);
|
||
EXPECT_NEAR(ascii.bmin[i], big.bmin[i], 1e-6);
|
||
EXPECT_NEAR(ascii.bmax[i], little.bmax[i], 1e-6);
|
||
EXPECT_NEAR(ascii.bmax[i], big.bmax[i], 1e-6);
|
||
}
|
||
EXPECT_EQ(ascii.faces, little.faces);
|
||
EXPECT_EQ(ascii.faces, big.faces);
|
||
EXPECT_NEAR(ascii.volume, little.volume, 1e-6);
|
||
EXPECT_NEAR(ascii.volume, big.volume, 1e-6);
|
||
}
|
||
|
||
TEST_F(PlyBinaryFixture, ReadBinaryWithNormalsAndExtraProperties) {
|
||
std::string error;
|
||
ASSERT_TRUE(load_ply(&error)) << error;
|
||
const Cube cube = make_cube();
|
||
const std::string path =
|
||
save_fixture("p33_cube_props.plyb", ply_binary(cube, true, true, true));
|
||
const Metrics metrics = import_metrics(path, &error);
|
||
ASSERT_TRUE(metrics.valid) << error;
|
||
expect_cube_metrics(metrics, 1e-6);
|
||
}
|
||
|
||
TEST_F(PlyBinaryFixture, ReadAsciiWithNormals) {
|
||
std::string error;
|
||
ASSERT_TRUE(load_ply(&error)) << error;
|
||
const Cube cube = make_cube();
|
||
const std::string path = save_fixture("p33_cube_ascii_props.ply", ply_ascii(cube, true));
|
||
const Metrics metrics = import_metrics(path, &error);
|
||
ASSERT_TRUE(metrics.valid) << error;
|
||
expect_cube_metrics(metrics, 1e-6);
|
||
}
|
||
|
||
TEST_F(PlyBinaryFixture, ManifestCapabilitiesAndExtensions) {
|
||
std::string error;
|
||
ASSERT_TRUE(load_ply(&error)) << error;
|
||
PluginManager& manager = PluginManager::instance();
|
||
EXPECT_TRUE(manager.capabilities().has("format:ply.read"));
|
||
EXPECT_TRUE(manager.capabilities().has("format:ply.write"));
|
||
const auto formats = manager.formats();
|
||
bool found = false;
|
||
for (const auto& format : formats) {
|
||
if (format.format_id != "ply") continue;
|
||
found = true;
|
||
EXPECT_NE(format.extensions.find(".ply"), std::string::npos);
|
||
EXPECT_NE(format.extensions.find(".plyb"), std::string::npos);
|
||
}
|
||
EXPECT_TRUE(found);
|
||
}
|
||
|
||
TEST_F(PlyBinaryFixture, WriteAsciiThenReadBack) {
|
||
std::string error;
|
||
ASSERT_TRUE(load_ply(&error)) << error;
|
||
VdeBody body = host_.body_create_box(20.0, 30.0, 40.0);
|
||
ASSERT_NE(body, nullptr);
|
||
const std::string path = export_body(body, "p33_write_ascii.ply");
|
||
host_.body_destroy(body);
|
||
ASSERT_FALSE(path.empty());
|
||
const std::string bytes = read_bytes_file(path);
|
||
ASSERT_FALSE(bytes.empty());
|
||
EXPECT_NE(bytes.find("format ascii 1.0"), std::string::npos);
|
||
const Metrics metrics = import_metrics(path, &error);
|
||
ASSERT_TRUE(metrics.valid) << error;
|
||
expect_cube_metrics(metrics, 1e-3);
|
||
}
|
||
|
||
TEST_F(PlyBinaryFixture, WriteBinaryLittleThenReadBack) {
|
||
std::string error;
|
||
ASSERT_TRUE(load_ply(&error)) << error;
|
||
VdeBody body = host_.body_create_box(20.0, 30.0, 40.0);
|
||
ASSERT_NE(body, nullptr);
|
||
const std::string path = export_body(body, "p33_write_le.plyb");
|
||
host_.body_destroy(body);
|
||
const std::string bytes = read_bytes_file(path);
|
||
ASSERT_FALSE(bytes.empty());
|
||
EXPECT_NE(bytes.find("format binary_little_endian 1.0"), std::string::npos);
|
||
const Metrics metrics = import_metrics(path, &error);
|
||
ASSERT_TRUE(metrics.valid) << error;
|
||
expect_cube_metrics(metrics, 1e-3);
|
||
}
|
||
|
||
TEST_F(PlyBinaryFixture, WriteBinaryViaEnvironmentOverride) {
|
||
std::string error;
|
||
ASSERT_TRUE(load_ply(&error)) << error;
|
||
VdeBody body = host_.body_create_box(20.0, 30.0, 40.0);
|
||
ASSERT_NE(body, nullptr);
|
||
ASSERT_TRUE(set_process_env("VDE_PLY_WRITE_FORMAT", "binary_little_endian"));
|
||
const std::string path = export_body(body, "p33_write_env.ply");
|
||
set_process_env("VDE_PLY_WRITE_FORMAT", nullptr);
|
||
host_.body_destroy(body);
|
||
const std::string bytes = read_bytes_file(path);
|
||
ASSERT_FALSE(bytes.empty());
|
||
EXPECT_NE(bytes.find("format binary_little_endian 1.0"), std::string::npos);
|
||
const Metrics metrics = import_metrics(path, &error);
|
||
ASSERT_TRUE(metrics.valid) << error;
|
||
expect_cube_metrics(metrics, 1e-3);
|
||
}
|
||
|
||
TEST_F(PlyBinaryFixture, BadFilesFailureIsolation) {
|
||
std::string error;
|
||
ASSERT_TRUE(load_ply(&error)) << error;
|
||
const Cube cube = make_cube();
|
||
|
||
// 1) 截断二进制:面数据被砍掉一半
|
||
std::string truncated = ply_binary(cube, true, false, false);
|
||
truncated.resize(truncated.size() / 2);
|
||
const std::string trunc_path = save_fixture("p33_truncated.plyb", truncated);
|
||
error.clear();
|
||
const Metrics truncated_metrics = import_metrics(trunc_path, &error);
|
||
EXPECT_FALSE(truncated_metrics.valid);
|
||
EXPECT_FALSE(error.empty());
|
||
|
||
// 2) 非法编码
|
||
std::string bad_format = ply_ascii(cube, false);
|
||
const std::size_t pos = bad_format.find("ascii");
|
||
ASSERT_NE(pos, std::string::npos);
|
||
bad_format.replace(pos, 5, "binary_middle_endian");
|
||
const std::string bad_format_path = save_fixture("p33_bad_format.ply", bad_format);
|
||
error.clear();
|
||
const Metrics bad_format_metrics = import_metrics(bad_format_path, &error);
|
||
EXPECT_FALSE(bad_format_metrics.valid);
|
||
EXPECT_FALSE(error.empty());
|
||
|
||
// 3) 面索引越界
|
||
std::string bad_index = ply_ascii(cube, false);
|
||
const std::size_t face_pos = bad_index.find("3 0 2 1");
|
||
ASSERT_NE(face_pos, std::string::npos);
|
||
bad_index.replace(face_pos, 7, "3 0 2 99");
|
||
const std::string bad_index_path = save_fixture("p33_bad_index.ply", bad_index);
|
||
error.clear();
|
||
const Metrics bad_index_metrics = import_metrics(bad_index_path, &error);
|
||
EXPECT_FALSE(bad_index_metrics.valid);
|
||
EXPECT_FALSE(error.empty());
|
||
|
||
// 4) 失败隔离:坏文件之后正常文件仍可导入
|
||
const std::string good_path = save_fixture("p33_good_after_bad.ply", ply_ascii(cube, false));
|
||
const Metrics good = import_metrics(good_path, &error);
|
||
ASSERT_TRUE(good.valid) << error;
|
||
expect_cube_metrics(good, 1e-6);
|
||
}
|