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.
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.
相关文章推荐
- 交叉验证 matlab实现
- matlab usage: cellfun
- Matlab special usage : @ and Struct
- opencv、matlab查看函数的定义、封装
- LU分解(matlab实现)
- 基于小波变换的图像去噪matlab仿真
- MATLAB 的绘图:二维和三维
- 针对MATLAB(imfilter)翻译C语言(opencv)遇到的问题
- matlab .m文件的编写&使用
- matlab冒号的用法
- matlab--关机命令与断点命令
- YOLO训练之标注数据转化XML matlab编程
- matlab中的卷积——filter,conv之间的区别
- MATLAB如何定义函数
- C++ 调用Matlab画图
- matlab 信号与系统(一)—— 上采样(Upsampling)和下采样(Downsampling)
- 用MATLAB将矩阵数据写入txt文件中,打开乱码原因
- matlab小数取整函数
- 海明嵌入Hamming Embedding生成Binary Signature之matlab程序代码
- matlab机器人工具箱的安装和使用