void mexFunction( int nlhs, mxArray *plhs[],
                  int nrhs, const mxArray *prhs[])
{
  srand(0xffffff);
      
  //options struct
  KdtOptions opt;
  
  //get input class
  mxClassID classID = getClassID(dataIn);

  //get the inputs
  if (nrhs<1)
    mexErrMsgTxt("At least one input is required");  
  if (nrhs>1) opt.ntrees = (int) *(mxGetPr(ntreesIn));
  if (nrhs>2) opt.varrange= (float) *(mxGetPr(varrangeIn));
  if (nrhs>3) opt.meanrange = (float) *(mxGetPr(meanrangeIn));
  if (nrhs>4) opt.maxdepth = (int) *(mxGetPr(maxdepthIn));
  if (nrhs>5) opt.minvar = (float) *(mxGetPr(minvarIn));
  if (nrhs>6) opt.cycle = (char) *(mxGetPr(cycleIn));
  if (nrhs>7) opt.dist = getDistanceType(distIn); 
  if (nrhs>8) opt.maxbins = (int) *(mxGetPr(maxbinsIn));
  if (nrhs>9) opt.sample = (int) *(mxGetPr(sampleIn));  
  if (nrhs>10) opt.bitsperdim = (int) *(mxGetPr(bitsperdimIn));

//   mexPrintf("depth = %d\n", opt.maxdepth);
//   mexPrintf("bitsperdim = %d\n", opt.bitsperdim);
//  mexPrintf("n inputs = %d\n", nrhs);
//  mexPrintf("sample = %f\n", opt.sample);
//  plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL);
//  return;
  

  //allocate the kdforest
  Kdf* kdf = new Kdf(opt);
  
  //fill with the data
#define __CASE(CLASS)                                                     \
  {                                                                       \
    /*get the data*/                                                      \
    Data<TYPEOF_##CLASS> data;                                            \
    /*set data*/                                                          \
    fillData(data, dataIn);                                               \
    /*create*/                                                            \
    create(*kdf, data);                                                   \
    /*clear data*/                                                        \
    data.clear();                                                         \
  }

  __SWITCH(classID, __CASE)
  

  //return output
  mxArray* ret = mxCreateNumericMatrix(1,1,mxINDEX_CLASS,mxREAL);
  *(Kdf**) mxGetPr(ret) = kdf;
  kdfOut = ret;
  
}
Beispiel #2
0
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;  
}
void mexFunction( int nlhs, mxArray *plhs[],
                  int nrhs, const mxArray *prhs[])
{
   
  if (nrhs!=2)	mexErrMsgTxt("Five inputs required");

  //get the class id from hkm
  mxClassID cid = getClassIdMatlabPointer(hkmIn);

  mxArray *ids;
  uint n1;
#define __CASE(CLASS)                                                     \
  {                                                                       \
  	/*allocate the kdforest*/                                             \
    TYPEOF_##CLASS dummy;                                                 \
    Hkms<TYPEOF_##CLASS>* hkm = getPointerMatlabPointer(hkmIn, dummy);    \
    /*datas*/                                                                 \
    Data<TYPEOF_##CLASS> points;                                              \
    fillData(points, pointsIn);                                               \
    /*allocate output*/                                                       \
    n1 = points.size();                                                       \
    ids = mxCreateNumericMatrix(hkm->opt.ntrees, n1, mxUINT32_CLASS, mxREAL);	\
    /*compute*/                                                               \
    hkm->getLeafIds(points, (HkmClassId*) mxGetData(ids));                    \
    /*clear memory*/                                                          \
    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;
  
}
void mexFunction( int nlhs, mxArray *plhs[],
                  int nrhs, const mxArray *prhs[])
{
   
  if (nrhs!=8)	mexErrMsgTxt("Eight inputs required");

  //get the class of points
  mxClassID classID = mxGetClassID(dataIn);
  //if cell
  if (mxIsCell(dataIn) && mxGetNumberOfElements(dataIn)>0)
    for (uint i=0; i<mxGetNumberOfElements(dataIn); ++i)
    {
      mxArray* el = mxGetCell(dataIn, i);
      if (el != NULL)
      {
        classID = mxGetClassID(el);
        break;
      }
    }
  
  //number of documents
  uint ndocs = mxIsCell(dataIn) ? mxGetNumberOfElements(dataIn) : 1;
//   cout << "ndocs: " << ndocs << endl;
  
  //number of returns
  uint nret = 0;
  if (nrhs>=6)  nret = (uint) *mxGetPr(nretIn);  
  
  //overlap only?
  bool overlapOnly = true;
  if (nrhs>6) overlapOnly = (bool) *mxGetPr(overlapIn);  
  
  //verbose?
  bool verbose = (bool) *mxGetPr(verboseIn);  
  
  //get the weighting to use
  ivFile::Weight wt = getInvFileWeight(wtIn);
  
  //get the normalization to use
  ivFile::Norm norm = getInvFileNorm(normIn);
  
  //get the distance to use
  ivFile::Dist dist = getInvFileDist(distIn);

  //make the ivFile object  
  ivFile* ivfile = *(ivFile**)mxGetData(ivFileIn);
  //check if not passed in an object, then create a new one
  if (ivfile == NULL)
    mexErrMsgTxt("Empty inverted file input");
  
  //the return score list
  ivNodeLists scorelists;
  scorelists.resize(ndocs);
  
//   cout << "  searching!!";
//   mexPrintf("searching!!\n");

  //call function
#define __CASE(CLASS)                                                   \
    {                                                                     \
    /*get the data*/                                                      \
    Data<TYPEOF_##CLASS> data;                                            \
    /*set data*/                                                          \
    fillData(data, dataIn);                                               \
    /*search*/                                                            \
    ivSearchFile(*ivfile, data, wt, norm, dist, overlapOnly, nret,        \
            scorelists, verbose);                                         \
    /*clear data*/                                                        \
    data.clear();                                                         \
    }
            
    __SWITCH(classID, __CASE)
    
    
  mxArray *scores, *docs;
    
  //if we get all documents, then update nret with the number of docmuents
  //if it's zero
  if (!overlapOnly && nret==0)
    nret = ivfile->docs.size();
    
  //allocate output
  if (nret==0)
  {
    scores = mxCreateCellMatrix(1, ndocs);
    docs = mxCreateCellMatrix(1, ndocs);
  }
  else
  {
    scores = mxCreateNumericMatrix(nret, ndocs, mxSINGLE_CLASS,mxREAL);
    docs = mxCreateNumericMatrix(nret, ndocs, mxUINT32_CLASS,mxREAL);      
  }
  uint32_t* pdocs;
  float* pscores;
 
  //now loop on number of documents
//  cout << ndocs << " " << nret << endl;
  for (uint d=0; d<ndocs; ++d)
  {
    //get size of this list
    uint nlist = scorelists[d].size();
//    cout << " " << nlist;
    
    //get pointer to docs and score
    if (nret==0)
    {
      //create and get pointer
      mxSetCell(docs, d, mxCreateNumericMatrix(1, nlist, mxUINT32_CLASS, mxREAL));
      pdocs = (uint32_t*) mxGetPr(mxGetCell(docs, d));
      mxSetCell(scores, d, mxCreateNumericMatrix(1, nlist, mxSINGLE_CLASS, mxREAL));
      pscores = (float*) mxGetPr(mxGetCell(scores, d));
    }
    else
    {
      pdocs = (uint32_t*)mxGetPr(docs) + nret*d;
      pscores = (float*)mxGetPr(scores) + nret*d;      
    }
    
    //now we have pointers, so put data
//    nret = nret==0 ? nlist : nret;    
    for (uint i=0; i<nlist && (nret==0 || i<nret); ++i)
    {
      pscores[i] = (scorelists[d])[i].val;
      pdocs[i]   = (scorelists[d])[i].id + 1;      
    }
  }
   
  docsOut = docs;
  if (nlhs>1)
    scoresOut = scores;
  
  //clear 
  scorelists.clear();
}
void mexFunction( int nlhs, mxArray *plhs[],
                  int nrhs, const mxArray *prhs[])
{
   
  if (nrhs!=2)	mexErrMsgTxt("Two inputs required");
  
  //get the lsh objcect
  Lsh* lsh = *(Lsh**)mxGetData(lshIn);
  
  //get the class of points
  mxClassID classID = getClassID(pointsIn); 

  //get number of points
  mwSize npoints; // = getNpoints(pointsIn, true);
//   cout << "npoints:" << npoints << endl;

  //create an array of LshPointList
  BucketIdList* ids;
//   BucketIdList* ids = new BucketIdList[npoints];
  
  //now loop on the points and search
#define __CASE(CLASS)                                                   \
    {                                                                   \
    /*get the data*/                                                    \
    Data<TYPEOF_##CLASS> data;                                          \
    /*set data*/                                                        \
    fillData(data, pointsIn);                                           \
    /*allocate*/                                                        \
    npoints = data.size();                                              \
    ids = new BucketIdList[npoints];                                \
    /*insert*/                                                          \
    getBucketId(*lsh, data, ids);                                       \
    /*clear data*/                                                      \
    data.clear();                                                       \
    }

  __SWITCH(classID, __CASE) 
  
  //allocate output data and get pointer
  //## change the type of this matlab type if uint is something other than unsigned int
  mxClassID retId;
  switch (numeric_limits<BucketId_t>::digits)
  {
    case 32: retId = mxUINT32_CLASS; break;
    case 64: retId = mxUINT64_CLASS; break;
  }
  //return an array
  idsOut = mxCreateNumericMatrix(lsh->opt.ntables, npoints, retId, mxREAL);
  //pointer
  BucketId_t* idsp =  (BucketId_t*) mxGetPr(idsOut);
  
  //loop on points and put output  
  for (uint i=0; i<npoints; ++i)
  {
    for (uint j=0; j<ids[i].size(); ++j)
      idsp[i*lsh->opt.ntables + j] = ids[i][j]; 
    //clear this list
    ids[i].clear();
  }
  //clear list of plist
  delete [] ids;        
}
Beispiel #6
0
void mexFunction( int nlhs, mxArray *plhs[],
                  int nrhs, const mxArray *prhs[])
{
   
  if (nrhs!=5)	mexErrMsgTxt("Five inputs required");
  
  //get the lsh objcect
  Lsh* lsh = *(Lsh**)mxGetData(lshIn);
  
  //get the class of points
  mxClassID classID = getClassID(lshPointsIn); 

  //get number of points in search data
  mwSize npoints = getNpoints(pointsIn);

  //number of nearest neighbors to return 
  uint k = (uint) *mxGetPr(kIn);
  
  //empty distance, then use the one inside the lsh
  DistanceType dist;
  if (mxIsEmpty(distIn))
    dist = lsh->opt.dist;
  else
  	dist = getDistanceType(distIn);
  

  //  mexPrintf("sizes are: %dx%d %dx%d\n", rows1, cols1, rows2, cols2);
  mxArray *dists, *ids;
  uint n1;
  #define __CASE(CLASS)                                                       \
  {                                                                           \
    /*datas*/                                                                 \
    Data<TYPEOF_##CLASS> lshPoints, points;                                   \
    fillData(lshPoints, lshPointsIn);                                         \
    fillData(points, pointsIn);                                               \
    /*allocate output*/                                                       \
    n1 = points.size();                                                       \
    dists = mxCreateNumericMatrix(k, n1, mxSINGLE_CLASS, mxREAL);             \
    ids = mxCreateNumericMatrix(k, n1, mxUINT32_CLASS, mxREAL);               \
    /*compute*/                                                               \
    getKnn(*lsh, lshPoints, points, k,  dist, (uint*) mxGetData(ids),         \
            (float*) mxGetData(dists));                                       \
    /*clear memory*/                                                          \
    lshPoints.clear();                                                        \
    points.clear();                                                           \
  }

  __SWITCH(classID, __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;  
}
void mexFunction( int nlhs, mxArray *plhs[],
                  int nrhs, const mxArray *prhs[])
{
  if (nrhs!=3)	mexErrMsgTxt("Three inputs required");
  
  //get the lsh objcect
  Lsh* lsh = *(Lsh**)mxGetData(lshIn);
  
  //get the class of points
  mxClassID classID = getClassID(pointsIn); 

  //get number of points
  mwSize npoints = getNpoints(pointsIn);

  //number of returns
  bool cellout = (bool) *mxGetPr(celloutIn);
  
  //create an array of LshPointList
  LshFuncValList* vals = new LshFuncValList[npoints];
  
  //now loop on the points and search
#define __CASE(CLASS)                                                   \
    {                                                                   \
    /*get the data*/                                                    \
    Data<TYPEOF_##CLASS> data;                                          \
    /*set data*/                                                        \
    fillData(data, pointsIn);                                           \
    /*insert*/                                                          \
    getFuncVal(*lsh, data, vals);                                       \
    /*clear data*/                                                      \
    data.clear();                                                       \
    }

  __SWITCH(classID, __CASE) 
  
  //allocate output data and get pointer
  //## change the type of this matlab type if uint is something other than unsigned int
  mxClassID retId;
  switch (numeric_limits<LshFuncVal_t>::digits)
  {
    case 32: retId = mxUINT32_CLASS; break;
    case 64: retId = mxUINT64_CLASS; break;
  }
  //check if to return cell: if requested by user, or if have more than one table
  //otherwise, we can just return a matrix where each column is for a point
  //and each row is for a function
  cellout = cellout || (lsh->opt.ntables > 1);
  
  //return all points -> return a cell array
  if (cellout)
    valsOut = mxCreateCellMatrix(1, npoints);
  //return an array
  else
    valsOut = mxCreateNumericMatrix((mwSize)lsh->opt.nfuncs, (mwSize)npoints, 
            retId, mxREAL);

  //loop on points and put output
  for (uint i=0; i<npoints; ++i)
  {
    //get pointer
    LshFuncVal_t* valsp;
    if (cellout)
    {
      //get cell array
      mxArray* pcell = mxCreateNumericMatrix(lsh->opt.nfuncs, lsh->opt.ntables, 
              retId, mxREAL);
      mxSetCell(valsOut, i, pcell);              
      //allocate
      valsp = (LshFuncVal_t*) mxGetPr(pcell);
    }
    else            
    {
      //get pointer
      valsp = (LshFuncVal_t*) mxGetPr(valsOut);
      valsp += lsh->opt.nfuncs*i;
    }
    
    //loop on lists
    for (uint j=0; j<vals[i].size(); ++j)
      //get value
      valsp[j] = vals[i][j];
    
    //clear this list
    vals[i].clear();
  }
  //clear list of plist
  delete [] vals;    
    
}