0001 function [V, F, xxn] = vb_surf_smooth(V,F,xx,Para)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036 normal_mode = 1;
0037
0038 if ~exist('Para','var'), Para = []; end
0039 if ~isfield(Para,'Nloop'), Para.Nloop = 50; end
0040 if ~isfield(Para,'tangent_rate'), Para.tangent_rate = 0.3; end
0041 if isfield(Para,'normal_mode'), normal_mode = Para.normal_mode; end;
0042
0043 tangent_rate = Para.tangent_rate;
0044
0045 Nloop = Para.Nloop;
0046
0047 Npoint = size(V,1);
0048 Npatch = size(F,1);
0049
0050
0051 V1 = zeros(Npatch,3);
0052 V2 = zeros(Npatch,3);
0053 V3 = zeros(Npatch,3);
0054
0055 VV1 = zeros(Npatch,3);
0056 VV2 = zeros(Npatch,3);
0057 VV3 = zeros(Npatch,3);
0058
0059
0060 fd = zeros(Npoint,3);
0061
0062 Nv = zeros(Npoint,1);
0063
0064 nnf = zeros(Npoint,1);
0065
0066
0067 F1 = F(:,1);
0068 F2 = F(:,2);
0069 F3 = F(:,3);
0070
0071
0072 xxf = vb_cross2( V(F2,:)-V(F1,:) , V(F3,:)-V(F1,:) );
0073
0074
0075 xxk = xx(F1,:)+xx(F2,:)+xx(F3,:);
0076
0077 xdot = sum( xxk .* xxf ,2);
0078 ix = find(sign(xdot) < 0 );
0079
0080
0081
0082 F(ix,2) = F3(ix);
0083 F(ix,3) = F2(ix);
0084
0085 F2 = F(:,2);
0086 F3 = F(:,3);
0087
0088
0089 for n=1:Npatch,
0090 inx = F(n,:)';
0091 Nv(inx) = Nv(inx) + 2;
0092 end;
0093
0094
0095
0096 for i=1:Nloop,
0097
0098
0099 V1=V(F1,:);
0100 V2=V(F2,:);
0101 V3=V(F3,:);
0102
0103
0104 xxf = vb_cross2( V2 - V1, V3 - V1 );
0105
0106
0107 xxf = vb_repmultiply(xxf, 1./sqrt(sum(xxf.^2,2)));
0108
0109
0110 VV1 = V2 + V3 - 2*V1;
0111 VV2 = V3 + V1 - 2*V2;
0112 VV3 = V1 + V2 - 2*V3;
0113
0114
0115 xxn = zeros(Npoint,3);
0116
0117 fd = zeros(Npoint,3);
0118
0119 for n=1:Npatch,
0120
0121 j1=F1(n);
0122 j2=F2(n);
0123 j3=F3(n);
0124
0125
0126 xxn(j1,:) = xxn(j1,:) + xxf(n,:);
0127 xxn(j2,:) = xxn(j2,:) + xxf(n,:);
0128 xxn(j3,:) = xxn(j3,:) + xxf(n,:);
0129
0130
0131 fd(j1,:) = fd(j1,:) + VV1(n,:);
0132 fd(j2,:) = fd(j2,:) + VV2(n,:);
0133 fd(j3,:) = fd(j3,:) + VV3(n,:);
0134 end;
0135
0136
0137 xxn = vb_repmultiply(xxn, 1./sqrt(sum(xxn.^2,2)));
0138
0139
0140 fd = vb_repmultiply(fd, 1./Nv);
0141
0142
0143 ds = mean( sum(fd .* xxn, 2) );
0144
0145
0146 if normal_mode==1
0147
0148 V = V + tangent_rate * (fd - ds*xxn);
0149 else
0150
0151 V = V + tangent_rate * fd ;
0152 end
0153 end
0154
0155 if nargout < 3, return; end;
0156
0157
0158 V1=V(F1,:);
0159 V2=V(F2,:);
0160 V3=V(F3,:);
0161
0162
0163 xxf = vb_cross2( V2 - V1, V3 - V1 );
0164
0165
0166 xxf = vb_repmultiply(xxf, 1./sqrt(sum(xxf.^2,2)));
0167
0168
0169 xxn = zeros(Npoint,3);
0170
0171 for n=1:Npatch,
0172
0173 j1=F1(n);
0174 j2=F2(n);
0175 j3=F3(n);
0176
0177 xxn(j1,:) = xxn(j1,:) + xxf(n,:);
0178 xxn(j2,:) = xxn(j2,:) + xxf(n,:);
0179 xxn(j3,:) = xxn(j3,:) + xxf(n,:);
0180 end;
0181
0182
0183 xxn = vb_repmultiply(xxn, 1./sqrt(sum(xxn.^2,2)));