| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using NLog; |
| | | 4 | | using ReFlex.Core.Common.Components; |
| | | 5 | | using ReFlex.Core.Common.Util; |
| | | 6 | | using Math = System.Math; |
| | | 7 | | |
| | | 8 | | namespace ReFlex.Core.Networking.Util |
| | | 9 | | { |
| | | 10 | | public class EmulatedPointCloud |
| | | 11 | | { |
| | 0 | 12 | | private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); |
| | | 13 | | |
| | | 14 | | private StreamParameter _currentParams; |
| | | 15 | | private readonly EmulatorParameters _emulatorParameters; |
| | | 16 | | private readonly float _pushRange; |
| | | 17 | | private readonly float _pullRange; |
| | | 18 | | |
| | | 19 | | private byte[] _image; |
| | | 20 | | |
| | | 21 | | private uint _numChannels; |
| | | 22 | | private uint _bytesPerChannel; |
| | 31456 | 23 | | public uint MaxDepthImageValue { get => (1u << (int)(8 * _bytesPerChannel)) - 1; } |
| | | 24 | | |
| | 140934 | 25 | | public Point3[] Points { get; private set; } |
| | | 26 | | |
| | 70450 | 27 | | public bool GenerateDepthImage {get; set; } = true; |
| | | 28 | | |
| | 42 | 29 | | public EmulatedPointCloud(EmulatorParameters param) |
| | 42 | 30 | | { |
| | 42 | 31 | | _emulatorParameters = param; |
| | 42 | 32 | | _pushRange = _emulatorParameters.PlaneDistanceInMeter - _emulatorParameters.MinDepthInMeter; |
| | 42 | 33 | | _pullRange = _emulatorParameters.MaxDepthInMeter - _emulatorParameters.PlaneDistanceInMeter; |
| | 42 | 34 | | } |
| | | 35 | | |
| | | 36 | | public void InitializePointCloud(StreamParameter param) |
| | 42 | 37 | | { |
| | 42 | 38 | | if (param == null) |
| | 0 | 39 | | { |
| | 0 | 40 | | Logger.Error("Invalid parameter for intializatoin of point cloud."); |
| | 0 | 41 | | return; |
| | | 42 | | } |
| | | 43 | | |
| | 42 | 44 | | _bytesPerChannel = DepthImageFormatTools.BytesPerChannel(param.Format); |
| | 42 | 45 | | _numChannels = DepthImageFormatTools.NumChannels(param.Format); |
| | 42 | 46 | | var defaultValue = GetImageDefaultValue(); |
| | | 47 | | |
| | 42 | 48 | | _currentParams = param; |
| | | 49 | | |
| | 42 | 50 | | var result = new Point3[param.Width * param.Height].AsSpan<Point3>(); |
| | 42 | 51 | | _image = new byte[param.Width * param.Height * _bytesPerChannel * _numChannels]; |
| | | 52 | | |
| | 42 | 53 | | var xOffset = _emulatorParameters.WidthInMeters / param.Width; |
| | 42 | 54 | | var yOffset = _emulatorParameters.HeightInMeters / param.Height; |
| | | 55 | | |
| | 42 | 56 | | var xMin = 0 -_emulatorParameters.WidthInMeters * 0.5f; |
| | 42 | 57 | | var yMin = 0 -_emulatorParameters.HeightInMeters * 0.5f; |
| | | 58 | | |
| | 5208 | 59 | | for (var x = 0; x < param.Width; ++x) |
| | 2562 | 60 | | { |
| | 2562 | 61 | | var xPos = xMin + (xOffset * x); |
| | | 62 | | |
| | 250888 | 63 | | for (var y = 0; y < param.Height; ++y) |
| | 122882 | 64 | | { |
| | 122882 | 65 | | var yPos = yMin + (yOffset * y); |
| | | 66 | | |
| | 122882 | 67 | | var idx = ComputeIndex(x, y); |
| | | 68 | | |
| | 122882 | 69 | | result[idx] = new Point3(xPos, yPos, _emulatorParameters.PlaneDistanceInMeter); |
| | | 70 | | |
| | 122882 | 71 | | SetDepthImageValue(defaultValue, idx); |
| | 122882 | 72 | | } |
| | 2562 | 73 | | } |
| | | 74 | | |
| | 42 | 75 | | Points = result.ToArray(); |
| | 42 | 76 | | } |
| | | 77 | | |
| | | 78 | | public void Reset() |
| | 28 | 79 | | { |
| | 28 | 80 | | var defaultValue = GetImageDefaultValue(); |
| | | 81 | | |
| | 122952 | 82 | | for (int i = 0; i < Points.Length; ++i) |
| | 61448 | 83 | | { |
| | 61448 | 84 | | Points[i].Z = _emulatorParameters.PlaneDistanceInMeter; |
| | | 85 | | |
| | 61448 | 86 | | if (GenerateDepthImage) |
| | 61448 | 87 | | SetDepthImageValue(defaultValue, i); |
| | 61448 | 88 | | } |
| | 28 | 89 | | } |
| | | 90 | | |
| | | 91 | | public void UpdateFromInteractions(List<Interaction> interactions) |
| | 20 | 92 | | { |
| | 20 | 93 | | var squaredRadius = _emulatorParameters.Radius * _emulatorParameters.Radius; |
| | | 94 | | |
| | 80 | 95 | | for (var i = 0; i < interactions.Count; ++i) |
| | 20 | 96 | | { |
| | 20 | 97 | | var interaction = interactions[i]; |
| | | 98 | | |
| | 20 | 99 | | var centerX = (int)interaction.Position.X; |
| | 20 | 100 | | var centerY = (int)interaction.Position.Y; |
| | | 101 | | |
| | 20 | 102 | | var offset = interaction.Position.Z < 0 ? interaction.Position.Z * _pushRange : interaction.Position.Z * |
| | | 103 | | |
| | 20 | 104 | | var radius = (int) Math.Abs(Math.Round(_emulatorParameters.Radius * offset)); |
| | | 105 | | |
| | 744 | 106 | | for (var x = -radius; x < radius; ++x) |
| | 352 | 107 | | { |
| | 18624 | 108 | | for (var y = -radius; y < radius; ++y) |
| | 8960 | 109 | | { |
| | 8960 | 110 | | var idx = ComputeIndex(centerX + x, centerY + y); |
| | | 111 | | |
| | 8960 | 112 | | if (x == 0 && y == 0) |
| | 20 | 113 | | { |
| | 20 | 114 | | Points[idx].Z = _emulatorParameters.PlaneDistanceInMeter + offset; |
| | | 115 | | |
| | 20 | 116 | | if (GenerateDepthImage) |
| | 20 | 117 | | SetDepthImageValueFromDepth(Points[idx].Z, idx); |
| | 20 | 118 | | continue; |
| | | 119 | | } |
| | | 120 | | |
| | 8940 | 121 | | var dist = Math.Sqrt( x * x + y * y); |
| | | 122 | | |
| | 8940 | 123 | | var maxDist = Math.Sqrt(2f * radius * radius); |
| | | 124 | | |
| | 8940 | 125 | | var smooth = (float) Math.Sqrt(dist/maxDist); |
| | | 126 | | |
| | 8940 | 127 | | if (dist < squaredRadius) |
| | 8940 | 128 | | { |
| | 8940 | 129 | | var factor = 1.0f - smooth; |
| | | 130 | | |
| | 8940 | 131 | | var centerZ = _emulatorParameters.PlaneDistanceInMeter + (offset * factor); |
| | | 132 | | |
| | 8940 | 133 | | Points[idx].Z = centerZ; |
| | | 134 | | |
| | 8940 | 135 | | if (GenerateDepthImage) |
| | 8940 | 136 | | SetDepthImageValueFromDepth(centerZ, idx); |
| | 8940 | 137 | | } |
| | 8940 | 138 | | } |
| | 352 | 139 | | } |
| | 20 | 140 | | } |
| | 20 | 141 | | } |
| | | 142 | | |
| | | 143 | | public void UpdateFromGreyScaleImage(ImageByteArray imageData) |
| | 28 | 144 | | { |
| | 28 | 145 | | if (imageData.ImageData.Length < imageData.Width * imageData.Height * imageData.BytesPerChannel * _numChanne |
| | 0 | 146 | | { |
| | 0 | 147 | | Logger.Log(LogLevel.Error, $"Incorrect Size for image data: Size of Byte Array: {imageData.ImageData.Len |
| | 0 | 148 | | return; |
| | | 149 | | } |
| | | 150 | | |
| | 28 | 151 | | var defaultDepth = GetImageDefaultValue(); |
| | | 152 | | |
| | | 153 | | // compute global offset - center of the plane is in the origin |
| | 28 | 154 | | var xMin = -_emulatorParameters.WidthInMeters * 0.5f; |
| | 28 | 155 | | var yMin = -_emulatorParameters.HeightInMeters * 0.5f; |
| | | 156 | | |
| | | 157 | | // compute offset per pixel |
| | 28 | 158 | | var pxOffsetX = _emulatorParameters.WidthInMeters / _currentParams.Width; |
| | 28 | 159 | | var pxOffsetY = _emulatorParameters.HeightInMeters / _currentParams.Height; |
| | | 160 | | |
| | 28 | 161 | | var iterationStep = _numChannels * _bytesPerChannel; |
| | | 162 | | |
| | 122952 | 163 | | for (uint i = 0; i < imageData.ImageData.Length; i+=iterationStep) |
| | 61448 | 164 | | { |
| | 61448 | 165 | | var zIdx = i + ((_numChannels - 1) * _bytesPerChannel); |
| | | 166 | | |
| | 61448 | 167 | | var dist = (int) imageData.ImageData[zIdx]; |
| | | 168 | | |
| | 61448 | 169 | | if (_bytesPerChannel == 2) |
| | 24584 | 170 | | { |
| | 24584 | 171 | | dist = Combine(imageData.ImageData[zIdx], imageData.ImageData[zIdx + 1]); |
| | 24584 | 172 | | } |
| | | 173 | | |
| | | 174 | | // The midpoint encodes the undeformed plane. |
| | 61448 | 175 | | if (dist == defaultDepth) |
| | 52500 | 176 | | continue; |
| | | 177 | | |
| | | 178 | | // 3 channels --> divide |
| | 8948 | 179 | | var pos = i / (iterationStep); |
| | | 180 | | |
| | | 181 | | // compute pixel index |
| | 8948 | 182 | | var xPx = pos % _currentParams.Width; |
| | 8948 | 183 | | var yPx = pos / _currentParams.Width; |
| | | 184 | | |
| | 8948 | 185 | | var x = xMin + (xPx * pxOffsetX); |
| | 8948 | 186 | | var y = yMin + (yPx * pxOffsetY); |
| | | 187 | | |
| | 8948 | 188 | | var pushValueRange = (float)defaultDepth; |
| | 8948 | 189 | | var pullValueRange = (float)(MaxDepthImageValue - defaultDepth); |
| | | 190 | | |
| | 8948 | 191 | | var z = dist < defaultDepth |
| | 8948 | 192 | | |
| | 8948 | 193 | | // push: normalized depth * push range + min depth |
| | 8948 | 194 | | ? (dist / pushValueRange) * _pushRange + _emulatorParameters.MinDepthInMeter |
| | 8948 | 195 | | |
| | 8948 | 196 | | // pull: subtract push range, normalize depth behind plane * pull range + plane distance |
| | 8948 | 197 | | : ((dist - defaultDepth) / pullValueRange) * _pullRange + _emulatorParameters.PlaneDistanceInMeter; |
| | | 198 | | |
| | | 199 | | |
| | 8948 | 200 | | Points[pos] = new Point3(x, y, z); |
| | 8948 | 201 | | } |
| | 28 | 202 | | } |
| | | 203 | | |
| | | 204 | | public int ComputeIndex(int x, int y) |
| | 131842 | 205 | | { |
| | 131842 | 206 | | if (x < 0 || x >= _currentParams.Width || y < 0 || y >= _currentParams.Height) |
| | 0 | 207 | | return 0; |
| | | 208 | | |
| | 131842 | 209 | | return y * _currentParams.Width + x; |
| | 131842 | 210 | | } |
| | | 211 | | |
| | | 212 | | public ImageByteArray GetUpdatedDepthImage() |
| | 20 | 213 | | { |
| | 20 | 214 | | return _image == null |
| | 20 | 215 | | ? new ImageByteArray(new byte[1],1,1, 1, 1, DepthImageFormat.Greyscale8bpp) |
| | 20 | 216 | | : new ImageByteArray(_image, _currentParams.Width, _currentParams.Height, _bytesPerChannel, _numChannels |
| | 20 | 217 | | } |
| | | 218 | | |
| | | 219 | | private uint GetImageDefaultValue() |
| | 9058 | 220 | | { |
| | | 221 | | // divide by 2: shifting bytes to the right |
| | 9058 | 222 | | return MaxDepthImageValue >> 1; |
| | 9058 | 223 | | } |
| | | 224 | | |
| | | 225 | | private void SetDepthImageValueFromDepth(float depth, int index) |
| | 8960 | 226 | | { |
| | 8960 | 227 | | var midpoint = GetImageDefaultValue(); |
| | 8960 | 228 | | var offset = depth - _emulatorParameters.PlaneDistanceInMeter; |
| | 8960 | 229 | | var value = offset < 0 |
| | 8960 | 230 | | ? midpoint + offset / _pushRange * midpoint |
| | 8960 | 231 | | : midpoint + offset / _pullRange * (MaxDepthImageValue - midpoint); |
| | | 232 | | |
| | 8960 | 233 | | SetDepthImageValue((uint)Math.Round(Math.Clamp(value, 0, MaxDepthImageValue)), index); |
| | 8960 | 234 | | } |
| | | 235 | | |
| | | 236 | | private void SetDepthImageValue(uint value, int index) |
| | 193290 | 237 | | { |
| | 1397840 | 238 | | for (var channel = 0; channel < _numChannels; channel++) |
| | 2491060 | 239 | | for (var b = 0; b < _bytesPerChannel; b++) |
| | 739900 | 240 | | _image[(index * _numChannels + channel) * _bytesPerChannel + b] = (byte)(value >> (8 * b)); |
| | 193290 | 241 | | } |
| | | 242 | | |
| | | 243 | | private static int Combine(byte b1, byte b2) |
| | 24584 | 244 | | { |
| | | 245 | | // return b1 << 8 | b2; |
| | 24584 | 246 | | return b1 | (b2 << 8); |
| | 24584 | 247 | | } |
| | | 248 | | } |
| | | 249 | | } |