-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathspm_mesh_normals.m
34 lines (27 loc) · 959 Bytes
/
spm_mesh_normals.m
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
function N = spm_mesh_normals(M, unit)
% Compute (unit) normals of a surface mesh
% FORMAT N = spm_mesh_normals(M)
% M - a patch structure or a handle to a patch
% unit - boolean to indicate unit normals or not [default: false]
%
% N - a [Nx3] array of (unit) normals on vertices
%__________________________________________________________________________
% Copyright (C) 2008 Wellcome Trust Centre for Neuroimaging
% Guillaume Flandin
% $Id: spm_mesh_normals.m 5903 2014-03-03 14:56:10Z guillaume $
if nargin < 2, unit = false; end
if ~ishandle(M)
f = figure('visible','off');
M = patch(M, 'parent',axes('parent',f), 'visible', 'off');
end
N = double(get(M,'VertexNormals'));
if isempty(N)
t = triangulation(double(get(M,'Faces')),double(get(M,'Vertices')));
N = -double(t.vertexNormal);
end
try, close(f); end
if unit
normN = sqrt(sum(N.^2,2));
normN(normN < eps) = 1;
N = N ./ repmat(normN,1,3);
end