forked from ZouCheng321/fusion_kitti
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsavepcd.m
308 lines (247 loc) · 7.93 KB
/
savepcd.m
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
%SAVEPCD Write a point cloud to file in PCD format
%
% SAVEPCD(FNAME, P) writes the point cloud P to the file FNAME as an
% as a PCD format file.
%
% SAVEPCD(FNAME, P, 'binary') as above but save in binary format. Default
% is ascii format.
%
% If P is a 2-dimensional matrix (MxN) then the columns of P represent the
% 3D points and an unorganized point cloud is generated.
%
% If M=3 then the rows of P are x, y, z.
% If M=6 then the rows of P are x, y, z, R, G, B where R,G,B are in the
% range 0 to 1.
% If M=7 then the rows of P are x, y, z, R, G, B, A where R,G,B,A are in
% the range 0 to 1.
%
% If P is a 3-dimensional matrix (HxWxM) then an organized point cloud is
% generated.
%
% If M=3 then the planes of P are x, y, z.
% If M=6 then the planes of P are x, y, z, R, G, B where R,G,B are in the
% range 0 to 1.
% If M=7 then the planes of P are x, y, z, R, G, B, A where R,G,B,A are in
% the range 0 to 1.
%
% Notes::
% - Only the "x y z", "x y z rgb" and "x y z rgba" field formats are currently
% supported.
% - Cannot write binary_compressed format files
% See also pclviewer, lspcd, loaddpcd.
%
% Copyright (C) 2013, by Peter I. Corke
% TODO
% - option for binary write
function savepcd(fname, points, binmode)
% save points in xyz format
% TODO
% binary format, RGB
ascii = true;
if nargin < 3
ascii = true;
else
switch binmode
case 'binary'
ascii = false;
case 'ascii'
ascii = true;
otherwise
error('specify ascii or binary');
end
end
fp = fopen(fname, 'w');
% find the attributes of the point cloud
if ndims(points) == 2
% unorganized point cloud
npoints = size(points, 2);
width = npoints;
height = 1;
nfields = size(points, 1);
else
width = size(points, 2);
height = size(points, 1);
npoints = width*height;
nfields = size(points, 3);
% put the data in order with one column per point
points = permute(points, [2 1 3]);
points = reshape(points, [], size(points,3))';
end
switch nfields
case 3
fields = 'x y z';
count = '1 1 1';
typ = 'F F F';
siz = '4 4 4';
case 6
fields = 'x y z rgb';
count = '1 1 1 1';
if ascii
typ = 'F F F F';
else
typ = 'F F F F';
end
siz = '4 4 4 4';
case 7
fields = 'x y z rgba';
fields = 'x y z rgb';
count = '1 1 1 1';
if ascii
typ = 'F F F I';
else
typ = 'F F F F';
end
siz = '4 4 4 4';
end
% write the PCD file header
fprintf(fp, '# .PCD v.7 - Point Cloud Data file format\r\n');
fprintf(fp, 'VERSION .7\r\n');
fprintf(fp, 'FIELDS %s\r\n', fields);
fprintf(fp, 'SIZE %s\r\n', siz);
fprintf(fp, 'TYPE %s\r\n', typ);
fprintf(fp, 'COUNT %s\r\n', count);
fprintf(fp, 'WIDTH %d\r\n', width);
fprintf(fp, 'HEIGHT %d\r\n', height);
fprintf(fp, 'POINTS %d\r\n', npoints);
switch nfields
case 3
case 6
% RGB data
RGB = uint32(points(4:6,:)*255);
rgb = bitor(bitshift(RGB(1,:),16),bitshift(RGB(2,:),8));
rgb = bitor(rgb,RGB(3,:));
aaa = typecast(rgb, 'single');
points = [ points(1:3,:); aaa];
case 7
% RGBA data
RGBA = uint32(points(4:7,:)*255);
rgba = ((RGBA(1,:)*256+RGBA(2,:))*256+RGBA(3,:))*256+RGBA(4,:);
points = [ points(1:3,:); double(rgba)];
end
if ascii
% Write ASCII format data
fprintf(fp, 'DATA ascii\r\n');
if nfields == 3
% uncolored points
fprintf(fp, '%f %f %f \r\n', points);
else
% colored points
fprintf(fp, '%f %f %f %d \r\n', points);
end
else
% Write binary format data
fprintf(fp, 'DATA binary\r\n');
% for a full color point cloud the colors are not quite right in pclviewer,
% color as a float has only 23 bits of mantissa precision, not enough for
% RGB as 8 bits each
% write color as a float not an int
fwrite(fp, points, 'float32');
end
fclose(fp);
end
function savepcd(fname, points, binmode)
% save points in xyz format
% TODO
% binary format, RGB
ascii = true;
if nargin < 3
ascii = true;
else
switch binmode
case 'binary'
ascii = false;
case 'ascii'
ascii = true;
otherwise
error('specify ascii or binary');
end
end
fp = fopen(fname, 'w');
% find the attributes of the point cloud
if ndims(points) == 2
% unorganized point cloud
npoints = size(points, 2);
width = npoints;
height = 1;
nfields = size(points, 1);
else
width = size(points, 2);
height = size(points, 1);
npoints = width*height;
nfields = size(points, 3);
% put the data in order with one column per point
points = permute(points, [2 1 3]);
points = reshape(points, [], size(points,3))';
end
switch nfields
case 3
fields = 'x y z';
count = '1 1 1';
typ = 'F F F';
siz = '4 4 4';
case 6
fields = 'x y z rgb';
count = '1 1 1 1';
if ascii
typ = 'F F F F';
else
typ = 'F F F F';
end
siz = '4 4 4 4';
case 7
fields = 'x y z rgba';
fields = 'x y z rgb';
count = '1 1 1 1';
if ascii
typ = 'F F F I';
else
typ = 'F F F F';
end
siz = '4 4 4 4';
end
% write the PCD file header
fprintf(fp, '# .PCD v.7 - Point Cloud Data file format\r\n');
fprintf(fp, 'VERSION .7\r\n');
fprintf(fp, 'FIELDS %s\r\n', fields);
fprintf(fp, 'SIZE %s\r\n', siz);
fprintf(fp, 'TYPE %s\r\n', typ);
fprintf(fp, 'COUNT %s\r\n', count);
fprintf(fp, 'WIDTH %d\r\n', width);
fprintf(fp, 'HEIGHT %d\r\n', height);
fprintf(fp, 'POINTS %d\r\n', npoints);
switch nfields
case 3
case 6
% RGB data
RGB = uint32(points(4:6,:)*255);
rgb = bitor(bitshift(RGB(1,:),16),bitshift(RGB(2,:),8));
rgb = bitor(rgb,RGB(3,:));
aaa = typecast(rgb, 'single');
points = [ points(1:3,:); aaa];
case 7
% RGBA data
RGBA = uint32(points(4:7,:)*255);
rgba = ((RGBA(1,:)*256+RGBA(2,:))*256+RGBA(3,:))*256+RGBA(4,:);
points = [ points(1:3,:); double(rgba)];
end
if ascii
% Write ASCII format data
fprintf(fp, 'DATA ascii\r\n');
if nfields == 3
% uncolored points
fprintf(fp, '%f %f %f \r\n', points);
else
% colored points
fprintf(fp, '%f %f %f %d \r\n', points);
end
else
% Write binary format data
fprintf(fp, 'DATA binary\r\n');
% for a full color point cloud the colors are not quite right in pclviewer,
% color as a float has only 23 bits of mantissa precision, not enough for
% RGB as 8 bits each
% write color as a float not an int
fwrite(fp, points, 'float32');
end
fclose(fp);
end