0001 function mesh_vertex_smooth_check(FV, index),
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025 if isfield(FV,'edges'),
0026 if isempty(FV.edges),
0027 FV.edges = mesh_edges(FV);
0028 end
0029 else
0030 FV.edges = mesh_edges(FV);
0031 end
0032
0033
0034
0035 radius_min = 3.33;
0036 radius_max = 10.00;
0037
0038
0039 E = mean([(1 / radius_min), (1 / radius_max)]);
0040 F = 6 * ( (1 / radius_min) - (1 / radius_max) );
0041
0042 Nvert = size(FV.vertices,1);
0043
0044 if ~exist('index','var'),
0045 p = randperm(Nvert);
0046 index = p(1)
0047 end
0048 if isempty(index),
0049 p = randperm(Nvert);
0050 index = p(1)
0051 end
0052
0053 vi = index(1);
0054
0055 v = FV.vertices(vi,:);
0056
0057
0058 neighbour_index = find(FV.edges(vi,:));
0059
0060
0061
0062 edge_distance = full(FV.edges(vi,neighbour_index));
0063 L = mean(edge_distance);
0064
0065
0066
0067
0068 neighbour_vertices = FV.vertices(neighbour_index,:);
0069 Nmean = mean(neighbour_vertices);
0070
0071
0072
0073
0074
0075 s = Nmean - v;
0076
0077
0078 vertNormal = surf_normal(FV, vi)
0079
0080 unit_normal = vertNormal / norm(vertNormal);
0081
0082
0083
0084 sn = dot( s, unit_normal ) * unit_normal;
0085
0086
0087
0088 st = s - sn;
0089
0090
0091 r = L^2 / (2 * norm(sn));
0092
0093
0094 f2 = (1 + tanh( F * (1 / r - E))) / 2;
0095
0096
0097
0098
0099
0100
0101
0102
0103 vn = sum([ v; st / 2; f2 * sn ])
0104
0105
0106 [faceIndexI,faceIndexJ] = find(FV.faces == vi);
0107 f = faceIndexI(1);
0108
0109 vi1 = FV.faces(f,1);
0110 vi2 = FV.faces(f,2);
0111 vi3 = FV.faces(f,3);
0112
0113 v1 = FV.vertices(vi1,:);
0114 v2 = FV.vertices(vi2,:);
0115 v3 = FV.vertices(vi3,:);
0116
0117 vNorm = vertNormal;
0118
0119 figure;
0120 subplot(1,2,1); hold on
0121 patch('faces',FV.faces,'vertices',FV.vertices,...
0122 'facecolor',[.8 .8 .8],'edgecolor',[.4 .4 .4],'facealpha',0.8)
0123 [faceIndexI,faceIndexJ] = find(FV.faces == vi);
0124 patch('faces',FV.faces(faceIndexI,:),'vertices',FV.vertices,'facecolor',[.6 .6 .6])
0125 scatter3(v(1), v(2), v(3), 60, 'r', 'filled')
0126 tmp = neighbour_vertices;
0127 scatter3(tmp(:,1), tmp(:,2), tmp(:,3), 40, 'b', 'filled')
0128 scatter3(v1(1), v1(2), v1(3), 40, 'r', 'filled')
0129 scatter3(v2(1), v2(2), v2(3), 40, 'g', 'filled')
0130 scatter3(v3(1), v3(2), v3(3), 40, 'y', 'filled')
0131 quiver3(v(1), v(2), v(3), vNorm(1), vNorm(2), vNorm(3),'r')
0132 rotate3d on
0133
0134
0135
0136
0137 magnify = 10;
0138 vNorm = magnify * 10 * vertNormal;
0139 s = magnify * s;
0140 sn = magnify * sn;
0141 st = magnify * st;
0142
0143 subplot(1,2,2); hold on
0144 scatter3(v(1), v(2), v(3), 40, 'r', 'filled')
0145 scatter3(vn(1), vn(2), vn(3), 40, 'g', 'filled')
0146
0147 quiver3(v(1), v(2), v(3), vNorm(1), vNorm(2), vNorm(3),0,'r')
0148 quiver3(v(1), v(2), v(3), s(1), s(2), s(3),0,'g')
0149 quiver3(v(1), v(2), v(3), sn(1), sn(2), sn(3),0,'b')
0150 quiver3(v(1)+sn(1), v(2)+sn(2), v(3)+sn(3), st(1), st(2), st(3),0,'y')
0151
0152 legend({'current vertex','new vertex','vertNorm','s','sn','st'},-1)
0153
0154 [faceIndexI,faceIndexJ] = find(FV.faces == vi);
0155 patch('faces',FV.faces(faceIndexI,:),'vertices',FV.vertices,'facecolor',[.6 .6 .6])
0156 tmp = neighbour_vertices;
0157 scatter3(tmp(:,1), tmp(:,2), tmp(:,3), 40, 'b', 'filled')
0158 scatter3(v1(1), v1(2), v1(3), 40, 'r', 'filled')
0159 scatter3(v2(1), v2(2), v2(3), 40, 'c', 'filled')
0160 scatter3(v3(1), v3(2), v3(3), 40, 'y', 'filled')
0161
0162 rotate3d on
0163
0164
0165
0166
0167
0168
0169
0170
0171
0172 return
0173
0174
0175
0176
0177 function vertNormal = surf_normal(FV, vi),
0178
0179
0180
0181
0182
0183
0184
0185
0186
0187
0188
0189 [faceIndexI,faceIndexJ] = find(FV.faces == vi);
0190
0191 nface = length(faceIndexI);
0192
0193 faceNormals = zeros(nface,3);
0194
0195 for i = 1:nface,
0196
0197 f = faceIndexI(i);
0198
0199 i1 = FV.faces(f,1);
0200 i2 = FV.faces(f,2);
0201 i3 = FV.faces(f,3);
0202
0203 v1 = FV.vertices(i1,:);
0204 v2 = FV.vertices(i2,:);
0205 v3 = FV.vertices(i3,:);
0206
0207 edge1 = v2 - v1;
0208 edge2 = v3 - v1;
0209
0210
0211
0212
0213 faceNormals(i,:) = cross( edge2, edge1 );
0214
0215
0216
0217
0218
0219
0220 end
0221
0222
0223
0224
0225
0226
0227
0228
0229
0230
0231
0232 vertNormal = sum(faceNormals) / nface;
0233
0234 return