From dbcfe76af49bfdf41656d421fe79d5c47b804e29 Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nwpope@utu.fi>
Date: Tue, 4 Jun 2019 15:36:27 +0300
Subject: [PATCH] WIP Config resolve changes

---
 .../reconstruct/include/ftl/ray_cast_sdf.hpp  |  5 +++--
 .../include/ftl/scene_rep_hash_sdf.hpp        | 19 +++++++++---------
 .../common/cpp/include/ftl/configuration.hpp  |  2 +-
 components/renderers/cpp/src/display.cpp      | 20 +++++++++----------
 components/rgbd-sources/src/disparity.cpp     |  2 +-
 components/rgbd-sources/src/rgbd_source.cpp   |  9 +++++----
 .../rgbd-sources/src/stereovideo_source.cpp   |  2 +-
 7 files changed, 31 insertions(+), 28 deletions(-)

diff --git a/applications/reconstruct/include/ftl/ray_cast_sdf.hpp b/applications/reconstruct/include/ftl/ray_cast_sdf.hpp
index 6e0796bfc..206e37c5d 100644
--- a/applications/reconstruct/include/ftl/ray_cast_sdf.hpp
+++ b/applications/reconstruct/include/ftl/ray_cast_sdf.hpp
@@ -13,8 +13,9 @@ class CUDARayCastSDF : public ftl::Configurable
 {
 public:
 	CUDARayCastSDF(nlohmann::json& config) : ftl::Configurable(config) {
-		create(parametersFromConfig(config));
-		hash_render_ = config.value("hash_renderer", false);
+		auto &cfg = ftl::config::resolve(config);
+		create(parametersFromConfig(cfg));
+		hash_render_ = cfg.value("hash_renderer", false);
 	}
 
 	~CUDARayCastSDF(void) {
diff --git a/applications/reconstruct/include/ftl/scene_rep_hash_sdf.hpp b/applications/reconstruct/include/ftl/scene_rep_hash_sdf.hpp
index 20072e7f3..a9f32b4c3 100644
--- a/applications/reconstruct/include/ftl/scene_rep_hash_sdf.hpp
+++ b/applications/reconstruct/include/ftl/scene_rep_hash_sdf.hpp
@@ -54,22 +54,23 @@ class SceneRep : public ftl::Configurable {
 	}
 
 	static HashParams parametersFromConfig(nlohmann::json &config) {
+		auto &cfg = ftl::config::resolve(config);
 		HashParams params;
 		// First camera view is set to identity pose to be at the centre of
 		// the virtual coordinate space.
 		params.m_rigidTransform.setIdentity();
 		params.m_rigidTransformInverse.setIdentity();
-		params.m_hashNumBuckets = config["hashNumBuckets"];
+		params.m_hashNumBuckets = cfg["hashNumBuckets"];
 		params.m_hashBucketSize = HASH_BUCKET_SIZE;
-		params.m_hashMaxCollisionLinkedListSize = config["hashMaxCollisionLinkedListSize"];
+		params.m_hashMaxCollisionLinkedListSize = cfg["hashMaxCollisionLinkedListSize"];
 		params.m_SDFBlockSize = SDF_BLOCK_SIZE;
-		params.m_numSDFBlocks = config["hashNumSDFBlocks"];
-		params.m_virtualVoxelSize = config["SDFVoxelSize"];
-		params.m_maxIntegrationDistance = config["SDFMaxIntegrationDistance"];
-		params.m_truncation = config["SDFTruncation"];
-		params.m_truncScale = config["SDFTruncationScale"];
-		params.m_integrationWeightSample = config["SDFIntegrationWeightSample"];
-		params.m_integrationWeightMax = config["SDFIntegrationWeightMax"];
+		params.m_numSDFBlocks = cfg["hashNumSDFBlocks"];
+		params.m_virtualVoxelSize = cfg["SDFVoxelSize"];
+		params.m_maxIntegrationDistance = cfg["SDFMaxIntegrationDistance"];
+		params.m_truncation = cfg["SDFTruncation"];
+		params.m_truncScale = cfg["SDFTruncationScale"];
+		params.m_integrationWeightSample = cfg["SDFIntegrationWeightSample"];
+		params.m_integrationWeightMax = cfg["SDFIntegrationWeightMax"];
 		// Note (Nick): We are not streaming voxels in/out of GPU
 		//params.m_streamingVoxelExtents = MatrixConversion::toCUDA(gas.s_streamingVoxelExtents);
 		//params.m_streamingGridDimensions = MatrixConversion::toCUDA(gas.s_streamingGridDimensions);
diff --git a/components/common/cpp/include/ftl/configuration.hpp b/components/common/cpp/include/ftl/configuration.hpp
index 6cbd2ac37..b26dc4a4d 100644
--- a/components/common/cpp/include/ftl/configuration.hpp
+++ b/components/common/cpp/include/ftl/configuration.hpp
@@ -104,7 +104,7 @@ T *ftl::config::create(json_t &link, ARGS ...args) {
 template <typename T, typename... ARGS>
 T *ftl::config::create(ftl::Configurable *parent, const std::string &name, ARGS ...args) {
     //nlohmann::json &entity = ftl::config::resolve(parent->getConfig()[name]);
-    nlohmann::json &entity = (parent->getConfig()[name].is_structured()) ? parent->getConfig()[name] : ftl::config::resolve(parent->getConfig())[name];
+    nlohmann::json &entity = (!parent->getConfig()[name].is_null()) ? parent->getConfig()[name] : ftl::config::resolve(parent->getConfig())[name];
 
     if (entity.is_object()) {
         if (!entity["$id"].is_string()) {
diff --git a/components/renderers/cpp/src/display.cpp b/components/renderers/cpp/src/display.cpp
index 23288289b..96952312b 100644
--- a/components/renderers/cpp/src/display.cpp
+++ b/components/renderers/cpp/src/display.cpp
@@ -132,7 +132,7 @@ static pcl::PointCloud<pcl::PointXYZRGB>::Ptr rgbdToPointXYZ(const cv::Mat &rgb,
 bool Display::render(const cv::Mat &rgb, const cv::Mat &depth, const ftl::rgbd::CameraParameters &p) {
 	Mat idepth;
 
-	if (config_["points"] && rgb.rows != 0) {
+	if (value("points", false) && rgb.rows != 0) {
 #if defined HAVE_PCL
 		auto pc = rgbdToPointXYZ(rgb, depth, p);
 
@@ -166,15 +166,15 @@ bool Display::render(const cv::Mat &rgb, const cv::Mat &depth, const ftl::rgbd::
 #endif  // HAVE_VIZ
 	}
 
-	if (config_["left"]) {
-		if (config_["crosshair"]) {
+	if (value("left", false)) {
+		if (value("crosshair", false)) {
 			cv::line(rgb, cv::Point(0, rgb.rows/2), cv::Point(rgb.cols-1, rgb.rows/2), cv::Scalar(0,0,255), 1);
             cv::line(rgb, cv::Point(rgb.cols/2, 0), cv::Point(rgb.cols/2, rgb.rows-1), cv::Scalar(0,0,255), 1);
 		}
 		cv::namedWindow("Left: " + name_, cv::WINDOW_KEEPRATIO);
 		cv::imshow("Left: " + name_, rgb);
 	}
-	if (config_["right"]) {
+	if (value("right", false)) {
 		/*if (config_["crosshair"]) {
 			cv::line(rgbr, cv::Point(0, rgbr.rows/2), cv::Point(rgbr.cols-1, rgbr.rows/2), cv::Scalar(0,0,255), 1);
             cv::line(rgbr, cv::Point(rgbr.cols/2, 0), cv::Point(rgbr.cols/2, rgbr.rows-1), cv::Scalar(0,0,255), 1);
@@ -183,7 +183,7 @@ bool Display::render(const cv::Mat &rgb, const cv::Mat &depth, const ftl::rgbd::
 		cv::imshow("Right: " + name_, rgbr);*/
 	}
 
-	if (config_["disparity"]) {
+	if (value("disparity", false)) {
 		/*Mat depth32F = (focal * (float)l.cols * base_line) / depth;
 		normalize(depth32F, depth32F, 0, 255, NORM_MINMAX, CV_8U);
 		cv::imshow("Depth", depth32F);
@@ -191,8 +191,8 @@ bool Display::render(const cv::Mat &rgb, const cv::Mat &depth, const ftl::rgbd::
 	        //exit if ESC is pressed
 	       	active_ = false;
 	    }*/
-    } else if (config_["depth"]) {
-		if ((bool)config_["flip_vert"]) {
+    } else if (value("depth", false)) {
+		if (value("flip_vert", false)) {
 			cv::flip(depth, idepth, 0);
 		} else {
 			idepth = depth;
@@ -228,7 +228,7 @@ bool Display::render(const cv::Mat &img, style_t s) {
 	} else if (s = STYLE_DISPARITY) {
 		Mat idepth;
 
-		if ((bool)config_["flip_vert"]) {
+		if (value("flip_vert", false)) {
 			cv::flip(img, idepth, 0);
 		} else {
 			idepth = img;
@@ -244,7 +244,7 @@ bool Display::render(const cv::Mat &img, style_t s) {
 }
 
 void Display::wait(int ms) {
-	if (config_["points"]) {
+	if (value("points", false)) {
 		#if defined HAVE_PCL
 		if (pclviz_) pclviz_->spinOnce(20);
 		#elif defined HAVE_VIZ
@@ -252,7 +252,7 @@ void Display::wait(int ms) {
 		#endif  // HAVE_VIZ
 	}
 	
-	if (config_["depth"] || config_["left"] || config_["right"]) {
+	if (value("depth", false) || value("left", false) || value("right", false)) {
 		while (true) {
 			int key = cv::waitKey(ms);
 
diff --git a/components/rgbd-sources/src/disparity.cpp b/components/rgbd-sources/src/disparity.cpp
index a6af11e7d..dca2d97f6 100644
--- a/components/rgbd-sources/src/disparity.cpp
+++ b/components/rgbd-sources/src/disparity.cpp
@@ -18,7 +18,7 @@ Disparity::Disparity(nlohmann::json &config)
 		max_disp_(value("maximum", 256)) {}
 
 Disparity *Disparity::create(ftl::Configurable *parent, const std::string &name) {
-	nlohmann::json &config = ftl::config::resolve(parent->getConfig()[name]);
+	nlohmann::json &config = ftl::config::resolve((!parent->getConfig()[name].is_null()) ? parent->getConfig()[name] : ftl::config::resolve(parent->getConfig())[name]); // ftl::config::resolve(parent->getConfig()[name]);
 
 	//auto alg = parent->get<std::string>("algorithm");
 	if (!config["algorithm"].is_string()) {
diff --git a/components/rgbd-sources/src/rgbd_source.cpp b/components/rgbd-sources/src/rgbd_source.cpp
index df70d4543..8399a516c 100644
--- a/components/rgbd-sources/src/rgbd_source.cpp
+++ b/components/rgbd-sources/src/rgbd_source.cpp
@@ -53,12 +53,13 @@ bool RGBDSource::snapshot(const std::string &fileprefix) {
 }
 
 RGBDSource *RGBDSource::create(nlohmann::json &config, ftl::net::Universe *net) {
-	if (config["type"].type_name() != "string") {
-		LOG(ERROR) << "Missing RGB-D source type: " << config["type"].type_name();
+	auto &cfg = ftl::config::resolve(config);
+	if (cfg["type"].type_name() != "string") {
+		LOG(ERROR) << "Missing RGB-D source type: " << cfg["type"].type_name();
 		//return nullptr;
 	}
-	if (sources__->count(config["type"].get<string>()) != 1) return nullptr;
-	return (*sources__)[config["type"].get<string>()](config, net);
+	if (sources__->count(cfg["type"].get<string>()) != 1) return nullptr;
+	return (*sources__)[cfg["type"].get<string>()](config, net);
 }
 
 void RGBDSource::_register(const std::string &n,
diff --git a/components/rgbd-sources/src/stereovideo_source.cpp b/components/rgbd-sources/src/stereovideo_source.cpp
index 6264caff2..d8fd08512 100644
--- a/components/rgbd-sources/src/stereovideo_source.cpp
+++ b/components/rgbd-sources/src/stereovideo_source.cpp
@@ -75,7 +75,7 @@ StereoVideoSource::StereoVideoSource(nlohmann::json &config, const string &file)
 	mask_l_ = (mask_l == 0);
 	
 	disp_ = Disparity::create(this, "disparity");
-    if (!disp_) LOG(FATAL) << "Unknown disparity algorithm : " << config["disparity"];
+    if (!disp_) LOG(FATAL) << "Unknown disparity algorithm : " << *get<ftl::config::json_t>("disparity");
 
 	LOG(INFO) << "StereoVideo source ready...";
 	ready_ = true;
-- 
GitLab