您的位置:首页 > 编程语言 > MATLAB

read .off file in matlab

2016-05-02 20:39 896 查看
OFF 文件格式如下:

OFF
441 800 0
0 0 0
0.05 0 0
0.1 0 0
0.15 0 0
...
0.9 1 0
0.95 1 0
1 1 0
3 0 1 22
3 0 22 21
...
3 397 398 419
3 397 419 418
3 418 419 440
3 418 440 439


matlab文件如下:

function [V,F,UV,C,N] = readOFF( filename )
% READOFF reads an OFF file with vertex/face information
%
% [V,F,UV,C,N] = readOFF( filename )
%
% Input:
% filename path to .obj file
% Outputs:
% V #V by 3 list of vertices
% F #F by 3 list of triangle indices
% UV #V by 2 list of texture coordinates
% C #V by 3 list of colors
% N #V by 3 list of normals
%
% See also: load_mesh, readOBJfast, readOBJ

% (C) 2007 Denis Kovacs, NYU
%-------------------------------------------------------------------------

V = [];
F = [];
UV = [];
C = [];
N = [];

fp = fopen( filename, 'r' );
OFFheader = upper(fscanf( fp, '%s\n', 1 ));
if (OFFheader(end-2:end) ~= 'OFF') warning('no OFF file!'); return; end
OFFdim = 3;
OFF_N = 0; OFF_C=0; OFF_ST=0;

if find(OFFheader=='N') OFFdim = OFFdim+3; OFF_N=1; end
if find(OFFheader=='C') OFFdim = OFFdim+3; OFF_C=1; end
if find(OFFheader=='S') OFFdim = OFFdim+2; OFF_ST=1; end

d = fscanf( fp, '%d', 3);
nV = d(1); nF = d(2); nE = d(3);

disp(sprintf(' - Reading %d vertices', nV));

switch OFFdim
case 3; OFFV = textscan( fp, '%f %f %f', nV);
case 5; OFFV = textscan( fp, '%f %f %f %f %f', nV);
case 6; OFFV = textscan( fp, '%f %f %f %f %f %f', nV);
case 7; OFFV = textscan( fp, '%f %f %f %f %f %f %f', nV);
case 8; OFFV = textscan( fp, '%f %f %f %f %f %f %f %f', nV);
case 9; OFFV = textscan( fp, '%f %f %f %f %f %f %f %f %f', nV);
case 10; OFFV = textscan( fp, '%f %f %f %f %f %f %f %f %f %f', nV);
case 11; OFFV = textscan( fp, '%f %f %f %f %f %f %f %f %f %f %f', nV);
otherwise; error('Unsupported number of vertex entries');
end

try
OFFV = cell2mat(OFFV);
end

OFFdim = 1;
V = OFFV(:,OFFdim:(OFFdim+2)); OFFdim = OFFdim + 3;
if (OFF_N) N = OFFV(:,OFFdim:(OFFdim+2)); OFFdim = OFFdim + 3; end
if (OFF_C) C = OFFV(:,OFFdim:(OFFdim+2)); OFFdim = OFFdim + 3; end
if (OFF_ST) UV = OFFV(:,OFFdim:(OFFdim+1)); OFFdim = OFFdim + 2; end

if (nF ~= 0)
disp(sprintf(' - Reading %d faces', nF));
F = cell2mat( textscan( fp, '%d %d %d %d %d %d', nF ) );
F = double(F(:, 2:(F(1,1)+1) ) + 1 );
else
F = [];
end

disp(' - done.');
end


执行命令:

[V,F,UV,C,N] = readOFF( 'square_21.off' )我们得到
  - Reading 441 vertices

  - Reading 800 faces

  - done.

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: