Add ScanData Controller

This commit is contained in:
Tim Wundenberg
2021-07-11 10:14:43 +02:00
parent 0cf75695b4
commit f8e2e08886
8 changed files with 260 additions and 0 deletions

View File

@@ -0,0 +1,24 @@
using Microsoft.AspNetCore.Mvc;
using PointCloudWeb.Server.Models;
using PointCloudWeb.Server.Services;
namespace PointCloudWeb.Server.Controllers
{
[ApiController]
[Route("[controller]")]
public class DataController : ControllerBase
{
private readonly DataService _scanDataService;
public DataController(DataService scanDataService)
{
_scanDataService = scanDataService;
}
[HttpPost]
public void PostScanData([FromBody] ScanDataList data)
{
_scanDataService.AddScan(data);
}
}
}

View File

@@ -0,0 +1,73 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace PointCloudWeb.Server.Models
{
public class Point
{
public double X { get; set; }
public double Y { get; set; }
public double Z { get; set; }
public Point() : this(0, 0, 0) { }
public Point(double x, double y, double z)
{
X = x;
Y = y;
Z = z;
}
public override string ToString() => (X.ToString() + " " + Y.ToString() + " " + Z.ToString());
}
public class PointCloud
{
public Guid Id { get; private set; }
public string Name { get; set; }
public IList<Point> Points { get; private set; }
/// <summary>
/// Function <c>ScaleFactor</c> defines relation between X/Y/Z distances in mm
/// So, Point A(0,0,0) is 1mm from B(1,0,0) apart
/// </summary>
public static int ScaleFactor() => 1;
public PointCloud(String name, Guid id)
{
Points = new List<Point>();
Id = id;
Name = name;
}
}
public class PointCloudCollection : List<PointCloud>
{
public PointCloud GetById(Guid id)
{
return this.First(pc => pc.Id == id);
}
public PointCloud AddNew()
{
var id = Guid.NewGuid();
var pc = new PointCloud(id.ToString(), id);
Add(pc);
return pc;
}
public bool Contains(Guid id)
{
return this.Any(pc => pc.Id == id);
}
public void RemoveById(Guid id)
{
Remove(GetById(id));
}
}
}

View File

@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
namespace PointCloudWeb.Server.Models
{
public class ScanDataPoint
{
//RotationAngle on {Y,Z,X} Axis
public double RAY { get; set; }
public double RAZ { get; set; }
public double RAX { get; set; }
public double DistanceMM { get; set; }
}
public class ScanDataList
{
public Guid Id { get; set; }
public IList<ScanDataPoint> List { get; set; }
}
}

View File

@@ -0,0 +1,14 @@
using PointCloudWeb.Server.Models;
namespace PointCloudWeb.Server.Services
{
public interface IPointCloudRegistation
{
class TransformationMatrix { }
public TransformationMatrix RegisterPointCloud(PointCloud pc1, PointCloud pc2)
{
return null;
}
}
}

View File

@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using PointCloudWeb.Server.Models;
namespace PointCloudWeb.Server.Services
{
public class PointCloudService
{
private readonly PointCloudCollection _pointClouds;
public PointCloudService()
{
_pointClouds = new PointCloudCollection();
}
public void AddPoints(Guid id, IList<Point> points)
{
if (!_pointClouds.Contains(id))
throw new ArgumentOutOfRangeException("The Id {0} was not found!", id.ToString());
var pc = _pointClouds.GetById(id);
foreach (var point in points)
pc.Points.Add(point);
}
}
}

View File

@@ -0,0 +1,66 @@
using System;
using System.Numerics;
using PointCloudWeb.Server.Models;
namespace PointCloudWeb.Server.Services
{
public class ScanConverterService
{
private enum RotationAxis { X, Y, Z };
private Matrix4x4 GetTransformationMatrix(ScanDataPoint scan, RotationAxis type)
{
var angle = type switch
{
RotationAxis.X => scan.RAX,
RotationAxis.Y => scan.RAY,
RotationAxis.Z => scan.RAZ,
_ => throw new NotImplementedException(),
};
if (angle == 0)
return new Matrix4x4(
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
);
var sin = (float)Math.Sin(angle);
var cos = (float)Math.Cos(angle);
return type switch
{
RotationAxis.X => new Matrix4x4(
1, 0, 0, 0,
0, sin, -sin, 0,
0, sin, cos, 0,
0, 0, 0, 1
),
RotationAxis.Y => new Matrix4x4(
cos, 0, sin, 0,
0, 1, 0, 0,
-sin, 0, cos, 0,
0, 0, 0, 1
),
RotationAxis.Z => new Matrix4x4(
cos, -sin, 0, 0,
sin, cos, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
),
_ => throw new NotImplementedException(),
};
}
public Point Transform(ScanDataPoint scan)
{
Vector3 v = new Vector3(0, 0, (int)scan.DistanceMM);
Vector3.Transform(v, GetTransformationMatrix(scan, RotationAxis.X));
Vector3.Transform(v, GetTransformationMatrix(scan, RotationAxis.Z));
Vector3.Transform(v, GetTransformationMatrix(scan, RotationAxis.Y));
return new Point(v.X, v.Y, v.Z);
}
}
}

View File

@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using PointCloudWeb.Server.Models;
namespace PointCloudWeb.Server.Services
{
public class DataService
{
private readonly PointCloudService _pointCloudService;
private readonly ScanConverterService _scanConverterService;
public DataService(PointCloudService pointCloudService, ScanConverterService scanConverterService)
{
_pointCloudService = pointCloudService;
_scanConverterService = scanConverterService;
}
public void AddScan(ScanDataList scanData)
{
_pointCloudService.AddPoints(scanData.Id, ConvertToPoints(scanData));
}
private IList<Point> ConvertToPoints(ScanDataList scanData)
{
var list = new List<Point>();
foreach (var scan in scanData.List)
{
list.Add(_scanConverterService.Transform(scan));
}
return list;
}
}
}