implement registration

try running simple wsl-command fails
add Ply-File export
change build-target to x64 (for wsl)
This commit is contained in:
Tim Wundenberg
2021-08-22 18:58:41 +02:00
parent 4c131c103b
commit acda8b9eac
13 changed files with 204 additions and 82 deletions

View File

@@ -5,6 +5,14 @@
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<PlatformTarget>x64</PlatformTarget>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<PlatformTarget>x64</PlatformTarget>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\PointCloudWeb.Server\PointCloudWeb.Server.csproj" />
</ItemGroup>

View File

@@ -5,6 +5,14 @@
<IsPackable>false</IsPackable>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<PlatformTarget>x64</PlatformTarget>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<PlatformTarget>x64</PlatformTarget>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />

View File

@@ -1,4 +1,5 @@
using System.IO;
using System;
using System.IO;
namespace PointCloudWeb.Server
{
@@ -12,12 +13,24 @@ namespace PointCloudWeb.Server
TempPath = basePath + "/temp";
CloudCompareExe = "C:/Program Files/CloudCompare/CloudCompare.exe";
LasToolsTxt2Las = basePath + "/PointCloudWeb.Server/Tools/LAStools/txt2las.exe";
TeaserPp = basePath + "/PointCloudWeb.Server/Tools/TEASERpp/build/teaser_cpp_ply";
}
public static string LasToolsTxt2Las { get; }
public static string LasToolsTxt2Las { get; }
public static string PotreeDataPath { get; }
public static string PotreeConverterExe { get; }
public static string TempPath { get; }
public static string CloudCompareExe { get; }
public static string TeaserPp { get; }
public static string ToWslPath(string path)
{
path = path.Replace("\\", "/");
if (path.StartsWith("C:"))
return path.Replace("C:", "/mnt/c");
throw new NotImplementedException();
}
}
}

View File

