/
mxHKmeans.hpp
369 lines (307 loc) · 9.81 KB
/
mxHKmeans.hpp
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
// Author: Mohamed Aly <malaa at vision d0t caltech d0t edu>
// Date: October 6, 2010
#ifndef MX_HKMEANS_HPP
#define MX_HKMEANS_HPP
#include "mxData.hpp"
//-----------------------------------------------------------------------
// import and export HkmPointList to Matlab
mxArray* exportHkmPointList(HkmPointList &pl)
{
mxArray* plist = mxCreateNumericMatrix(1, 0, mxDOUBLE_CLASS, mxREAL);
//size
uint npoints = pl.size();
if (npoints > 0)
{
//create mxarary
plist = mxCreateNumericMatrix(1, npoints, getClassIDfromT(pl[0]), mxREAL);
//get pointer
HkmPoint* pp = (HkmPoint*) mxGetPr(plist);
//loop
for (uint j=0; j<npoints; ++j) pp[j] = pl[j];
}
return plist;
}
void importHkmPointList(HkmPointList &pl, const mxArray* plist)
{
//get number of points
uint npoints;
if (plist)
{
npoints = mxGetNumberOfElements(plist);
//resize pl
pl.resize(npoints);
//pointer
HkmPoint* pp = (HkmPoint*) mxGetPr(plist);
//loop
for (uint j=0; j<npoints; ++j) pl[j] = pp[j];
}
}
//-----------------------------------------------------------------------
// import and export HkmClassIds to Matlab
mxArray* exportHkmClassIds(HkmClassIds &cids)
{
mxArray* mxcids = mxCreateNumericMatrix(1, 0, mxDOUBLE_CLASS, mxREAL);
//size
uint npoints = cids.size();
if (npoints > 0)
{
//create mxarary
mxcids = mxCreateNumericMatrix(1, npoints, getClassIDfromT(cids[0]), mxREAL);
//get pointer
HkmClassId* pp = (HkmClassId*) mxGetPr(mxcids);
//loop
for (uint j=0; j<npoints; ++j) pp[j] = cids[j];
}
return mxcids;
}
void importHkmClassIds(HkmClassIds &cids, const mxArray* mxcids)
{
if (mxcids)
{
//get number of points
uint npoints = mxGetNumberOfElements(mxcids);
//resize pl
cids.resize(npoints);
//pointer
HkmClassId* pp = (HkmClassId*) mxGetPr(mxcids);
//loop
for (uint j=0; j<npoints; ++j) cids[j] = pp[j];
}
}
//-----------------------------------------------------------------------
// import and export MeanStds to Matlab
mxArray* exportMeanStds(MeanStds &ms)
{
mxArray* mxms = mxCreateNumericMatrix(1, 0, mxDOUBLE_CLASS, mxREAL);
//size
uint npoints = ms.size();
if (npoints > 0)
{
//create mxarary
mxms = mxCreateNumericMatrix(1, npoints, getClassIDfromT(ms[0]), mxREAL);
//get pointer
MeanStd* pp = (MeanStd*) mxGetPr(mxms);
//loop
for (uint j=0; j<npoints; ++j) pp[j] = ms[j];
}
return mxms;
}
void importMeanStds(MeanStds &ms, const mxArray* mxms)
{
if (mxms)
{
//get number of points
uint npoints = mxGetNumberOfElements(mxms);
//resize pl
ms.resize(npoints);
//pointer
MeanStd* pp = (MeanStd*) mxGetPr(mxms);
//loop
for (uint j=0; j<npoints; ++j) ms[j] = pp[j];
}
}
//-----------------------------------------------------------------------
// import and export options struct to matlab
mxArray* exportHkmOptions(HkmOptions &opt)
{
//get its size
mwSize sz = sizeof(opt);
//allocate
mxArray* mxopt = mxCreateNumericMatrix(1, sz, mxUINT8_CLASS, mxREAL);
//get pointers
uint8_t *pmxopt = (uint8_t*) mxGetData(mxopt);
uint8_t *popt = (uint8_t*) &opt;
//put data
copy(popt, popt+sz, pmxopt);
// for (uint i=0; i<sz; ++i) cout << " | " << (int)popt[i] << "==" << (int)pmxopt[i];
// cout << endl;
// cout << opt.niters << " " << opt.nbranches << " " << opt.nlevels <<
// " " << opt.dist << " " << opt.usekdt << " " << opt.kdtopt.ntrees <<
// " " << opt.kdtopt.varrange << " " << opt.kdtopt.cycle <<
// " " << opt.kdtopt.dist << " " << opt.kdtopt.maxbins <<
// " " << opt.kdtopt.sample << endl;
//return
return mxopt;
}
void importHkmOptions(HkmOptions &opt, const mxArray* mxopt)
{
//get its size
mwSize sz = sizeof(opt);
//get pointers
uint8_t *pmxopt = (uint8_t*) mxGetData(mxopt);
uint8_t *popt = (uint8_t*) &opt;
//put data
copy(pmxopt, pmxopt+sz, popt);
// cout << opt.niters << " " << opt.nbranches << " " << opt.nlevels <<
// " " << opt.dist << " " << opt.usekdt << " " << opt.kdtopt.ntrees <<
// " " << opt.kdtopt.varrange << " " << opt.kdtopt.cycle <<
// " " << opt.kdtopt.dist << " " << opt.kdtopt.maxbins <<
// " " << opt.kdtopt.sample << endl;
// for (uint i=0; i<sz; ++i) cout << " | " << (int)popt[i] << "==" << (int)pmxopt[i];
// cout << endl;
}
//-----------------------------------------------------------------------
// Exports to matlab structure
template <class T>
mxArray* exportMatlab(Hkms<T>& hkms, bool getids)
{
//size of array
uint nnodes = hkms.hkms[0].size();
uint ntrees = hkms.opt.ntrees;
//The output is a structure array: nodes, opt, class
int nfields1 = 3;
const char* fieldnames1[] = {"nodes", "opt", "class"};
mxArray* out = mxCreateStructMatrix(1, 1, nfields1, fieldnames1);
//make the nodes matrix: ntables X nnodes
int nfields2 = 5;
const char* fieldnames2[] = {"means", "plist", "classids",
"visited", "meanstd"};
mxArray* nodes = mxCreateStructMatrix(ntrees, nnodes, nfields2, fieldnames2);
mxSetField(out, 0, "nodes", nodes);
//add the options to the first one
mxSetField(out, 0, "opt", exportHkmOptions(hkms.opt));
//add class
T t;
mxSetField(out, 0, "class", mxCreateDoubleScalar((double)getClassIDfromT(t)));
// cout << "nnodes:" << nnodes << endl;
mxArray* a;
//now loop and build nodes
for (uint t=0; t<ntrees; ++t)
{
//get that tree
Hkm<T>& hkm = hkms.hkms[t];
for (uint i=0; i<nnodes; ++i)
{
// cout << "node:" << i+1 << endl;
//rows for trees, columns for nodes within the tree
//so index is i*ntrees (to get the right column) + t (row)
mwSize nid = i*ntrees+t;
//get the node
HkmNode<T> &node = hkm.at(i+1);
//get the data into an mxarray
mxSetField(nodes, nid, "means", fillMxArray(node.means));
//visited
a = mxCreateNumericMatrix(1, 1, mxUINT8_CLASS, mxREAL);
*((uint8_t*)mxGetData(a)) = (uint8_t) node.visited;
mxSetField(nodes, nid, "visited", a);
//meanStds
mxSetField(nodes, nid, "meanstd", exportMeanStds(node.meanStds));
//get the lists
if (getids && node.size()>0)
{
//plist
// cout << "plist" << endl;
mxSetField(nodes, nid, "plist", exportHkmPointList(node.plist));
//classids
// cout << "classids" << endl;
mxSetField(nodes, nid, "classids", exportHkmClassIds(node.classIds));
}
}
}
return out;
}
//-----------------------------------------------------------------------
// Imports from matlab structure exported using exportMatlab
template <class T>
Hkms<T>* importMatlab(const mxArray* in, T dummy)
{
//get the options
HkmOptions opt;
importHkmOptions(opt, mxGetField(in, 0, "opt"));
//allocate
Hkms<T>* hkms = new Hkms<T>(opt);
//get the nodes
mxArray* nodes = mxGetField(in, 0, "nodes");
//size of array
// uint nnodes = mxGetNumberOfElements(nodes);
uint nnodes = mxGetN(nodes);
uint ntrees = mxGetM(nodes);
//resize hkms
hkms->resize(ntrees);
//loop on trees
for (uint t=0; t<ntrees; ++t)
{
//get hkm
Hkm<T>& hkm = hkms->hkms[t];
//resize
hkm.resize(nnodes);
//set opt
hkm.opt = opt;
// cout << "opt.nlevels:" << hkm.opt.nlevels << ", orig:" << opt.nlevels << endl;
mxArray* a;
//now loop and build
for (uint i=0; i<nnodes; ++i)
{
//node id
mwSize nid = i*ntrees + t;
//get the node
HkmNode<T> &node = hkm.at(i+1);
//get the means
fillData(node.means, mxGetField(nodes, nid, "means"), true);
//mean stds
importMeanStds(node.meanStds, mxGetField(nodes, nid, "meanstd"));
//get the lists
importHkmPointList(node.plist, mxGetField(nodes, nid, "plist"));
importHkmClassIds(node.classIds, mxGetField(nodes, nid, "classids"));
//visited
node.visited = (bool) *((uint8_t*)mxGetData(mxGetField(nodes, nid, "visited")));
}
// uint8_t* popt = (uint8_t*) &hkm->opt;
// cout << "hkmopt:" << endl;
// for (uint i=0; i<sizeof(opt); ++i) cout << " | " << (int)popt[i];
// cout << endl;
// popt = (uint8_t*) &opt;
// cout << "opt:" << endl;
// for (uint i=0; i<sizeof(opt); ++i) cout << " | " << (int)popt[i];
// cout << endl;
}
return hkms;
}
//-----------------------------------------------------------------------
// gets a matlab pointer fomr hkm
template <class T>
mxArray* exportMatlabPointer(Hkms<T>& hkm)
{
//make a strucuture holding a pointer and the classId corresponding to the data
int nfields = 2;
const char* fieldnames[] = {"ptr", "class"};
mxArray* out = mxCreateStructMatrix(1, 1, nfields, fieldnames);
mxArray* a;
//set the pointer
a = mxCreateNumericMatrix(1,1,mxINDEX_CLASS,mxREAL);
*(Hkms<T>**) mxGetPr(a) = &hkm;
mxSetField(out, 0, "ptr", a);
//set the class id
T t;
// mxClassID cid = getClassIDfromT(t);
// a = mxCreateNumericMatrix(1,1,mxDOUBLE_CLASS,mxREAL);
// *mxGetPr(a) = (double) cid;
mxSetField(out, 0, "class", mxCreateDoubleScalar((double)getClassIDfromT(t)));
return out;
}
mxClassID getClassIdMatlabPointer(const mxArray* in)
{
//get the classid
mxClassID cid = (mxClassID) *(mxGetPr(mxGetField(in, 0, "class")));
return cid;
}
template <class T>
Hkms<T>* getPointerMatlabPointer(const mxArray* in, T dummy)
{
//get the pointer
return *(Hkms<T>**)mxGetData(mxGetField(in, 0, "ptr"));
}
//-------------------------------------------------------------------------
//#define TEMPLATE(F) \
// F(float) \
// F(double) \
// F(char) \
// F(int) \
// F(unsigned int) \
// F(unsigned char)
//
//#define EXPORTMATLABPIONTER(T) \
// template mxArray* exportMatlabPointer(Hkm<T>&);
//
//TEMPLATE(EXPORTMATLABPIONTER)
#endif