From 893bceeaa8bbee1c040be77a5afb009ef00e9a8e Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nwpope@utu.fi>
Date: Mon, 30 Sep 2019 14:43:13 +0300
Subject: [PATCH] Add skip bad colour

---
 applications/reconstruct/src/ilw/ilw.cpp      | 6 ++++++
 applications/reconstruct/src/ilw/ilw.cu       | 8 +++++---
 applications/reconstruct/src/ilw/ilw_cuda.hpp | 1 +
 3 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/applications/reconstruct/src/ilw/ilw.cpp b/applications/reconstruct/src/ilw/ilw.cpp
index f2ec70af8..65e1a19df 100644
--- a/applications/reconstruct/src/ilw/ilw.cpp
+++ b/applications/reconstruct/src/ilw/ilw.cpp
@@ -57,6 +57,7 @@ ILW::ILW(nlohmann::json &config) : ftl::Configurable(config) {
 
     params_.flags = 0;
     if (value("ignore_bad", false)) params_.flags |= ftl::cuda::kILWFlag_IgnoreBad;
+    if (value("ignore_bad_colour", false)) params_.flags |= ftl::cuda::kILWFlag_SkipBadColour;
     if (value("restrict_z", true)) params_.flags |= ftl::cuda::kILWFlag_RestrictZ;
 
     on("ignore_bad", [this](const ftl::config::Event &e) {
@@ -64,6 +65,11 @@ ILW::ILW(nlohmann::json &config) : ftl::Configurable(config) {
         else params_.flags &= ~ftl::cuda::kILWFlag_IgnoreBad;
     });
 
+    on("ignore_bad_colour", [this](const ftl::config::Event &e) {
+        if (value("ignore_bad_colour", false)) params_.flags |= ftl::cuda::kILWFlag_SkipBadColour;
+        else params_.flags &= ~ftl::cuda::kILWFlag_SkipBadColour;
+    });
+
     on("restrict_z", [this](const ftl::config::Event &e) {
         if (value("restrict_z", false)) params_.flags |= ftl::cuda::kILWFlag_RestrictZ;
         else params_.flags &= ~ftl::cuda::kILWFlag_RestrictZ;
diff --git a/applications/reconstruct/src/ilw/ilw.cu b/applications/reconstruct/src/ilw/ilw.cu
index 9da781d91..67c1c9d8d 100644
--- a/applications/reconstruct/src/ilw/ilw.cu
+++ b/applications/reconstruct/src/ilw/ilw.cu
@@ -63,7 +63,7 @@ __global__ void correspondence_energy_vector_kernel(
         const float v = (i / COR_WIN_RADIUS) - (COR_WIN_RADIUS / 2);
         
         const float3 world2 = make_float3(p2.tex2D(screen2.x+u, screen2.y+v));
-        if (params.flags & ftl::cuda::kILWFlag_IgnoreBad && world2.x == MINF) continue;
+        if ((params.flags & ftl::cuda::kILWFlag_IgnoreBad) && world2.x == MINF) continue;
         const uchar4 colour2 = c2.tex2D(screen2.x+u, screen2.y+v);
 
         // Determine degree of correspondence
@@ -71,8 +71,10 @@ __global__ void correspondence_energy_vector_kernel(
 		// Point is too far away to even count
 		if (world2.x != MINF && cost == 1.0f) continue;
 
-		// Mix ratio of colour and distance costs
-        cost = params.cost_ratio * (1.0f - ftl::cuda::colourWeighting(colour1, colour2, params.colour_smooth)) + (1.0f - params.cost_ratio) * cost;
+        // Mix ratio of colour and distance costs
+        const float ccost = 1.0f - ftl::cuda::colourWeighting(colour1, colour2, params.colour_smooth);
+        if ((params.flags & ftl::cuda::kILWFlag_SkipBadColour) && ccost == 1.0f) continue;
+        cost = params.cost_ratio * (ccost) + (1.0f - params.cost_ratio) * cost;
         //cost /= 2.0f;
 
 		++count;
diff --git a/applications/reconstruct/src/ilw/ilw_cuda.hpp b/applications/reconstruct/src/ilw/ilw_cuda.hpp
index 63c5a57af..8c6c925b1 100644
--- a/applications/reconstruct/src/ilw/ilw_cuda.hpp
+++ b/applications/reconstruct/src/ilw/ilw_cuda.hpp
@@ -18,6 +18,7 @@ struct ILWParams {
 
 static const uint kILWFlag_IgnoreBad = 0x0001;
 static const uint kILWFlag_RestrictZ = 0x0002;
+static const uint kILWFlag_SkipBadColour = 0x0004;
 
 void correspondence_energy_vector(
     ftl::cuda::TextureObject<float4> &p1,
-- 
GitLab