-
Notifications
You must be signed in to change notification settings - Fork 379
/
Copy pathPointCloud.cs
126 lines (110 loc) · 4.31 KB
/
PointCloud.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
© Siemens AG, 2017
Author: Dr. Martin Bischoff (martin.bischoff@siemens.com)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
<http://www.apache.org/licenses/LICENSE-2.0>.
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
using System;
namespace RosSharp.RosBridgeClient
{
public class PointCloud
{
public Point[] Points;
public PointCloud(SensorPointCloud2 sensorPointCloud2)
{
int I = sensorPointCloud2.data.Length / sensorPointCloud2.point_step;
Points = new Point[I];
byte[] byteSlice = new byte[sensorPointCloud2.point_step];
for (int i = 0; i < I; i++)
{
Array.Copy(sensorPointCloud2.data, i * sensorPointCloud2.point_step, byteSlice, 0, sensorPointCloud2.point_step);
Points[i] = new Point(byteSlice, sensorPointCloud2.fields);
}
}
public PointCloud(SensorImage depthImage, SensorImage rgbImage, float focal)
{
int width = depthImage.width;
int height = depthImage.height;
float invFocal = 1.0f / focal;
Points = new Point[width * height];
for (int v = 0; v < height; v++)
{
for (int u = 0; u < width; u++)
{
float depth = 0;// depthImage[u, v];
if (depth == 0)
{
Points[u + v * width].x = float.NaN;
Points[u + v * width].y = float.NaN;
Points[u + v * width].z = float.NaN;
Points[u + v * width].rgb = new int[] { 0, 0, 0 };
}
else
{
Points[u + v * width].z = depth * invFocal;
Points[u + v * width].x = u * depth * invFocal;
Points[u + v * width].y = v * depth * invFocal;
Points[u + v * width].rgb = new int[] { 0, 0, 0 };// rgbImage[u,v];
}
}
}
}
}
public class Point
{
public float x;
public float y;
public float z;
public int[] rgb;
public Point(byte[] bytes, SensorPointField[] fields)
{
foreach(var field in fields)
{
byte[] slice = new byte[field.count * 4];
Array.Copy(bytes, field.offset, slice, 0, field.count * 4);
switch (field.name)
{
case "x":
x = getValue(slice);
break;
case "y":
y = getValue(slice);
break;
case "z":
z = getValue(slice);
break;
case "rgb":
rgb = getRGB(slice);
break;
}
}
}
public override string ToString()
{
return "xyz=(" + x.ToString() + ", " + y.ToString() + ", " + z.ToString() + ")"
+ " rgb=(" + rgb[0].ToString() + ", " + rgb[1].ToString() + ", " + rgb[2].ToString() + ")";
}
private static float getValue(byte[] bytes)
{
if (!BitConverter.IsLittleEndian)
Array.Reverse(bytes);
float result = BitConverter.ToSingle(bytes, 0);
return result;
}
private static int[] getRGB(byte[] bytes)
{
int[] rgb = new int[3];
rgb[0] = Convert.ToInt16(bytes[0]);
rgb[1] = Convert.ToInt16(bytes[1]);
rgb[2] = Convert.ToInt16(bytes[2]);
return rgb;
}
}
}