From 7029fb63dbdc42d4dc360c91a7dc6d39e921f657 Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nwpope@utu.fi>
Date: Mon, 20 Jul 2020 18:29:50 +0300
Subject: [PATCH] Display depth at cursor

---
 applications/gui2/src/modules/camera.cpp      | 25 ++++++++++++++++---
 applications/gui2/src/modules/camera.hpp      |  2 ++
 applications/gui2/src/views/camera.cpp        | 11 ++++++++
 applications/gui2/src/views/statistics.cpp    |  9 +------
 .../common/cpp/include/ftl/utility/string.hpp | 12 +++++++++
 5 files changed, 47 insertions(+), 12 deletions(-)
 create mode 100644 components/common/cpp/include/ftl/utility/string.hpp

diff --git a/applications/gui2/src/modules/camera.cpp b/applications/gui2/src/modules/camera.cpp
index 1bf50c6a9..1103dd0df 100644
--- a/applications/gui2/src/modules/camera.cpp
+++ b/applications/gui2/src/modules/camera.cpp
@@ -235,6 +235,11 @@ void Camera::activate(ftl::data::FrameID id) {
 			std::atomic_store(&current_fs_, fs);
 			std::atomic_store(&latest_, fs);
 
+			// Deal with audio
+			if (fs->frames[frame_idx].hasOwn(Channel::AudioStereo)) {
+				speaker->queue(fs->timestamp(), fs->frames[frame_idx]);
+			}
+
 			// Need to notify GUI thread when first data comes
 			if (!has_seen_frame_) {
 				//std::unique_lock<std::mutex> lk(m);
@@ -270,10 +275,6 @@ void Camera::activate(ftl::data::FrameID id) {
 				}
 			}
 
-			// Deal with audio
-			if (fs->frames[frame_idx].hasOwn(Channel::AudioStereo)) {
-				speaker->queue(fs->timestamp(), fs->frames[frame_idx]);
-			}
 			screen->redraw();
 			nframes_++;
 			return true;
@@ -396,3 +397,19 @@ void Camera::touch(int id, ftl::codecs::TouchType t, int x, int y, float d, int
 
 	}*/
 }
+
+float Camera::depthAt(int x, int y) {
+	auto ptr = std::atomic_load(&latest_);
+
+	if (ptr) {
+		const auto &frame = ptr->frames[frame_idx].cast<ftl::rgbd::Frame>();
+
+		if (frame.hasChannel(Channel::Depth)) {
+			const auto &depth = frame.get<cv::Mat>(Channel::Depth);
+			if (x >= 0 && y >= 0 && x < depth.cols && y < depth.rows) {
+				return depth.at<float>(y, x);
+			}
+		}
+	}
+	return 0.0f;
+}
diff --git a/applications/gui2/src/modules/camera.hpp b/applications/gui2/src/modules/camera.hpp
index a33a1950c..2f2857b3a 100644
--- a/applications/gui2/src/modules/camera.hpp
+++ b/applications/gui2/src/modules/camera.hpp
@@ -52,6 +52,8 @@ public:
 
 	std::string getActiveSourceURI();
 
+	float depthAt(int x, int y);
+
 	bool isRecording();
 	void stopRecording();
 	void startRecording(const std::string &filename, const std::unordered_set<ftl::codecs::Channel> &channels);
diff --git a/applications/gui2/src/views/camera.cpp b/applications/gui2/src/views/camera.cpp
index 4b4dd782f..87469f851 100644
--- a/applications/gui2/src/views/camera.cpp
+++ b/applications/gui2/src/views/camera.cpp
@@ -2,6 +2,7 @@
 #include <nanogui/layout.h>
 #include <nanogui/button.h>
 #include <nanogui/vscrollpanel.h>
+#include <ftl/utility/string.hpp>
 
 #include <ftl/codecs/touch.hpp>
 
@@ -394,6 +395,8 @@ bool CameraView::mouseMotionEvent(const Eigen::Vector2i &p, const Eigen::Vector2
 		auto pos = imview_->imageCoordinateAt((p - mPos + rel).cast<float>());
 		if (pos.x() >= 0.0f && pos.y() >= 0.0f) {
 			ctrl_->touch(0, ftl::codecs::TouchType::MOUSE_LEFT, pos.x(), pos.y(), 0.0f, (button > 0) ? 255 : 0);
+
+			//LOG(INFO) << "Depth at " << pos.x() << "," << pos.y() << " = " << ctrl_->depthAt(pos.x(), pos.y());
 		}
 		return true;
 	//}
@@ -423,6 +426,14 @@ void CameraView::draw(NVGcontext*ctx) {
 		}
 	}
 	View::draw(ctx);
+
+	auto mouse = screen()->mousePos();
+	auto pos = imview_->imageCoordinateAt((mouse - mPos).cast<float>());
+	float d = ctrl_->depthAt(pos.x(), pos.y());
+
+	if (d > 0.0f) {
+		nvgText(ctx, mouse.x()+25.0f, mouse.y()+20.0f, (to_string_with_precision(d,2) + std::string("m")).c_str(), nullptr);
+	}
 }
 
 void CameraView::performLayout(NVGcontext* ctx) {
diff --git a/applications/gui2/src/views/statistics.cpp b/applications/gui2/src/views/statistics.cpp
index 7a512c096..a593ac942 100644
--- a/applications/gui2/src/views/statistics.cpp
+++ b/applications/gui2/src/views/statistics.cpp
@@ -4,6 +4,7 @@
 #include <ftl/streams/builder.hpp>
 #include <ftl/streams/netstream.hpp>
 #include <ftl/render/colouriser.hpp>
+#include <ftl/utility/string.hpp>
 
 #include <nanogui/screen.h>
 #include <nanogui/opengl.h>
@@ -13,14 +14,6 @@
 using ftl::gui2::StatisticsWidget;
 using std::string;
 
-template <typename T>
-std::string to_string_with_precision(const T a_value, const int n = 6) {
-	std::ostringstream out;
-	out.precision(n);
-	out << std::fixed << a_value;
-	return out.str();
-}
-
 StatisticsWidget::StatisticsWidget(nanogui::Widget* parent, ftl::gui2::Statistics* ctrl) :
 		nanogui::Widget(parent), ctrl_(ctrl), last_stats_count_(0) {
 
diff --git a/components/common/cpp/include/ftl/utility/string.hpp b/components/common/cpp/include/ftl/utility/string.hpp
new file mode 100644
index 000000000..d88aa1153
--- /dev/null
+++ b/components/common/cpp/include/ftl/utility/string.hpp
@@ -0,0 +1,12 @@
+#ifndef _FTL_UTILITY_STRING_HPP_
+#define _FTL_UTILITY_STRING_HPP_
+
+template <typename T>
+std::string to_string_with_precision(const T a_value, const int n = 6) {
+	std::ostringstream out;
+	out.precision(n);
+	out << std::fixed << a_value;
+	return out.str();
+}
+
+#endif
\ No newline at end of file
-- 
GitLab