From 9baa4a5c7a82f761acfa625193fbe2175ff0f6ad Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nwpope@utu.fi>
Date: Fri, 1 Nov 2019 17:50:54 +0200
Subject: [PATCH] Working MLS with poor normals

---
 applications/reconstruct/src/main.cpp |  2 +-
 components/filters/src/smoothing.cpp  | 14 +++++++++++---
 2 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/applications/reconstruct/src/main.cpp b/applications/reconstruct/src/main.cpp
index 55c6e6d1c..8202aff31 100644
--- a/applications/reconstruct/src/main.cpp
+++ b/applications/reconstruct/src/main.cpp
@@ -246,7 +246,7 @@ static void run(ftl::Configurable *root) {
 
 	bool busy = false;
 
-	auto *smooth = ftl::config::create<ftl::filters::DepthSmoother>(root, "filters");
+	auto *smooth = ftl::config::create<ftl::filters::MLSSmoother>(root, "filters");
 
 	group->setLatency(4);
 	group->setName("ReconGroup");
diff --git a/components/filters/src/smoothing.cpp b/components/filters/src/smoothing.cpp
index ce0e03db3..517179976 100644
--- a/components/filters/src/smoothing.cpp
+++ b/components/filters/src/smoothing.cpp
@@ -1,6 +1,8 @@
 #include <ftl/filters/smoothing.hpp>
 #include "smoothing_cuda.hpp"
 
+#include <ftl/cuda/normals.hpp>
+
 using ftl::filters::DepthSmoother;
 using ftl::filters::MLSSmoother;
 using ftl::codecs::Channel;
@@ -66,9 +68,15 @@ MLSSmoother::~MLSSmoother() {
 }
 
 void MLSSmoother::smooth(ftl::rgbd::Frame &f, ftl::rgbd::Source *s) {
+	bool do_smooth = value("mls_smooth", false);
+	if (!do_smooth) return;
+
 	if (!f.hasChannel(Channel::Normals)) {
-		LOG(ERROR) << "Missing normals for MLS smooth";
-		return;
+		ftl::cuda::normals(
+			f.createTexture<float4>(Channel::Normals, ftl::rgbd::Format<float4>(f.get<cv::cuda::GpuMat>(Channel::Depth).size())),
+			f.createTexture<float>(Channel::Depth),
+			s->parameters(), 0
+		);
 	}
 
 	float thresh = value("mls_threshold", 0.04f);
@@ -81,6 +89,6 @@ void MLSSmoother::smooth(ftl::rgbd::Frame &f, ftl::rgbd::Source *s) {
 		0
 	);
 
-
+	f.swapChannels(Channel::Depth, Channel::Depth2);
 }
 
-- 
GitLab