Skip to content

Commit

Permalink
Add tracy zones and rename Property to PropertyInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
hyperlogic committed Jun 26, 2024
1 parent cf554cd commit 3b7efc7
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 89 deletions.
9 changes: 7 additions & 2 deletions src/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@
#include <filesystem>
#include <thread>

#ifdef TRACY_ENABLE
#include <tracy/Tracy.hpp>
#else
#define ZoneScoped
#define ZoneScopedNC(NAME, COLOR)
#endif

#include "core/framebuffer.h"
#include "core/log.h"
#include "core/debugrenderer.h"
Expand Down Expand Up @@ -209,7 +216,6 @@ static void RenderDesktop(glm::ivec2 windowSize, std::shared_ptr<Program> deskto
}
}

// AJT: TODO this wrapper func is not needed anymore
static std::shared_ptr<PointCloud> LoadPointCloud(const std::string& plyFilename)
{
auto pointCloud = std::make_shared<PointCloud>();
Expand All @@ -222,7 +228,6 @@ static std::shared_ptr<PointCloud> LoadPointCloud(const std::string& plyFilename
return pointCloud;
}

// AJT: TODO this wrapper func is not needed anymore
static std::shared_ptr<GaussianCloud> LoadGaussianCloud(const std::string& plyFilename)
{
auto gaussianCloud = std::make_shared<GaussianCloud>();
Expand Down
149 changes: 88 additions & 61 deletions src/gaussiancloud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@
#include <string>
#include <string.h>

#ifdef TRACY_ENABLE
#include <tracy/Tracy.hpp>
#else
#define ZoneScoped
#define ZoneScopedNC(NAME, COLOR)
#endif

#include "core/log.h"
#include "core/util.h"

Expand All @@ -24,6 +31,8 @@ GaussianCloud::GaussianCloud()

bool GaussianCloud::ImportPly(const std::string& plyFilename)
{
ZoneScopedNC("GC::ImportPly", tracy::Color::Red4);

std::ifstream plyFile(plyFilename, std::ios::binary);
if (!plyFile.is_open())
{
Expand All @@ -32,93 +41,111 @@ bool GaussianCloud::ImportPly(const std::string& plyFilename)
}

Ply ply;
if (!ply.Parse(plyFile))

{
Log::E("Error parsing ply file \"%s\"\n", plyFilename.c_str());
return false;
ZoneScopedNC("ply.Parse", tracy::Color::Blue);
if (!ply.Parse(plyFile))
{
Log::E("Error parsing ply file \"%s\"\n", plyFilename.c_str());
return false;
}
}

struct
{
Ply::Property x, y, z;
Ply::Property f_dc[3];
Ply::Property f_rest[45];
Ply::Property opacity;
Ply::Property scale[3];
Ply::Property rot[4];
Ply::PropertyInfo x, y, z;
Ply::PropertyInfo f_dc[3];
Ply::PropertyInfo f_rest[45];
Ply::PropertyInfo opacity;
Ply::PropertyInfo scale[3];
Ply::PropertyInfo rot[4];
} props;

if (!ply.GetProperty("x", props.x) || !ply.GetProperty("y", props.y) || !ply.GetProperty("z", props.z))
{
Log::E("Error parsing ply file \"%s\", missing position property\n", plyFilename.c_str());
}

for (int i = 0; i < 3; i++)
{
if (!ply.GetProperty("f_dc_" + std::to_string(i), props.f_dc[i]))
{
Log::E("Error parsing ply file \"%s\", missing f_dc property\n", plyFilename.c_str());
}
}
ZoneScopedNC("ply.GetProps", tracy::Color::Green);

for (int i = 0; i < 45; i++)
{
if (!ply.GetProperty("f_rest_" + std::to_string(i), props.f_rest[i]))
if (!ply.GetPropertyInfo("x", props.x) ||
!ply.GetPropertyInfo("y", props.y) ||
!ply.GetPropertyInfo("z", props.z))
{
// f_rest properties are optional
Log::W("PLY file \"%s\", missing f_rest property\n", plyFilename.c_str());
break;
Log::E("Error parsing ply file \"%s\", missing position property\n", plyFilename.c_str());
}
}

if (!ply.GetProperty("opacity", props.opacity))
{
Log::E("Error parsing ply file \"%s\", missing opacity property\n", plyFilename.c_str());
}

for (int i = 0; i < 3; i++)
{
if (!ply.GetProperty("scale_" + std::to_string(i), props.scale[i]))
for (int i = 0; i < 3; i++)
{
Log::E("Error parsing ply file \"%s\", missing scale property\n", plyFilename.c_str());
if (!ply.GetPropertyInfo("f_dc_" + std::to_string(i), props.f_dc[i]))
{
Log::E("Error parsing ply file \"%s\", missing f_dc property\n", plyFilename.c_str());
}
}
}

for (int i = 0; i < 4; i++)
{
if (!ply.GetProperty("rot_" + std::to_string(i), props.rot[i]))
for (int i = 0; i < 45; i++)
{
Log::E("Error parsing ply file \"%s\", missing rot property\n", plyFilename.c_str());
if (!ply.GetPropertyInfo("f_rest_" + std::to_string(i), props.f_rest[i]))
{
// f_rest properties are optional
Log::W("PLY file \"%s\", missing f_rest property\n", plyFilename.c_str());
break;
}
}
}

gaussianVec.resize(ply.GetVertexCount());

int i = 0;
ply.ForEachVertex([this, &i, &props](const uint8_t* data, size_t size)
{
gaussianVec[i].position[0] = props.x.Get<float>(data);
gaussianVec[i].position[1] = props.y.Get<float>(data);
gaussianVec[i].position[2] = props.z.Get<float>(data);
for (int j = 0; j < 3; j++)
if (!ply.GetPropertyInfo("opacity", props.opacity))
{
gaussianVec[i].f_dc[j] = props.f_dc[j].Get<float>(data);
Log::E("Error parsing ply file \"%s\", missing opacity property\n", plyFilename.c_str());
}
for (int j = 0; j < 45; j++)

for (int i = 0; i < 3; i++)
{
gaussianVec[i].f_rest[j] = props.f_rest[j].Get<float>(data);
if (!ply.GetPropertyInfo("scale_" + std::to_string(i), props.scale[i]))
{
Log::E("Error parsing ply file \"%s\", missing scale property\n", plyFilename.c_str());
}
}
gaussianVec[i].opacity = props.opacity.Get<float>(data);
for (int j = 0; j < 3; j++)

for (int i = 0; i < 4; i++)
{
gaussianVec[i].scale[j] = props.scale[j].Get<float>(data);
if (!ply.GetPropertyInfo("rot_" + std::to_string(i), props.rot[i]))
{
Log::E("Error parsing ply file \"%s\", missing rot property\n", plyFilename.c_str());
}
}
for (int j = 0; j < 4; j++)

}

{
ZoneScopedNC("ply.resize", tracy::Color::Red4);
gaussianVec.resize(ply.GetVertexCount());
}

{
ZoneScopedNC("ply.ForEachVertex", tracy::Color::Blue);
int i = 0;
ply.ForEachVertex([this, &i, &props](const uint8_t* data, size_t size)
{
gaussianVec[i].rot[j] = props.rot[j].Get<float>(data);
}
i++;
});
gaussianVec[i].position[0] = props.x.Get<float>(data);
gaussianVec[i].position[1] = props.y.Get<float>(data);
gaussianVec[i].position[2] = props.z.Get<float>(data);
for (int j = 0; j < 3; j++)
{
gaussianVec[i].f_dc[j] = props.f_dc[j].Get<float>(data);
}
for (int j = 0; j < 45; j++)
{
gaussianVec[i].f_rest[j] = props.f_rest[j].Get<float>(data);
}
gaussianVec[i].opacity = props.opacity.Get<float>(data);
for (int j = 0; j < 3; j++)
{
gaussianVec[i].scale[j] = props.scale[j].Get<float>(data);
}
for (int j = 0; j < 4; j++)
{
gaussianVec[i].rot[j] = props.rot[j].Get<float>(data);
}
i++;
});
}

return true;
}
Expand Down
56 changes: 40 additions & 16 deletions src/ply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
#include <iostream>
#include <sstream>

