From 43f3284112e4ab95f35eb5fd5a961d7c5a8d131f Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nwpope@utu.fi>
Date: Sun, 21 Jul 2019 09:58:08 +0300
Subject: [PATCH] WIP Depth camera normals calc

---
 .../reconstruct/include/ftl/depth_camera.hpp  | 73 +++----------------
 applications/reconstruct/src/depth_camera.cpp | 58 +++++++++++++++
 2 files changed, 69 insertions(+), 62 deletions(-)
 create mode 100644 applications/reconstruct/src/depth_camera.cpp

diff --git a/applications/reconstruct/include/ftl/depth_camera.hpp b/applications/reconstruct/include/ftl/depth_camera.hpp
index 7553f8aa8..7df38bb00 100644
--- a/applications/reconstruct/include/ftl/depth_camera.hpp
+++ b/applications/reconstruct/include/ftl/depth_camera.hpp
@@ -36,66 +36,16 @@ struct DepthCamera {
 	// Host part //
 	///////////////
 
-	__host__
-	DepthCamera() {
-		/*d_depthData = NULL;
-		d_colorData = NULL;
-		d_depthArray = NULL;
-		d_colorArray = NULL;*/
-
-		depth_mat_ = nullptr;
-		colour_mat_ = nullptr;
-		point_mat_ = nullptr;
-		normal_mat_ = nullptr;
-		depth_tex_ = nullptr;
-		colour_tex_ = nullptr;
-		normal_tex_ = nullptr;
-	}
-
-	__host__
-	void alloc(const DepthCameraParams& params, bool withNormals=false) { //! todo resizing???
-		depth_mat_ = new cv::cuda::GpuMat(params.m_imageHeight, params.m_imageWidth, CV_32FC1);
-		colour_mat_ = new cv::cuda::GpuMat(params.m_imageHeight, params.m_imageWidth, CV_8UC4);
-		depth_tex_ = new ftl::cuda::TextureObject<float>((cv::cuda::PtrStepSz<float>)*depth_mat_);
-		colour_tex_ = new ftl::cuda::TextureObject<uchar4>((cv::cuda::PtrStepSz<uchar4>)*colour_mat_);
-		data.depth = depth_tex_->cudaTexture();
-		data.colour = colour_tex_->cudaTexture();
-		data.params = params;
-
-		if (withNormals) {
-			point_mat_ = new cv::cuda::GpuMat(params.m_imageHeight, params.m_imageWidth, CV_32FC3);
-			normal_mat_ = new cv::cuda::GpuMat(params.m_imageHeight, params.m_imageWidth, CV_32FC4);
-			normal_tex_ = new ftl::cuda::TextureObject<float4>((cv::cuda::PtrStepSz<float4>)*normal_mat_);
-			data.normal = normal_tex_->cudaTexture();
-		} else {
-			data.normal = 0;
-		}
-	}
-
-	//__host__
-	//void updateParams(const DepthCameraParams& params) {
-	//	updateConstantDepthCameraParams(params);
-	//}
-
-	__host__
-	void updateData(const cv::Mat &depth, const cv::Mat &rgb, cv::cuda::Stream &stream) {
-		depth_mat_->upload(depth, stream);
-		colour_mat_->upload(rgb, stream);
-	}
-
-	__host__
-	void free() {
-		if (depth_mat_) delete depth_mat_;
-		if (colour_mat_) delete colour_mat_;
-		if (point_mat_) delete point_mat_;
-		if (normal_mat_) delete normal_mat_;
-		delete depth_tex_;
-		delete colour_tex_;
-		if (normal_tex_) delete normal_tex_;
-	}
-
-
-	// TODO(Nick) Should not need to pass all these pointers to device
+	__host__ DepthCamera();
+
+	__host__ void alloc(const DepthCameraParams& params, bool withNormals=false);
+
+	__host__ void updateData(const cv::Mat &depth, const cv::Mat &rgb, cv::cuda::Stream &stream);
+
+	__host__ void free();
+
+	__host__ void _computeNormals(cudaStream_t stream);
+
 	cv::cuda::GpuMat *depth_mat_;
 	cv::cuda::GpuMat *colour_mat_;
 	cv::cuda::GpuMat *point_mat_;
@@ -103,9 +53,8 @@ struct DepthCamera {
 	ftl::cuda::TextureObject<float> *depth_tex_;
 	ftl::cuda::TextureObject<uchar4> *colour_tex_;
 	ftl::cuda::TextureObject<float4> *normal_tex_;
-	//cudaTextureObject_t depth_obj_;
-	//cudaTextureObject_t colour_obj_;
 
+	// This part is sent to device
 	DepthCameraCUDA data;
 };
 }
