Skip to content

Commit

Permalink
Simplified BinaryAttribute ctor
Browse files Browse the repository at this point in the history
Also some prep work for Ply exporting,
build fix for linux
  • Loading branch information
hyperlogic committed Jun 29, 2024
1 parent e558090 commit 48767c4
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 49 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions src/core/binaryattribute.cpp
Original file line number Diff line number Diff line change
@@ -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)
{
;
}
9 changes: 5 additions & 4 deletions src/core/binaryattribute.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <cassert>
#include <cstdint>
#include <functional>

class BinaryAttribute
{
Expand All @@ -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 <typename T>
const T* Get(const void* data) const
Expand All @@ -43,7 +44,7 @@ class BinaryAttribute
}

template <typename T>
T* Get(const void* data)
T* Get(void* data)
{
if (type == Type::Unknown)
{
Expand Down Expand Up @@ -79,7 +80,7 @@ class BinaryAttribute
}

template<typename T>
void ForEach(void* data, size_t stride, size_t count, const std::function<void(T*)>& cb)
void ForEachMut(void* data, size_t stride, size_t count, const std::function<void(T*)>& cb)
{
assert(type != Type::Unknown);
assert(data);
Expand All @@ -92,7 +93,7 @@ class BinaryAttribute
}

template<typename T>
void ForEachConst(const void* data, size_t stride, size_t count, const std::function<void(const T*)>& cb) const
void ForEach(const void* data, size_t stride, size_t count, const std::function<void(const T*)>& cb) const
{
assert(type != Type::Unknown);
assert(data);
Expand Down
36 changes: 18 additions & 18 deletions src/gaussiancloud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<float>(data);
gd[i].posWithAlpha[1] = props.y.Read<float>(data);
Expand Down Expand Up @@ -465,25 +465,25 @@ void GaussianCloud::PruneSplats(const glm::vec3& origin, uint32_t numSplats)

void GaussianCloud::ForEachPosWithAlpha(const ForEachPosWithAlphaCallback& cb) const
{
posWithAlphaAttrib.ForEachConst<float>(GetRawDataPtr(), GetStride(), GetNumGaussians(), cb);
posWithAlphaAttrib.ForEach<float>(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)};
}
29 changes: 14 additions & 15 deletions src/ply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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<std::string, BinaryAttribute>;

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)
Expand All @@ -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;
}
}


5 changes: 4 additions & 1 deletion src/ply.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@ class Ply
void AddProperty(const std::string& key, BinaryAttribute::Type type);
void AllocData(size_t numVertices);

using VertexCallback = std::function<void(const uint8_t*, size_t)>;
using VertexCallback = std::function<void(const void*, size_t)>;
void ForEachVertex(const VertexCallback& cb) const;

using VertexCallbackMut = std::function<void(void*, size_t)>;
void ForEachVertexMut(const VertexCallbackMut& cb);

size_t GetVertexCount() const { return vertexCount; }

protected:
Expand Down
20 changes: 9 additions & 11 deletions src/pointcloud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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)
{
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -248,11 +246,11 @@ void PointCloud::InitDebugCloud()

void PointCloud::ForEachPosition(const ForEachPositionCallback& cb) const
{
positionAttrib.ForEachConst<float>(GetRawDataPtr(), GetStride(), GetNumPoints(), cb);
positionAttrib.ForEach<float>(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)};
}

0 comments on commit 48767c4

Please sign in to comment.