Exemple #1
0
void mexFunction( int nlhs, mxArray *plhs[1], int nrhs, const mxArray *prhs[2] ) {
    double *Ref;
    double *Que;
    Coord3D pk;
    double *idcdouble, *outdistances;
    int Nr, Nq, Dr, Dq, i, idc;	// idc, closest point id

	GLTREE *Tree = NULL;

    // Errors check
    if( nrhs != 2 )
        mexErrMsgTxt("Two inputs required.");
    
    if( nlhs > 2 )
		mexErrMsgTxt("Maximum two outputs supported");

	Dr = (int)mxGetN(prhs[0]);
	Dq = (int)mxGetN(prhs[1]);//non ?il numero dei query solo un uso temporaneo
 
	if( Dr!=3 || Dq!=3 )
		mexErrMsgTxt("Only 3D points supported ");
  
	if( !mxIsDouble(prhs[0]) || !mxIsDouble(prhs[1]))
		mexErrMsgTxt("Inputs must be double vectors ");

    Ref = mxGetPr(prhs[0]);//puntatore all'array dei punti
    Que = mxGetPr(prhs[1]);//puntatore all'array dei punti query

	Nr = (int)mxGetM(prhs[0]);//number of reference points
    Nq = (int)mxGetM(prhs[1]);//number of query points  

    Coord3D* pstruct = new Coord3D[Nr]; 
       
    // copying the point array to GLTree data structure
    for (i=0;i<Nr;i++) {
		pstruct[i].x = Ref[i];
        pstruct[i].y = Ref[i+Nr];
        pstruct[i].z = Ref[i+2*Nr];
    }
    
	Tree = new GLTREE( pstruct, Nr );//chiamata al costruttore

	if( Tree == NULL )
		mexErrMsgTxt("Invalid tree pointer");

    plhs[0] = mxCreateDoubleMatrix(Nq, 1,mxREAL);//costruisce l'output array
	plhs[1] = mxCreateDoubleMatrix(Nq, 1,mxREAL);//costruisce l'output array
    idcdouble = mxGetPr(plhs[0]);//appicicaci il puntatore
	outdistances = mxGetPr(plhs[1]);//appicicaci il puntatore

	for (i=0;i<Nq;i++) {
		pk.x=Que[i];
		pk.y=Que[i+Nq];
		pk.z=Que[i+Nq+Nq]; 
		Tree->SearchClosest( &pstruct[0], &pk, &idc, &outdistances[i] );//lancio la routine per la ricerca del pi?vicino
		idcdouble[i] = idc + 1;	//convert to matlab notation
	}
 
    //Free aloocated memory
    delete [] pstruct;
	delete Tree;
	//Tree->~GLTREE();
	return;
}
void mexFunction( int nlhs, mxArray *plhs[1],
int nrhs,  const mxArray *prhs[3])
{
    
    double       *p;

    double       *qp;

    Coord3D       pk;
    
    double  *ptrtree;
    double * idcdouble;
    int N,Nq,i,j,idc;
    double* outdistances;
    
    
    
    // Errors check
    
    if(nrhs!=3)
        mexErrMsgTxt("3 inputs required.");
    
      if(nlhs>2)
    {  mexErrMsgTxt("Maximum two outputs supported");}
    
    
    
  
   
 N=mxGetN(prhs[0]);
 Nq=mxGetN(prhs[1]);//non è il numero dei query solo un uso temporaneo
 
 if(N!=3 || Nq!=3)
  { mexErrMsgTxt("Only 3D points supported ");
         } 
  
  if( !mxIsDouble(prhs[0]) || !mxIsDouble(prhs[1]))
 { mexErrMsgTxt("Inputs must be double vectors ");
         } 
    
    
    
    p = mxGetPr(prhs[0]);//puntatore all'array dei punti
   
    qp = mxGetPr(prhs[1]);//puntatore all'array dei punti query
   
    ptrtree = mxGetPr(prhs[2]);//puntatore all'albero precedentemente fornito

    
    
    
     
     N=mxGetM(prhs[0]);//dimensione reference
     Nq=mxGetM(prhs[1]);//numero dei query 
     
     
    
    plhs[0] = mxCreateDoubleMatrix(Nq, 1,mxREAL);//costruisce l'output array
    idcdouble = mxGetPr(plhs[0]);//appicicaci il puntatore 
    
    
    GLTREE *Tree;//dichiaro il puntatore l'oggetto
    
    Tree=(GLTREE*)((long)(ptrtree[0]));//ritrasformo il puntatore passato
    
//     mexPrintf("puntatore= %4.4x\n",Tree);
    
   
    //x e y sono i vettori dei reference points passati
    //pk è un array [2x1] con x e y del query point
    //idc double è l'id del più vicino, sarebbe un integer ma per ridare il puntatore a double
    
    
   
   
    //declaration
    //void GLTREE::SearchKClosest(Coord3D *p,Coord3D *pk,int* idc,double* mindist,int k)
    
     Coord3D* pstruct=new Coord3D[N]; 
       
      //copying the point array to GLTree data structure
      for (i=0;i<N;i++)
      {
          
       pstruct[i].x=p[i];
        pstruct[i].y=p[i+N];
         pstruct[i].z=p[i+2*N];
       
      }
    
  plhs[1] = mxCreateDoubleMatrix(Nq, 1,mxREAL);//costruisce l'output array
   outdistances = mxGetPr(plhs[1]);//appicicaci il puntatore

        for (i=0;i<Nq;i++)
        {
              // mexPrintf("Prima della ricerca");
            pk.x=qp[i];pk.y=qp[i+Nq];pk.z=qp[i+Nq+Nq]; 
            Tree->SearchClosest(&pstruct[0],&pk,&idc,&outdistances[i]);//lancio la routine per la ricerca del più vicino
            //mexPrintf("Dopo la ricerca");
           
                idcdouble[i]=idc+1;//convert to matlab notation
          
        }
 
    //Free aloocated memory
 
    delete [] pstruct;
}