From 9235184a4e2e45c63a38dfaead4ba707962252af Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Thomas=20M=C3=BCller?= <thomas94@gmx.net>
Date: Tue, 17 Jan 2023 09:51:35 +0100
Subject: [PATCH] NeRF: catch malformed camera rotations and emit a warning

Best effort to normalize nonetheless.
---
 src/testbed_nerf.cu | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/src/testbed_nerf.cu b/src/testbed_nerf.cu
index 0f9f31e..c020237 100644
--- a/src/testbed_nerf.cu
+++ b/src/testbed_nerf.cu
@@ -2526,6 +2526,15 @@ void Testbed::Nerf::Training::update_transforms(int first, int last) {
 
 	for (uint32_t i = 0; i < n; ++i) {
 		auto xform = dataset.xforms[i + first];
+		if (std::abs(xform.start.block<3, 3>(0, 0).determinant() - 1.0f) > 0.01f || std::abs(xform.end.block<3, 3>(0, 0).determinant() - 1.0f) > 0.01f) {
+			tlog::warning() << "Rotation of camera matrix in frame " << i + first << " has a scaling component (determinant!=1).";
+			tlog::warning() << "Normalizing the matrix. This hints at an issue in your data generation pipeline and should be fixed.";
+
+			dataset.xforms[i + first].start.block<3, 3>(0, 0) /= std::cbrt(xform.start.block<3, 3>(0, 0).determinant());
+			dataset.xforms[i + first].end.block<3, 3>(0, 0) /= std::cbrt(xform.end.block<3, 3>(0, 0).determinant());
+			xform = dataset.xforms[i + first];
+		}
+
 		Vector3f rot = cam_rot_offset[i + first].variable();
 		float angle = rot.norm();
 		rot /= angle;
-- 
GitLab