Files
ViewDesignEngine/tests/mesh/test_point_cloud.cpp
T

671 lines
28 KiB
C++

#include <gtest/gtest.h>
#include "vde/mesh/point_cloud.h"
#include <fstream>
#include <cstring>
#include <cstdio>
#include <iterator>
#include <sstream>
using namespace vde::mesh;
#if defined(_MSC_VER)
#define VDE_TEST_PACKED_BEGIN __pragma(pack(push, 1))
#define VDE_TEST_PACKED_END __pragma(pack(pop))
#define VDE_TEST_PACKED
#else
#define VDE_TEST_PACKED_BEGIN
#define VDE_TEST_PACKED_END
#define VDE_TEST_PACKED __attribute__((packed))
#endif
// ═══════════════════════════════════════════════════════════
// Helper: write synthetic binary E57 file
// ═══════════════════════════════════════════════════════════
static void write_synthetic_e57(const std::string& path,
const char* xml_body,
const char* point_data, size_t point_data_size) {
std::ofstream out(path, std::ios::binary);
std::string xml_full(xml_body);
// Pad XML to 4-byte alignment
while (xml_full.size() % 4 != 0) xml_full += ' ';
// Build 48-byte header
char header[48] = {};
std::memcpy(header, "ASTM-E57", 8);
uint32_t major = 1, minor = 0;
std::memcpy(header + 8, &major, 4);
std::memcpy(header + 12, &minor, 4);
// phys_offset = 48, phys_length = xml_full.size()
uint64_t phys_off = 48, phys_len = xml_full.size();
std::memcpy(header + 16, &phys_off, 8);
std::memcpy(header + 24, &phys_len, 8);
// xml_offset = 48, xml_length = xml_full.size()
uint64_t xml_off = 48, xml_len = xml_full.size();
std::memcpy(header + 32, &xml_off, 8);
std::memcpy(header + 40, &xml_len, 8);
out.write(header, 48);
out.write(xml_full.data(), static_cast<std::streamsize>(xml_full.size()));
out.write(point_data, static_cast<std::streamsize>(point_data_size));
out.close();
}
// ═══════════════════════════════════════════════════════════
// Helper: write synthetic binary LAS file
// ═══════════════════════════════════════════════════════════
static void write_synthetic_las(const std::string& path,
double scale_x, double scale_y, double scale_z,
double off_x, double off_y, double off_z,
uint8_t point_format,
const char* point_data, size_t record_len, size_t num_records) {
std::ofstream out(path, std::ios::binary);
// Build LAS 1.2 header (227 bytes)
char header[227] = {};
std::memcpy(header, "LASF", 4);
// file_source_id at 4 (2 bytes)
// global_encoding at 6 (2 bytes)
// guid_data at 8 (16 bytes) - zeros
uint8_t ver_major = 1, ver_minor = 2;
header[24] = ver_major;
header[25] = ver_minor;
// system_identifier at 26 (32 bytes)
// generating_software at 58 (32 bytes)
// creation_day at 90 (2 bytes) = 0
// creation_year at 92 (2 bytes) = 0
uint16_t header_size = 227;
std::memcpy(header + 94, &header_size, 2);
uint32_t offset_to_points = 227;
std::memcpy(header + 96, &offset_to_points, 4);
// number_of_vlrs at 100 (4 bytes) = 0
header[104] = point_format;
uint16_t rec_len = static_cast<uint16_t>(record_len);
std::memcpy(header + 105, &rec_len, 2);
// Legacy point count at offset 107 (4 bytes)
uint32_t num_pts = static_cast<uint32_t>(num_records);
std::memcpy(header + 107, &num_pts, 4);
// Scale factors at offset 131 (8 bytes each)
std::memcpy(header + 131, &scale_x, 8);
std::memcpy(header + 139, &scale_y, 8);
std::memcpy(header + 147, &scale_z, 8);
// Offsets at offset 155 (8 bytes each)
std::memcpy(header + 155, &off_x, 8);
std::memcpy(header + 163, &off_y, 8);
std::memcpy(header + 171, &off_z, 8);
// Max/Min bounds
double max_x = 100, max_y = 100, max_z = 100;
double min_x = 0, min_y = 0, min_z = 0;
std::memcpy(header + 179, &max_x, 8);
std::memcpy(header + 187, &min_x, 8);
std::memcpy(header + 195, &max_y, 8);
std::memcpy(header + 203, &min_y, 8);
std::memcpy(header + 211, &max_z, 8);
std::memcpy(header + 219, &min_z, 8);
out.write(header, 227);
out.write(point_data, static_cast<std::streamsize>(record_len * num_records));
out.close();
}
// ═══════════════════════════════════════════════════════════
// E57 Tests (3 tests)
// ═══════════════════════════════════════════════════════════
TEST(E57Test, LoadSyntheticPoints_XYZ) {
// Minimal XML with one data3D and a prototype with x,y,z as doubles
const char* xml = R"XML(
<?xml version="1.0" encoding="UTF-8"?>
<e57Root type="Structure">
<data3D type="Vector">
<points type="Structure" prototype="PointProto">
<pointCount type="Integer">3</pointCount>
<fileOffset type="Integer">0</fileOffset>
</points>
<pose type="Structure">
<rotation type="Structure">
<Float name="w" precision="double">1</Float>
<Float name="x" precision="double">0</Float>
<Float name="y" precision="double">0</Float>
<Float name="z" precision="double">0</Float>
</rotation>
<translation type="Structure">
<Float name="x" precision="double">0</Float>
<Float name="y" precision="double">0</Float>
<Float name="z" precision="double">0</Float>
</translation>
</pose>
</data3D>
<vectorChild type="Vector"/>
</e57Root>
<PointProto type="Structure">
<Float name="cartesianX" precision="double"/>
<Float name="cartesianY" precision="double"/>
<Float name="cartesianZ" precision="double"/>
</PointProto>
)XML";
// 3 points as doubles: (1,2,3), (4,5,6), (7,8,9)
double pts[] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0};
std::string tmpfile = VDE_TEST_TEMP_DIR "test_e57_xyz.e57";
write_synthetic_e57(tmpfile, xml, reinterpret_cast<const char*>(pts), sizeof(pts));
auto cloud = PointCloud::load_e57(tmpfile);
std::remove(tmpfile.c_str());
ASSERT_EQ(cloud.vertices.size(), 3u);
EXPECT_DOUBLE_EQ(cloud.vertices[0].x(), 1.0);
EXPECT_DOUBLE_EQ(cloud.vertices[0].y(), 2.0);
EXPECT_DOUBLE_EQ(cloud.vertices[0].z(), 3.0);
EXPECT_DOUBLE_EQ(cloud.vertices[1].x(), 4.0);
EXPECT_DOUBLE_EQ(cloud.vertices[1].y(), 5.0);
EXPECT_DOUBLE_EQ(cloud.vertices[1].z(), 6.0);
EXPECT_DOUBLE_EQ(cloud.vertices[2].x(), 7.0);
EXPECT_DOUBLE_EQ(cloud.vertices[2].y(), 8.0);
EXPECT_DOUBLE_EQ(cloud.vertices[2].z(), 9.0);
}
TEST(E57Test, BadMagic_Throws) {
// Write a file with bad magic bytes
std::string tmpfile = VDE_TEST_TEMP_DIR "test_e57_bad.e57";
{
std::ofstream out(tmpfile, std::ios::binary);
char header[48] = {};
std::memcpy(header, "NOT-E57!", 8);
out.write(header, 48);
out.close();
}
EXPECT_THROW(PointCloud::load_e57(tmpfile), std::runtime_error);
std::remove(tmpfile.c_str());
}
TEST(E57Test, LoadSyntheticPoints_WithNormalsColorsIntensity) {
// XML with a prototype containing x,y,z, normalX/Y/Z, color R/G/B, intensity
const char* xml = R"XML(
<?xml version="1.0" encoding="UTF-8"?>
<e57Root type="Structure">
<data3D type="Vector">
<points type="Structure" prototype="RichProto">
<pointCount type="Integer">2</pointCount>
<fileOffset type="Integer">0</fileOffset>
</points>
<pose type="Structure">
<rotation type="Structure">
<Float name="w" precision="double">1</Float>
<Float name="x" precision="double">0</Float>
<Float name="y" precision="double">0</Float>
<Float name="z" precision="double">0</Float>
</rotation>
<translation type="Structure">
<Float name="x" precision="double">0</Float>
<Float name="y" precision="double">0</Float>
<Float name="z" precision="double">0</Float>
</translation>
</pose>
</data3D>
<vectorChild type="Vector"/>
</e57Root>
<RichProto type="Structure">
<Float name="cartesianX" precision="double"/>
<Float name="cartesianY" precision="double"/>
<Float name="cartesianZ" precision="double"/>
<Float name="nor:normalX" precision="double"/>
<Float name="nor:normalY" precision="double"/>
<Float name="nor:normalZ" precision="double"/>
<Integer name="colorRed" minimum="0" maximum="255"/>
<Integer name="colorGreen" minimum="0" maximum="255"/>
<Integer name="colorBlue" minimum="0" maximum="255"/>
<Float name="intensity" precision="double"/>
</RichProto>
)XML";
// 2 points, each: 3 doubles (xyz) + 3 doubles (normals) + 3 bytes (color) + 1 double (intensity)
// = 6*8 + 3*1 + 8 = 48 + 3 + 8 = 59 bytes per record
// But colors are Integer with max 255 → 1 byte each
VDE_TEST_PACKED_BEGIN
struct VDE_TEST_PACKED RichRecord {
double x, y, z;
double nx, ny, nz;
uint8_t r, g, b;
double intensity;
};
VDE_TEST_PACKED_END
RichRecord records[2] = {
{10.0, 20.0, 30.0, 0.0, 0.0, 1.0, 255, 0, 0, 100.0},
{40.0, 50.0, 60.0, 1.0, 0.0, 0.0, 0, 255, 0, 200.0}
};
std::string tmpfile = VDE_TEST_TEMP_DIR "test_e57_rich.e57";
write_synthetic_e57(tmpfile, xml, reinterpret_cast<const char*>(records), sizeof(records));
auto cloud = PointCloud::load_e57(tmpfile);
std::remove(tmpfile.c_str());
ASSERT_EQ(cloud.vertices.size(), 2u);
EXPECT_DOUBLE_EQ(cloud.vertices[0].x(), 10.0);
EXPECT_DOUBLE_EQ(cloud.vertices[0].y(), 20.0);
EXPECT_DOUBLE_EQ(cloud.vertices[0].z(), 30.0);
EXPECT_EQ(cloud.normals.size(), 2u);
EXPECT_DOUBLE_EQ(cloud.normals[0].z(), 1.0);
EXPECT_EQ(cloud.colors.size(), 2u);
EXPECT_EQ(cloud.colors[0][0], 255);
EXPECT_EQ(cloud.colors[0][1], 0);
EXPECT_EQ(cloud.colors[0][2], 0);
EXPECT_EQ(cloud.colors[1][1], 255);
EXPECT_EQ(cloud.intensities.size(), 2u);
EXPECT_DOUBLE_EQ(cloud.intensities[0], 100.0);
EXPECT_DOUBLE_EQ(cloud.intensities[1], 200.0);
}
TEST(E57Test, Standard_CompressedVector_XyzRoundTrip) {
PointCloud cloud;
cloud.vertices = {Point3D(1.0, 2.0, 3.0), Point3D(-4.0, 5.5, 6.25),
Point3D(7.0, -8.0, 9.0)};
const std::string tmpfile = VDE_TEST_TEMP_DIR "test_e57_standard_xyz.e57";
const auto save = cloud.save_e57_standard(tmpfile);
ASSERT_TRUE(save.ok()) << save.message;
EXPECT_NE(save.message.find("standard CompressedVector"), std::string::npos);
EXPECT_NE(save.message.find("bitPack"), std::string::npos);
std::ifstream in(tmpfile, std::ios::binary);
const std::string file_text((std::istreambuf_iterator<char>(in)),
std::istreambuf_iterator<char>());
EXPECT_NE(file_text.find("type=\"CompressedVector\""), std::string::npos);
EXPECT_NE(file_text.find("<bitPackCodec"), std::string::npos);
EXPECT_NE(file_text.find("<prototype type=\"Structure\">"), std::string::npos);
EXPECT_NE(file_text.find("<cartesianX type=\"Float\" precision=\"double\"/>"),
std::string::npos);
const auto loaded = PointCloud::load_e57(tmpfile);
ASSERT_EQ(loaded.vertices.size(), 3u);
for (std::size_t i = 0; i < cloud.vertices.size(); ++i) {
EXPECT_DOUBLE_EQ(loaded.vertices[i].x(), cloud.vertices[i].x());
EXPECT_DOUBLE_EQ(loaded.vertices[i].y(), cloud.vertices[i].y());
EXPECT_DOUBLE_EQ(loaded.vertices[i].z(), cloud.vertices[i].z());
}
std::remove(tmpfile.c_str());
}
TEST(E57Test, Standard_CompressedVector_RichFieldsRoundTrip) {
PointCloud cloud;
cloud.vertices = {Point3D(0.5, 1.5, 2.5), Point3D(-3.0, 4.0, -5.0)};
cloud.normals = {Vector3D(0.0, 0.0, 1.0), Vector3D(1.0, 0.0, 0.0)};
cloud.colors = {{{255, 0, 0}}, {{0, 128, 255}}};
cloud.intensities = {12.5, 99.0};
const std::string tmpfile = VDE_TEST_TEMP_DIR "test_e57_standard_rich.e57";
const auto save = cloud.save_e57_standard(tmpfile);
ASSERT_TRUE(save.ok()) << save.message;
const auto loaded = PointCloud::load_e57(tmpfile);
std::remove(tmpfile.c_str());
ASSERT_EQ(loaded.vertices.size(), 2u);
EXPECT_DOUBLE_EQ(loaded.vertices[0].x(), 0.5);
EXPECT_DOUBLE_EQ(loaded.vertices[1].z(), -5.0);
ASSERT_EQ(loaded.normals.size(), 2u);
EXPECT_DOUBLE_EQ(loaded.normals[1].x(), 1.0);
ASSERT_EQ(loaded.colors.size(), 2u);
EXPECT_EQ(loaded.colors[0][0], 255);
EXPECT_EQ(loaded.colors[1][1], 128);
EXPECT_EQ(loaded.colors[1][2], 255);
ASSERT_EQ(loaded.intensities.size(), 2u);
EXPECT_DOUBLE_EQ(loaded.intensities[0], 12.5);
EXPECT_DOUBLE_EQ(loaded.intensities[1], 99.0);
}
TEST(E57Test, Standard_CompressedVector_MultipleDataPackets) {
PointCloud cloud;
cloud.vertices.reserve(1500);
for (int i = 0; i < 1500; ++i) {
cloud.vertices.emplace_back(static_cast<double>(i), i * 0.5, -i * 0.25);
}
const std::string tmpfile = VDE_TEST_TEMP_DIR "test_e57_standard_packets.e57";
const auto save = cloud.save_e57_standard(tmpfile);
ASSERT_TRUE(save.ok()) << save.message;
EXPECT_NE(save.message.find("3 data packet"), std::string::npos);
const auto loaded = PointCloud::load_e57(tmpfile);
std::remove(tmpfile.c_str());
ASSERT_EQ(loaded.vertices.size(), 1500u);
EXPECT_DOUBLE_EQ(loaded.vertices.front().x(), 0.0);
EXPECT_DOUBLE_EQ(loaded.vertices.back().x(), 1499.0);
EXPECT_DOUBLE_EQ(loaded.vertices[777].z(), -777.0 * 0.25);
}
TEST(E57Test, Standard_UnknownCodec_ThrowsWithoutCrash) {
PointCloud cloud;
cloud.vertices = {Point3D(1.0, 2.0, 3.0)};
const std::string tmpfile = VDE_TEST_TEMP_DIR "test_e57_standard_bad_codec.e57";
const auto save = cloud.save_e57_standard(tmpfile);
ASSERT_TRUE(save.ok()) << save.message;
std::ifstream in(tmpfile, std::ios::binary);
std::string text((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
in.close();
const auto codec = text.find("bitPackCodec");
ASSERT_NE(codec, std::string::npos);
text.replace(codec, std::strlen("bitPackCodec"), "unknownCodec");
std::ofstream out(tmpfile, std::ios::binary | std::ios::trunc);
out.write(text.data(), static_cast<std::streamsize>(text.size()));
out.close();
EXPECT_THROW(PointCloud::load_e57(tmpfile), std::runtime_error);
std::remove(tmpfile.c_str());
}
TEST(E57Test, Standard_TruncatedFile_ThrowsWithoutCrash) {
PointCloud cloud;
cloud.vertices = {Point3D(1.0, 2.0, 3.0), Point3D(4.0, 5.0, 6.0)};
const std::string tmpfile = VDE_TEST_TEMP_DIR "test_e57_standard_truncated.e57";
const auto save = cloud.save_e57_standard(tmpfile);
ASSERT_TRUE(save.ok()) << save.message;
std::ifstream in(tmpfile, std::ios::binary);
std::string text((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
in.close();
ASSERT_GT(text.size(), 80u);
text.resize(text.size() - 40u);
std::ofstream out(tmpfile, std::ios::binary | std::ios::trunc);
out.write(text.data(), static_cast<std::streamsize>(text.size()));
out.close();
EXPECT_THROW(PointCloud::load_e57(tmpfile), std::runtime_error);
std::remove(tmpfile.c_str());
}
static uint64_t e57_le64(const char* p) {
uint64_t value = 0;
for (int i = 0; i < 8; ++i) {
value |= static_cast<uint64_t>(static_cast<uint8_t>(p[i])) << (i * 8);
}
return value;
}
TEST(E57Test, Standard_HeaderAndSectionLayout) {
PointCloud cloud;
cloud.vertices = {Point3D(1.0, 2.0, 3.0), Point3D(4.0, 5.0, 6.0),
Point3D(7.0, 8.0, 9.0)};
const std::string tmpfile = VDE_TEST_TEMP_DIR "test_e57_standard_layout.e57";
const auto save = cloud.save_e57_standard(tmpfile);
ASSERT_TRUE(save.ok()) << save.message;
std::ifstream in(tmpfile, std::ios::binary);
std::string bytes((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
ASSERT_GT(bytes.size(), 48u);
EXPECT_EQ(bytes.substr(0, 8), std::string("ASTM-E57"));
const uint64_t file_length = e57_le64(bytes.data() + 16);
EXPECT_EQ(file_length, bytes.size());
const uint64_t xml_offset = e57_le64(bytes.data() + 24);
const uint64_t xml_length = e57_le64(bytes.data() + 32);
EXPECT_EQ(xml_offset, 48u);
ASSERT_LE(xml_offset + xml_length, bytes.size());
EXPECT_EQ(e57_le64(bytes.data() + 40), 0u); // FMT-12 no-page: pageSize=0
const std::string xml = bytes.substr(xml_offset, xml_length);
const auto points = xml.find("<points type=\"CompressedVector\"");
ASSERT_NE(points, std::string::npos);
const auto points_gt = xml.find('>', points);
ASSERT_NE(points_gt, std::string::npos);
const std::string points_tag = xml.substr(points, points_gt - points + 1);
const auto file_attr = points_tag.find("fileOffset=\"");
ASSERT_NE(file_attr, std::string::npos);
const auto file_begin = file_attr + std::strlen("fileOffset=\"");
const auto file_end = points_tag.find('"', file_begin);
ASSERT_NE(file_end, std::string::npos);
const uint64_t section_offset = std::stoull(points_tag.substr(file_begin,
file_end - file_begin));
ASSERT_LE(section_offset + 32, bytes.size());
EXPECT_EQ(static_cast<uint8_t>(bytes[section_offset]), 1u); // compressed-vector section
EXPECT_GT(e57_le64(bytes.data() + section_offset + 8), 0u);
const uint64_t data_offset = e57_le64(bytes.data() + section_offset + 16);
const uint64_t index_offset = e57_le64(bytes.data() + section_offset + 24);
EXPECT_EQ(data_offset, section_offset + 32);
EXPECT_GT(index_offset, data_offset);
ASSERT_LT(data_offset, bytes.size());
EXPECT_EQ(static_cast<uint8_t>(bytes[data_offset]), 1u); // DATA_PACKET
std::remove(tmpfile.c_str());
}
TEST(E57Test, Standard_PageCrc_RichRoundTrip) {
PointCloud cloud;
cloud.vertices.reserve(1300);
for (int i = 0; i < 1300; ++i) {
cloud.vertices.emplace_back(static_cast<double>(i) * 0.25, -i * 0.5, i * 0.125);
}
cloud.normals.assign(cloud.vertices.size(), Vector3D(0.0, 0.0, 1.0));
cloud.colors.assign(cloud.vertices.size(), {{10, 20, 30}});
cloud.intensities.assign(cloud.vertices.size(), 7.5);
const std::string tmpfile = VDE_TEST_TEMP_DIR "test_e57_page_crc_rich.e57";
E57WriteOptions options;
options.standard_compressed_vector = true;
options.page_crc = true;
const auto save = cloud.save_e57(tmpfile, options);
ASSERT_TRUE(save.ok()) << save.message;
EXPECT_NE(save.message.find("page/CRC"), std::string::npos);
std::ifstream in(tmpfile, std::ios::binary | std::ios::ate);
const auto physical_size = static_cast<uint64_t>(in.tellg());
in.seekg(0);
std::string bytes((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
EXPECT_EQ(physical_size % 1024u, 0u);
EXPECT_EQ(e57_le64(bytes.data() + 40), 1024u); // page layer active
EXPECT_EQ(e57_le64(bytes.data() + 16), physical_size);
const auto loaded = PointCloud::load_e57(tmpfile);
std::remove(tmpfile.c_str());
ASSERT_EQ(loaded.vertices.size(), 1300u);
EXPECT_DOUBLE_EQ(loaded.vertices.front().x(), 0.0);
EXPECT_DOUBLE_EQ(loaded.vertices.back().z(), 1299.0 * 0.125);
EXPECT_DOUBLE_EQ(loaded.vertices[513].y(), -513.0 * 0.5);
ASSERT_EQ(loaded.normals.size(), 1300u);
EXPECT_DOUBLE_EQ(loaded.normals[1299].z(), 1.0);
ASSERT_EQ(loaded.colors.size(), 1300u);
EXPECT_EQ(loaded.colors[0][0], 10);
EXPECT_EQ(loaded.colors[1299][2], 30);
ASSERT_EQ(loaded.intensities.size(), 1300u);
EXPECT_DOUBLE_EQ(loaded.intensities[777], 7.5);
}
TEST(E57Test, Standard_PageCrc_CorruptionDetectedAndCanBeSkipped) {
PointCloud cloud;
cloud.vertices = {Point3D(1.0, 2.0, 3.0), Point3D(4.0, 5.0, 6.0)};
const std::string tmpfile = VDE_TEST_TEMP_DIR "test_e57_page_crc_corrupt.e57";
E57WriteOptions options;
options.standard_compressed_vector = true;
options.page_crc = true;
const auto save = cloud.save_e57(tmpfile, options);
ASSERT_TRUE(save.ok()) << save.message;
std::ifstream in(tmpfile, std::ios::binary);
std::string bytes((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
in.close();
ASSERT_GE(bytes.size(), 1024u);
ASSERT_EQ(bytes.size() % 1024u, 0u);
// Corrupt only the final page's 4-byte CRC trailer; payload remains intact.
bytes.back() = static_cast<char>(bytes.back() ^ 0xFF);
std::ofstream out(tmpfile, std::ios::binary | std::ios::trunc);
out.write(bytes.data(), static_cast<std::streamsize>(bytes.size()));
out.close();
EXPECT_THROW(PointCloud::load_e57(tmpfile), std::runtime_error);
E57ReadOptions tolerant;
tolerant.verify_page_crc = false;
const auto loaded = PointCloud::load_e57(tmpfile, tolerant);
std::remove(tmpfile.c_str());
ASSERT_EQ(loaded.vertices.size(), 2u);
EXPECT_DOUBLE_EQ(loaded.vertices[1].x(), 4.0);
EXPECT_DOUBLE_EQ(loaded.vertices[1].z(), 6.0);
}
TEST(E57Test, Standard_NoPageCrc_StillReadable) {
PointCloud cloud;
cloud.vertices = {Point3D(-1.0, -2.0, -3.0)};
const std::string tmpfile = VDE_TEST_TEMP_DIR "test_e57_no_page_still_read.e57";
const auto save = cloud.save_e57_standard(tmpfile);
ASSERT_TRUE(save.ok()) << save.message;
std::ifstream in(tmpfile, std::ios::binary);
std::string bytes((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
EXPECT_EQ(e57_le64(bytes.data() + 40), 0u);
EXPECT_NE(bytes.size() % 1024u, 0u); // not page-layer
const auto loaded = PointCloud::load_e57(tmpfile);
std::remove(tmpfile.c_str());
ASSERT_EQ(loaded.vertices.size(), 1u);
EXPECT_DOUBLE_EQ(loaded.vertices[0].y(), -2.0);
}
TEST(E57Test, LegacyMinimalSaveRoundTripStillWorks) {
PointCloud cloud;
cloud.vertices = {Point3D(10.0, 20.0, 30.0), Point3D(40.0, 50.0, 60.0)};
const std::string tmpfile = VDE_TEST_TEMP_DIR "test_e57_legacy_roundtrip.e57";
const auto save = cloud.save_e57(tmpfile);
ASSERT_TRUE(save.ok()) << save.message;
const auto loaded = PointCloud::load_e57(tmpfile);
std::remove(tmpfile.c_str());
ASSERT_EQ(loaded.vertices.size(), 2u);
EXPECT_DOUBLE_EQ(loaded.vertices[1].x(), 40.0);
EXPECT_DOUBLE_EQ(loaded.vertices[1].z(), 60.0);
}
TEST(E57Test, Standard_OptionsApiMatchesStandardWriter) {
PointCloud cloud;
cloud.vertices = {Point3D(1.5, -2.5, 3.5)};
const std::string tmpfile = VDE_TEST_TEMP_DIR "test_e57_standard_options.e57";
E57WriteOptions options;
options.standard_compressed_vector = true;
const auto save = cloud.save_e57(tmpfile, options);
ASSERT_TRUE(save.ok()) << save.message;
const auto loaded = PointCloud::load_e57(tmpfile);
std::remove(tmpfile.c_str());
ASSERT_EQ(loaded.vertices.size(), 1u);
EXPECT_DOUBLE_EQ(loaded.vertices[0].x(), 1.5);
}
// ═══════════════════════════════════════════════════════════
// LAS Tests (3 tests)
// ═══════════════════════════════════════════════════════════
TEST(LASTest, LoadFormat0_BasicXYZIntensity) {
// Format 0: 20-byte records
// Field layout: x(4) y(4) z(4) intensity(2) return(1) num_returns(1) scan_dir(1)
// edge(1) classification(1) scan_angle(1) user_data(1) point_source(2)
VDE_TEST_PACKED_BEGIN
struct VDE_TEST_PACKED Fmt0Record {
int32_t x, y, z;
uint16_t intensity;
uint8_t return_number : 3;
uint8_t num_returns : 3;
uint8_t scan_dir : 1;
uint8_t edge_flag : 1;
uint8_t classification;
int8_t scan_angle;
uint8_t user_data;
uint16_t point_source_id;
};
VDE_TEST_PACKED_END
Fmt0Record records[3] = {};
// Point 0: (10000, 20000, 30000) → scale 0.001 = (10, 20, 30), intensity 256
records[0].x = 10000; records[0].y = 20000; records[0].z = 30000;
records[0].intensity = 256;
records[0].classification = 2;
// Point 1: (40000, 50000, 60000) → (40, 50, 60), intensity 512
records[1].x = 40000; records[1].y = 50000; records[1].z = 60000;
records[1].intensity = 512;
records[1].classification = 3;
// Point 2: (70000, 80000, 90000) → (70, 80, 90), intensity 1024
records[2].x = 70000; records[2].y = 80000; records[2].z = 90000;
records[2].intensity = 1024;
records[2].classification = 5;
std::string tmpfile = VDE_TEST_TEMP_DIR "test_las_fmt0.las";
write_synthetic_las(tmpfile, 0.001, 0.001, 0.001, 0.0, 0.0, 0.0, 0,
reinterpret_cast<const char*>(records), 20, 3);
auto cloud = PointCloud::load_las(tmpfile);
std::remove(tmpfile.c_str());
ASSERT_EQ(cloud.vertices.size(), 3u);
EXPECT_NEAR(cloud.vertices[0].x(), 10.0, 1e-9);
EXPECT_NEAR(cloud.vertices[0].y(), 20.0, 1e-9);
EXPECT_NEAR(cloud.vertices[0].z(), 30.0, 1e-9);
EXPECT_NEAR(cloud.vertices[1].x(), 40.0, 1e-9);
EXPECT_NEAR(cloud.vertices[2].x(), 70.0, 1e-9);
EXPECT_EQ(cloud.intensities.size(), 3u);
EXPECT_DOUBLE_EQ(cloud.intensities[0], 256.0);
EXPECT_DOUBLE_EQ(cloud.intensities[1], 512.0);
EXPECT_DOUBLE_EQ(cloud.intensities[2], 1024.0);
}
TEST(LASTest, BadSignature_Throws) {
std::string tmpfile = VDE_TEST_TEMP_DIR "test_las_bad.las";
{
std::ofstream out(tmpfile, std::ios::binary);
char header[227] = {};
std::memcpy(header, "BAD!", 4);
out.write(header, 227);
out.close();
}
EXPECT_THROW(PointCloud::load_las(tmpfile), std::runtime_error);
std::remove(tmpfile.c_str());
}
TEST(LASTest, LoadFormat2_WithRGB) {
// Format 2: 26-byte records
// x(4) y(4) z(4) intensity(2) return(1) num_returns(1) scan_dir(1) edge(1)
// classification(1) scan_angle(1) user_data(1) point_source(2)
// red(2) green(2) blue(2)
VDE_TEST_PACKED_BEGIN
struct VDE_TEST_PACKED Fmt2Record {
int32_t x, y, z;
uint16_t intensity;
uint8_t return_info; // 3+3+1+1 bits: return#, num_returns, scan_dir, edge
uint8_t classification;
int8_t scan_angle;
uint8_t user_data;
uint16_t point_source_id;
uint16_t red, green, blue;
};
VDE_TEST_PACKED_END
Fmt2Record records[2] = {};
records[0].x = 5000; records[0].y = 6000; records[0].z = 7000;
records[0].intensity = 100;
records[0].red = 65535; records[0].green = 0; records[0].blue = 0;
records[1].x = 8000; records[1].y = 9000; records[1].z = 10000;
records[1].intensity = 200;
records[1].red = 0; records[1].green = 65535; records[1].blue = 0;
std::string tmpfile = VDE_TEST_TEMP_DIR "test_las_fmt2.las";
write_synthetic_las(tmpfile, 0.01, 0.01, 0.01, 0.0, 0.0, 0.0, 2,
reinterpret_cast<const char*>(records), 26, 2);
auto cloud = PointCloud::load_las(tmpfile);
std::remove(tmpfile.c_str());
ASSERT_EQ(cloud.vertices.size(), 2u);
EXPECT_NEAR(cloud.vertices[0].x(), 50.0, 1e-9);
EXPECT_NEAR(cloud.vertices[0].y(), 60.0, 1e-9);
EXPECT_NEAR(cloud.vertices[0].z(), 70.0, 1e-9);
ASSERT_EQ(cloud.colors.size(), 2u);
EXPECT_EQ(cloud.colors[0][0], 255); // 0xFFFF >> 8 = 0xFF = 255
EXPECT_EQ(cloud.colors[0][1], 0);
EXPECT_EQ(cloud.colors[0][2], 0);
EXPECT_EQ(cloud.colors[1][0], 0);
EXPECT_EQ(cloud.colors[1][1], 255);
EXPECT_EQ(cloud.colors[1][2], 0);
EXPECT_EQ(cloud.intensities.size(), 2u);
EXPECT_DOUBLE_EQ(cloud.intensities[0], 100.0);
EXPECT_DOUBLE_EQ(cloud.intensities[1], 200.0);
}