srf_recalcnormals.m

来自「toolbox of BVQX, This is the access betw」· M 代码 · 共 66 行

M
66
字号
function hfile = srf_RecalcNormals(hfile)
% SRF::RecalcNormals  - recalculate normals by using the right-hand rule
%
% FORMAT:       [srf] = srf.RecalcNormals;
%
% No input fields
%
% Output fields:
%
%       srf         altered object

% Version:  v0.7b
% Build:    7090215
% Date:     Sep-02 2007, 3:41 PM CEST
% Author:   Jochen Weber, Brain Innovation, B.V., Maastricht, NL
% URL/Info: http://wiki.brainvoyager.com/BVQXtools

% check arguments
if nargin ~= 1 || ...
    numel(hfile) ~= 1 || ...
   ~isBVQXfile(hfile, 'srf')
    error( ...
        'BVQXfile:BadArguments', ...
        'Invalid call to %s.', ...
        mfilename ...
    );
end
bc = bvqxfile_getcont(hfile.L);

% get coordinates and neighbors
crd = bc.VertexCoordinate;
nei = bc.Neighbors(:, 2);
ncd = size(crd, 1);
if ncd ~= numel(nei)
    error( ...
        'BVQXfile:BadObject', ...
        'Invalid object.' ...
    );
end

% generate some intermediate arrays for cross
csa = zeros(ncd, 12, 3);
csb = zeros(ncd, 12, 3);

% iterate over coordinates
for cc = 1:ncd
    
    % get neighbors and their number
    tne = nei{cc};
    tnn = numel(tne);
    
    % get coordinates of neighbors
    crn = crd(tne, :);
    
    % build vectors of triangles
    csa(cc, 1:tnn, :) = crn - crd(cc * ones(1, tnn), :);
    csb(cc, 1:tnn, :) = crn([2:tnn,1], :) - crn;
end

% compute cross products (normals)
crs = squeeze(mean(cross(csa, csb, 3), 2));

% resize to length and store
bc.VertexNormal = crs ./ (sqrt(sum(crs .* crs, 2)) * ones(1, 3));
bvqxfile_setcont(hfile.L, bc);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?