-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathIndices.lua
187 lines (156 loc) · 4.01 KB
/
Indices.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
local ffi = require 'ffi'
local torch = require 'torch'
local utils = require 'pcl.utils'
local pcl = require 'pcl.PointTypes'
local Indices = torch.class('pcl.Indices', pcl)
local Indices_factory = torch.factory('pcl.Indices')
local methods = {}
local function init()
local Indices_method_names = {
'new',
'clone',
'delete',
'fromPtr',
'size',
'capacity',
'reserve',
'append',
'insertMany',
'viewAsTensor',
'copyToTensor',
'copyFromTensor',
'insertFromTensor',
'getAt',
'setAt',
'push_back',
'pop_back',
'clear',
'insert',
'erase'
}
-- use utility function to automatically register new/clone results for GC delete
methods = utils.create_typed_methods("pcl_Indices_", Indices_method_names, '')
end
init()
function Indices:__init(x)
rawset(self, 'f', methods)
rawset(self, 'o', self.f.new())
if type(x) == 'table' then
x = torch.IntTensor(x)-1
end
if torch.isTensor(x) then
self:insertFromTensor(x)
end
end
function Indices.fromPtr(x)
local y = Indices_factory()
rawset(y, 'f', methods)
rawset(y, 'o', methods.fromPtr(x))
ffi.gc(y.o, methods.delete) -- register cleanup function
return y
end
function Indices:cdata()
return self.o
end
function Indices:clone()
local c = Indices_factory()
rawset(c, 'f', self.f)
rawset(c, 'o', self.f.clone(self.o))
return c
end
function Indices:size()
return self.f.size(self.o)
end
function Indices:__len()
return self:size()
end
function Indices:capacity()
return self.f.capacity(self.o)
end
function Indices:reserve(capacity)
self.f.reserve(self.o, capacity)
end
function Indices:append(source)
self.f.append(self.o, source:cdata())
end
function Indices:insertMany(source, src_index, dst_index, count)
self.f.insertMany(self.o, source:cdata(), (src_index or 1)-1, (dst_index or 1)-1, count or source:size())
end
function Indices:viewAsTensor(t)
t = t or torch.IntTensor()
self.f.viewAsTensor(self.o, t:cdata())
return t
end
function Indices:copyToTensor(dst_tensor, src_index, dst_index, count)
self.f.copyToTensor(self.o, dst_tensor:cdata(), (src_index or 1)-1, (dst_index or 1)-1, count or self:size())
end
function Indices:copyFromTensor(src_tensor, src_index, dst_index, count)
self.f.copyFromTensor(self.o, src_tensor:cdata(), (src_index or 1)-1, (dst_index or 1)-1, count or src_tensor:size(1))
end
function Indices:insertFromTensor(src_tensor, src_index, dst_index, count)
self.f.insertFromTensor(self.o, src_tensor:cdata(), (src_index or 1)-1, (dst_index or 1)-1, count or src_tensor:size(1))
end
function Indices:__index(idx)
local v = rawget(self, idx)
if not v then
v = Indices[idx]
if not v and type(idx) == 'number' then
local f, o = rawget(self, 'f'), rawget(self, 'o')
v = f.getAt(o, idx-1) + 1
end
end
return v
end
function Indices:__newindex(idx, v)
local f, o = rawget(self, 'f'), rawget(self, 'o')
if type(idx) == 'number' then
f.setAt(o, idx-1, v-1)
else
rawset(self, idx, v)
end
end
function Indices:push_back(value)
self.f.push_back(self.o, value-1)
end
function Indices:pop_back()
return self.f.pop_back(self.o)
end
function Indices:clear()
self.f.clear(self.o)
end
function Indices:insert(pos, value, n)
if torch.isTypeOf(value, pcl.Indices) then
self:insertMany(value:cdata(), 1, pos, n or value:size())
else
self.f.insert(self.o, pos-1, n or 1, value)
end
end
function Indices:erase(begin_pos, end_pos)
self.f.erase(self.o, begin_pos-1, (end_pos or begin_pos + 1)-1)
end
function Indices:__pairs()
return function (t, k)
local i = k or 1
if i > #t then
return nil
else
local v = t[i]
return i+1, v
end
end, self, nil
end
function Indices:__ipairs()
return self:__pairs()
end
function Indices:totable()
local t = {}
for i,v in ipairs(self) do
table.insert(t, v)
end
return t
end
function Indices:__tostring()
local t = self:totable()
table.insert(t, string.format("[pcl.Indices of size %d]", #self))
return table.concat(t, '\n')
end