PLOT_PATCH - plots a vertex or face and its neighbors function plot_patch(FV,varargin); The arguments should be entered in pairs of: 'faces',indices, 'vertices',indices, 'figure', handle, 'all', ignore If 'faces', then each face in the index is painted green with its immediate face neighbors. If 'vertices', then each vertex in the index is marked green with its immediate face neighbors. if 'figure', then the figure handle is used, otherwise a new one is made. If 'all', then the entire FV is displayed as a semitransparent gray background, and indices are ignored See also TESSELLATION_STATS
0001 function plot_patch(FV,varargin); 0002 %PLOT_PATCH - plots a vertex or face and its neighbors 0003 % function plot_patch(FV,varargin); 0004 % The arguments should be entered in pairs of: 0005 % 'faces',indices, 0006 % 'vertices',indices, 0007 % 'figure', handle, 0008 % 'all', ignore 0009 % 0010 % If 'faces', then each face in the index is painted green with its immediate 0011 % face neighbors. 0012 % If 'vertices', then each vertex in the index is marked green with its 0013 % immediate face neighbors. 0014 % if 'figure', then the figure handle is used, otherwise a new one is made. 0015 % If 'all', then the entire FV is displayed as a semitransparent gray 0016 % background, and indices are ignored 0017 % 0018 % See also TESSELLATION_STATS 0019 0020 %<autobegin> ---------------------- 21-May-2004 15:14:19 ----------------------- 0021 % --------- Automatically Generated Comments Block Using AUTO_COMMENTS --------- 0022 % 0023 % CATEGORY: Visualization 0024 % 0025 % At Check-in: $Author: psdlw $ $Revision: 1.1 $ $Date: 2004/11/12 01:32:35 $ 0026 % 0027 % This software is part of BrainStorm Toolbox Version 2.0 (Alpha) 19-May-2004 0028 % 0029 % Principal Investigators and Developers: 0030 % ** Richard M. Leahy, PhD, Signal & Image Processing Institute, 0031 % University of Southern California, Los Angeles, CA 0032 % ** John C. Mosher, PhD, Biophysics Group, 0033 % Los Alamos National Laboratory, Los Alamos, NM 0034 % ** Sylvain Baillet, PhD, Cognitive Neuroscience & Brain Imaging Laboratory, 0035 % CNRS, Hopital de la Salpetriere, Paris, France 0036 % 0037 % See BrainStorm website at http://neuroimage.usc.edu for further information. 0038 % 0039 % Copyright (c) 2004 BrainStorm by the University of Southern California 0040 % This software distributed under the terms of the GNU General Public License 0041 % as published by the Free Software Foundation. Further details on the GPL 0042 % license can be found at http://www.gnu.org/copyleft/gpl.html . 0043 % 0044 % FOR RESEARCH PURPOSES ONLY. THE SOFTWARE IS PROVIDED "AS IS," AND THE 0045 % UNIVERSITY OF SOUTHERN CALIFORNIA AND ITS COLLABORATORS DO NOT MAKE ANY 0046 % WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO WARRANTIES OF 0047 % MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, NOR DO THEY ASSUME ANY 0048 % LIABILITY OR RESPONSIBILITY FOR THE USE OF THIS SOFTWARE. 0049 %<autoend> ------------------------ 21-May-2004 15:14:19 ----------------------- 0050 0051 % ----------------------------- Script History --------------------------------- 0052 % JCM 15-May-2004 Creation 0053 % ----------------------------- Script History --------------------------------- 0054 0055 0056 0057 METHODS = varargin(1:2:end); 0058 Indices = varargin(2:2:end); 0059 0060 0061 % first calculate what faces go with which vertex 0062 0063 numTri = size(FV.faces,1); % number of faces 0064 numVert = size(FV.vertices,1); % number of vertices 0065 0066 [VertexNumbering,I] = sort(FV.faces(:)); % sorted Vertex numbers 0067 0068 FacesNumbering = rem(I-1,numTri)+1; % triangle number for each Vertex 0069 0070 % For the ith vertex, then FacesNumbering(VertexNumbering == i) returns the indices of the 0071 % polygons attached to the ith vertex. 0072 % 0073 % For the set of vertices in the row vector sv (e.g sv = [3 5 115 121]), then use 0074 % [i,ignore] = find(VertexNumbering(:,ones(1,length(sv))) == (ones(size(VertexNumbering,1),1)*sv)); 0075 % (compares the Vertex numbers to the indices, extracts the row indices into i) 0076 % then FacesNumbering(i) returns the indices. Apply unique to clean up. 0077 0078 % So now we know what faces are connected to each vertex 0079 0080 0081 % --------------------------- Triangle Statistics ------------------------------ 0082 % calculate the area and normals for each triangle 0083 0084 Vertices = FV.vertices(FV.faces',:); 0085 % each set of three rows of Vertices is one triangle 0086 0087 % now difference them to get the vectors on two sides 0088 dVertices = diff(Vertices); 0089 dVertices(3:3:end,:) = []; % remove the transition between triangles 0090 0091 % now each pair of rows in dVertices represents each triangle 0092 % row 1 is vector from 1 to 2 0093 % row 2 is vector from 2 to 3 0094 0095 % v1 = dVertices(1:2:end,:)'; % each column is side one of a triangle 0096 % v2 = dVertices(2:2:end,:)'; % side two 0097 0098 % right-hand rule, counter-clockwise ordering of the triangle yields a positive 0099 % upward area and normal. 0100 % Call a fast subfunction of this function: 0101 WeightedNormals = cross(dVertices(1:2:end,:)',dVertices(2:2:end,:)')/2; 0102 % each column is the normal for each triangle 0103 % the length the vector gives the area 0104 0105 FaceArea = sqrt(sum(WeightedNormals .* WeightedNormals)); % the area 0106 FaceNormal = WeightedNormals ./ (FaceArea([1 1 1],:)); % normalize them 0107 0108 % now calculate the centers of each triangle 0109 FaceCenter = cumsum(Vertices); 0110 FaceCenter = FaceCenter(3:3:end,:); % every third summation for every triangle 0111 FaceCenter = diff([0 0 0;FaceCenter])'/3; % average of each summation 0112 % each column is the mean of the vertices of the triangles 0113 % so now we know the center, area, and the normal vector of each triangle 0114 0115 0116 ih = find(strcmp('figure',varargin)); 0117 if ~isempty(ih), 0118 hf = figure(varargin{ih+1}); 0119 else 0120 hf = figure; % open new figure 0121 end 0122 0123 hold on 0124 0125 for imethod = 1:length(METHODS), 0126 0127 switch lower(METHODS{imethod}) 0128 case 'faces' 0129 0130 iFace = FV.faces([Indices{imethod}],:); 0131 iFace = iFace(:)'; % ensure row vector 0132 0133 % get the faces attached to these vertices 0134 % For the set of vertices in the row vector sv (e.g sv = [3 5 115 121]), then 0135 % use 0136 % [i,ignore] = find(VertexNumbering(:,ones(1,length(sv))) == (ones(size(VertexNumbering,1),1)*sv)); 0137 % (compares the Vertex numbers to the indices, extracts the row indices into i) 0138 % then FacesNumbering(i) returns the indices. 0139 0140 [fndx, ignore] = find(VertexNumbering(:,ones(1,length(iFace))) == (ones(size(VertexNumbering,1),1)*iFace)); 0141 fndx = FacesNumbering(fndx); % the faces attached to these vertices 0142 fndx = unique(fndx); % ensure unique 0143 nifndx = fndx(:)'; 0144 nifndx(intersect(nifndx,iFace)) = []; % remove the ith face 0145 0146 % not the ith face 0147 h = patch('vertices',FV.vertices,'faces',FV.faces(nifndx,:),'facecolor','r','edgecolor','k'); 0148 % the ith face 0149 hi = patch('vertices',FV.vertices,'faces',FV.faces([Indices{imethod}],:),'facecolor','g'); 0150 plot3(FaceCenter(1,fndx),FaceCenter(2,fndx),FaceCenter(3,fndx),'*') 0151 quiver3(FaceCenter(1,fndx),FaceCenter(2,fndx),FaceCenter(3,fndx),... 0152 FaceNormal(1,fndx),FaceNormal(2,fndx),FaceNormal(3,fndx),0.25); 0153 set(h,'FaceAlpha',.8) 0154 0155 case 'vertices' 0156 % plot each vertex with a green point and the immediate faces around it 0157 0158 % get the vertices 0159 iVert = [Indices{imethod}]; 0160 [fndx, ignore] = find(VertexNumbering(:,ones(1,length(iVert))) == (ones(size(VertexNumbering,1),1)*iVert)); 0161 fndx = FacesNumbering(fndx); % the faces attached to these vertices 0162 fndx = unique(fndx); % ensure unique 0163 0164 h = patch('vertices',FV.vertices,'faces',FV.faces(fndx,:),'facecolor','r','edgecolor','k'); 0165 hold on 0166 plot3(FaceCenter(1,fndx),FaceCenter(2,fndx),FaceCenter(3,fndx),'*'); 0167 plot3(FV.vertices(iVert,1),FV.vertices(iVert,2),FV.vertices(iVert,3),'g+'); 0168 ma = mean(FaceArea(fndx)); % mean area 0169 quiver3(FaceCenter(1,fndx),FaceCenter(2,fndx),FaceCenter(3,fndx),... 0170 FaceNormal(1,fndx),FaceNormal(2,fndx),FaceNormal(3,fndx),.25); 0171 set(h,'FaceAlpha',.8) 0172 0173 case 'all' 0174 h = patch(FV,... 0175 'facecolor', [.9 .9 .9],'edgecolor',[.8 .8 .8]); 0176 set(h,'facealpha',.5,'edgealpha',.5); 0177 otherwise 0178 % unknown method 0179 end 0180 end 0181 0182 axis equal 0183 axis vis3d 0184 0185 hold off 0186 cameratoolbar('Show'); % activate the camera toolbar 0187 ret = cameratoolbar; % for strange reason, this activates the default orbit mode. 0188 drawnow