File: meshcentroid.m

package info (click to toggle)
octave-iso2mesh 1.9.8%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 11,128 kB
  • sloc: cpp: 11,982; ansic: 10,158; sh: 365; makefile: 59
file content (37 lines) | stat: -rw-r--r-- 1,012 bytes parent folder | download
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
35
36
37
function centroid = meshcentroid(v, f)
%
% centroid=meshcentroid(v,f)
%
% compute the centroids of a mesh defined by nodes and elements
% (surface or tetrahedra) in R^n space
%
% author: Qianqian Fang, <q.fang at neu.edu>
%
% input:
%      v: surface node list, dimension (nn,3)
%      f: surface face element list, dimension (be,3)
%
% output:
%      centroid: centroid positions, one row for each element
%
% -- this function is part of iso2mesh toolbox (http://iso2mesh.sf.net)
%

if (~iscell(f))
    ec = reshape(v(f(:, 1:size(f, 2))', :)', [size(v, 2) size(f, 2) size(f, 1)]);
    centroid = squeeze(mean(ec, 2))';
else
    len = length(f);
    centroid = zeros(len, size(v, 2));
    try
        for i = 1:len
            fc = f{i};
            if (~isempty(fc)) % need to set centroid to NaN if fc is empty?
                vlist = fc{1};
                centroid(i, :) = mean(v(vlist(find(~isnan(vlist))), :));
            end
        end
    catch
        error('malformed face cell array');
    end
end