From 7da1655bc203bfe746f7f78622a6deab226f6085 Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nwpope@utu.fi>
Date: Fri, 28 Jun 2019 19:31:31 +0300
Subject: [PATCH] Code tidy

---
 applications/reconstruct/src/main.cpp | 112 +++++---------------------
 1 file changed, 22 insertions(+), 90 deletions(-)

diff --git a/applications/reconstruct/src/main.cpp b/applications/reconstruct/src/main.cpp
index cfd8be782..1f0471454 100644
--- a/applications/reconstruct/src/main.cpp
+++ b/applications/reconstruct/src/main.cpp
@@ -15,40 +15,21 @@
 #include <ftl/rgbd/streamer.hpp>
 #include <ftl/slave.hpp>
 
-// #include <zlib.h>
-// #include <lz4.h>
-
 #include <string>
 #include <vector>
 #include <thread>
 #include <chrono>
-#include <mutex>
 
 #include <opencv2/opencv.hpp>
 #include <ftl/net/universe.hpp>
-#include <ftl/rgbd_display.hpp>
-#include <nlohmann/json.hpp>
-
-#include <opencv2/imgproc.hpp>
-#include <opencv2/imgcodecs.hpp>
-#include <opencv2/highgui.hpp>
-#include <opencv2/core/utility.hpp>
 
-#include <ftl/utility/opencv_to_pcl.hpp>
 #include <ftl/registration.hpp>
 
 #ifdef WIN32
 #pragma comment(lib, "Rpcrt4.lib")
 #endif
 
-#ifdef HAVE_PCL
-#include <pcl/point_cloud.h>
-#include <pcl/common/transforms.h>
-#include <pcl/filters/uniform_sampling.h>
-#endif
-
 using ftl::net::Universe;
-using ftl::rgbd::Display;
 using std::string;
 using std::vector;
 using ftl::rgbd::Source;
@@ -57,22 +38,14 @@ using ftl::config::json_t;
 using json = nlohmann::json;
 using std::this_thread::sleep_for;
 using std::chrono::milliseconds;
-using std::mutex;
-using std::unique_lock;
+//using std::mutex;
+//using std::unique_lock;
 
-using std::vector;
-
-using cv::Mat;
+//using cv::Mat;
 
 using ftl::registration::loadTransformations;
 using ftl::registration::saveTransformations;
 
