1 | /*!\file InterpFromMesh2d.c
|
---|
2 | * \brief: data interpolation from a list of (x,y,values) into mesh vertices
|
---|
3 |
|
---|
4 | InterpFromMesh2d.c
|
---|
5 |
|
---|
6 | usage:
|
---|
7 | data_mesh=InterpFromMesh2d(index,x,y,data,x_mesh,y_mesh);
|
---|
8 |
|
---|
9 | where:
|
---|
10 |
|
---|
11 | input:
|
---|
12 | x,y: coordinates of matrix data
|
---|
13 | data - matrix holding the data to be interpolated onto the mesh.
|
---|
14 | x_mesh,y_mesh: coordinates of the mesh vertices onto which we interpolate.
|
---|
15 |
|
---|
16 | output:
|
---|
17 | data_mesh: vector of mesh interpolated data.
|
---|
18 |
|
---|
19 | */
|
---|
20 |
|
---|
21 | #include "./InterpFromMesh2d.h"
|
---|
22 |
|
---|
23 | void mexFunction( int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]) {
|
---|
24 |
|
---|
25 | /*input: */
|
---|
26 | double* index_data=NULL;
|
---|
27 | int index_data_rows;
|
---|
28 | int dummy;
|
---|
29 |
|
---|
30 | double* x_data=NULL;
|
---|
31 | int x_data_rows;
|
---|
32 |
|
---|
33 | double* y_data=NULL;
|
---|
34 | int y_data_rows;
|
---|
35 |
|
---|
36 | double* data=NULL;
|
---|
37 | int data_rows;
|
---|
38 | int data_cols;
|
---|
39 |
|
---|
40 | double* x_prime=NULL;
|
---|
41 | double* y_prime=NULL;
|
---|
42 |
|
---|
43 | int x_prime_rows;
|
---|
44 | int y_prime_rows;
|
---|
45 |
|
---|
46 |
|
---|
47 | double* default_values=NULL;
|
---|
48 | int num_default_values=0;
|
---|
49 |
|
---|
50 | //contours
|
---|
51 | mxArray* matlabstructure=NULL;
|
---|
52 | Contour** contours=NULL;
|
---|
53 | int numcontours;
|
---|
54 | Contour* contouri=NULL;
|
---|
55 | int i;
|
---|
56 |
|
---|
57 | /*Intermediary*/
|
---|
58 | int nods_data;
|
---|
59 | int nels_data;
|
---|
60 | int nods_prime;
|
---|
61 |
|
---|
62 | /* output: */
|
---|
63 | Vec data_prime=NULL;
|
---|
64 |
|
---|
65 | /*Boot module: */
|
---|
66 | MODULEBOOT();
|
---|
67 |
|
---|
68 | /*checks on arguments on the matlab side: */
|
---|
69 | if(nlhs!=NLHS){
|
---|
70 | InterpFromMesh2dUsage();
|
---|
71 | _error_("InterpFromMeshToMesh2dUsage usage error");
|
---|
72 | }
|
---|
73 | if((nrhs!=6) && (nrhs!=7) && (nrhs!=8)){
|
---|
74 | InterpFromMesh2dUsage();
|
---|
75 | _error_("InterpFromMeshToMesh2dUsage usage error");
|
---|
76 | }
|
---|
77 |
|
---|
78 | /*Input datasets: */
|
---|
79 | FetchData(&index_data,&index_data_rows,&dummy,INDEXHANDLE);
|
---|
80 | FetchData(&x_data,&x_data_rows,NULL,XHANDLE);
|
---|
81 | FetchData(&y_data,&y_data_rows,NULL,YHANDLE);
|
---|
82 | FetchData(&data,&data_rows,&data_cols,DATAHANDLE);
|
---|
83 | FetchData(&x_prime,&x_prime_rows,NULL,XPRIMEHANDLE);
|
---|
84 | FetchData(&y_prime,&y_prime_rows,NULL,YPRIMEHANDLE);
|
---|
85 |
|
---|
86 | if(nrhs>=7){
|
---|
87 | /*default values: */
|
---|
88 | FetchData(&default_values,&num_default_values,DEFAULTHANDLE);
|
---|
89 | }
|
---|
90 | else{
|
---|
91 | default_values=NULL;
|
---|
92 | num_default_values=0;
|
---|
93 | }
|
---|
94 |
|
---|
95 | if(nrhs>=8){
|
---|
96 |
|
---|
97 | /*Call expread on filename to build a contour array in the matlab workspace: */
|
---|
98 | mexCallMATLAB( 1, &matlabstructure, 1, (mxArray**)&FILENAME, "expread");
|
---|
99 |
|
---|
100 | /*contours: */
|
---|
101 | numcontours=mxGetNumberOfElements(matlabstructure);
|
---|
102 | contours=(Contour**)xmalloc(numcontours*sizeof(Contour*));
|
---|
103 | for(i=0;i<numcontours;i++){
|
---|
104 | //allocate
|
---|
105 | contouri=(Contour*)xmalloc(sizeof(Contour));
|
---|
106 | //retrieve dimension of this contour.
|
---|
107 | contouri->nods=(int)mxGetScalar(mxGetField(matlabstructure,i,"nods"));
|
---|
108 | //set pointers.
|
---|
109 | contouri->x=mxGetPr(mxGetField(matlabstructure,i,"x"));
|
---|
110 | contouri->y=mxGetPr(mxGetField(matlabstructure,i,"y"));
|
---|
111 | *(contours+i)=contouri;
|
---|
112 | }
|
---|
113 |
|
---|
114 | /* Debugging of contours :{{{1*/
|
---|
115 | /*for(i=0;i<numcontours;i++){
|
---|
116 | printf("\nContour echo: contour number %i / %i\n",i+1,numcontours);
|
---|
117 | contouri=*(contours+i);
|
---|
118 | printf(" Number of vertices %i\n",contouri->nods);
|
---|
119 | for (j=0;j<contouri->nods;j++){
|
---|
120 | printf(" %lf %lf\n",*(contouri->x+j),*(contouri->y+j));
|
---|
121 | }
|
---|
122 | }*/
|
---|
123 | /*}}}*/
|
---|
124 | }
|
---|
125 | else{
|
---|
126 | numcontours=0;
|
---|
127 | contours=NULL;
|
---|
128 | }
|
---|
129 |
|
---|
130 |
|
---|
131 | /*some checks*/
|
---|
132 | if (x_data_rows!=y_data_rows){
|
---|
133 | _error_("vectors x and y should have the same length!");
|
---|
134 | }
|
---|
135 | if (x_prime_rows!=y_prime_rows){
|
---|
136 | _error_("vectors x_prime and y_prime should have the same length!");
|
---|
137 | }
|
---|
138 |
|
---|
139 | /*get number of elements and number of nodes in the data*/
|
---|
140 | nels_data=index_data_rows;
|
---|
141 | nods_data=x_data_rows;
|
---|
142 | nods_prime=x_prime_rows;
|
---|
143 |
|
---|
144 | /* Run core computations: */
|
---|
145 | InterpFromMesh2dx(&data_prime,index_data,x_data,y_data,nods_data,nels_data,data,data_rows,x_prime,y_prime,nods_prime,default_values,num_default_values,contours,numcontours);
|
---|
146 |
|
---|
147 | /*Write data: */
|
---|
148 | WriteData(DATAPRIME,data_prime);
|
---|
149 |
|
---|
150 | /*end module: */
|
---|
151 | MODULEEND();
|
---|
152 | }
|
---|
153 |
|
---|
154 | void InterpFromMesh2dUsage(void)
|
---|
155 | {
|
---|
156 | _printf_(true," usage:\n");
|
---|
157 | _printf_(true," data_prime=InterpFromMesh2d(index,x,y,data,x_prime,y_prime);\n\n");
|
---|
158 | _printf_(true," or data_prime=InterpFromMesh2d(index,x,y,data,x_prime,y_prime,default_value);\n\n");
|
---|
159 | _printf_(true," or data_prime=InterpFromMesh2d(index,x,y,data,x_prime,y_prime,default_value,contourname);\n\n");
|
---|
160 | _printf_(true," where:\n");
|
---|
161 | _printf_(true," x,y: coordinates of the nodes where data is defined\n");
|
---|
162 | _printf_(true," index: index of the mesh where data is defined\n");
|
---|
163 | _printf_(true," data - vector holding the data to be interpolated onto the points.\n");
|
---|
164 | _printf_(true," x_prime,y_prime: coordinates of the mesh vertices onto which we interpolate.\n");
|
---|
165 | _printf_(true," default_value: a scalar or vector of size length(x_prime).\n");
|
---|
166 | _printf_(true," contourname: linear interpolation will happen on all x_interp,y_interp inside the contour, default value will be adopted on the rest of the mesh.\n");
|
---|
167 | _printf_(true," data_prime: vector of prime interpolated data.\n");
|
---|
168 | _printf_(true,"\n");
|
---|
169 | }
|
---|