NCCOOS Trac Projects: Top | Web | Platforms | Processing | Viz | Sprints | Sandbox | (Wind)

root/gliderproc/trunk/MATLAB/opnml/MEXSRC/ele2neimex5.c

Revision 495 (checked in by cbc, 11 years ago)

Initial import of Stark code.

Line 
1 #include <math.h>
2 #include <stdio.h>
3 #include "mex.h"
4 #define NEIMAX 20
5 #include "opnml_mex5_allocs.c"
6
7 /************************************************************
8
9   ####     ##     #####  ######  #    #    ##     #   #
10  #    #   #  #      #    #       #    #   #  #     # #
11  #       #    #     #    #####   #    #  #    #     #
12  #  ###  ######     #    #       # ## #  ######     #
13  #    #  #    #     #    #       ##  ##  #    #     #
14   ####   #    #     #    ######  #    #  #    #     #
15
16 ************************************************************/
17
18 /* PROTOTYPES */
19 void isort2(unsigned long n, int arr[], int brr[]);
20 void mexFunction(int            nlhs,
21                  mxArray       *plhs[],
22                  int            nrhs,
23                  const mxArray *prhs[])
24
25 {
26    /* all pointers start off as NULL */
27    int **ele,**nei=NULL,*numnei=NULL,*itheta=NULL,*ilist=NULL;
28    double *dnei, *dele, *x,*y,xc,yc,dx,dy;
29    int maxnei=0;
30    int i,j,k,kk,l,ll,ne,nn,no;
31
32 /* ---- check I/O arguments ----------------------------------------- */
33    if (nrhs != 3)  mexErrMsgTxt("ele2nei requires 3 input arguments.");
34    else if (nlhs!= 1)  mexErrMsgTxt("ele2nei requires 1 output arguments.");
35
36 /* ---- dereference input ------------------------------------------- */
37    dele=mxGetPr(prhs[0]);
38    ne=mxGetM(prhs[0]);
39    x=mxGetPr(prhs[1]);
40    y=mxGetPr(prhs[2]);
41    nn=mxGetM(prhs[1]);
42    fprintf(stderr,"NE = %d\n",ne); 
43    fprintf(stderr,"NN = %d\n",nn); 
44
45 /* ---- allocate space for int representation of dele &
46         convert double element representation to int         -------- */
47    ele=(int **)mxImatrix(1,ne,1,3);
48    for (i=1;i<=ne;i++){
49       for(j=1;j<=3;j++){
50          ele[i][j]=((int)dele[(i-1)+(j-1)*ne]);
51       }
52    }
53            
54 /* Allocate neighbor list array with matlab alloc routines in NRC style */
55    nei=(int **)mxImatrix(1,nn,1,NEIMAX);
56    numnei=(int *)mxIvector(1,nn);
57      
58    fprintf(stderr,"Computing neighbor list (Max=%d)...\n",NEIMAX); 
59    for(k=1;k<=nn;k++){
60       nei[k][1]=k;
61       numnei[k]=1;
62       for(l=1;l<=ne;l++){
63          for(ll=1;ll<=3;ll++){
64             if(ele[l][ll]==k)
65                goto l83;
66          }                   
67          goto l81;
68  l83:    no=numnei[k];
69          for(ll=1;ll<=3;ll++){
70             for(kk=1;kk<=no;kk++)
71                if(ele[l][ll]==nei[k][kk])goto l84;
72             /* new neighbor */
73             no++;
74             nei[k][no]=ele[l][ll];
75             numnei[k]=no;
76  l84:       continue;
77          }
78  l81:    continue;
79       }
80    }
81      
82    
83    maxnei=0;
84    for(k=1;k<=nn;k++)
85       maxnei=IMAX(maxnei,numnei[k]);
86    maxnei--;
87    fprintf(stderr,"Max Number of neighbors = %d\n",maxnei);
88
89 /* sort each node's neighbors into CCW around center node; ---------- */
90    puts("Sorting neighboring node order on ANGLE");
91    itheta=(int *)mxIvector(1,NEIMAX);
92    ilist=(int *)mxIvector(1,NEIMAX);
93    
94    for(k=1;k<=nn;k++){
95       xc=x[k-1];yc=y[k-1];
96       for(j=2;j<=numnei[k];j++){         
97          dx=x[nei[k][j]-1]-xc;
98          dy=y[nei[k][j]-1]-yc;
99          itheta[j-1]=(int)floor((atan2(dy,dx))*180./3.14159);
100          if(itheta[j-1]<0)itheta[j-1]=itheta[j-1]+360;
101          ilist[j-1]=nei[k][j];
102       }
103
104       isort2(numnei[k]-1,itheta,ilist);
105       for(j=2;j<=numnei[k];j++)
106          nei[k][j]=ilist[j-1];
107
108    }
109    puts("Sorting finished");
110    
111 /* allocate and fill double neighbor vector for return */
112    dnei=(double *)mxDvector(0,nn*maxnei);   
113    for(j=0;j<maxnei;j++)
114       for(k=0;k<nn;k++)
115          dnei[k+j*nn]=(double)nei[k+1][j+2];
116    
117    plhs[0]=mxCreateDoubleMatrix(nn,maxnei,mxREAL);
118    mxSetPr(plhs[0],dnei);
119          
120 /* ---- No need to free memory allocated with "mxCalloc"; MATLAB
121    does this automatically.  The CMEX allocation functions in
122    "opnml_allocs.c" use mxCalloc. ----------------------------------- */
123    return;   
124
125
126 }
127 /*  NRC isort2 routine */
128
129 #define M 7
130 #define NSTACK 50
131 #define SWAP(AA,BB) temp=(AA);(AA)=(BB);(BB)=temp;
132
133 void isort2(unsigned long n, int arr[], int brr[])
134 {
135    unsigned long i,ir=n,j,k,l=1;
136    int *istack,jstack=0;
137    int a,b,temp;
138
139    istack=(int *)Ivector(1,NSTACK);
140    for (;;) {
141       if (ir-l < M) {
142          for (j=l+1;j<=ir;j++) {
143             a=arr[j];
144             b=brr[j];
145             for (i=j-1;i>=1;i--) {
146                if (arr[i] <= a) break;
147                arr[i+1]=arr[i];
148                brr[i+1]=brr[i];
149             }
150             arr[i+1]=a;
151             brr[i+1]=b;
152          }
153          if (!jstack) {
154             free_Ivector(istack,1,NSTACK);
155             return;
156          }
157          ir=istack[jstack];
158          l=istack[jstack-1];
159          jstack -= 2;
160       } else {
161          k=(l+ir) >> 1;
162          SWAP(arr[k],arr[l+1])
163          SWAP(brr[k],brr[l+1])
164          if (arr[l+1] > arr[ir]) {
165             SWAP(arr[l+1],arr[ir])
166             SWAP(brr[l+1],brr[ir])
167          }
168          if (arr[l] > arr[ir]) {
169             SWAP(arr[l],arr[ir])
170             SWAP(brr[l],brr[ir])
171          }
172          if (arr[l+1] > arr[l]) {
173             SWAP(arr[l+1],arr[l])
174             SWAP(brr[l+1],brr[l])
175          }
176          i=l+1;
177          j=ir;
178          a=arr[l];
179          b=brr[l];
180          for (;;) {
181             do i++; while (arr[i] < a);
182             do j--; while (arr[j] > a);
183             if (j < i) break;
184             SWAP(arr[i],arr[j])
185             SWAP(brr[i],brr[j])
186          }
187          arr[l]=arr[j];
188          arr[j]=a;
189          brr[l]=brr[j];
190          brr[j]=b;
191          jstack += 2;
192          if (jstack > NSTACK) nrerror("NSTACK too small in isort2.");
193          if (ir-i+1 >= j-l) {
194             istack[jstack]=ir;
195             istack[jstack-1]=i;
196             ir=j-1;
197          } else {
198             istack[jstack]=j-1;
199             istack[jstack-1]=l;
200             l=i;
201          }
202       }
203    }
204 }
205 #undef M
206 #undef NSTACK
207
208
209
Note: See TracBrowser for help on using the browser.