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