#ifdef TRACY_ENABLE
#include <tracy/Tracy.hpp>
#else
#define ZoneScoped
#define ZoneScopedNC(NAME, COLOR)
#endif

#include "core/log.h"

static bool CheckLine(std::ifstream& plyFile, const std::string& validLine)
Expand All @@ -30,8 +37,10 @@ static bool GetNextPlyLine(std::ifstream& plyFile, std::string& lineOut)
return false;
}

bool Ply::Parse(std::ifstream& plyFile)
bool Ply::ParseHeader(std::ifstream& plyFile)
{
ZoneScopedNC("Ply::ParseHeader", tracy::Color::Green);

// validate start of header
std::string token1, token2, token3;

Expand Down Expand Up @@ -95,6 +104,8 @@ bool Ply::Parse(std::ifstream& plyFile)
break;
}

using PropInfoPair = std::pair<std::string, PropertyInfo>;

iss.str(line);
iss.clear();
iss >> token1 >> token2 >> token3;
Expand All @@ -105,42 +116,42 @@ bool Ply::Parse(std::ifstream& plyFile)
}
if (token2 == "char" || token2 == "int8")
{
propertyMap.emplace(std::pair<std::string, Property>(token3, {offset, 1, Ply::Type::Char}));
propertyInfoMap.emplace(PropInfoPair(token3, {offset, 1, Ply::Type::Char}));
offset += 1;
}
else if (token2 == "uchar" || token2 == "uint8")
{
propertyMap.emplace(std::pair<std::string, Property>(token3, {offset, 1, Ply::Type::UChar}));
propertyInfoMap.emplace(PropInfoPair(token3, {offset, 1, Ply::Type::UChar}));
offset += 1;
}
else if (token2 == "short" || token2 == "int16")
{
propertyMap.emplace(std::pair<std::string, Property>(token3, {offset, 2, Ply::Type::Short}));
propertyInfoMap.emplace(PropInfoPair(token3, {offset, 2, Ply::Type::Short}));
offset += 2;
}
else if (token2 == "ushort" || token2 == "uint16")
{
propertyMap.emplace(std::pair<std::string, Property>(token3, {offset, 2, Ply::Type::UShort}));
propertyInfoMap.emplace(PropInfoPair(token3, {offset, 2, Ply::Type::UShort}));
offset += 2;
}
else if (token2 == "int" || token2 == "int32")
{
propertyMap.emplace(std::pair<std::string, Property>(token3, {offset, 4, Ply::Type::Int}));
propertyInfoMap.emplace(PropInfoPair(token3, {offset, 4, Ply::Type::Int}));
offset += 4;
}
else if (token2 == "uint" || token2 == "uint32")
{
propertyMap.emplace(std::pair<std::string, Property>(token3, {offset, 4, Ply::Type::UInt}));
propertyInfoMap.emplace(PropInfoPair(token3, {offset, 4, Ply::Type::UInt}));
offset += 4;
}
else if (token2 == "float" || token2 == "float32")
{
propertyMap.emplace(std::pair<std::string, Property>(token3, {offset, 4, Ply::Type::Float}));
propertyInfoMap.emplace(PropInfoPair(token3, {offset, 4, Ply::Type::Float}));
offset += 4;
}
else if (token2 == "double" || token2 == "float64")
{
propertyMap.emplace(std::pair<std::string, Property>(token3, {offset, 8, Ply::Type::Double}));
propertyInfoMap.emplace(PropInfoPair(token3, {offset, 8, Ply::Type::Double}));
offset += 8;
}
else
Expand All @@ -152,25 +163,38 @@ bool Ply::Parse(std::ifstream& plyFile)

