-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathPointCloud.lua
435 lines (379 loc) · 10.5 KB
/
PointCloud.lua
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
local ffi = require 'ffi'
local torch = require 'torch'
local utils = require 'pcl.utils'
local pcl = require 'pcl.PointTypes'
local PointCloud = torch.class('pcl.PointCloud', pcl)
local func_by_type = {}
local function init()
local PointCloud_method_names = {
'new',
'clone',
'delete',
'getHeaderSeq',
'setHeaderSeq',
'getHeaderStamp_sec',
'getHeaderStamp_nsec',
'setHeaderStamp',
'getHeaderFrameId',
'setHeaderFrameId',
'getWidth',
'getHeight',
'getIsDense',
'setIsDense',
'at1D',
'at2D',
'clear',
'reserve',
'size',
'empty',
'isOrganized',
'push_back',
'insert',
'erase',
'points',
'sensorOrientation',
'sensorOrigin',
'transform',
'transformWithNormals',
'getMinMax3D',
'compute3DCentroid',
'computeCovarianceMatrix',
'add',
'fromPCLPointCloud2',
'toPCLPointCloud2',
'loadPCDFile',
'savePCDFile',
'loadPLYFile',
'savePLYFile',
'loadOBJFile',
'savePNGFile',
'readXYZfloat',
'readRGBAfloat',
'readRGBAbyte',
'writeRGBAfloat',
'writeRGBAbyte',
'writeRGBfloat',
'writeRGBbyte',
'addNormals',
'copyXYZ',
'copyXYZI',
'copyXYZRGBA',
'copyXYZNormal',
'copyXYZINormal',
'copyXYZRGBNormal',
'copyNormal'
}
local supported_types = {}
supported_types[pcl.PointXYZ] = 'XYZ'
supported_types[pcl.PointXYZI] = 'XYZI'
supported_types[pcl.PointXYZRGBA] = 'XYZRGBA'
supported_types[pcl.PointNormal] = 'XYZNormal'
supported_types[pcl.PointXYZINormal] = 'XYZINormal'
supported_types[pcl.PointXYZRGBNormal] = 'XYZRGBNormal'
supported_types[pcl.Normal] = 'Normal'
supported_types[pcl.FPFHSignature33] = 'FPFHSignature33'
supported_types[pcl.VFHSignature308] = 'VFHSignature308'
supported_types[pcl.Boundary] = 'Boundary'
supported_types[pcl.Label] = 'Label'
for k,v in pairs(supported_types) do
func_by_type[k] = utils.create_typed_methods('pcl_PointCloud_TYPE_KEY_', PointCloud_method_names, v)
end
func_by_type[pcl.Normal] = utils.create_typed_methods('pcl_PointCloud_TYPE_KEY_', PointCloud_method_names, 'Normal')
end
init()
function PointCloud:__init(pointType, width, height)
if type(pointType) == 'number' then
width = pointType
pointType = pcl.PointXYZ
end
pointType = pcl.pointType(pointType)
width = width or 0
rawset(self, 'f', func_by_type[pointType])
self.pointType = pointType
if torch.isTensor(width) then
width = width:float()
local sz = width:size()
local w,h
if width:nDimension() == 3 then
w, h = sz[2], sz[1]
elseif width:nDimension() == 2 then
w, h = sz[1], 1
end
self.o = self.f.new(w, h)
self:points():copy(width)
elseif type(width) == 'cdata' then
self.o = width
else
height = height or width and width > 0 and 1 or 0
self.o = self.f.new(width, height)
end
end
function PointCloud:cdata()
return self.o
end
function PointCloud:readXYZ(t)
local t = t or torch.FloatTensor()
if torch.type(t) == 'torch.FloatTensor' then
self.f.readXYZfloat(self.o, t:cdata())
else
error('torch.FloatTensor expected')
end
return t
end
function PointCloud:readRGBA(t)
local t = t or torch.FloatTensor()
if torch.type(t) == 'torch.FloatTensor' then
self.f.readRGBAfloat(self.o, t:cdata())
elseif torch.type(t) == 'torch.ByteTensor' then
self.f.readRGBAbyte(self.o, t:cdata())
else
error('unsupported tensor type')
end
return t
end
function PointCloud:writeRGBA(t)
if torch.type(t) == 'torch.FloatTensor' then
self.f.writeRGBAfloat(self.o, t:cdata())
elseif torch.type(t) == 'torch.ByteTensor' then
self.f.writeRGBAbyte(self.o, t:cdata())
else
error('unsupported tensor type')
end
end
function PointCloud:writeRGB(t, setAlpha, alpha)
if torch.type(t) == 'torch.FloatTensor' then
self.f.writeRGBfloat(self.o, t:cdata(), setAlpha or false, alpha or 1)
elseif torch.type(t) == 'torch.ByteTensor' then
self.f.writeRGBbyte(self.o, t:cdata(), setAlpha or false, alpha or 255)
else
error('unsupported tensor type')
end
end
function PointCloud:clone()
local clone = self.f.clone(self.o)
return PointCloud.new(self.pointType, clone)
end
function PointCloud:__index(idx)
local v = rawget(self, idx)
if not v then
v = PointCloud[idx]
if not v then
local f, o = rawget(self, 'f'), rawget(self, 'o')
if type(idx) == 'number' then
v = f.at1D(o, idx-1)
elseif type(idx) == 'table' then
v = f.at2D(o, idx[1]-1, idx[2]-1)
end
end
end
return v
end
function PointCloud:__newindex(idx, v)
local f, o = rawget(self, 'f'), rawget(self, 'o')
if type(idx) == 'number' then
f.at1D(o, idx-1):set(v)
elseif type(idx) == 'table' then
f.at2D(o, idx[1]-1, idx[2]-1):set(v)
else
rawset(self, idx, v)
end
end
function PointCloud:__len()
return self:size()
end
function PointCloud:getHeaderSeq()
return self.f.getHeaderSeq(self.o)
end
function PointCloud:setHeaderSeq(value)
self.f.setHeaderSeq(self.o, value)
end
function PointCloud:getHeaderStamp()
return self.f.getHeaderStamp_sec(self.o), self.f.getHeaderStamp_nsec(self.o)
end
function PointCloud:setHeaderStamp(sec, nsec)
self.f.setHeaderStamp(self.o, sec, nsec)
end
function PointCloud:getHeaderFrameId()
return ffi.string(self.f.getHeaderFrameId(self.o))
end
function PointCloud:setHeaderFrameId(value)
self.f.setHeaderFrameId(self.o, value or '')
end
function PointCloud:getWidth()
return self.f.getWidth(self.o)
end
function PointCloud:getHeight()
return self.f.getHeight(self.o)
end
function PointCloud:getIsDense()
return self.f.getIsDense(self.o)
end
function PointCloud:setIsDense(value)
self.f.setIsDense(self.o, value)
end
function PointCloud:clear()
self.f.clear(self.o)
end
function PointCloud:reserve(n)
self.f.reserve(self.o, n)
end
function PointCloud:size()
return self.f.size(self.o);
end
function PointCloud:empty()
return self.f.empty(self.o)
end
function PointCloud:isOrganized()
return self.f.isOrganized(self.o)
end
function PointCloud:push_back(pt)
self.f.push_back(self.o, pt);
end
function PointCloud:insert(pos, pt, n)
self.f.insert(self.o, pos-1, n or 1, pt)
end
function PointCloud:erase(begin_pos, end_pos)
self.f.erase(self.o, begin_pos-1, (end_pos or begin_pos + 1)-1)
end
function PointCloud:points()
local t = torch.FloatTensor()
local buf = self.f.points(self.o)
t:cdata().storage = buf.storage
t:resize(buf.height, buf.width, buf.dim)
return t
end
function PointCloud:pointsXYZ()
return self:points()[{{},{},{1,3}}]
end
function PointCloud:sensorOrigin()
local t = torch.FloatTensor()
local s = self.f.sensorOrigin(self.o)
t:cdata().storage = s
t:resize(4)
return t
end
function PointCloud:sensorOrientation()
local t = torch.FloatTensor()
local s = self.f.sensorOrientation(self.o)
t:cdata().storage = s
t:resize(4)
return t
end
function PointCloud:transform(mat, output)
if torch.isTypeOf(mat, pcl.affine.Transform) then
mat = mat:totensor()
end
output = output or self
if self.pointType.hasNormal then
self.f.transformWithNormals(self.o, mat:cdata(), output:cdata())
else
self.f.transform(self.o, mat:cdata(), output:cdata())
end
return output
end
function PointCloud:getMinMax3D()
local min, max = self.pointType(), self.pointType()
self.f.getMinMax3D(self.o, min, max)
return min, max
end
function PointCloud:compute3DCentroid()
local centroid = torch.FloatTensor()
self.f.compute3DCentroid(self.o, centroid:cdata())
return centroid
end
function PointCloud:computeCovarianceMatrix(centroid)
local covariance = torch.FloatTensor()
if not centroid then
centroid = self:compute3DCentroid()
end
self.f.computeCovarianceMatrix(self.o, utils.cdata(centroid), covariance:cdata())
return covariance, centroid
end
function PointCloud:add(other)
self.f.add(self.o, other.o)
end
function PointCloud:removeNaN(output, removed_indices)
if torch.isTypeOf(output, pcl.Indices) then
return pcl.filter.removeNaNFromPointCloud(self, nil, output)
else
return pcl.filter.removeNaNFromPointCloud(self, output or self, removed_indices)
end
end
function PointCloud:fromPCLPointCloud2(src_msg)
if not torch.isTypeOf(src_msg, pcl.PCLPointCloud2) then
error("Invalid type of argument 'src_msg': pcl.PCLPointCloud2 expected.")
end
self.f.fromPCLPointCloud2(self.o, src_msg:cdata())
end
function PointCloud:toPCLPointCloud2(dst_msg)
dst_msg = dst_msg or pcl.PCLPointCloud2()
self.f.toPCLPointCloud2(self.o, dst_msg:cdata())
return dst_msg
end
function PointCloud:loadPCDFile(fn)
return self.f.loadPCDFile(self.o, fn)
end
function PointCloud:savePCDFile(fn, binary)
return self.f.savePCDFile(self.o, fn, binary or true)
end
function PointCloud:loadPLYFile(fn)
return self.f.loadPLYFile(self.o, fn)
end
function PointCloud:savePLYFile(fn, binary)
return self.f.savePLYFile(self.o, fn, binary or true)
end
function PointCloud:loadOBJFile(fn)
return self.f.loadOBJFile(self.o, fn)
end
function PointCloud:savePNGFile(fn, field_name)
return self.f.savePNGFile(self.o, fn, field_name or 'rgb')
end
function PointCloud:addNormals(normals, output)
if not output then
output = pcl.PointCloud(utils.getNormalTypeFor(self.pointType))
end
self.f.addNormals(self.o, normals:cdata(), output:cdata())
return output
end
function PointCloud:__tostring()
return string.format('PointCloud<%s> (w: %d, h: %d, organized: %s, dense: %s)',
pcl.getPointTypeName(self.pointType),
self:getWidth(),
self:getHeight(),
self:isOrganized(),
self:getIsDense()
)
end
function PointCloud:apply(func)
local p = self:points()
local count = p:nElement()
local data = p:data()
local pt = self.pointType()
local point_size = ffi.sizeof(pt)
local stride = point_size / #pt
local j=1
for i=0,count-1,stride do
ffi.copy(pt, data + i, point_size)
local r = func(pt, j) -- pass point and index to function
if r then
ffi.copy(data + i, r, point_size)
end
j = j + 1
end
end
function PointCloud.copy(cloud_in, indices, cloud_out)
if torch.isTypeOf(indices, pcl.PointCloud) then
cloud_out = indices
indices = nil
end
if not cloud_out then
cloud_out = pcl.PointCloud(cloud_in.pointType)
end
local copy = cloud_in.f["copy" .. (utils.type_key_map[cloud_out.pointType] or '')]
if not copy then
print("copy" .. (utils.type_key_map[cloud_out.pointType] or ''))
error("Copy to destination point cloud type not supported.")
end
copy(cloud_in:cdata(), utils.cdata(indices), cloud_out:cdata())
return cloud_out
end