diff --git a/applications/reconstruct/src/depth_camera.cpp b/applications/reconstruct/src/depth_camera.cpp
new file mode 100644
index 000000000..dd04da773
--- /dev/null
+++ b/applications/reconstruct/src/depth_camera.cpp
@@ -0,0 +1,58 @@
+#include <ftl/depth_camera.hpp>
+
+using ftl::voxhash::DepthCamera;
+
+extern "C" void computeNormals(float4* d_output, float3* d_input, unsigned int width, unsigned int height);
+extern "C" void convertDepthFloatToCameraSpaceFloat3(float3* d_output, float* d_input, float4x4 intrinsicsInv, unsigned int width, unsigned int height, const DepthCameraData& depthCameraData);
+
+DepthCamera::DepthCamera() {
+	depth_mat_ = nullptr;
+	colour_mat_ = nullptr;
+	point_mat_ = nullptr;
+	normal_mat_ = nullptr;
+	depth_tex_ = nullptr;
+	colour_tex_ = nullptr;
+	normal_tex_ = nullptr;
+}
+
+void DepthCamera::alloc(const DepthCameraParams& params, bool withNormals=false) { //! todo resizing???
+	depth_mat_ = new cv::cuda::GpuMat(params.m_imageHeight, params.m_imageWidth, CV_32FC1);
+	colour_mat_ = new cv::cuda::GpuMat(params.m_imageHeight, params.m_imageWidth, CV_8UC4);
+	depth_tex_ = new ftl::cuda::TextureObject<float>((cv::cuda::PtrStepSz<float>)*depth_mat_);
+	colour_tex_ = new ftl::cuda::TextureObject<uchar4>((cv::cuda::PtrStepSz<uchar4>)*colour_mat_);
+	data.depth = depth_tex_->cudaTexture();
+	data.colour = colour_tex_->cudaTexture();
+	data.params = params;
+
+	if (withNormals) {
+		point_mat_ = new cv::cuda::GpuMat(params.m_imageHeight, params.m_imageWidth, CV_32FC3);
+		normal_mat_ = new cv::cuda::GpuMat(params.m_imageHeight, params.m_imageWidth, CV_32FC4);
+		normal_tex_ = new ftl::cuda::TextureObject<float4>((cv::cuda::PtrStepSz<float4>)*normal_mat_);
+		data.normal = normal_tex_->cudaTexture();
+	} else {
+		data.normal = 0;
+	}
+}
+
+void DepthCamera::free() {
+	if (depth_mat_) delete depth_mat_;
+	if (colour_mat_) delete colour_mat_;
+	if (point_mat_) delete point_mat_;
+	if (normal_mat_) delete normal_mat_;
+	delete depth_tex_;
+	delete colour_tex_;
+	if (normal_tex_) delete normal_tex_;
+}
+
+void DepthCamera::updateData(const cv::Mat &depth, const cv::Mat &rgb, cv::cuda::Stream &stream) {
+	depth_mat_->upload(depth, stream);
+	colour_mat_->upload(rgb, stream);
+	if (normal_mat_) {
+		_computeNormals(cv::cuda::StreamAccessor::getStream(stream));
+	}
+}
+
+void DepthCamera::_computeNormals(cudaStream_t stream) {
+	convertDepthFloatToCameraSpaceFloat3(m_data.d_depth3, m_data.d_depth, m_params.m_intrinsicsInverse, m_params.m_width, m_params.m_height, cameraData);
+	computeNormals(m_data.d_normals, m_data.d_depth3, m_params.m_width, m_params.m_height);
+}
-- 
GitLab