Repository navigation
Expand file tree
/
Copy pathmxHkmKnn.cpp
More file actions
83 lines (66 loc) · 2.85 KB
/
Copy pathmxHkmKnn.cpp
File metadata and controls
83 lines (66 loc) · 2.85 KB
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
// Author: Mohamed Aly <malaa at vision d0t caltech d0t edu>
// Date: October 6, 2010
#include "mex.h"
#include <string>
#include <cassert>
#include <cstring>
#include <climits>
#include <limits>
#include "ccHKmeans.hpp"
#include "mxHKmeans.hpp"
#include "mxDistance.hpp"
//inputs
#define hkmIn prhs[0]
#define hkmPointsIn prhs[1]
#define pointsIn prhs[2]
#define kIn prhs[3]
#define nchecksIn prhs[4]
//#define distIn prhs[4]
//outputs
#define idsOut plhs[0]
#define distsOut plhs[1]
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
if (nrhs<4) mexErrMsgTxt("At least four inputs are required");
//number of nearest neighbors to return
uint k = (uint) *mxGetPr(kIn);
//get the class id from hkm
mxClassID cid = getClassIdMatlabPointer(hkmIn);
mxArray *dists, *ids;
uint n1;
#define __CASE(CLASS) \
{ \
/*allocate the kdforest*/ \
TYPEOF_##CLASS dummy; \
Hkms<TYPEOF_##CLASS>* hkm = getPointerMatlabPointer(hkmIn, dummy); \
/*number of checks*/ \
if (nrhs>4) hkm->opt.nchecks = (int) *(mxGetPr(nchecksIn)); \
/*datas*/ \
Data<TYPEOF_##CLASS> hkmPoints, points; \
fillData(hkmPoints, hkmPointsIn); \
fillData(points, pointsIn); \
/*allocate output*/ \
n1 = points.size(); \
dists = mxCreateNumericMatrix(k, n1, mxSINGLE_CLASS, mxREAL); \
ids = mxCreateNumericMatrix(k, n1, mxUINT32_CLASS, mxREAL); \
/*compute*/ \
hkm->hkmKnn(hkmPoints, points, k, (uint*) mxGetData(ids), \
(float*) mxGetData(dists)); \
/*clear memory*/ \
hkmPoints.clear(); \
points.clear(); \
}
__SWITCH(cid, __CASE)
//add one to the ids to make it Matlab compatible
uint* idp = (uint*) mxGetData(ids);
float* distp = (float*) mxGetData(dists);
for (mwSize i=0; i<k*n1; ++i)
{
if (distp[i] < numeric_limits<float>::infinity())
idp[i] += 1;
}
idsOut = ids;
if (nlhs>1)
distsOut = dists;
}