#include #include "vde/mesh/point_cloud.h" #include #include #include using namespace vde::mesh; // ═══════════════════════════════════════════════════════════ // 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(xml_full.size())); out.write(point_data, static_cast(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(record_len); std::memcpy(header + 105, &rec_len, 2); // Legacy point count at offset 107 (4 bytes) uint32_t num_pts = static_cast(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(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( 3 0 1 0 0 0 0 0 0 )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(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( 2 0 1 0 0 0 0 0 0 )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 struct __attribute__((packed)) RichRecord { double x, y, z; double nx, ny, nz; uint8_t r, g, b; double intensity; }; 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(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); } // ═══════════════════════════════════════════════════════════ // 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) struct __attribute__((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; }; 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(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) struct __attribute__((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; }; 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(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); }