Files
ViewDesignEngine/tests/plugin/test_official_ply.cpp
T

764 lines
30 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// WP-P4-44 官方 PLY 格式插件测试(确定性 mock 宿主 + 真实 C ABI 动态加载)。
//
// 覆盖:
// 1) query/init/describe/format_id/extensions/capabilities(.ply;.plyb);
// 2) binary_little_endian 读:vertex float x/y/z + face list uchar/int;vertex double + face list ushort/ushort;
// 额外标量/list 属性跳过;ASCII 兼容读取;
// 3) 截断、非法/未知属性类型、索引越界、顶点上限(big-endian 由既有 PLY binary 测试覆盖);
// 4) 写出:ASCII 默认、.plyb / VDE_PLY_WRITE_FORMAT 二进制;VDE_PLY_WRITE_FLOAT=double、
// VDE_PLY_WRITE_INDEX=uchar|ushort|int|uint;P4-51:VDE_PLY_WRITE_NORMALS=1 可选写 nx/ny/nz
// (默认 0 不写;tessellation normals 缺失/NaN 按三角面法向回退,退化面 0,0,1);
// write_file/write_memory 结果可回读;
// 5) 非法写选项/空实体/无三角化等错误路径。
//
// 本测试只依赖纯 C ABI(vde_plugin_api.h)+ gtest,不链接内核。
#include <gtest/gtest.h>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <filesystem>
#include <fstream>
#include <string>
#include <vector>
#if defined(_WIN32)
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
#else
#include <dlfcn.h>
#endif
#include "vde/plugin/vde_plugin_api.h"
#ifndef VDE_PLUGIN_OFFICIAL_DIR
#define VDE_PLUGIN_OFFICIAL_DIR ""
#endif
#ifndef VDE_TEST_TEMP_DIR
#define VDE_TEST_TEMP_DIR ""
#endif
namespace {
constexpr const char* kPluginId = "com.vde.official.ply";
constexpr const char* kFormatId = "ply";
#if defined(_WIN32)
constexpr const char* kPluginRelativePath = "/ply/vde_official_ply.dll";
#elif defined(__APPLE__)
constexpr const char* kPluginRelativePath = "/ply/libvde_official_ply.dylib";
#else
constexpr const char* kPluginRelativePath = "/ply/libvde_official_ply.so";
#endif
#if defined(_WIN32)
using LibraryHandle = HMODULE;
using RawSymbol = void (*)();
LibraryHandle open_library(const char* path) { return ::LoadLibraryA(path); }
RawSymbol find_symbol(LibraryHandle library, const char* name) {
return reinterpret_cast<RawSymbol>(::GetProcAddress(library, name));
}
void close_library(LibraryHandle library) {
if (library) ::FreeLibrary(library);
}
#else
using LibraryHandle = void*;
using RawSymbol = void (*)();
LibraryHandle open_library(const char* path) { return ::dlopen(path, RTLD_NOW | RTLD_LOCAL); }
RawSymbol find_symbol(LibraryHandle library, const char* name) {
return reinterpret_cast<RawSymbol>(::dlsym(library, name));
}
void close_library(LibraryHandle library) {
if (library) ::dlclose(library);
}
#endif
std::string official_dir();
struct PluginApi {
LibraryHandle library = nullptr;
VdePluginQueryFn query = nullptr;
VdePluginInitFn init = nullptr;
VdePluginShutdownFn shutdown = nullptr;
const VdeFormatPlugin* format = nullptr;
~PluginApi() { close(); }
bool open(std::string* error) {
const std::string path = official_dir() + kPluginRelativePath;
library = open_library(path.c_str());
if (!library) {
if (error) *error = "无法加载插件动态库: " + path;
return false;
}
query = reinterpret_cast<VdePluginQueryFn>(find_symbol(library, "vde_plugin_query"));
init = reinterpret_cast<VdePluginInitFn>(find_symbol(library, "vde_plugin_init"));
shutdown = reinterpret_cast<VdePluginShutdownFn>(
find_symbol(library, "vde_plugin_shutdown"));
if (!query || !init) {
if (error) *error = "插件缺少 query/init 导出: " + path;
close();
return false;
}
return true;
}
void close() {
if (library) close_library(library);
library = nullptr;
query = nullptr;
init = nullptr;
shutdown = nullptr;
format = nullptr;
}
};
std::string env_or_macro(const char* macro_value, const char* env_name) {
if (macro_value && macro_value[0] != '\0') return std::string(macro_value);
const char* env = std::getenv(env_name);
return env && *env ? std::string(env) : std::string();
}
std::string official_dir() {
std::string dir = env_or_macro(VDE_PLUGIN_OFFICIAL_DIR, "VDE_PLUGIN_OFFICIAL_DIR");
if (dir.empty()) dir = "build-fb-plybin/plugins/official";
return dir;
}
std::string temp_dir() {
std::string dir = env_or_macro(VDE_TEST_TEMP_DIR, "VDE_TEST_TEMP_DIR");
if (dir.empty()) dir = "build-fb-plybin/tmp/";
return dir;
}
std::string temp_path(const char* name) { return temp_dir() + name; }
// ── 确定性 mock 宿主 ────────────────────────────────────────────────
std::string g_last_error;
int g_live_bodies = 0;
struct MockBody {
std::vector<double> positions;
std::vector<std::uint32_t> indices;
std::vector<double> normals; // 可选:与 positions 等长时 tessellate 输出法向
bool fail_tessellate = false;
};
MockBody* as_mock(VdeBody body) { return reinterpret_cast<MockBody*>(body); }
void mock_log(int, const char*) {}
void mock_set_error(const char* message) { g_last_error = message ? message : ""; }
void* mock_alloc(std::size_t size) { return std::malloc(size == 0 ? 1 : size); }
void* mock_realloc(void* pointer, std::size_t size) {
return std::realloc(pointer, size == 0 ? 1 : size);
}
void mock_free(void* pointer) { std::free(pointer); }
void mock_free_buffer(void* pointer) { std::free(pointer); }
VdeBody mock_body_from_mesh(const double* positions, std::size_t num_vertices,
const std::uint32_t* indices, std::size_t num_triangles) {
if (!positions || num_vertices == 0) return nullptr;
if (num_triangles > 0 && !indices) return nullptr;
for (std::size_t i = 0; i < num_triangles * 3u; ++i) {
if (static_cast<std::size_t>(indices[i]) >= num_vertices) return nullptr;
}
auto* body = new MockBody();
body->positions.assign(positions, positions + num_vertices * 3u);
if (num_triangles > 0) body->indices.assign(indices, indices + num_triangles * 3u);
++g_live_bodies;
return reinterpret_cast<VdeBody>(body);
}
void mock_body_destroy(VdeBody handle) {
auto* body = as_mock(handle);
if (!body) return;
delete body;
--g_live_bodies;
}
int mock_body_tessellate(VdeBody handle, double, VdeMeshData* out) {
if (!out || !handle) return VDE_PLUGIN_ERR_INVALID_ARG;
MockBody* body = as_mock(handle);
out->positions = nullptr;
out->num_vertices = 0;
out->indices = nullptr;
out->num_triangles = 0;
out->normals = nullptr;
if (body->fail_tessellate) return VDE_PLUGIN_ERR_INTERNAL;
if (body->indices.empty()) return VDE_PLUGIN_OK;
const std::size_t triangles = body->indices.size() / 3u;
const std::size_t vertices = triangles * 3u;
auto* positions = static_cast<double*>(std::malloc(vertices * 3u * sizeof(double)));
auto* indices = static_cast<std::uint32_t*>(std::malloc(vertices * sizeof(std::uint32_t)));
const bool with_normals = body->normals.size() == body->positions.size();
auto* normals = with_normals
? static_cast<double*>(std::malloc(vertices * 3u * sizeof(double)))
: nullptr;
if (!positions || !indices || (with_normals && !normals)) {
std::free(positions);
std::free(indices);
std::free(normals);
return VDE_PLUGIN_ERR_BUFFER;
}
for (std::size_t t = 0; t < triangles; ++t) {
for (std::size_t k = 0; k < 3; ++k) {
const std::uint32_t source = body->indices[3u * t + k];
positions[(3u * t + k) * 3u + 0u] = body->positions[3u * source + 0u];
positions[(3u * t + k) * 3u + 1u] = body->positions[3u * source + 1u];
positions[(3u * t + k) * 3u + 2u] = body->positions[3u * source + 2u];
if (with_normals) {
normals[(3u * t + k) * 3u + 0u] = body->normals[3u * source + 0u];
normals[(3u * t + k) * 3u + 1u] = body->normals[3u * source + 1u];
normals[(3u * t + k) * 3u + 2u] = body->normals[3u * source + 2u];
}
indices[3u * t + k] = static_cast<std::uint32_t>(3u * t + k);
}
}
out->positions = positions;
out->num_vertices = vertices;
out->indices = indices;
out->num_triangles = triangles;
out->normals = normals;
return VDE_PLUGIN_OK;
}
VdePluginHost make_mock_host() {
VdePluginHost host{};
host.struct_size = static_cast<std::uint32_t>(sizeof(VdePluginHost));
host.abi_major = VDE_PLUGIN_ABI_MAJOR;
host.abi_minor = VDE_PLUGIN_ABI_MINOR;
host.kernel_version = "mock";
host.alloc = &mock_alloc;
host.realloc = &mock_realloc;
host.free = &mock_free;
host.log = &mock_log;
host.set_error = &mock_set_error;
host.body_destroy = &mock_body_destroy;
host.free_buffer = &mock_free_buffer;
host.body_from_mesh = &mock_body_from_mesh;
host.body_tessellate = &mock_body_tessellate;
return host;
}
// ── PLY 文本/二进制构造 ─────────────────────────────────────────────
void append_float_le(std::string* out, float value) {
unsigned char bytes[4];
std::memcpy(bytes, &value, 4);
out->append(reinterpret_cast<const char*>(bytes), 4);
}
void append_double_le(std::string* out, double value) {
unsigned char bytes[8];
std::memcpy(bytes, &value, 8);
out->append(reinterpret_cast<const char*>(bytes), 8);
}
void append_uint_le(std::string* out, std::uint64_t value, std::size_t width) {
unsigned char bytes[8] = {0, 0, 0, 0, 0, 0, 0, 0};
for (std::size_t i = 0; i < width; ++i)
bytes[i] = static_cast<unsigned char>((value >> (8 * i)) & 0xFFu);
out->append(reinterpret_cast<const char*>(bytes), width);
}
std::string binary_ply_float_int() {
std::string out =
"ply\nformat binary_little_endian 1.0\n"
"element vertex 3\nproperty float x\nproperty float y\nproperty float z\n"
"element face 1\nproperty list uchar int vertex_indices\nend_header\n";
const float vertices[3][3] = {{0, 0, 0}, {1, 0, 0}, {0, 1, 0}};
for (const auto& vertex : vertices)
for (float value : vertex) append_float_le(&out, value);
append_uint_le(&out, 3, 1);
append_uint_le(&out, 0, 4);
append_uint_le(&out, 1, 4);
append_uint_le(&out, 2, 4);
return out;
}
std::string binary_ply_double_ushort() {
std::string out =
"ply\nformat binary_little_endian 1.0\n"
"element vertex 4\nproperty double x\nproperty double y\nproperty double z\n"
"property uchar red\n"
"element face 1\nproperty list ushort ushort vertex_indices\nend_header\n";
const double vertices[4][3] = {{0, 0, 0}, {2, 0, 0}, {2, 1, 0}, {0, 1, 0}};
for (const auto& vertex : vertices) {
for (double value : vertex) append_double_le(&out, value);
out.push_back(static_cast<char>(7)); // 额外 uchar 属性
}
append_uint_le(&out, 4, 2);
for (std::uint64_t index : {0ull, 1ull, 2ull, 3ull}) append_uint_le(&out, index, 2);
return out;
}
std::string ascii_ply() {
return std::string(
"ply\nformat ascii 1.0\n"
"element vertex 4\nproperty double x\nproperty double y\nproperty double z\n"
"property float nx\nproperty float ny\nproperty float nz\n"
"element face 1\nproperty list uchar ushort vertex_indices\nend_header\n"
"0 0 0 0 0 1\n1 0 0 0 0 1\n1 1 0 0 0 1\n0 1 0 0 0 1\n"
"4 0 1 2 3\n");
}
std::string read_text_file(const std::string& path) {
std::ifstream in(path, std::ios::binary);
return std::string((std::istreambuf_iterator<char>(in)),
std::istreambuf_iterator<char>());
}
bool write_text_file(const std::string& path, const std::string& text) {
std::ofstream out(path, std::ios::binary | std::ios::trunc);
if (!out.good()) return false;
out.write(text.data(), static_cast<std::streamsize>(text.size()));
out.close();
return out.good();
}
bool contains(const std::string& text, const std::string& needle) {
return text.find(needle) != std::string::npos;
}
class PlyPluginFixture : public ::testing::Test {
protected:
void SetUp() override {
g_last_error.clear();
g_live_bodies = 0;
host_ = make_mock_host();
ASSERT_TRUE(api_.open(&load_error_)) << load_error_;
VdePluginInfo info{};
info.struct_size = static_cast<std::uint32_t>(sizeof(VdePluginInfo));
ASSERT_EQ(api_.query(VDE_PLUGIN_ABI_MAJOR, &info), VDE_PLUGIN_OK);
VdePluginRegistration registration{};
registration.struct_size = static_cast<std::uint32_t>(sizeof(VdePluginRegistration));
ASSERT_EQ(api_.init(&host_, &registration), VDE_PLUGIN_OK) << g_last_error;
ASSERT_EQ(registration.kind, VDE_PLUGIN_KIND_FORMAT);
ASSERT_NE(registration.format, nullptr);
format_ = registration.format;
std::filesystem::create_directories(temp_dir());
}
void TearDown() override {
unsetenv_throwaway();
if (api_.shutdown) api_.shutdown();
api_.close();
EXPECT_EQ(g_live_bodies, 0) << "mock 实体未全部销毁";
}
void unsetenv_throwaway() {
#if defined(_WIN32)
_putenv_s("VDE_PLY_WRITE_FORMAT", "");
_putenv_s("VDE_PLY_WRITE_FLOAT", "");
_putenv_s("VDE_PLY_WRITE_INDEX", "");
_putenv_s("VDE_PLY_WRITE_NORMALS", "");
#else
unsetenv("VDE_PLY_WRITE_FORMAT");
unsetenv("VDE_PLY_WRITE_FLOAT");
unsetenv("VDE_PLY_WRITE_INDEX");
unsetenv("VDE_PLY_WRITE_NORMALS");
#endif
}
int read_memory(const std::string& text, VdeBody** bodies, std::size_t* count) {
g_last_error.clear();
return format_->read_memory(&host_, text.empty() ? nullptr : text.data(),
text.size(), bodies, count);
}
void destroy(VdeBody* bodies, std::size_t count) {
if (!bodies) return;
for (std::size_t i = 0; i < count; ++i) host_.body_destroy(bodies[i]);
host_.free(bodies);
}
VdeBody make_triangle_body(MockBody* source = nullptr) {
MockBody local;
local.positions = {0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0};
local.indices = {0u, 1u, 2u};
if (source) local = *source;
return host_.body_from_mesh(local.positions.data(),
local.positions.size() / 3u,
local.indices.data(), local.indices.size() / 3u);
}
PluginApi api_;
std::string load_error_;
VdePluginHost host_{};
const VdeFormatPlugin* format_ = nullptr;
};
} // namespace
TEST_F(PlyPluginFixture, QueryAndRegistrationMetadata) {
VdePluginInfo info{};
info.struct_size = static_cast<std::uint32_t>(sizeof(VdePluginInfo));
ASSERT_EQ(api_.query(VDE_PLUGIN_ABI_MAJOR, &info), VDE_PLUGIN_OK);
EXPECT_STREQ(info.id, kPluginId);
EXPECT_EQ(info.kind, VDE_PLUGIN_KIND_FORMAT);
EXPECT_EQ(info.abi_major, VDE_PLUGIN_ABI_MAJOR);
EXPECT_EQ(info.abi_minor, VDE_PLUGIN_ABI_MINOR);
EXPECT_STREQ(info.capabilities, "format:ply.read;format:ply.write");
ASSERT_NE(format_, nullptr);
EXPECT_STREQ(format_->format_id, kFormatId);
EXPECT_STREQ(format_->extensions, ".ply;.plyb");
EXPECT_NE(format_->probe, nullptr);
EXPECT_NE(format_->read_file, nullptr);
EXPECT_NE(format_->read_memory, nullptr);
EXPECT_NE(format_->write_file, nullptr);
EXPECT_NE(format_->write_memory, nullptr);
}
TEST_F(PlyPluginFixture, ReadsBinaryLittleEndianFloatAndInt) {
VdeBody* bodies = nullptr;
std::size_t count = 0;
ASSERT_EQ(read_memory(binary_ply_float_int(), &bodies, &count), VDE_PLUGIN_OK)
<< g_last_error;
ASSERT_EQ(count, 1u);
MockBody* body = as_mock(bodies[0]);
ASSERT_NE(body, nullptr);
ASSERT_EQ(body->positions.size(), 9u);
ASSERT_EQ(body->indices.size(), 3u);
EXPECT_DOUBLE_EQ(body->positions[3], 1.0);
EXPECT_DOUBLE_EQ(body->positions[7], 1.0);
EXPECT_EQ(body->indices[0], 0u);
EXPECT_EQ(body->indices[1], 1u);
EXPECT_EQ(body->indices[2], 2u);
destroy(bodies, count);
}
TEST_F(PlyPluginFixture, ReadsBinaryDoubleAndUshortTypesWithExtraProps) {
VdeBody* bodies = nullptr;
std::size_t count = 0;
ASSERT_EQ(read_memory(binary_ply_double_ushort(), &bodies, &count), VDE_PLUGIN_OK)
<< g_last_error;
ASSERT_EQ(count, 1u);
MockBody* body = as_mock(bodies[0]);
ASSERT_NE(body, nullptr);
ASSERT_EQ(body->positions.size(), 12u);
ASSERT_EQ(body->indices.size(), 6u); // quad -> 2 triangles
EXPECT_DOUBLE_EQ(body->positions[3], 2.0);
EXPECT_DOUBLE_EQ(body->positions[10], 1.0);
destroy(bodies, count);
}
TEST_F(PlyPluginFixture, ReadsAsciiCompatibility) {
VdeBody* bodies = nullptr;
std::size_t count = 0;
ASSERT_EQ(read_memory(ascii_ply(), &bodies, &count), VDE_PLUGIN_OK) << g_last_error;
ASSERT_EQ(count, 1u);
MockBody* body = as_mock(bodies[0]);
ASSERT_NE(body, nullptr);
EXPECT_EQ(body->positions.size(), 12u);
EXPECT_EQ(body->indices.size(), 6u);
destroy(bodies, count);
}
TEST_F(PlyPluginFixture, RejectsBigEndianTruncatedAndBadProperties) {
VdeBody* bodies = nullptr;
std::size_t count = 0;
// big-endian 由既有 test_plugin_ply_binary.cpp 覆盖;此处负例从截断开始。
std::string truncated = binary_ply_float_int();
truncated.resize(truncated.size() - 3);
EXPECT_EQ(read_memory(truncated, &bodies, &count), VDE_PLUGIN_ERR_PARSE);
std::string bad_type =
"ply\nformat binary_little_endian 1.0\n"
"element vertex 3\nproperty foo x\nproperty float y\nproperty float z\n"
"element face 1\nproperty list uchar int vertex_indices\nend_header\n";
EXPECT_EQ(read_memory(bad_type, &bodies, &count), VDE_PLUGIN_ERR_PARSE);
std::string bad_index = binary_ply_float_int();
// 修改最后一个索引(int LE)为 99,越界。
const std::size_t index_pos = bad_index.size() - 4;
std::memset(&bad_index[index_pos], 0, 4);
bad_index[index_pos] = 99;
EXPECT_EQ(read_memory(bad_index, &bodies, &count), VDE_PLUGIN_ERR_PARSE);
}
TEST_F(PlyPluginFixture, RejectsVertexLimit) {
const std::string over_limit =
"ply\nformat binary_little_endian 1.0\n"
"element vertex 5000001\nproperty float x\nproperty float y\nproperty float z\n"
"element face 0\nproperty list uchar int vertex_indices\nend_header\n";
VdeBody* bodies = nullptr;
std::size_t count = 0;
EXPECT_EQ(read_memory(over_limit, &bodies, &count), VDE_PLUGIN_ERR_PARSE);
EXPECT_TRUE(contains(g_last_error, "上限")) << g_last_error;
}
TEST_F(PlyPluginFixture, WritesAsciiDefaultAndReadsBack) {
VdeBody body = make_triangle_body();
ASSERT_NE(body, nullptr);
const std::string path = temp_path("write_default.ply");
ASSERT_EQ(format_->write_file(&host_, path.c_str(), &body, 1), VDE_PLUGIN_OK)
<< g_last_error;
const std::string text = read_text_file(path);
EXPECT_TRUE(text.rfind("ply", 0) == 0);
EXPECT_TRUE(contains(text, "format ascii 1.0"));
EXPECT_TRUE(contains(text, "property float x"));
VdeBody* bodies = nullptr;
std::size_t count = 0;
ASSERT_EQ(read_memory(text, &bodies, &count), VDE_PLUGIN_OK) << g_last_error;
ASSERT_EQ(count, 1u);
destroy(bodies, count);
host_.body_destroy(body);
}
TEST_F(PlyPluginFixture, WritesBinaryWithDoubleAndUshortIndex) {
#if defined(_WIN32)
_putenv_s("VDE_PLY_WRITE_FORMAT", "binary_little_endian");
_putenv_s("VDE_PLY_WRITE_FLOAT", "double");
_putenv_s("VDE_PLY_WRITE_INDEX", "ushort");
#else
setenv("VDE_PLY_WRITE_FORMAT", "binary_little_endian", 1);
setenv("VDE_PLY_WRITE_FLOAT", "double", 1);
setenv("VDE_PLY_WRITE_INDEX", "ushort", 1);
#endif
VdeBody body = make_triangle_body();
ASSERT_NE(body, nullptr);
const std::string path = temp_path("write_binary_double.ply");
ASSERT_EQ(format_->write_file(&host_, path.c_str(), &body, 1), VDE_PLUGIN_OK)
<< g_last_error;
const std::string text = read_text_file(path);
EXPECT_TRUE(contains(text, "format binary_little_endian 1.0"));
EXPECT_TRUE(contains(text, "property double x"));
EXPECT_TRUE(contains(text, "property list uchar ushort vertex_indices"));
VdeBody* bodies = nullptr;
std::size_t count = 0;
ASSERT_EQ(read_memory(text, &bodies, &count), VDE_PLUGIN_OK) << g_last_error;
ASSERT_EQ(count, 1u);
MockBody* roundtrip = as_mock(bodies[0]);
EXPECT_EQ(roundtrip->indices.size(), 3u);
destroy(bodies, count);
host_.body_destroy(body);
}
TEST_F(PlyPluginFixture, WritesPlybExtensionForcesBinaryFloatUint) {
VdeBody body = make_triangle_body();
ASSERT_NE(body, nullptr);
const std::string path = temp_path("write_extension.plyb");
ASSERT_EQ(format_->write_file(&host_, path.c_str(), &body, 1), VDE_PLUGIN_OK)
<< g_last_error;
const std::string text = read_text_file(path);
EXPECT_TRUE(contains(text, "format binary_little_endian 1.0"));
EXPECT_TRUE(contains(text, "property float x"));
EXPECT_TRUE(contains(text, "property list uchar int vertex_indices"));
VdeBody* bodies = nullptr;
std::size_t count = 0;
ASSERT_EQ(read_memory(text, &bodies, &count), VDE_PLUGIN_OK);
destroy(bodies, count);
host_.body_destroy(body);
}
TEST_F(PlyPluginFixture, WriteMemoryHonorsAsciiDefault) {
VdeBody body = make_triangle_body();
ASSERT_NE(body, nullptr);
VdeBuffer buffer{};
ASSERT_EQ(format_->write_memory(&host_, &body, 1, &buffer), VDE_PLUGIN_OK)
<< g_last_error;
ASSERT_NE(buffer.data, nullptr);
const std::string text(static_cast<const char*>(buffer.data), buffer.size);
EXPECT_TRUE(contains(text, "format ascii 1.0"));
host_.free_buffer(buffer.data);
host_.body_destroy(body);
}
TEST_F(PlyPluginFixture, RejectsInvalidWriteIndexOptionAndEmptyBodies) {
#if defined(_WIN32)
_putenv_s("VDE_PLY_WRITE_INDEX", "bogus");
#else
setenv("VDE_PLY_WRITE_INDEX", "bogus", 1);
#endif
VdeBody body = make_triangle_body();
ASSERT_NE(body, nullptr);
const std::string path = temp_path("write_bad_option.ply");
EXPECT_EQ(format_->write_file(&host_, path.c_str(), &body, 1),
VDE_PLUGIN_ERR_INVALID_ARG);
EXPECT_TRUE(contains(g_last_error, "VDE_PLY_WRITE_INDEX")) << g_last_error;
EXPECT_EQ(format_->write_file(&host_, path.c_str(), nullptr, 0),
VDE_PLUGIN_ERR_INTERNAL);
host_.body_destroy(body);
}
TEST_F(PlyPluginFixture, ReadsAllIntegerIndexTypes) {
const std::pair<const char*, std::size_t> types[] = {
{"uchar", 1}, {"short", 2}, {"ushort", 2}, {"int", 4}, {"uint", 4}};
for (const auto& type : types) {
std::string text =
std::string("ply\nformat binary_little_endian 1.0\n"
"element vertex 3\nproperty float x\nproperty float y\n"
"property float z\nelement face 1\nproperty list uchar ") +
type.first + " vertex_indices\nend_header\n";
const float vertices[3][3] = {{0, 0, 0}, {1, 0, 0}, {0, 1, 0}};
for (const auto& vertex : vertices)
for (float value : vertex) append_float_le(&text, value);
append_uint_le(&text, 3, 1);
for (std::uint64_t index : {0ull, 1ull, 2ull})
append_uint_le(&text, index, type.second);
VdeBody* bodies = nullptr;
std::size_t count = 0;
ASSERT_EQ(read_memory(text, &bodies, &count), VDE_PLUGIN_OK)
<< type.first << ": " << g_last_error;
ASSERT_EQ(count, 1u);
MockBody* body = as_mock(bodies[0]);
EXPECT_EQ(body->indices.size(), 3u) << type.first;
destroy(bodies, count);
}
}
TEST_F(PlyPluginFixture, WritesUcharAndUintIndexTypes) {
const std::pair<const char*, const char*> types[] = {
{"uchar", "property list uchar uchar vertex_indices"},
{"uint", "property list uchar uint vertex_indices"}};
VdeBody body = make_triangle_body();
ASSERT_NE(body, nullptr);
for (const auto& type : types) {
#if defined(_WIN32)
_putenv_s("VDE_PLY_WRITE_FORMAT", "binary_little_endian");
_putenv_s("VDE_PLY_WRITE_INDEX", type.first);
#else
setenv("VDE_PLY_WRITE_FORMAT", "binary_little_endian", 1);
setenv("VDE_PLY_WRITE_INDEX", type.first, 1);
#endif
const std::string path = temp_path("write_index_type.ply");
ASSERT_EQ(format_->write_file(&host_, path.c_str(), &body, 1), VDE_PLUGIN_OK)
<< type.first << ": " << g_last_error;
const std::string text = read_text_file(path);
EXPECT_TRUE(contains(text, type.second)) << text;
VdeBody* bodies = nullptr;
std::size_t count = 0;
ASSERT_EQ(read_memory(text, &bodies, &count), VDE_PLUGIN_OK)
<< type.first << ": " << g_last_error;
destroy(bodies, count);
}
host_.body_destroy(body);
}
TEST_F(PlyPluginFixture, DefaultWriteOmitsNormals) {
VdeBody body = make_triangle_body();
ASSERT_NE(body, nullptr);
const std::string ascii_path = temp_path("normals_off.ply");
ASSERT_EQ(format_->write_file(&host_, ascii_path.c_str(), &body, 1), VDE_PLUGIN_OK)
<< g_last_error;
const std::string ascii = read_text_file(ascii_path);
EXPECT_FALSE(contains(ascii, "property float nx")) << ascii;
EXPECT_TRUE(contains(ascii, "format ascii 1.0"));
const std::string binary_path = temp_path("normals_off.plyb");
ASSERT_EQ(format_->write_file(&host_, binary_path.c_str(), &body, 1), VDE_PLUGIN_OK)
<< g_last_error;
const std::string binary = read_text_file(binary_path);
EXPECT_FALSE(contains(binary, "property float nx")) << binary;
EXPECT_TRUE(contains(binary, "format binary_little_endian 1.0"));
host_.body_destroy(body);
}
TEST_F(PlyPluginFixture, WritesAsciiNormalsFromTessellation) {
#if defined(_WIN32)
_putenv_s("VDE_PLY_WRITE_NORMALS", "1");
#else
setenv("VDE_PLY_WRITE_NORMALS", "1", 1);
#endif
VdeBody body = make_triangle_body();
ASSERT_NE(body, nullptr);
MockBody* mock = as_mock(body);
mock->normals = {0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0};
const std::string path = temp_path("normals_ascii.ply");
ASSERT_EQ(format_->write_file(&host_, path.c_str(), &body, 1), VDE_PLUGIN_OK)
<< g_last_error;
const std::string text = read_text_file(path);
EXPECT_TRUE(contains(text, "property float nx"));
EXPECT_TRUE(contains(text, "property float ny"));
EXPECT_TRUE(contains(text, "property float nz"));
EXPECT_TRUE(contains(text, " 0 0 1\n")) << text;
VdeBody* bodies = nullptr;
std::size_t count = 0;
ASSERT_EQ(read_memory(text, &bodies, &count), VDE_PLUGIN_OK) << g_last_error;
ASSERT_EQ(count, 1u);
destroy(bodies, count);
host_.body_destroy(body);
}
TEST_F(PlyPluginFixture, WritesBinaryNormalsAndRoundtrips) {
#if defined(_WIN32)
_putenv_s("VDE_PLY_WRITE_NORMALS", "true");
_putenv_s("VDE_PLY_WRITE_FORMAT", "binary_little_endian");
#else
setenv("VDE_PLY_WRITE_NORMALS", "true", 1);
setenv("VDE_PLY_WRITE_FORMAT", "binary_little_endian", 1);
#endif
VdeBody body = make_triangle_body();
ASSERT_NE(body, nullptr);
as_mock(body)->normals = {0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0};
const std::string path = temp_path("normals_binary.ply");
ASSERT_EQ(format_->write_file(&host_, path.c_str(), &body, 1), VDE_PLUGIN_OK)
<< g_last_error;
const std::string text = read_text_file(path);
EXPECT_TRUE(contains(text, "format binary_little_endian 1.0"));
EXPECT_TRUE(contains(text, "property float nx"));
VdeBody* bodies = nullptr;
std::size_t count = 0;
ASSERT_EQ(read_memory(text, &bodies, &count), VDE_PLUGIN_OK) << g_last_error;
ASSERT_EQ(count, 1u);
EXPECT_EQ(as_mock(bodies[0])->indices.size(), 3u);
destroy(bodies, count);
host_.body_destroy(body);
}
TEST_F(PlyPluginFixture, FallbackFaceNormalWhenNormalsMissingOrNaN) {
#if defined(_WIN32)
_putenv_s("VDE_PLY_WRITE_NORMALS", "1");
#else
setenv("VDE_PLY_WRITE_NORMALS", "1", 1);
#endif
// 无 normals:面法向 +Z
VdeBody body = make_triangle_body();
ASSERT_NE(body, nullptr);
const std::string path = temp_path("normals_fallback.ply");
ASSERT_EQ(format_->write_file(&host_, path.c_str(), &body, 1), VDE_PLUGIN_OK)
<< g_last_error;
const std::string text = read_text_file(path);
EXPECT_TRUE(contains(text, "property float nx"));
EXPECT_TRUE(contains(text, " 0 0 1\n")) << text;
host_.body_destroy(body);
// NaN normals:回退面法向 +Z
VdeBody nan_body = make_triangle_body();
ASSERT_NE(nan_body, nullptr);
as_mock(nan_body)->normals = {std::nan(""), std::nan(""), std::nan(""),
std::nan(""), std::nan(""), std::nan(""),
std::nan(""), std::nan(""), std::nan("")};
const std::string nan_path = temp_path("normals_nan.ply");
ASSERT_EQ(format_->write_file(&host_, nan_path.c_str(), &nan_body, 1), VDE_PLUGIN_OK)
<< g_last_error;
const std::string nan_text = read_text_file(nan_path);
EXPECT_TRUE(contains(nan_text, " 0 0 1\n")) << nan_text;
host_.body_destroy(nan_body);
}
TEST_F(PlyPluginFixture, RejectsInvalidNormalsSwitchAndDeterministic) {
#if defined(_WIN32)
_putenv_s("VDE_PLY_WRITE_NORMALS", "maybe");
#else
setenv("VDE_PLY_WRITE_NORMALS", "maybe", 1);
#endif
VdeBody body = make_triangle_body();
ASSERT_NE(body, nullptr);
const std::string bad_path = temp_path("normals_bad_switch.ply");
EXPECT_EQ(format_->write_file(&host_, bad_path.c_str(), &body, 1),
VDE_PLUGIN_ERR_INVALID_ARG);
EXPECT_TRUE(contains(g_last_error, "VDE_PLY_WRITE_NORMALS")) << g_last_error;
#if defined(_WIN32)
_putenv_s("VDE_PLY_WRITE_NORMALS", "1");
#else
setenv("VDE_PLY_WRITE_NORMALS", "1", 1);
#endif
const std::string path_a = temp_path("normals_det_a.ply");
const std::string path_b = temp_path("normals_det_b.ply");
ASSERT_EQ(format_->write_file(&host_, path_a.c_str(), &body, 1), VDE_PLUGIN_OK);
ASSERT_EQ(format_->write_file(&host_, path_b.c_str(), &body, 1), VDE_PLUGIN_OK);
EXPECT_EQ(read_text_file(path_a), read_text_file(path_b));
host_.body_destroy(body);
}