-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmesh_smooth_double.c
178 lines (145 loc) · 4.78 KB
/
mesh_smooth_double.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "mex.h"
#include "math.h"
/*
* function [ET_table,EV_table,ETV_index]=edge_tangents(V,Ne)
*/
/* Coordinates to index */
int mindex3(int x, int y, int z, int sizx, int sizy) { return z*sizx*sizy+y*sizx+x;}
int mindex2(int x, int y, int sizx) { return y*sizx+x;}
/* The matlab mex function */
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ) {
/* Vertex list input */
double *V;
/* Neighbour list input */
const mxArray *Ne;
mxArray *V_tmp;
double *W;
mxArray *PneigMatlab;
mwSize *PneigDims;
int PneigLength=0;
int PneigCount=0;
double t=0;
double w,wsum;
double nx,ny,nz;
double x,y,z;
double a,b;
double *ap, *bp;
const double *itertmp=NULL;
int iterN=1;
/* Outputs */
double *V_smooth;
double *V_smooth_tmp;
double *Pneig;
/* Table Size */
mwSize table_Dims[2]={1,3};
/* Number of vertices */
const mwSize *VertexDims;
int VertexN=0;
/* Loop variables */
int i, j, k, n;
/* Check for proper number of arguments. */
if(nrhs != 6) {
mexErrMsgTxt("6 inputs are required.");
/*mexErrMsgTxt("2 inputs are required.");*/
} else if(nlhs!=1) {
mexErrMsgTxt("1 output is required");
}
/* Connect Inputs */
V=(double *)mxGetPr(prhs[0]);
Ne=prhs[1];
W=(double *)mxGetPr(prhs[2]);
/* iterN argument given? */
itertmp = (const double*) mxGetData(prhs[3]);
iterN = *itertmp;
if (!mxIsInf(*itertmp) &&
!mxIsNaN(*itertmp))
iterN = (int)*itertmp;
ap = (const double*)mxGetData(prhs[4]);
bp = (const double*)mxGetData(prhs[5]);
a = ap[0];
b = bp[0];
/* Get number of VertexN */
VertexDims = mxGetDimensions(prhs[0]);
/* mexPrintf("%d %d\n",VertexDims[0],VertexDims[1]);*/
VertexN=VertexDims[0];
/* Reserve memory */
table_Dims[0]=VertexN; table_Dims[1]=3;
plhs[0]= mxCreateNumericArray(2, table_Dims, mxDOUBLE_CLASS, mxREAL);
V_tmp = mxCreateNumericArray(2, table_Dims, mxDOUBLE_CLASS, mxREAL);
/* Connect Outputs */
V_smooth = (double *)mxGetPr(plhs[0]);
V_smooth_tmp = (double *)mxGetPr(V_tmp);
memcpy(V_smooth,V, VertexDims[0]*VertexDims[1]*sizeof(double));
for (n=0; n<iterN; n++) {
memcpy(V_smooth_tmp,V_smooth, VertexDims[0]*VertexDims[1]*sizeof(double));
for (i=0; i<VertexN; i++) {
PneigMatlab=mxGetCell(Ne, i);
if( PneigMatlab == NULL)
{
PneigLength=-1;
}
else
{
PneigDims=(mwSize *)mxGetDimensions(PneigMatlab);
PneigLength=(PneigDims[0]*PneigDims[1]);
Pneig=(double *)mxGetPr(PneigMatlab);
}
x=0;
y=0;
z=0;
nx=0;
ny=0;
nz=0;
wsum=0;
PneigCount=0;
for (k=0; k<PneigLength; k++) {
t = V_smooth_tmp[(int)(Pneig[k]-1)];
if(mxIsFinite(t)) {
w = W[(int)(Pneig[k]-1)];
nx += w*t;
ny += w*V_smooth_tmp[(int)((Pneig[k]-1) + VertexN*1)];
nz += w*V_smooth_tmp[(int)((Pneig[k]-1) + VertexN*2)];
wsum += w;
PneigCount ++;
}
}
/*
if(PneigCount > 0)
{
nx/=PneigCount;
ny/=PneigCount;
nz/=PneigCount;
}*/
if(wsum > 0)
{
nx/=wsum;
ny/=wsum;
nz/=wsum;
}
t = V_smooth_tmp[i];
if(mxIsFinite(t))
{
w = W[i];
x = w*t;
y = w*V_smooth_tmp[(int)(i + VertexN*1)];
z = w*V_smooth_tmp[(int)(i + VertexN*2)];
/*PneigCount ++;*/
}
if(PneigCount > 0)
{
V_smooth[i] = (1-a)*x + a*nx;
V_smooth[(int)(i + VertexN*1)] = (1-a)*y + a*ny;
V_smooth[(int)(i + VertexN*2)] = (1-a)*z + a*nz;
} else {
V_smooth[i] = V_smooth_tmp[i];
V_smooth[(int)(i + VertexN*1)] = V_smooth_tmp[(int)(i + VertexN*1)];
V_smooth[(int)(i + VertexN*2)] = V_smooth_tmp[(int)(i + VertexN*2)];
/* If the center vertex and ALL of its neighbors are NaN, put NaN there
* .... oh wait... that would be the same as putting V[i] back */
/* V_diffuse[i] = mxGetNaN(); */
}
}
}
mxDestroyArray(V_tmp);
/* Remove temporary memory */
}