vertexSize = offset;

return true;
}

bool Ply::Parse(std::ifstream& plyFile)
{
if (!ParseHeader(plyFile))
{
return false;
}

// read rest of file into dataVec
dataVec.resize(vertexSize * vertexCount);
plyFile.read((char*)dataVec.data(), vertexSize * vertexCount);
{
ZoneScopedNC("Ply::Parse() read data", tracy::Color::Yellow);
dataVec.resize(vertexSize * vertexCount);
plyFile.read((char*)dataVec.data(), vertexSize * vertexCount);
}

return true;
}

bool Ply::GetProperty(const std::string& key, Ply::Property& propertyOut) const
bool Ply::GetPropertyInfo(const std::string& key, Ply::PropertyInfo& propertyInfoOut) const
{
auto iter = propertyMap.find(key);
if (iter != propertyMap.end())
auto iter = propertyInfoMap.find(key);
if (iter != propertyInfoMap.end())
{
propertyOut = iter->second;
propertyInfoOut = iter->second;
return true;
}
return false;
}

void Ply::ForEachVertex(const VertexCallback& cb)
void Ply::ForEachVertex(const VertexCallback& cb) const
{
const uint8_t* vertexPtr = dataVec.data();
for (size_t i = 0; i < vertexCount; i++)
Expand Down
14 changes: 8 additions & 6 deletions src/ply.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ class Ply
Double
};

struct Property
struct PropertyInfo
{
Property() : type(Type::Unknown) {}
Property(size_t offsetIn, size_t sizeIn, Type typeIn) : offset(offsetIn), size(sizeIn), type(typeIn) {}
PropertyInfo() : type(Type::Unknown) {}
PropertyInfo(size_t offsetIn, size_t sizeIn, Type typeIn) : offset(offsetIn), size(sizeIn), type(typeIn) {}

size_t offset;
size_t size;
Expand All @@ -54,15 +54,17 @@ class Ply
}
};

bool GetProperty(const std::string& key, Property& propertyOut) const;
bool GetPropertyInfo(const std::string& key, PropertyInfo& propertyInfoOut) const;

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

size_t GetVertexCount() const { return vertexCount; }

protected:
std::unordered_map<std::string, Property> propertyMap;
bool ParseHeader(std::ifstream& plyFile);

std::unordered_map<std::string, PropertyInfo> propertyInfoMap;
std::vector<uint8_t> dataVec;
size_t vertexCount;
size_t vertexSize;
Expand Down
Loading

0 comments on commit 3b7efc7

Please sign in to comment.