From ac9ca2ba83783c2f51de0e644f61590a472d9296 Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nwpope@utu.fi>
Date: Sun, 7 Jun 2020 12:22:25 +0300
Subject: [PATCH] Docs and tidy

---
 components/rgbd-sources/src/basesource.hpp | 21 +++++++++++--
 components/rgbd-sources/src/camera.cpp     | 22 ++++++++++++++
 components/rgbd-sources/src/group.cpp      | 33 ++-------------------
 components/rgbd-sources/src/source.cpp     | 34 +---------------------
 4 files changed, 44 insertions(+), 66 deletions(-)

diff --git a/components/rgbd-sources/src/basesource.hpp b/components/rgbd-sources/src/basesource.hpp
index 80cda5be6..e3996d8af 100644
--- a/components/rgbd-sources/src/basesource.hpp
+++ b/components/rgbd-sources/src/basesource.hpp
@@ -3,7 +3,6 @@
 
 #include <Eigen/Eigen>
 #include <ftl/cuda_util.hpp>
-//#include <opencv2/opencv.hpp>
 #include <ftl/rgbd/camera.hpp>
 #include <ftl/rgbd/frame.hpp>
 
@@ -12,8 +11,20 @@ namespace rgbd {
 
 class Source;
 
+/**
+ * Base class for source device implementations. Each device provides a capture
+ * and retrieve functionality. `capture` is called with a high resolution timer
+ * at a precise timestamp to ensure synchronisation. In another thread the
+ * `retrieve` function is called after `capture` to download any data into the
+ * provided frame object. The frame object is then dispatched for further
+ * processing, such as disparity calculation, or is discarded if a previous
+ * processing dispatch is still on going.
+ * 
+ * @see ftl::rgbd::Group
+ */
 class BaseSourceImpl {
 	public:
+	// TODO: Remove this
 	friend class ftl::rgbd::Source;
 
 	public:
@@ -21,12 +32,15 @@ class BaseSourceImpl {
 	virtual ~BaseSourceImpl() {}
 
 	/**
-	 * Perform hardware data capture. This should be low latency.
+	 * Perform hardware data capture. This should be low latency (<1ms).
 	 */
 	virtual bool capture(int64_t ts)=0;
 
 	/**
 	 * Perform slow IO operation to get the data into the given frame object.
+	 * This can take up to 1 fps (eg. ~40ms), but should be faster. It occurs
+	 * in a different thread to the `capture` call but will never occur at the
+	 * same time as `capture`. If `capture` fails then this will not be called.
 	 */
 	virtual bool retrieve(ftl::rgbd::Frame &frame)=0;
 
@@ -35,6 +49,7 @@ class BaseSourceImpl {
 	 */
 	virtual bool isReady() { return false; };
 
+	// These should be accessed via the state object in a frame.
 	[[deprecated]] virtual void setPose(const Eigen::Matrix4d &pose) { state_.setPose(pose); };
 	[[deprecated]] virtual Camera parameters(ftl::codecs::Channel) { return params_; };
 
@@ -43,7 +58,7 @@ class BaseSourceImpl {
 
 	protected:
 	ftl::rgbd::FrameState state_;
-	capability_t capabilities_;
+	capability_t capabilities_;    // To be deprecated
 	ftl::rgbd::Source *host_;
 	ftl::rgbd::Camera &params_;
 };
diff --git a/components/rgbd-sources/src/camera.cpp b/components/rgbd-sources/src/camera.cpp
index 96ac0be4c..b4631a69d 100644
--- a/components/rgbd-sources/src/camera.cpp
+++ b/components/rgbd-sources/src/camera.cpp
@@ -25,3 +25,25 @@ cv::Mat Camera::getCameraMatrix() const {
 	K.at<double>(1,2) = -cy;
 	return K;
 }
+
+/*
+ * Scale camera parameters to match resolution.
+ */
+Camera Camera::scaled(int width, int height) const {
+	const auto &cam = *this;
+	float scaleX = (float)width / (float)cam.width;
+	float scaleY = (float)height / (float)cam.height;
+
+	//CHECK( abs(scaleX - scaleY) < 0.00000001f );
+
+	Camera newcam = cam;
+	newcam.width = width;
+	newcam.height = height;
+	newcam.fx *= scaleX;
+	newcam.fy *= scaleY;
+	newcam.cx *= scaleX;
+	newcam.cy *= scaleY;
+	newcam.doffs *= scaleX;
+
+	return newcam;
+}
diff --git a/components/rgbd-sources/src/group.cpp b/components/rgbd-sources/src/group.cpp
index 4a8230c68..f1bc55a62 100644
--- a/components/rgbd-sources/src/group.cpp
+++ b/components/rgbd-sources/src/group.cpp
@@ -97,10 +97,6 @@ int Group::streamID(const ftl::rgbd::Source *s) const {
 }
 
 void Group::onFrameSet(const ftl::rgbd::VideoCallback &cb) {
-	//if (latency_ == 0) {
-	//	callback_ = cb;
-	//}
-
 	// 1. Capture camera frames with high precision
 	cap_id_ = ftl::timer::add(ftl::timer::kTimerHighPrecision, [this](int64_t ts) {
 		skip_ = jobs_ != 0;  // Last frame not finished so skip all steps
@@ -108,42 +104,25 @@ void Group::onFrameSet(const ftl::rgbd::VideoCallback &cb) {
 		if (skip_) return true;
 
 		for (auto s : sources_) {
-			s->capture(ts);
+			skip_ &= s->capture(ts);
 		}
 
 		return true;
 	});
 
-	// 2. After capture, swap any internal source double buffers
-	/*swap_id_ = ftl::timer::add(ftl::timer::kTimerSwap, [this](int64_t ts) {
-		if (skip_) return true;
-		for (auto s : sources_) {
-			s->swap();
-		}
-		return true;
-	});*/
-
-	// 3. Issue IO retrieve ad compute jobs before finding a valid
+	// 2. Issue IO retrieve ad compute jobs before finding a valid
 	// frame at required latency to pass to callback.
 	main_id_ = ftl::timer::add(ftl::timer::kTimerMain, [this,cb](int64_t ts) {
-		//if (skip_) LOG(ERROR) << "SKIPPING TIMER JOB " << ts;
 		if (skip_) return true;
-		//jobs_++;
 
 		for (auto s : sources_) {
-			jobs_++; // += 2;
+			jobs_++;
 
 			ftl::pool.push([this,s,ts](int id) {
 				_retrieveJob(s);
 				--jobs_;
-				//if (jobs_ == 0) LOG(INFO) << "LAST JOB =  Retrieve";
 				_dispatchJob(s, ts);
 			});
-			/*ftl::pool.push([this,s](int id) {
-				_computeJob(s);
-				//if (jobs_ == 0) LOG(INFO) << "LAST JOB =  Compute";
-				--jobs_;
-			});*/
 		}
 		return true;
 	});
@@ -154,12 +133,6 @@ void Group::onFrameSet(const ftl::rgbd::VideoCallback &cb) {
 	});
 }
 
-/*void Group::removeRawCallback(const std::function<void(ftl::rgbd::Source*, const ftl::codecs::StreamPacket &spkt, const ftl::codecs::Packet &pkt)> &f) {
-	for (auto s : sources_) {
-		s->removeRawCallback(f);
-	}
-}*/
-
 void Group::setName(const std::string &name) {
 	name_ = name;
 	builder_.setName(name);
diff --git a/components/rgbd-sources/src/source.cpp b/components/rgbd-sources/src/source.cpp
index ed3b856c6..b7ff97b2f 100644
--- a/components/rgbd-sources/src/source.cpp
+++ b/components/rgbd-sources/src/source.cpp
@@ -133,7 +133,7 @@ static ftl::rgbd::BaseSourceImpl *createFileImpl(const ftl::URI &uri, Source *ho
 }
 
 static ftl::rgbd::BaseSourceImpl *createDeviceImpl(const ftl::URI &uri, Source *host) {
-	if (uri.getPathSegment(0) == "video") {
+	if (uri.getPathSegment(0) == "video" || uri.getPathSegment(0) == "camera") {
 		return new StereoVideoSource(host);
 	} else if (uri.getPathSegment(0) == "pylon") {
 #ifdef HAVE_PYLON
@@ -149,17 +149,6 @@ static ftl::rgbd::BaseSourceImpl *createDeviceImpl(const ftl::URI &uri, Source *
 #endif
 	} else if (uri.getPathSegment(0) == "screen") {
 		return new ScreenCapture(host);
-	} else {
-		/*params_.width = value("width", 1280);
-		params_.height = value("height", 720);
-		params_.fx = value("focal", 700.0f);
-		params_.fy = params_.fx;
-		params_.cx = -(double)params_.width / 2.0;
-		params_.cy = -(double)params_.height / 2.0;
-		params_.minDepth = value("minDepth", 0.1f);
-		params_.maxDepth = value("maxDepth", 20.0f);
-		params_.doffs = 0;
-		params_.baseline = value("baseline", 0.0f);*/
 	}
 	return nullptr;
 }
@@ -253,25 +242,4 @@ void Source::setCallback(const FrameCallback &cb) {
 	callback_ = cb;
 }
 
-/*
- * Scale camera parameters to match resolution.
- */
-Camera Camera::scaled(int width, int height) const {
-	const auto &cam = *this;
-	float scaleX = (float)width / (float)cam.width;
-	float scaleY = (float)height / (float)cam.height;
-
-	//CHECK( abs(scaleX - scaleY) < 0.00000001f );
-
-	Camera newcam = cam;
-	newcam.width = width;
-	newcam.height = height;
-	newcam.fx *= scaleX;
-	newcam.fy *= scaleY;
-	newcam.cx *= scaleX;
-	newcam.cy *= scaleY;
-	newcam.doffs *= scaleX;
-
-	return newcam;
-}
 
-- 
GitLab