| | 1 | | using System; |
| | 2 | | using Implementation.Interfaces; |
| | 3 | | using NLog; |
| | 4 | | using ReFlex.Core.Common.Components; |
| | 5 | | using ReFlex.Core.Tracking.Util; |
| | 6 | |
|
| | 7 | | namespace Implementation.Components |
| | 8 | | { |
| | 9 | | public class DepthImageManager : IDepthImageManager |
| | 10 | | { |
| 0 | 11 | | private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); |
| | 12 | |
|
| | 13 | | private readonly IFilterManager _filterManager; |
| | 14 | |
|
| 0 | 15 | | public PointCloud3 PointCloud { get; private set; } |
| | 16 | |
|
| 0 | 17 | | public VectorField2 VectorField { get; private set; } |
| | 18 | |
|
| | 19 | | public event EventHandler<PointCloud3> PointcloudFiltered; |
| | 20 | | public event EventHandler<VectorField2> VectorfieldChanged; |
| | 21 | | public event EventHandler<ImageByteArray> DepthImageChanged; |
| | 22 | | public event EventHandler<DepthCameraFrame> RawDepthFrameReceived; |
| | 23 | |
|
| 0 | 24 | | public int ImageWidth { get; private set; } |
| | 25 | |
|
| 0 | 26 | | public int ImageHeight { get; private set; } |
| | 27 | |
|
| | 28 | | private bool _isUpdating; |
| | 29 | |
|
| 0 | 30 | | public DepthImageManager(IFilterManager filterManager) |
| 0 | 31 | | { |
| 0 | 32 | | _filterManager = filterManager; |
| 0 | 33 | | } |
| | 34 | |
|
| | 35 | | public void Initialize(int sizeX, int sizeY) |
| 0 | 36 | | { |
| 0 | 37 | | PointCloud = new PointCloud3(sizeX, sizeY); |
| 0 | 38 | | OnPointcloudFiltered(this, PointCloud); |
| | 39 | |
|
| 0 | 40 | | VectorField = new VectorField2(sizeX, sizeY); |
| 0 | 41 | | OnVectorfieldChanged(this, VectorField); |
| | 42 | |
|
| 0 | 43 | | ImageWidth = sizeX; |
| 0 | 44 | | ImageHeight = sizeY; |
| 0 | 45 | | } |
| | 46 | |
|
| | 47 | | public void Update(DepthCameraFrame frame) |
| 0 | 48 | | { |
| | 49 | | // skip current frame if pointcloud is still being processed... |
| 0 | 50 | | if (_isUpdating) |
| 0 | 51 | | return; |
| | 52 | |
|
| 0 | 53 | | _isUpdating = true; |
| | 54 | |
|
| 0 | 55 | | var depthData = frame.Depth; |
| | 56 | |
|
| | 57 | | // send raw data |
| 0 | 58 | | RawDepthFrameReceived?.Invoke(this, frame); |
| | 59 | |
|
| 0 | 60 | | _filterManager.FilterAndUpdate(depthData, PointCloud); |
| | 61 | | try |
| 0 | 62 | | { |
| 0 | 63 | | OnPointcloudFiltered(this, PointCloud); |
| 0 | 64 | | } |
| 0 | 65 | | catch (Exception exc) |
| 0 | 66 | | { |
| 0 | 67 | | Logger.Error(exc); |
| 0 | 68 | | } |
| | 69 | |
|
| 0 | 70 | | PopulateVectorfield(); |
| | 71 | |
|
| 0 | 72 | | _isUpdating = false; |
| 0 | 73 | | } |
| | 74 | |
|
| | 75 | | public void Update(ImageByteArray depthImage) |
| 0 | 76 | | { |
| | 77 | | //@ todo: maybe convert into 8-bit image ? |
| 0 | 78 | | DepthImageChanged?.Invoke(this, depthImage); |
| 0 | 79 | | } |
| | 80 | |
|
| | 81 | | protected virtual void OnPointcloudFiltered(object sender, PointCloud3 pointcloud) => |
| 0 | 82 | | PointcloudFiltered?.Invoke(sender, pointcloud); |
| | 83 | |
|
| | 84 | | protected virtual void OnVectorfieldChanged(object sender, VectorField2 vectorfield) => |
| 0 | 85 | | VectorfieldChanged?.Invoke(sender, vectorfield); |
| | 86 | |
|
| | 87 | | private void PopulateVectorfield() |
| 0 | 88 | | { |
| | 89 | | try |
| 0 | 90 | | { |
| 0 | 91 | | VectorField?.Populate(PointCloud); |
| 0 | 92 | | OnVectorfieldChanged(this, VectorField); |
| 0 | 93 | | } |
| 0 | 94 | | catch (Exception exc) |
| 0 | 95 | | { |
| 0 | 96 | | Logger.Error(exc); |
| 0 | 97 | | } |
| 0 | 98 | | } |
| | 99 | | } |
| | 100 | | } |