From 71ced0023fd0c0aaaa83bb1ab32bce1d4ed630c7 Mon Sep 17 00:00:00 2001
From: bkerbl <bkerbl@ad.inria.fr>
Date: Tue, 4 Jul 2023 16:04:49 +0200
Subject: [PATCH] Automatic iteration lookup, ellipsoid fix

---
 .../apps/gaussianViewer/main.cpp              | 45 ++++++++++++++++++-
 .../gaussianviewer/renderer/Config.hpp        |  2 +-
 .../gaussianviewer/renderer/GaussianView.cpp  |  6 +--
 .../renderer/shaders/gaussian_surface.vert    |  4 +-
 4 files changed, 50 insertions(+), 7 deletions(-)

diff --git a/src/projects/gaussianviewer/apps/gaussianViewer/main.cpp b/src/projects/gaussianviewer/apps/gaussianViewer/main.cpp
index 4d38de33..8e903915 100644
--- a/src/projects/gaussianviewer/apps/gaussianViewer/main.cpp
+++ b/src/projects/gaussianviewer/apps/gaussianViewer/main.cpp
@@ -20,6 +20,41 @@
 #include <core/raycaster/Raycaster.hpp>
 #include <core/view/SceneDebugView.hpp>
 #include <algorithm>
+#include <boost/filesystem.hpp>
+#include <regex>
+
+namespace fs = boost::filesystem;
+
+std::string findLargestNumberedSubdirectory(const std::string& directoryPath) {
+	fs::path dirPath(directoryPath);
+	if (!fs::exists(dirPath) || !fs::is_directory(dirPath)) {
+		std::cerr << "Invalid directory: " << directoryPath << std::endl;
+		return "";
+	}
+
+	std::regex regexPattern(R"_(iteration_(\d+))_");
+	std::string largestSubdirectory;
+	int largestNumber = -1;
+
+	for (const auto& entry : fs::directory_iterator(dirPath)) {
+		if (fs::is_directory(entry)) {
+			std::string subdirectory = entry.path().filename().string();
+			std::smatch match;
+
+			if (std::regex_match(subdirectory, match, regexPattern)) {
+				int number = std::stoi(match[1]);
+
+				if (number > largestNumber) {
+					largestNumber = number;
+					largestSubdirectory = subdirectory;
+				}
+			}
+		}
+	}
+
+	return largestSubdirectory;
+}
+
 
 #define PROGRAM_NAME "sibr_3Dgaussian"
 using namespace sibr;
@@ -92,7 +127,15 @@ int main(int ac, char** av)
 	std::string plyfile = myArgs.modelPath.get();
 	if (plyfile.back() != '/')
 		plyfile += "/";
-	plyfile += "point_cloud/iteration_" + std::to_string(myArgs.iteration.get()) + "/point_cloud.ply";
+	plyfile += "point_cloud";
+	if (!myArgs.iteration.isInit())
+	{
+		plyfile += "/" + findLargestNumberedSubdirectory(plyfile) + "/point_cloud.ply";
+	}
+	else
+	{
+		plyfile += "/iteration_" + myArgs.iteration.get() + " / point_cloud.ply";
+	}
 
 	// Setup the scene: load the proxy, create the texture arrays.
 	const uint flags = SIBR_GPU_LINEAR_SAMPLING | SIBR_FLIP_TEXTURE;
diff --git a/src/projects/gaussianviewer/renderer/Config.hpp b/src/projects/gaussianviewer/renderer/Config.hpp
index 49fe21bf..25298e51 100644
--- a/src/projects/gaussianviewer/renderer/Config.hpp
+++ b/src/projects/gaussianviewer/renderer/Config.hpp
@@ -43,7 +43,7 @@ namespace sibr {
 	struct GaussianAppArgs :
 		virtual BasicIBRAppArgs {
 		RequiredArg<std::string> modelPath = { "model-path", "Model directory" };
-		Arg<int> iteration = { "iteration", 30000, "Iteration to load from model" };
+		RequiredArg<std::string> iteration = { "iteration", "Iteration to load from model" };
 		Arg<bool> loadImages = { "load_images", "Whether or not to load images for scene overview"};
 	};
 
diff --git a/src/projects/gaussianviewer/renderer/GaussianView.cpp b/src/projects/gaussianviewer/renderer/GaussianView.cpp
index 97900734..295b86da 100644
--- a/src/projects/gaussianviewer/renderer/GaussianView.cpp
+++ b/src/projects/gaussianviewer/renderer/GaussianView.cpp
@@ -316,7 +316,7 @@ void sibr::GaussianView::onRenderIBR(sibr::IRenderTarget & dst, const sibr::Came
 	{
 		_gaussianRenderer->process(count, *gData, eye, dst, 0.2f);
 	}
-	else if (currMode == "SfM Points")
+	else if (currMode == "Initial Points")
 	{
 		_pointbasedrenderer->process(_scene->proxies()->proxy(), eye, dst);
 	}
@@ -390,8 +390,8 @@ void sibr::GaussianView::onGUI()
 		{
 			if (ImGui::Selectable("Splats"))
 				currMode = "Splats";
-			if (ImGui::Selectable("SfM Points"))
-				currMode = "SfM Points";
+			if (ImGui::Selectable("Initial Points"))
+				currMode = "Initial Points";
 			if (ImGui::Selectable("Ellipsoids"))
 				currMode = "Ellipsoids";
 			ImGui::EndCombo();
diff --git a/src/projects/gaussianviewer/renderer/shaders/gaussian_surface.vert b/src/projects/gaussianviewer/renderer/shaders/gaussian_surface.vert
index 36695ffc..3fde3913 100644
--- a/src/projects/gaussianviewer/renderer/shaders/gaussian_surface.vert
+++ b/src/projects/gaussianviewer/renderer/shaders/gaussian_surface.vert
@@ -89,10 +89,10 @@ void main() {
     float a = alphas[boxID];
 	alphaVert = a;
 	ellipsoidScale = vec3(scales[3 * boxID + 0], scales[3 * boxID + 1], scales[3 * boxID + 2]);
-	ellipsoidScale = 2 * exp(ellipsoidScale);
+	ellipsoidScale = 2 * ellipsoidScale;
 
 	vec4 q = rots[boxID];
-	ellipsoidRotation = transpose(quatToMat3(q / length(q)));
+	ellipsoidRotation = transpose(quatToMat3(q));
 
     int vertexIndex = boxIndices[gl_VertexID];
     worldPos = ellipsoidRotation * (ellipsoidScale * boxVertices[vertexIndex]);
-- 
GitLab