From 48767c412ca01845a61c84357e167b379a401ee6 Mon Sep 17 00:00:00 2001 From: "Anthony J. Thibault" Date: Sat, 29 Jun 2024 11:48:36 -0700 Subject: [PATCH] Simplified BinaryAttribute ctor Also some prep work for Ply exporting, build fix for linux --- CMakeLists.txt | 1 + src/core/binaryattribute.cpp | 26 ++++++++++++++++++++++++++ src/core/binaryattribute.h | 9 +++++---- src/gaussiancloud.cpp | 36 ++++++++++++++++++------------------ src/ply.cpp | 29 ++++++++++++++--------------- src/ply.h | 5 ++++- src/pointcloud.cpp | 20 +++++++++----------- 7 files changed, 77 insertions(+), 49 deletions(-) create mode 100644 src/core/binaryattribute.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 10a3617..2eae190 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -56,6 +56,7 @@ find_package(OpenXR CONFIG REQUIRED) # src include_directories(src) add_executable(${PROJECT_NAME} + src/core/binaryattribute.cpp src/core/debugrenderer.cpp src/core/framebuffer.cpp src/core/image.cpp diff --git a/src/core/binaryattribute.cpp b/src/core/binaryattribute.cpp new file mode 100644 index 0000000..2f77759 --- /dev/null +++ b/src/core/binaryattribute.cpp @@ -0,0 +1,26 @@ +/* + Copyright (c) 2024 Anthony J. Thibault + This software is licensed under the MIT License. See LICENSE for more details. +*/ + +#include "binaryattribute.h" + +static uint32_t propertyTypeSizeArr[(size_t)BinaryAttribute::Type::NumTypes] = { + 0, // Unknown + sizeof(int8_t), // Char + sizeof(uint8_t), // UChar + sizeof(int16_t), // Short + sizeof(uint16_t), // UShort + sizeof(int32_t), // Int + sizeof(uint32_t), // UInt + sizeof(float), // Float + sizeof(double) // Double +}; + +BinaryAttribute::BinaryAttribute(Type typeIn, size_t offsetIn) : + type(typeIn), + size(propertyTypeSizeArr[(uint32_t)typeIn]), + offset(offsetIn) +{ + ; +} diff --git a/src/core/binaryattribute.h b/src/core/binaryattribute.h index d448cb4..681b525 100644 --- a/src/core/binaryattribute.h +++ b/src/core/binaryattribute.h @@ -7,6 +7,7 @@ #include #include +#include class BinaryAttribute { @@ -26,7 +27,7 @@ class BinaryAttribute }; BinaryAttribute() : type(Type::Unknown), size(0), offset(0) {} - BinaryAttribute(Type typeIn, size_t sizeIn, size_t offsetIn) : type(typeIn), size(sizeIn), offset(offsetIn) {} + BinaryAttribute(Type typeIn, size_t offsetIn); template const T* Get(const void* data) const @@ -43,7 +44,7 @@ class BinaryAttribute } template - T* Get(const void* data) + T* Get(void* data) { if (type == Type::Unknown) { @@ -79,7 +80,7 @@ class BinaryAttribute } template - void ForEach(void* data, size_t stride, size_t count, const std::function& cb) + void ForEachMut(void* data, size_t stride, size_t count, const std::function& cb) { assert(type != Type::Unknown); assert(data); @@ -92,7 +93,7 @@ class BinaryAttribute } template - void ForEachConst(const void* data, size_t stride, size_t count, const std::function& cb) const + void ForEach(const void* data, size_t stride, size_t count, const std::function& cb) const { assert(type != Type::Unknown); assert(data); diff --git a/src/gaussiancloud.cpp b/src/gaussiancloud.cpp index eb7c323..8c7407a 100644 --- a/src/gaussiancloud.cpp +++ b/src/gaussiancloud.cpp @@ -170,7 +170,7 @@ bool GaussianCloud::ImportPly(const std::string& plyFilename) { ZoneScopedNC("ply.ForEachVertex", tracy::Color::Blue); int i = 0; - ply.ForEachVertex([this, gd, &i, &props](const uint8_t* data, size_t size) + ply.ForEachVertex([this, gd, &i, &props](const void* data, size_t size) { gd[i].posWithAlpha[0] = props.x.Read(data); gd[i].posWithAlpha[1] = props.y.Read(data); @@ -465,25 +465,25 @@ void GaussianCloud::PruneSplats(const glm::vec3& origin, uint32_t numSplats) void GaussianCloud::ForEachPosWithAlpha(const ForEachPosWithAlphaCallback& cb) const { - posWithAlphaAttrib.ForEachConst(GetRawDataPtr(), GetStride(), GetNumGaussians(), cb); + posWithAlphaAttrib.ForEach(GetRawDataPtr(), GetStride(), GetNumGaussians(), cb); } void GaussianCloud::InitAttribs() { - posWithAlphaAttrib = {BinaryAttribute::Type::Float, sizeof(float), offsetof(GaussianData, posWithAlpha)}; - r_sh0Attrib = {BinaryAttribute::Type::Float, sizeof(float), offsetof(GaussianData, r_sh0)}; - r_sh1Attrib = {BinaryAttribute::Type::Float, sizeof(float), offsetof(GaussianData, r_sh1)}; - r_sh2Attrib = {BinaryAttribute::Type::Float, sizeof(float), offsetof(GaussianData, r_sh2)}; - r_sh3Attrib = {BinaryAttribute::Type::Float, sizeof(float), offsetof(GaussianData, r_sh3)}; - g_sh0Attrib = {BinaryAttribute::Type::Float, sizeof(float), offsetof(GaussianData, g_sh0)}; - g_sh1Attrib = {BinaryAttribute::Type::Float, sizeof(float), offsetof(GaussianData, g_sh1)}; - g_sh2Attrib = {BinaryAttribute::Type::Float, sizeof(float), offsetof(GaussianData, g_sh2)}; - g_sh3Attrib = {BinaryAttribute::Type::Float, sizeof(float), offsetof(GaussianData, g_sh3)}; - b_sh0Attrib = {BinaryAttribute::Type::Float, sizeof(float), offsetof(GaussianData, b_sh0)}; - b_sh1Attrib = {BinaryAttribute::Type::Float, sizeof(float), offsetof(GaussianData, b_sh1)}; - b_sh2Attrib = {BinaryAttribute::Type::Float, sizeof(float), offsetof(GaussianData, b_sh2)}; - b_sh3Attrib = {BinaryAttribute::Type::Float, sizeof(float), offsetof(GaussianData, b_sh3)}; - cov3_col0Attrib = {BinaryAttribute::Type::Float, sizeof(float), offsetof(GaussianData, cov3_col0)}; - cov3_col1Attrib = {BinaryAttribute::Type::Float, sizeof(float), offsetof(GaussianData, cov3_col1)}; - cov3_col2Attrib = {BinaryAttribute::Type::Float, sizeof(float), offsetof(GaussianData, cov3_col2)}; + posWithAlphaAttrib = {BinaryAttribute::Type::Float, offsetof(GaussianData, posWithAlpha)}; + r_sh0Attrib = {BinaryAttribute::Type::Float, offsetof(GaussianData, r_sh0)}; + r_sh1Attrib = {BinaryAttribute::Type::Float, offsetof(GaussianData, r_sh1)}; + r_sh2Attrib = {BinaryAttribute::Type::Float, offsetof(GaussianData, r_sh2)}; + r_sh3Attrib = {BinaryAttribute::Type::Float, offsetof(GaussianData, r_sh3)}; + g_sh0Attrib = {BinaryAttribute::Type::Float, offsetof(GaussianData, g_sh0)}; + g_sh1Attrib = {BinaryAttribute::Type::Float, offsetof(GaussianData, g_sh1)}; + g_sh2Attrib = {BinaryAttribute::Type::Float, offsetof(GaussianData, g_sh2)}; + g_sh3Attrib = {BinaryAttribute::Type::Float, offsetof(GaussianData, g_sh3)}; + b_sh0Attrib = {BinaryAttribute::Type::Float, offsetof(GaussianData, b_sh0)}; + b_sh1Attrib = {BinaryAttribute::Type::Float, offsetof(GaussianData, b_sh1)}; + b_sh2Attrib = {BinaryAttribute::Type::Float, offsetof(GaussianData, b_sh2)}; + b_sh3Attrib = {BinaryAttribute::Type::Float, offsetof(GaussianData, b_sh3)}; + cov3_col0Attrib = {BinaryAttribute::Type::Float, offsetof(GaussianData, cov3_col0)}; + cov3_col1Attrib = {BinaryAttribute::Type::Float, offsetof(GaussianData, cov3_col1)}; + cov3_col2Attrib = {BinaryAttribute::Type::Float, offsetof(GaussianData, cov3_col2)}; } diff --git a/src/ply.cpp b/src/ply.cpp index d908e66..5a38685 100644 --- a/src/ply.cpp +++ b/src/ply.cpp @@ -18,17 +18,6 @@ #include "core/log.h" -static uint32_t propertyTypeSizeArr[(size_t)BinaryAttribute::Type::NumTypes] = { - 0, // Unknown - 1, // Char - 1, // UChar - 2, // Short - 2, // UShort - 4, // Int - 4, // UInt - 4, // Float - 8 // Double -}; static bool CheckLine(std::ifstream& plyFile, const std::string& validLine) { @@ -201,10 +190,9 @@ bool Ply::GetProperty(const std::string& key, BinaryAttribute& binaryAttributeOu void Ply::AddProperty(const std::string& key, BinaryAttribute::Type type) { using PropInfoPair = std::pair; - - uint32_t propSize = propertyTypeSizeArr[(int)type]; - propertyMap.emplace(PropInfoPair(key, BinaryAttribute{type, propSize, (uint32_t)vertexSize})); - vertexSize += propSize; + BinaryAttribute attrib(type, vertexSize); + propertyMap.emplace(PropInfoPair(key, attrib)); + vertexSize += attrib.size; } void Ply::AllocData(size_t numVertices) @@ -222,3 +210,14 @@ void Ply::ForEachVertex(const VertexCallback& cb) const } } +void Ply::ForEachVertexMut(const VertexCallbackMut& cb) +{ + uint8_t* ptr = data.get(); + for (size_t i = 0; i < vertexCount; i++) + { + cb(ptr, vertexSize); + ptr += vertexSize; + } +} + + diff --git a/src/ply.h b/src/ply.h index 735296f..b34583e 100644 --- a/src/ply.h +++ b/src/ply.h @@ -26,9 +26,12 @@ class Ply void AddProperty(const std::string& key, BinaryAttribute::Type type); void AllocData(size_t numVertices); - using VertexCallback = std::function; + using VertexCallback = std::function; void ForEachVertex(const VertexCallback& cb) const; + using VertexCallbackMut = std::function; + void ForEachVertexMut(const VertexCallbackMut& cb); + size_t GetVertexCount() const { return vertexCount; } protected: diff --git a/src/pointcloud.cpp b/src/pointcloud.cpp index 106f692..ca4aa29 100644 --- a/src/pointcloud.cpp +++ b/src/pointcloud.cpp @@ -79,7 +79,7 @@ bool PointCloud::ImportPly(const std::string& plyFilename) if (useDoubles) { int i = 0; - ply.ForEachVertex([this, pd, &i, &props](const uint8_t* data, size_t size) + ply.ForEachVertex([this, pd, &i, &props](const void* data, size_t size) { if (useLinearColors) { @@ -104,7 +104,7 @@ bool PointCloud::ImportPly(const std::string& plyFilename) else { int i = 0; - ply.ForEachVertex([this, pd, &i, &props](const uint8_t* data, size_t size) + ply.ForEachVertex([this, pd, &i, &props](const void* data, size_t size) { if (useLinearColors) { @@ -155,13 +155,11 @@ bool PointCloud::ExportPly(const std::string& plyFilename) const ply.AllocData(numPoints); - /* - ply.ForEachVertex([this](uint8_t* data, size_t size)) + ply.ForEachVertexMut([this](void* data, size_t size) { - positionAttrib.Set - } - */ - + // + }); + /* // ply files have unix line endings. plyFile << "ply\n"; @@ -248,11 +246,11 @@ void PointCloud::InitDebugCloud() void PointCloud::ForEachPosition(const ForEachPositionCallback& cb) const { - positionAttrib.ForEachConst(GetRawDataPtr(), GetStride(), GetNumPoints(), cb); + positionAttrib.ForEach(GetRawDataPtr(), GetStride(), GetNumPoints(), cb); } void PointCloud::InitAttribs() { - positionAttrib = {BinaryAttribute::Type::Float, sizeof(float), offsetof(PointData, position)}; - colorAttrib = {BinaryAttribute::Type::Float, sizeof(float), offsetof(PointData, color)}; + positionAttrib = {BinaryAttribute::Type::Float, offsetof(PointData, position)}; + colorAttrib = {BinaryAttribute::Type::Float, offsetof(PointData, color)}; }