0001 function [V, F, xxn] = vb_surf_smooth_fit(V,F,xx,B,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
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050 if ~exist('Para','var'), Para = []; end
0051 if ~isfield(Para,'Nloop'), Para.Nloop = 200; end
0052 if ~isfield(Para,'Ndisp'), Para.Ndisp = Para.Nloop+1; end
0053 if ~isfield(Para,'vstep'), Para.vstep = 1; end
0054 if ~isfield(Para,'tangent_rate'), Para.tangent_rate = 0.3; end
0055 if ~isfield(Para,'mask_ratio'), Para.mask_ratio = 0.5; end
0056 if ~isfield(Para,'mask_threshold'), Para.mask_threshold = 0.3; end
0057
0058 tangent_rate = Para.tangent_rate;
0059 image_rate = Para.mask_ratio;
0060 threshold = Para.mask_threshold;
0061 step = Para.vstep;
0062
0063 Nloop = Para.Nloop;
0064 Npoint = size(V,1);
0065 Npatch = size(F,1);
0066 [NX,NY,NZ] = size(B);
0067
0068
0069 Ndisp = Para.Ndisp;
0070 Nfig = fix(Nloop/Ndisp);
0071
0072 if Nfig < 2,
0073 NYfig = 1;
0074 else
0075 NYfig = 2;
0076 end
0077 NXfig = max(ceil(Nfig/NYfig), 1);
0078 nfig = NXfig*NYfig + 1;
0079
0080 fclr = [];
0081 eclr = [];
0082 light_mode = 1;
0083 vangle = [-70 20];
0084
0085
0086 xxf = zeros(Npatch,3);
0087
0088 nns = zeros(Npatch,1);
0089
0090 VV1 = zeros(Npatch,3);
0091 VV2 = zeros(Npatch,3);
0092 VV3 = zeros(Npatch,3);
0093
0094
0095 xxn = zeros(Npoint,3);
0096 xxs = zeros(Npoint,1);
0097
0098 fd = zeros(Npoint,3);
0099
0100 Nv = zeros(Npoint,1);
0101
0102 nnf = zeros(Npoint,1);
0103
0104 ft = zeros(Npoint,3);
0105
0106 fn = zeros(Npoint,3);
0107
0108 fg = zeros(Npoint,3);
0109
0110
0111 jv = zeros(Npoint,3);
0112 jj = zeros(Npoint,1);
0113
0114 gg = zeros(Npoint,1);
0115
0116
0117 F1 = F(:,1);
0118 F2 = F(:,2);
0119 F3 = F(:,3);
0120
0121
0122 xxf = vb_cross2( V(F2,:)-V(F1,:) , V(F3,:)-V(F1,:) );
0123
0124
0125 xxk = xx(F1,:)+xx(F2,:)+xx(F3,:);
0126
0127 xdot = sum( xxk .* xxf ,2);
0128 ix = find(sign(xdot) < 0 );
0129
0130
0131
0132 F(ix,2) = F3(ix);
0133 F(ix,3) = F2(ix);
0134
0135 F2 = F(:,2);
0136 F3 = F(:,3);
0137
0138
0139 for n=1:Npatch,
0140 inx = F(n,:)';
0141 Nv(inx) = Nv(inx) + 2;
0142 end;
0143
0144
0145
0146 for i=1:Nloop,
0147
0148
0149 V1=V(F1,:);
0150 V2=V(F2,:);
0151 V3=V(F3,:);
0152
0153
0154 xxf = vb_cross2( V2 - V1, V3 - V1 );
0155
0156
0157 nns = sqrt(sum(xxf.^2,2));
0158 xxf = xxf./nns(:,ones(1,3));
0159
0160
0161 VV1 = V2 + V3 - 2*V1;
0162 VV2 = V3 + V1 - 2*V2;
0163 VV3 = V1 + V2 - 2*V3;
0164
0165
0166 xxn = zeros(Npoint,3);
0167
0168 fd = zeros(Npoint,3);
0169
0170 for n=1:Npatch,
0171
0172 j1=F1(n);
0173 j2=F2(n);
0174 j3=F3(n);
0175
0176 xxn(j1,:) = xxn(j1,:) + xxf(n,:);
0177 xxn(j2,:) = xxn(j2,:) + xxf(n,:);
0178 xxn(j3,:) = xxn(j3,:) + xxf(n,:);
0179
0180
0181 fd(j1,:) = fd(j1,:) + VV1(n,:);
0182 fd(j2,:) = fd(j2,:) + VV2(n,:);
0183 fd(j3,:) = fd(j3,:) + VV3(n,:);
0184 end;
0185
0186
0187 xxs = sqrt(sum(xxn.^2,2));
0188 xxn = xxn./xxs(:,ones(1,3));
0189
0190
0191 fd = fd./Nv(:,ones(1,3));
0192
0193
0194 jv = floor(V/step + 0.5) + 1;
0195
0196 ixv = find(jv(:,1) > 0 & jv(:,1) <= NX ...
0197 & jv(:,2) > 0 & jv(:,2) <= NY ...
0198 & jv(:,3) > 0 & jv(:,3) <= NZ);
0199
0200
0201 jj = jv(ixv,1) + NX*( (jv(ixv,2) - 1) + NY*(jv(ixv,3) - 1) );
0202
0203
0204 gg = zeros(Npoint,1);
0205 gg(ixv) = B(jj) - threshold;
0206
0207
0208 fg = xxn.*gg(:,ones(1,3));
0209
0210 V = V + tangent_rate*fd + image_rate*fg;
0211
0212 if rem(i,Ndisp)==0,
0213 if nfig > NYfig*NXfig
0214 figure;
0215 nfig=1;
0216 else
0217 nfig=nfig+1;
0218 end
0219 subplot(NYfig,NXfig,nfig);
0220 vb_plot_surf(V,F,fclr,eclr,light_mode);
0221 view(vangle);
0222 tlabel = sprintf('Iteration = %d',i);
0223 title(tlabel);
0224 end;
0225 end
0226