read .off file in matlab

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.



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值