File: readoff.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 (72 lines) | stat: -rw-r--r-- 1,528 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
function [node, elem] = readoff(fname)
%
% [node,elem]=readoff(fname)
%
% read Geomview Object File Format (OFF)
%
% author: Qianqian Fang, <q.fang at neu.edu>
% date: 2008/03/28
%
% input:
%    fname: name of the OFF data file
%
% output:
%    node: node coordinates of the mesh
%    elem: list of elements of the mesh
%
% -- this function is part of iso2mesh toolbox (http://iso2mesh.sf.net)
%

node = [];
elem = [];
fid = fopen(fname, 'rb');
line = fgetl(fid);
dim = sscanf(line, 'OFF %d %d %d');
line = nonemptyline(fid);
if (size(dim, 1) ~= 3)
    dim = sscanf(line, '%d', 3);
    line = nonemptyline(fid);
end
nodalcount = 3;
if (~isempty(line))
    [val nodalcount] = sscanf(line, '%f', inf);
else
    fclose(fid);
    return
end
node = fscanf(fid, '%f', [nodalcount, dim(1) - 1])';
node = [val(:)'; node];

line = nonemptyline(fid);
facetcount = 4;
if (~isempty(line))
    [val facetcount] = sscanf(line, '%f', inf);
else
    fclose(fid);
    return
end
elem = fscanf(fid, '%f', [facetcount, dim(2) - 1])';
elem = [val(:)'; elem];
fclose(fid);
elem(:, 1) = [];

if (size(elem, 2) <= 3)
    elem(:, 1:3) = round(elem(:, 1:3)) + 1;
else
    elem(:, 1:4) = round(elem(:, 1:4)) + 1;
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function str = nonemptyline(fid)
str = '';
if (fid == 0)
    error('invalid file');
end
while ((isempty(regexp(str, '\S')) || ~isempty(regexp(str, '^#')))  && ~feof(fid))
    str = fgetl(fid);
    if (~ischar(str))
        str = '';
        return
    end
end