@@ -63,12 +63,20 @@ namespace PointCloudWeb.Server.Models
public string ToStringXyz()
{
var stringBuilder = new StringBuilder();
foreach (var point in Points)
// var maxPoints = 5_000;
var takeEvery = 1;//Points.Count / maxPoints;
for (var i = 0; i < Points.Count; i++)
{
if (i % takeEvery != 0)
continue;
var point = Points[i];
stringBuilder.AppendLine(string.Join(',',
(point.X).ToString(CultureInfo.InvariantCulture),
(point.Y).ToString(CultureInfo.InvariantCulture),
(point.Z).ToString(CultureInfo.InvariantCulture)
point.X.ToString(CultureInfo.InvariantCulture),
point.Y.ToString(CultureInfo.InvariantCulture),
point.Z.ToString(CultureInfo.InvariantCulture)
)
);
}
@@ -76,12 +84,55 @@ namespace PointCloudWeb.Server.Models
return stringBuilder.ToString();
}
private string ToStringPly(int maxPoints)
{
var stringBuilder = new StringBuilder();
stringBuilder.Append("ply\n");
stringBuilder.Append("format ascii 1.0\n");
stringBuilder.Append($"element vertex COUNT_PLACEHOLDER {Points.Count}\n");
stringBuilder.Append("property float x\n");
stringBuilder.Append("property float y\n");
stringBuilder.Append("property float z\n");
stringBuilder.Append("end_header\n");
if (maxPoints == 0)
maxPoints = Points.Count;
var takeEvery = Points.Count / maxPoints;
var count = 0;
for (var i = 0; i < Points.Count; i++)
{
if (i % takeEvery != 0)
continue;
count += 1;
var point = Points[i];
stringBuilder.Append(string.Join(' ',
point.X,
point.Y,
point.Z
) + "\n"
);
}
stringBuilder.Replace("COUNT_PLACEHOLDER", count.ToString());
return stringBuilder.ToString();
}
// ReSharper disable once MemberCanBePrivate.Global
public void WriteToXyz(string fileName)
{
File.WriteAllText(fileName, ToStringXyz());
}
// ReSharper disable once MemberCanBePrivate.Global
public void WriteToPly(string fileName, int maxPoints)
{
File.WriteAllText(fileName, ToStringPly(maxPoints));
}
public void WriteToLasCloudCompare(string fileName)
{
var fileNameXyz = Path.ChangeExtension(fileName, ".xyz");

View File

@@ -4,6 +4,14 @@
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<PlatformTarget>x64</PlatformTarget>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<PlatformTarget>x64</PlatformTarget>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Abstractions" Version="2.2.0" />
</ItemGroup>

View File

@@ -3,8 +3,19 @@ using System.Numerics;
namespace PointCloudWeb.Server.Services
{
public interface IPointCloudRegistationService
public class RegistrationResult
{
public Matrix4x4 RegisterPointCloud(PointCloud source, PointCloud target);
public RegistrationResult()
{
Transformation = Vector3.Zero;
Rotation = Vector3.Zero;
}
public Vector3 Transformation { get; set; }
public Vector3 Rotation { get; set; }
}
public interface IPointCloudRegistrationService
{
public RegistrationResult RegisterPointCloud(PointCloud source, PointCloud target);
}
}

View File

@@ -0,0 +1,46 @@
using System;
using System.Diagnostics;
using System.Linq;
using PointCloudWeb.Server.Models;
namespace PointCloudWeb.Server.Services
{
public class PointCloudRegistrationServiceTeaerPp : IPointCloudRegistrationService
{
public RegistrationResult RegisterPointCloud(PointCloud source, PointCloud target)
{
var sourceFileName = Globals.TempPath + $"/{source.Id}.ply";
var targetFileName = Globals.TempPath + $"/{target.Id}.ply";
var maxPoints = 5_000;
source.WriteToPly(sourceFileName, maxPoints);
target.WriteToPly(targetFileName, maxPoints);
var teaserPp = new Process();
teaserPp.StartInfo.FileName = "C:\\Windows\\System32\\wsl.exe";
// teaserPp.StartInfo.FileName = "wsl";
teaserPp.StartInfo.Arguments = "curl https://www.google.de";
// teaserPp.StartInfo.Arguments = $"\"{sourceFileName}\" \"{targetFileName}\"";
//teaserPp.StartInfo.Arguments = $" ls "; //{Globals.ToWslPath(Globals.TeaserPp)} ";// +
//"\"/mnt/c/Users/timwu/source/repos/PointCloudWeb/PointCloudWeb.Server/Tools/TEASERpp/c4b9b7fc-0b97-4f52-ad1b-737aeca5ba97.ply\" " +
//"\"/mnt/c/Users/timwu/source/repos/PointCloudWeb/PointCloudWeb.Server/Tools/TEASERpp/c620b175-ace8-42e5-bf29-55b6c99372bc.ply\" ";
teaserPp.StartInfo.RedirectStandardOutput = true;
teaserPp.Start();
var output = teaserPp.StandardOutput.ReadToEnd();
Console.WriteLine(output[..Math.Min(output.Length, 100)]);
teaserPp.WaitForExit();
Console.WriteLine($"RegistrationExitCode: {teaserPp.ExitCode}");
var result = new RegistrationResult();
return result;
}
}
}

View File

@@ -2,7 +2,6 @@ using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Numerics;
using PointCloudWeb.Server.Models;
using PointCloudWeb.Server.Utils;
@@ -10,13 +9,13 @@ namespace PointCloudWeb.Server.Services
{
public class PointCloudService
{
//private readonly IPointCloudRegistrationService pointCloudRegistration;
private readonly IPointCloudRegistrationService _pointCloudRegistration;
private readonly PointCloudCollection _pointClouds;
public PointCloudService( /*IPointCloudRegistrationService pointCloudRegistration*/)
public PointCloudService( IPointCloudRegistrationService pointCloudRegistration)
{
_pointClouds = new PointCloudCollection();
//this.pointCloudRegistration = pointCloudRegistration;
_pointCloudRegistration = pointCloudRegistration;
InitSampleData();
}
@@ -78,17 +77,18 @@ namespace PointCloudWeb.Server.Services
{
RaiseIfNotExists(id);
var pc = GetById(id);
var source = GetById(id);
pc.Transformation = Vector3.Zero;
pc.Rotation = Vector3.Zero;
//the first can't be registered
if (_pointClouds.IndexOf(source) == 0)
return;
var target = _pointClouds[_pointClouds.IndexOf(source) - 1];
// //the first can't be registered
// if (_pointClouds.IndexOf(pointCloud) == 0)
// return;
//var transformation = pointCloudRegistration.RegisterPointCloud(pointCloud, pointClouds[0]);
//pointCloud.Transformation = transformation;
var result = _pointCloudRegistration.RegisterPointCloud(source, target);
source.Rotation = result.Rotation;
source.Transformation = result.Transformation;
}
public void RegisterPointClouds()

View File

@@ -51,6 +51,7 @@ namespace PointCloudWeb.Server
services.AddSingleton<PointCloudService>();
services.AddTransient<ScanConverterService>();
services.AddTransient<ScanDataService>();
services.AddTransient<IPointCloudRegistrationService, PointCloudRegistrationServiceTeaerPp>();
services.AddControllers();
}
}

View File

@@ -37,11 +37,11 @@ namespace PointCloudWeb.Server.Utils
public static void CreateData(PointCloudService pointCloudService)
{
var pc = pointCloudService.AddPointCloud();
var pc = pointCloudService.AddPointCloud(new Guid("c4b9b7fc-0b97-4f52-ad1b-737aeca5ba97"));
LoadPointCloudFromEthFile(pc, "ETH-Data/Hokuyo_0.csv");
pointCloudService.PointCloudCompleted(pc.Id);
pc = pointCloudService.AddPointCloud();
pc = pointCloudService.AddPointCloud(new Guid("c620b175-ace8-42e5-bf29-55b6c99372bc"));
LoadPointCloudFromEthFile(pc, "ETH-Data/Hokuyo_1.csv");
pointCloudService.PointCloudCompleted(pc.Id);
}

View File

@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.10)
project(teaser_cpp_ply)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD 17)
find_package(Eigen3 REQUIRED)
find_package(teaserpp REQUIRED)

