1 |
#include <math.h> |
---|
2 |
#include <stdio.h> |
---|
3 |
#include "mex.h" |
---|
4 |
#include "opnml_mex5_allocs.c" |
---|
5 |
#define BUFFER_SIZE 72 |
---|
6 |
|
---|
7 |
/************************************************************ |
---|
8 |
|
---|
9 |
#### ## ##### ###### # # ## # # |
---|
10 |
# # # # # # # # # # # # |
---|
11 |
# # # # ##### # # # # # |
---|
12 |
# ### ###### # # # ## # ###### # |
---|
13 |
# # # # # # ## ## # # # |
---|
14 |
#### # # # ###### # # # # # |
---|
15 |
|
---|
16 |
************************************************************/ |
---|
17 |
|
---|
18 |
|
---|
19 |
void mexFunction(int nlhs, |
---|
20 |
mxArray *plhs[], |
---|
21 |
int nrhs, |
---|
22 |
const mxArray *prhs[]) |
---|
23 |
{ |
---|
24 |
|
---|
25 |
/* ---- read_ucd_mex will be called as : |
---|
26 |
[et,xt,yt,zt,data]=read_ucd(inpname); |
---|
27 |
|
---|
28 |
I/O argument count has already been checked in the calling |
---|
29 |
routine, read_ucd. The input filename will have to be |
---|
30 |
checked here, though. |
---|
31 |
------------------------ */ |
---|
32 |
|
---|
33 |
double *x, *y, *z, *dele, *data; |
---|
34 |
int nnd,nne,nnd_data,nne_data,nmodel_data,i,j,**ele; |
---|
35 |
int errflag,strlen,itrash,tcomp,ncomp,*ncompcomp; |
---|
36 |
char *inpname, *line, *cell_type; |
---|
37 |
FILE *fp=NULL, *fopen(); |
---|
38 |
|
---|
39 |
/* ---- extract input filename from first pointer to RHS |
---|
40 |
--------------------------------------- */ |
---|
41 |
strlen=mxGetN(prhs[0])+1; |
---|
42 |
inpname=mxCalloc(strlen,sizeof(char)); |
---|
43 |
if (mxGetString(prhs[0],inpname,strlen)==1) |
---|
44 |
fprintf(stderr,"Input filename string extraction failed in READ_UCD_MEX."); |
---|
45 |
|
---|
46 |
/* ---- Open UCD filename ------------------------------------------- */ |
---|
47 |
if (!(fp = fopen(inpname,"r"))){ |
---|
48 |
fprintf(stderr,"Open of %s failed.\n",inpname); |
---|
49 |
/* Allocate the return matricies as empty [] */ |
---|
50 |
plhs[0]=mxCreateDoubleMatrix(0,0,mxREAL); |
---|
51 |
plhs[1]=mxCreateDoubleMatrix(0,0,mxREAL); |
---|
52 |
plhs[2]=mxCreateDoubleMatrix(0,0,mxREAL); |
---|
53 |
plhs[3]=mxCreateDoubleMatrix(0,0,mxREAL); |
---|
54 |
plhs[4]=mxCreateDoubleMatrix(0,0,mxREAL); |
---|
55 |
return; |
---|
56 |
} |
---|
57 |
|
---|
58 |
/* ---- Read until first line without leading character of "#" ------ */ |
---|
59 |
line=mxCalloc(BUFFER_SIZE,sizeof(char)); |
---|
60 |
fgets(line,BUFFER_SIZE,fp); |
---|
61 |
while (!strncmp(line,"#",1)) |
---|
62 |
fgets(line,BUFFER_SIZE,fp); |
---|
63 |
|
---|
64 |
/* ---- "line" now contains 5 integers embedded in a string |
---|
65 |
They are: nnd, nne, nnd_data, nne_data, nmodel_data ------- */ |
---|
66 |
sscanf(line,"%d %d %d %d %d",&nnd,&nne,&nnd_data,&nne_data,&nmodel_data); |
---|
67 |
|
---|
68 |
/* ---- Allocate space for x,y,z ------------------------------------ */ |
---|
69 |
x= (double *) mxDvector(0,nnd-1); |
---|
70 |
y= (double *) mxDvector(0,nnd-1); |
---|
71 |
z= (double *) mxDvector(0,nnd-1); |
---|
72 |
|
---|
73 |
for(i=0;i<nnd;i++) |
---|
74 |
fscanf(fp,"%d %lf %lf %lf",&itrash,&x[i],&y[i],&z[i]); |
---|
75 |
|
---|
76 |
/* ---- Allocate space for **ele, the integer element array (nneX3) |
---|
77 |
and for the double MATLAB representation *dele |
---|
78 |
then fill in the double* array ---------------------------- */ |
---|
79 |
cell_type=mxCalloc(3,sizeof(char)); |
---|
80 |
dele = (double *) mxDvector(0,3*nne-1); |
---|
81 |
ele= (int **) mxImatrix(0,nne-1,0,2); |
---|
82 |
|
---|
83 |
fgets(line,BUFFER_SIZE,fp); /* Read NewLine at end of current line */ |
---|
84 |
/* ---- Read next line and determine cell_type ------------------ */ |
---|
85 |
fgets(line,BUFFER_SIZE,fp); |
---|
86 |
get_token(&line,&itrash,"%d"); |
---|
87 |
get_token(&line,&itrash,"%d"); |
---|
88 |
get_token(&line,cell_type,"%s"); |
---|
89 |
if (strncmp(cell_type,"tri",3)!=0){ |
---|
90 |
fprintf(stderr,"READ_UCD can only interpret cell topologies of type \"TRI\".\n"); |
---|
91 |
fprintf(stderr,"Did this UCD file come from TRANSECT or QUODTRANS???\n"); |
---|
92 |
/* Allocate the return matricies as empty [] */ |
---|
93 |
plhs[0]=mxCreateDoubleMatrix(0,0,mxREAL); |
---|
94 |
plhs[1]=mxCreateDoubleMatrix(0,0,mxREAL); |
---|
95 |
plhs[2]=mxCreateDoubleMatrix(0,0,mxREAL); |
---|
96 |
plhs[3]=mxCreateDoubleMatrix(0,0,mxREAL); |
---|
97 |
plhs[4]=mxCreateDoubleMatrix(0,0,mxREAL); |
---|
98 |
return; |
---|
99 |
} |
---|
100 |
|
---|
101 |
/* --- If we're here, break down the remaining data in the current line |
---|
102 |
and scan the rest of the cell lines from 1:nne-1 |
---|
103 |
----------------------------- */ |
---|
104 |
get_token(&line,&dele[0],"%lf"); |
---|
105 |
get_token(&line,&dele[0+nne],"%lf"); |
---|
106 |
get_token(&line,&dele[0+2*nne],"%lf"); |
---|
107 |
|
---|
108 |
for(i=1;i<nne;i++) |
---|
109 |
fscanf(fp,"%d %d %s %lf %lf %lf", |
---|
110 |
&itrash,&itrash,cell_type,&dele[i],&dele[i+nne],&dele[i+2*nne]); |
---|
111 |
|
---|
112 |
/* ---- The next line is the number of node-data components |
---|
113 |
and their dimensions -------------------------------------- */ |
---|
114 |
fgets(line,BUFFER_SIZE,fp); /* Read NewLine at end of current line */ |
---|
115 |
fgets(line,BUFFER_SIZE,fp); |
---|
116 |
get_token(&line,&ncomp,"%d"); |
---|
117 |
|
---|
118 |
/* In AVS-UCD format, the nodedata components can be either scalar or vector. |
---|
119 |
We will treat a vector, specified by &ncompcomp[i]=3, as three scalars and |
---|
120 |
returned to the MATLAB workspace as such. Thus, a node data segment that |
---|
121 |
has compoments specified as (1 3 1) for (scalar vector scalar) will be |
---|
122 |
returned as (1 1 1 1 1) for (scalar scalar scalar scalar scalar). */ |
---|
123 |
|
---|
124 |
ncompcomp=(int*)mxIvector(0,ncomp); |
---|
125 |
tcomp=0; |
---|
126 |
for(i=0;i<ncomp;i++){ |
---|
127 |
get_token(&line,&ncompcomp[i],"%d"); |
---|
128 |
tcomp=tcomp+ncompcomp[i]; |
---|
129 |
} |
---|
130 |
|
---|
131 |
|
---|
132 |
for(j=0;j<tcomp;j++) |
---|
133 |
fgets(line,BUFFER_SIZE,fp); /* There are the data label lines, which we ignore */ |
---|
134 |
|
---|
135 |
|
---|
136 |
/* ---- Scan in the node data lines as per NCOMP from above ------ */ |
---|
137 |
data=(double *) mxDvector(0,tcomp*nnd-1); |
---|
138 |
for(i=0;i<nnd;i++){ |
---|
139 |
fscanf(fp,"%d",&itrash); /* Read the node number as trash */ |
---|
140 |
for(j=0;j<tcomp;j++) |
---|
141 |
fscanf(fp,"%lf",&data[i+j*nnd]); |
---|
142 |
} |
---|
143 |
|
---|
144 |
plhs[0]=mxCreateDoubleMatrix(nne,3,mxREAL); |
---|
145 |
plhs[1]=mxCreateDoubleMatrix(nnd,1,mxREAL); |
---|
146 |
plhs[2]=mxCreateDoubleMatrix(nnd,1,mxREAL); |
---|
147 |
plhs[3]=mxCreateDoubleMatrix(nnd,1,mxREAL); |
---|
148 |
plhs[4]=mxCreateDoubleMatrix(nnd,tcomp,mxREAL); |
---|
149 |
|
---|
150 |
mxSetPr(plhs[0],dele); |
---|
151 |
mxSetPr(plhs[1],x); |
---|
152 |
mxSetPr(plhs[2],y); |
---|
153 |
mxSetPr(plhs[3],z); |
---|
154 |
mxSetPr(plhs[4],data); |
---|
155 |
|
---|
156 |
fclose(fp); |
---|
157 |
/* No "frees" needed if arrays allocated with "mx" allocation routines */ |
---|
158 |
return; |
---|
159 |
} |
---|
160 |
/*-----------------------------------------------------* |
---|
161 |
* * |
---|
162 |
* **** get_token **** * |
---|
163 |
* * |
---|
164 |
* returns the next value in tline[] separated by a * |
---|
165 |
* blank. * |
---|
166 |
*-----------------------------------------------------*/ |
---|
167 |
|
---|
168 |
int get_token(char **tline, char *data,char *control) |
---|
169 |
{ |
---|
170 |
|
---|
171 |
char *line, token[80], c; |
---|
172 |
|
---|
173 |
int ntc; |
---|
174 |
|
---|
175 |
c = 0; |
---|
176 |
line = *tline; |
---|
177 |
|
---|
178 |
for (ntc = 0; c != '\n'; line++) { |
---|
179 |
if (((c = *line) != ' ') && (c != '\n')) |
---|
180 |
token[ntc++] = c; |
---|
181 |
else if (ntc != 0) { |
---|
182 |
token[ntc] = '\0'; |
---|
183 |
sscanf (token, control, data); |
---|
184 |
*tline = line; |
---|
185 |
break; |
---|
186 |
} |
---|
187 |
} |
---|
188 |
|
---|
189 |
return (ntc); |
---|
190 |
} |
---|