diff --git a/PointCloudWeb.Server/PointCloudWeb.Server/Controllers/ScanDataController.cs b/PointCloudWeb.Server/PointCloudWeb.Server/Controllers/ScanDataController.cs index e4e4a49..e6be899 100644 --- a/PointCloudWeb.Server/PointCloudWeb.Server/Controllers/ScanDataController.cs +++ b/PointCloudWeb.Server/PointCloudWeb.Server/Controllers/ScanDataController.cs @@ -1,6 +1,7 @@ using Microsoft.AspNetCore.Mvc; using PointCloudWeb.Server.Models; using PointCloudWeb.Server.Services; +using System; namespace PointCloudWeb.Server.Controllers { @@ -8,17 +9,23 @@ namespace PointCloudWeb.Server.Controllers [Route("[controller]")] public class DataController : ControllerBase { - private readonly DataService _scanDataService; + private readonly DataService scanDataService; public DataController(DataService scanDataService) { - _scanDataService = scanDataService; + this.scanDataService = scanDataService; } [HttpPost] public void PostScanData([FromBody] ScanDataList data) { - _scanDataService.AddScan(data); + scanDataService.AddScan(data); + } + + [HttpPut] + public void ScanFinished(Guid id) + { + scanDataService.ScanFinished(id); } } } \ No newline at end of file diff --git a/PointCloudWeb.Server/PointCloudWeb.Server/Controllers/WeatherForecastController.cs b/PointCloudWeb.Server/PointCloudWeb.Server/Controllers/WeatherForecastController.cs index 2f9067c..8685dd1 100644 --- a/PointCloudWeb.Server/PointCloudWeb.Server/Controllers/WeatherForecastController.cs +++ b/PointCloudWeb.Server/PointCloudWeb.Server/Controllers/WeatherForecastController.cs @@ -16,11 +16,11 @@ namespace PointCloudWeb.Server.Controllers "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" }; - private readonly ILogger _logger; + private readonly ILogger logger; public WeatherForecastController(ILogger logger) { - _logger = logger; + this.logger = logger; } [HttpGet] diff --git a/PointCloudWeb.Server/PointCloudWeb.Server/Models/PointCloud.cs b/PointCloudWeb.Server/PointCloudWeb.Server/Models/PointCloud.cs index c2e1e3b..f2b00d3 100644 --- a/PointCloudWeb.Server/PointCloudWeb.Server/Models/PointCloud.cs +++ b/PointCloudWeb.Server/PointCloudWeb.Server/Models/PointCloud.cs @@ -1,6 +1,10 @@ +using PointCloudWeb.Server.Utils; using System; using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Collections.Specialized; using System.Linq; +using System.Numerics; namespace PointCloudWeb.Server.Models { @@ -34,21 +38,71 @@ namespace PointCloudWeb.Server.Models public override int GetHashCode() => HashCode.Combine(X, Y, Z); - public override string ToString() => (X.ToString() + " " + Y.ToString() + " " + Z.ToString()); + public override string ToString() => X.ToString() + " " + Y.ToString() + " " + Z.ToString(); } public class PointCloud { - public PointCloud(String name, Guid id) + private ObservableCollection points; + private Matrix4x4 transformation; + + public PointCloud(string name, Guid id) { - Points = new List(); + points = new ObservableCollection(); + points.CollectionChanged += PointsCollectionChanged; + TransformedPoints = new List(); Id = id; Name = name; } public Guid Id { get; private set; } + public string Name { get; set; } - public IList Points { get; private set; } + + public IList Points { get => points; } + + public Matrix4x4 Transformation + { + get => transformation; + set + { + TransformationChanged(); + transformation = value; + } + } + + public IList TransformedPoints { get; private set; } + + private Point GetTransformedPoint(Point point) + { + if (Transformation.IsIdentity) + return new Point(point.X, point.Y, point.Z); + + var v = new Vector3(point.X, point.Y, point.Z); + v = Vector3.Transform(v, Transformation); + + return new Point(NumericUtils.Round(v.X), NumericUtils.Round(v.Y), NumericUtils.Round(v.Z)); + } + + private void PointsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) + { + if (TransformedPoints.Count > 0) + TransformationChanged(); + } + + public void TransformationChanged() + { + TransformedPoints.Clear(); + + if (Transformation.IsIdentity) + return; + + foreach (var point in Points) + { + var transformedPoint = GetTransformedPoint(point); + TransformedPoints.Add(transformedPoint); + } + } } public class PointCloudCollection : List diff --git a/PointCloudWeb.Server/PointCloudWeb.Server/Services/IPointCloudRegistration.cs b/PointCloudWeb.Server/PointCloudWeb.Server/Services/IPointCloudRegistration.cs deleted file mode 100644 index d64af5e..0000000 --- a/PointCloudWeb.Server/PointCloudWeb.Server/Services/IPointCloudRegistration.cs +++ /dev/null @@ -1,14 +0,0 @@ -using PointCloudWeb.Server.Models; - -namespace PointCloudWeb.Server.Services -{ - public interface IPointCloudRegistation - { - public TransformationMatrix RegisterPointCloud(PointCloud pc1, PointCloud pc2) - { - return null; - } - - class TransformationMatrix { } - } -} \ No newline at end of file diff --git a/PointCloudWeb.Server/PointCloudWeb.Server/Services/IPointCloudRegistrationService.cs b/PointCloudWeb.Server/PointCloudWeb.Server/Services/IPointCloudRegistrationService.cs new file mode 100644 index 0000000..609e9d9 --- /dev/null +++ b/PointCloudWeb.Server/PointCloudWeb.Server/Services/IPointCloudRegistrationService.cs @@ -0,0 +1,10 @@ +using PointCloudWeb.Server.Models; +using System.Numerics; + +namespace PointCloudWeb.Server.Services +{ + public interface IPointCloudRegistationService + { + public Matrix4x4 RegisterPointCloud(PointCloud source, PointCloud target); + } +} \ No newline at end of file diff --git a/PointCloudWeb.Server/PointCloudWeb.Server/Services/PointCloudService.cs b/PointCloudWeb.Server/PointCloudWeb.Server/Services/PointCloudService.cs index b89c5fa..80ac7a7 100644 --- a/PointCloudWeb.Server/PointCloudWeb.Server/Services/PointCloudService.cs +++ b/PointCloudWeb.Server/PointCloudWeb.Server/Services/PointCloudService.cs @@ -6,16 +6,18 @@ namespace PointCloudWeb.Server.Services { public class PointCloudService { - private readonly PointCloudCollection _pointClouds; + private readonly IPointCloudRegistationService pointCloudRegistation; + private readonly PointCloudCollection pointClouds; - public PointCloudService() + public PointCloudService(IPointCloudRegistationService pointCloudRegistation) { - _pointClouds = new PointCloudCollection(); + pointClouds = new PointCloudCollection(); + this.pointCloudRegistation = pointCloudRegistation; } private void RaiseIfNotExists(Guid id) { - if (!_pointClouds.Contains(id)) + if (!pointClouds.Contains(id)) throw new ArgumentOutOfRangeException("The Id {0} was not found!", id.ToString()); } @@ -23,7 +25,7 @@ namespace PointCloudWeb.Server.Services { RaiseIfNotExists(id); - var pc = _pointClouds.GetById(id); + var pc = pointClouds.GetById(id); foreach (var point in points) pc.Points.Add(point); @@ -31,16 +33,21 @@ namespace PointCloudWeb.Server.Services public void RegisterPointCloud(Guid id) { - RegisterPointClouds(new List() { id }); + RaiseIfNotExists(id); + var pointCloud = pointClouds.GetById(id); + + //the first can't be registered + if (pointClouds.IndexOf(pointCloud) == 0) + return; + + var transformation = pointCloudRegistation.RegisterPointCloud(pointCloud, pointClouds[0]); + pointCloud.Transformation = transformation; } - public void RegisterPointClouds(IList ids) + public void RegisterPointClouds() { - //ensure that every element in "ids" is in "_pointClouds" - foreach (var id in ids) - RaiseIfNotExists(id); - - throw new NotImplementedException(); + foreach (var pointCloud in pointClouds) + RegisterPointCloud(pointCloud.Id); } } } \ No newline at end of file diff --git a/PointCloudWeb.Server/PointCloudWeb.Server/Services/ScanConverterService.cs b/PointCloudWeb.Server/PointCloudWeb.Server/Services/ScanConverterService.cs index dc11998..1045fd6 100644 --- a/PointCloudWeb.Server/PointCloudWeb.Server/Services/ScanConverterService.cs +++ b/PointCloudWeb.Server/PointCloudWeb.Server/Services/ScanConverterService.cs @@ -1,13 +1,11 @@ using System; -using System.Numerics; using PointCloudWeb.Server.Models; +using PointCloudWeb.Server.Utils; namespace PointCloudWeb.Server.Services { public class ScanConverterService { - private int Round(double value) => (int)Math.Round(double.IsNaN(value) ? 0 : value, 0, MidpointRounding.AwayFromZero); - public Point Transform(ScanDataPoint scan) { var factorZ = 1; @@ -46,9 +44,9 @@ namespace PointCloudWeb.Server.Services var p = new Point() { - X = Round(z * sinYB / sinYA), - Y = Round(z * sinXB / sinXA), - Z = factorZ * Round(z) + X = NumericUtils.Round(z * sinYB / sinYA), + Y = NumericUtils.Round(z * sinXB / sinXA), + Z = factorZ * NumericUtils.Round(z) }; return p; diff --git a/PointCloudWeb.Server/PointCloudWeb.Server/Services/ScanDataService.cs b/PointCloudWeb.Server/PointCloudWeb.Server/Services/ScanDataService.cs index a8eae49..4a2fbf2 100644 --- a/PointCloudWeb.Server/PointCloudWeb.Server/Services/ScanDataService.cs +++ b/PointCloudWeb.Server/PointCloudWeb.Server/Services/ScanDataService.cs @@ -6,13 +6,13 @@ namespace PointCloudWeb.Server.Services { public class DataService { - private readonly PointCloudService _pointCloudService; - private readonly ScanConverterService _scanConverterService; + private readonly PointCloudService pointCloudService; + private readonly ScanConverterService scanConverterService; public DataService(PointCloudService pointCloudService, ScanConverterService scanConverterService) { - _pointCloudService = pointCloudService; - _scanConverterService = scanConverterService; + this.pointCloudService = pointCloudService; + this.scanConverterService = scanConverterService; } private IList ConvertToPoints(ScanDataList scanData) @@ -21,7 +21,7 @@ namespace PointCloudWeb.Server.Services foreach (var scan in scanData.ScanPoints) { - list.Add(_scanConverterService.Transform(scan)); + list.Add(scanConverterService.Transform(scan)); } return list; @@ -29,7 +29,12 @@ namespace PointCloudWeb.Server.Services public void AddScan(ScanDataList scanData) { - _pointCloudService.AddPoints(scanData.Id, ConvertToPoints(scanData)); + pointCloudService.AddPoints(scanData.Id, ConvertToPoints(scanData)); + } + + public void ScanFinished(Guid id) + { + pointCloudService.RegisterPointCloud(id); } } } \ No newline at end of file diff --git a/PointCloudWeb.Server/PointCloudWeb.Server/Utils/NumericUtils.cs b/PointCloudWeb.Server/PointCloudWeb.Server/Utils/NumericUtils.cs new file mode 100644 index 0000000..2d97c68 --- /dev/null +++ b/PointCloudWeb.Server/PointCloudWeb.Server/Utils/NumericUtils.cs @@ -0,0 +1,9 @@ +using System; + +namespace PointCloudWeb.Server.Utils +{ + public static class NumericUtils + { + public static int Round(double value) => (int)Math.Round(double.IsNaN(value) ? 0 : value, 0, MidpointRounding.AwayFromZero); + } +} \ No newline at end of file