View File

@@ -0,0 +1,3 @@
`bun_zipper.ply` is taken from the [Stanford 3D Scanning Repository](https://graphics.stanford.edu/data/3Dscanrep/).
In the `3dmatch_sample/` folder, a pair of scan from the `sun3d-home_at-home_at_scan1_2013_jan_1` scene in the 3DMatch dataset is provided. Descriptors calculated by using the `32_dim` pretrained network from [3DSmoothNet](https://github.com/zgojcic/3DSmoothNet) is also provided, together with the ground truth transformation.

View File

@@ -1,7 +1,9 @@
// An example showing TEASER++ registration with the Stanford bunny model
#include <chrono>
#include <iostream>
#include <fstream>
#include <random>
#include <filesystem>
#include <Eigen/Core>
@@ -10,70 +12,50 @@
// Macro constants for generating noise and outliers
#define NOISE_BOUND 0.05
#define N_OUTLIERS 1700
#define OUTLIER_TRANSLATION_LB 5
#define OUTLIER_TRANSLATION_UB 10
// #define N_OUTLIERS 1700
// #define OUTLIER_TRANSLATION_LB 5
// #define OUTLIER_TRANSLATION_UB 10
inline double getAngularError(Eigen::Matrix3d R_exp, Eigen::Matrix3d R_est) {
inline double getAngularError(Eigen::Matrix3d R_exp, Eigen::Matrix3d R_est)
{
return std::abs(std::acos(fmin(fmax(((R_exp.transpose() * R_est).trace() - 1) / 2, -1.0), 1.0)));
}
void addNoiseAndOutliers(Eigen::Matrix<double, 3, Eigen::Dynamic>& tgt) {
// Add uniform noise
Eigen::Matrix<double, 3, Eigen::Dynamic> noise =
Eigen::Matrix<double, 3, Eigen::Dynamic>::Random(3, tgt.cols()) * NOISE_BOUND;
NOISE_BOUND / 2;
tgt = tgt + noise;
int main(int argc, char **argv)
{
std::cout << "You have entered " << argc
<< " arguments:"
<< "\n";
// Add outliers
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis2(0, tgt.cols() - 1); // pos of outliers
std::uniform_int_distribution<> dis3(OUTLIER_TRANSLATION_LB,
OUTLIER_TRANSLATION_UB); // random translation
std::vector<bool> expected_outlier_mask(tgt.cols(), false);
for (int i = 0; i < N_OUTLIERS; ++i) {
int c_outlier_idx = dis2(gen);
assert(c_outlier_idx < expected_outlier_mask.size());
expected_outlier_mask[c_outlier_idx] = true;
tgt.col(c_outlier_idx).array() += dis3(gen); // random translation
}
}
for (int i = 0; i < argc; ++i)
std::cout << argv[i] << "\n";
auto src_fileName = argv[1];
auto tgt_fileName = argv[2];
int main() {
// Load the .ply file
teaser::PLYReader reader;
teaser::PointCloud src_cloud;
auto status = reader.read("./example_data/bun_zipper_res3.ply", src_cloud);
int N = src_cloud.size();
auto status = reader.read(src_fileName, src_cloud);
int src_cloud_size = src_cloud.size();
// Convert the point cloud to Eigen
Eigen::Matrix<double, 3, Eigen::Dynamic> src(3, N);
for (size_t i = 0; i < N; ++i) {
Eigen::Matrix<double, 3, Eigen::Dynamic> src(3, src_cloud_size);
for (size_t i = 0; i < src_cloud_size; ++i)
{
src.col(i) << src_cloud[i].x, src_cloud[i].y, src_cloud[i].z;
}
// Homogeneous coordinates
Eigen::Matrix<double, 4, Eigen::Dynamic> src_h;
src_h.resize(4, src.cols());
src_h.topRows(3) = src;
src_h.bottomRows(1) = Eigen::Matrix<double, 1, Eigen::Dynamic>::Ones(N);
teaser::PointCloud tgt_cloud;
status = reader.read(tgt_fileName, tgt_cloud);
int tgt_cloud_size = tgt_cloud.size();
// Apply an arbitrary SE(3) transformation
Eigen::Matrix4d T;
// clang-format off
T << 9.96926560e-01, 6.68735757e-02, -4.06664421e-02, -1.15576939e-01,
-6.61289946e-02, 9.97617877e-01, 1.94008687e-02, -3.87705398e-02,
4.18675510e-02, -1.66517807e-02, 9.98977765e-01, 1.14874890e-01,
0, 0, 0, 1;
// clang-format on
// Apply transformation
Eigen::Matrix<double, 4, Eigen::Dynamic> tgt_h = T * src_h;
Eigen::Matrix<double, 3, Eigen::Dynamic> tgt = tgt_h.topRows(3);
// Add some noise & outliers
addNoiseAndOutliers(tgt);
// Convert the point cloud to Eigen
Eigen::Matrix<double, 3, Eigen::Dynamic> tgt(3, tgt_cloud_size);
for (size_t i = 0; i < tgt_cloud_size; ++i)
{
tgt.col(i) << tgt_cloud[i].x, tgt_cloud[i].y, tgt_cloud[i].z;
}
// Run TEASER++ registration
// Prepare solver parameters
@@ -99,21 +81,12 @@ int main() {
std::cout << "=====================================" << std::endl;
std::cout << " TEASER++ Results " << std::endl;
std::cout << "=====================================" << std::endl;
std::cout << "Expected rotation: " << std::endl;
std::cout << T.topLeftCorner(3, 3) << std::endl;
std::cout << "Estimated rotation: " << std::endl;
std::cout << solution.rotation << std::endl;
std::cout << "Error (deg): " << getAngularError(T.topLeftCorner(3, 3), solution.rotation)
<< std::endl;
std::cout << std::endl;
std::cout << "Expected translation: " << std::endl;
std::cout << T.topRightCorner(3, 1) << std::endl;
std::cout << "Estimated translation: " << std::endl;
std::cout << solution.translation << std::endl;
std::cout << "Error (m): " << (T.topRightCorner(3, 1) - solution.translation).norm() << std::endl;
std::cout << std::endl;
std::cout << "Number of correspondences: " << N << std::endl;
std::cout << "Number of outliers: " << N_OUTLIERS << std::endl;
std::cout << "Time taken (s): "
<< std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count() /
1000000.0