-struct Cameras {
-	Source *source;
-	DepthCameraData gpu;
-	DepthCameraParams params;
-};
-
 static void run(ftl::Configurable *root) {
 	Universe *net = ftl::create<Universe>(root, "net");
 	ftl::ctrl::Slave slave(net, root);
@@ -80,56 +53,24 @@ static void run(ftl::Configurable *root) {
 	net->start();
 	net->waitConnections();
 	
-	std::vector<Cameras> inputs;
-	auto sources = ftl::createArray<Source>(root, "sources", net); //root->get<vector<json_t>>("sources");
+	// Create a vector of all input RGB-Depth sources
+	auto sources = ftl::createArray<Source>(root, "sources", net);
 
 	if (sources.size() == 0) {
 		LOG(ERROR) << "No sources configured!";
 		return;
 	}
 
-	// TODO	move this block in its own method and add automated tests
-	//		for error cases
-	
-	std::optional<json_t> merge = root->get<json_t>("merge");
-	if (!merge) {
-		LOG(WARNING) << "Input merging not configured, using only first input in configuration";
-		sources = { sources[0] };
-		sources[0]->setPose(Eigen::Matrix4d::Identity());
-	}
-
+	// Must find pose for each source...
 	if (sources.size() > 1) {
 		std::map<std::string, Eigen::Matrix4d> transformations;
 
-		/*if ((*merge)["register"]) {
-			LOG(INFO) << "Registration requested";
-
-			ftl::registration::Registration *reg = ftl::registration::ChessboardRegistration::create(*merge);
-			for (auto &input : inputs) { 
-				while(!input.source->isReady()) { std::this_thread::sleep_for(std::chrono::milliseconds(50)); }
-				reg->addSource(input.source);
-			}
-			
-			reg->run();
-			if (reg->findTransformations(transformations)) {
-				if (!saveTransformations(string(FTL_LOCAL_CONFIG_ROOT) + "/registration.json", transformations)) {
-					LOG(ERROR) << "Error saving new registration";
-				};
-			}
-			else {
-				LOG(ERROR) << "Registration failed";
-			}
-
-			free(reg);
+		if (loadTransformations(string(FTL_LOCAL_CONFIG_ROOT) + "/registration.json", transformations)) {
+			LOG(INFO) << "Loaded camera trasformations from file";
+		}
+		else {
+			LOG(ERROR) << "Error loading camera transformations from file";
 		}
-		else {*/
-			if (loadTransformations(string(FTL_LOCAL_CONFIG_ROOT) + "/registration.json", transformations)) {
-				LOG(INFO) << "Loaded camera trasformations from file";
-			}
-			else {
-				LOG(ERROR) << "Error loading camera transformations from file";
-			}
-		//}
 
 		for (auto &input : sources) {
 			string uri = input->getURI();
@@ -146,24 +87,15 @@ static void run(ftl::Configurable *root) {
 		}
 	}
 
-	// Displays and Inputs configured
-	
-	//LOG(INFO) << "Using sources:";
-	//for (auto &input : inputs) { LOG(INFO) << "    " + (string) input.source->getURI(); }
-	
-	//ftl::rgbd::Display *display = ftl::create<ftl::rgbd::Display>(root, "display");
+	ftl::voxhash::SceneRep *scene = ftl::create<ftl::voxhash::SceneRep>(root, "voxelhash");
+	ftl::rgbd::Streamer *stream = ftl::create<ftl::rgbd::Streamer>(root, "stream", net);
 	ftl::rgbd::Source *virt = ftl::create<ftl::rgbd::Source>(root, "virtual", net);
 
 	auto virtimpl = new ftl::rgbd::VirtualSource(virt);
 	virt->customImplementation(virtimpl);
-
-	ftl::voxhash::SceneRep *scene = ftl::create<ftl::voxhash::SceneRep>(root, "voxelhash");
 	virtimpl->setScene(scene);
-	//display->setSource(virt);
-
-	ftl::rgbd::Streamer *stream = ftl::create<ftl::rgbd::Streamer>(root, "stream", net);
 	stream->add(virt);
-	// Also proxy all inputs
+
 	for (int i=0; i<sources.size(); i++) {
 		Source *in = sources[i];
 		stream->add(in);
@@ -173,11 +105,6 @@ static void run(ftl::Configurable *root) {
 	unsigned char frameCount = 0;
 	bool paused = false;
 
-	// Keyboard camera controls
-	//display->onKey([&paused](int key) {
-	//	if (key == 32) paused = !paused;
-	//});
-
 	int active = sources.size();
 	while (ftl::running) {
 		if (active == 0) {
@@ -188,22 +115,27 @@ static void run(ftl::Configurable *root) {
 		active = 0;
 
 		if (!slave.isPaused()) {
-			//net.broadcast("grab");  // To sync cameras
+			// Mark voxels as cleared
 			scene->nextFrame();
 		
+			// Grab, upload frames and allocate voxel blocks
 			active = scene->upload();
+
+			// Make sure previous virtual camera frame has finished rendering
 			stream->wait();
 
+			// Merge new frames into the voxel structure
 			scene->integrate();
+
+			// Remove any redundant voxels
 			scene->garbage();
 
 		} else {
 			active = 1;
 		}
 
+		// Start virtual camera rendering and previous frame compression
 		stream->poll();
-		//display->update();
-		//sleep_for(milliseconds(10));
 	}
 }
 
-- 
GitLab