Skip to content
Snippets Groups Projects
Commit c9629aed authored by Nicolas Pope's avatar Nicolas Pope
Browse files

Merge branch 'feature/222/meshmerge' into 'master'

Implements #222 simple mesh merge

Closes #222

See merge request nicolas.pope/ftl!154
parents 2829286b 13b30b81
No related branches found
No related tags found
1 merge request!154Implements #222 simple mesh merge
Pipeline #16062 passed
......@@ -21,6 +21,13 @@ namespace cuda {
ftl::cuda::TextureObject<short2> &screen,
const ftl::render::SplatParams &params,
cudaStream_t stream);
void mesh_blender(
ftl::cuda::TextureObject<int> &depth_in,
ftl::cuda::TextureObject<int> &depth_out,
const ftl::rgbd::Camera &camera,
float alpha,
cudaStream_t stream);
void dibr_merge(
ftl::cuda::TextureObject<float4> &points,
......
......@@ -292,8 +292,17 @@ void Triangular::_dibr(cudaStream_t stream) {
void Triangular::_mesh(cudaStream_t stream) {
cv::cuda::Stream cvstream = cv::cuda::StreamAccessor::wrapStream(stream);
temp_.get<GpuMat>(Channel::Depth2).setTo(cv::Scalar(0x7FFFFFFF), cvstream);
bool do_blend = value("mesh_blend", true);
float blend_alpha = value("blend_alpha", 0.02f);
if (do_blend) {
temp_.get<GpuMat>(Channel::Depth).setTo(cv::Scalar(0x7FFFFFFF), cvstream);
temp_.get<GpuMat>(Channel::Depth2).setTo(cv::Scalar(0x7FFFFFFF), cvstream);
} else {
temp_.get<GpuMat>(Channel::Depth2).setTo(cv::Scalar(0x7FFFFFFF), cvstream);
}
// For each source depth map
for (size_t i=0; i < scene_->frames.size(); ++i) {
auto &f = scene_->frames[i];
auto *s = scene_->sources[i];
......@@ -305,6 +314,7 @@ void Triangular::_mesh(cudaStream_t stream) {
auto pose = MatrixConversion::toCUDA(s->getPose().cast<float>());
// Calculate and save virtual view screen position of each source pixel
ftl::cuda::screen_coord(
f.createTexture<float>(Channel::Depth),
f.createTexture<float>(Channel::Depth2, Format<float>(f.get<GpuMat>(Channel::Depth).size())),
......@@ -312,14 +322,29 @@ void Triangular::_mesh(cudaStream_t stream) {
params_, pose, s->parameters(), stream
);
// Must reset depth channel if blending
if (do_blend) {
temp_.get<GpuMat>(Channel::Depth).setTo(cv::Scalar(0x7FFFFFFF), cvstream);
}
// Decide on and render triangles around each point
ftl::cuda::triangle_render1(
f.getTexture<float>(Channel::Depth2),
temp_.createTexture<int>(Channel::Depth2),
temp_.createTexture<int>((do_blend) ? Channel::Depth : Channel::Depth2),
f.getTexture<short2>(Channel::Screen),
params_, stream
);
//LOG(INFO) << "DIBR DONE";
if (do_blend) {
// Blend this sources mesh with previous meshes
ftl::cuda::mesh_blender(
temp_.getTexture<int>(Channel::Depth),
temp_.createTexture<int>(Channel::Depth2),
params_.camera,
blend_alpha,
stream
);
}
}
}
......
......@@ -3,6 +3,8 @@
#include <ftl/rgbd/camera.hpp>
#include <ftl/cuda_common.hpp>
#include <ftl/cuda/weighting.hpp>
using ftl::rgbd::Camera;
using ftl::cuda::TextureObject;
using ftl::render::SplatParams;
......@@ -162,3 +164,39 @@ void ftl::cuda::triangle_render1(TextureObject<float> &depth_in, TextureObject<i
triangle_render_1_kernel<-1,-1><<<gridSize, blockSize, 0, stream>>>(depth_in, depth_out, screen, params);
cudaSafeCall( cudaGetLastError() );
}
// ==== BLENDER ========
/*
* Merge two depth maps together
*/
__global__ void mesh_blender_kernel(
TextureObject<int> depth_in,
TextureObject<int> depth_out,
ftl::rgbd::Camera camera,
float alpha) {
const int x = blockIdx.x*blockDim.x + threadIdx.x;
const int y = blockIdx.y*blockDim.y + threadIdx.y;
if (x < 0 || x >= depth_in.width() || y < 0 || y >= depth_in.height()) return;
int a = depth_in.tex2D(x,y);
int b = depth_out.tex2D(x,y);
float mindepth = (float)min(a,b) / 100000.0f;
float maxdepth = (float)max(a,b) / 100000.0f;
float weight = ftl::cuda::weighting(maxdepth-mindepth, alpha);
//depth_out(x,y) = (int)(((float)mindepth + (float)maxdepth*weight) / (1.0f + weight) * 100000.0f);
float depth = (mindepth + maxdepth*weight) / (1.0f + weight);
depth_out(x,y) = (int)(depth * 100000.0f);
}
void ftl::cuda::mesh_blender(TextureObject<int> &depth_in, TextureObject<int> &depth_out, const ftl::rgbd::Camera &camera, float alpha, cudaStream_t stream) {
const dim3 gridSize((depth_in.width() + T_PER_BLOCK - 1)/T_PER_BLOCK, (depth_in.height() + T_PER_BLOCK - 1)/T_PER_BLOCK);
const dim3 blockSize(T_PER_BLOCK, T_PER_BLOCK);
mesh_blender_kernel<<<gridSize, blockSize, 0, stream>>>(depth_in, depth_out, camera, alpha);
cudaSafeCall( cudaGetLastError() );
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment