geomview_read_off - Read GeomView .off mesh file format [vertices,faces] = geomview_read_off(file) This function will load an ascii file that contains a one line specification of the 'OFF' file format followed by another line to specify number of vertices, faces and edges, followed by rows of vertex points and then rows of face indices into the vertex rows. Each vertex row contains x,y,z coordinates. Each face row contains the number of vertices in the face and then the vertex indices. Vertices in the .off file are indexed from zero, but those returned are indexed from one. See http://www.geomview.org/docs/oogltour.html The returned values can be input to the patch command, like so: Hpatch = patch('Vertices',vertices,'Faces',faces,... 'EdgeColor',[.8 .8 .8],'FaceColor',[0.9 0.9 0.9]); This will plot the mesh as a patch object. See the patch command and matlab help for more information on coloring this object.
0001 function [vertices,faces] = geomview_read_off(file) 0002 0003 % geomview_read_off - Read GeomView .off mesh file format 0004 % 0005 % [vertices,faces] = geomview_read_off(file) 0006 % 0007 % This function will load an ascii file that contains a one line 0008 % specification of the 'OFF' file format followed by another line to 0009 % specify number of vertices, faces and edges, followed by rows of vertex 0010 % points and then rows of face indices into the vertex rows. Each vertex 0011 % row contains x,y,z coordinates. Each face row contains the number of 0012 % vertices in the face and then the vertex indices. Vertices in the .off 0013 % file are indexed from zero, but those returned are indexed from 0014 % one. 0015 % 0016 % See http://www.geomview.org/docs/oogltour.html 0017 % 0018 % The returned values can be input to the patch command, like so: 0019 % 0020 % Hpatch = patch('Vertices',vertices,'Faces',faces,... 0021 % 'EdgeColor',[.8 .8 .8],'FaceColor',[0.9 0.9 0.9]); 0022 % 0023 % This will plot the mesh as a patch object. See the patch command 0024 % and matlab help for more information on coloring this object. 0025 % 0026 0027 % $Revision: 1.1 $ $Date: 2004/11/12 01:32:35 $ 0028 0029 % Licence: GNU GPL, no implied or express warranties 0030 % History: 11/2004 Darren.Weber_at_radiology.ucsf.edu 0031 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0032 0033 fid = fopen(file,'r'); 0034 0035 if isequal(fid,-1), 0036 S=sprintf('Could not open file: "%s"',file); 0037 error(S); 0038 else 0039 0040 fprintf('...Reading GeomView .off mesh file\n'); 0041 0042 tic; 0043 0044 % Check for comment on first line of file 0045 frewind(fid); temp = fgetl(fid); frewind(fid); 0046 if findstr(temp,'OFF'), 0047 % good, we have the right format file 0048 temp = fgetl(fid); 0049 else 0050 error('This is not a GeomView .off file, the first line must contain the header ''OFF'''); 0051 end 0052 0053 % Read number of vertices/faces/edges 0054 Nvertices = fscanf(fid,'%d',1); 0055 Nfaces = fscanf(fid,'%d',1); 0056 Nedges = fscanf(fid,'%d',1); 0057 0058 % Read vertices 0059 fprintf('...Reading %d Vertices\n',Nvertices); 0060 vertices = fscanf(fid,'%f',[3,Nvertices]); 0061 % select first 3 rows and translate 0062 vertices = vertices(1:3,:)'; 0063 0064 % Read faces 0065 fprintf('...Reading %d Faces\n',Nfaces); 0066 for f = 1:Nfaces, 0067 Nvert = fscanf(fid,'%d',1); 0068 faces(f,:) = fscanf(fid,'%d',Nvert); 0069 end 0070 % add 1 because GeomView vertices start at zero 0071 faces = faces + 1; 0072 0073 fclose(fid); 0074 0075 t = toc; 0076 fprintf('...done (%6.2f sec).\n',t); 0077 0078 end 0079 0080 return 0081 0082 0083 0084 % "dodec.off": 0085 % 0086 % OFF 0087 % 20 12 30 0088 % 1.214124 0.000000 1.589309 0089 % 0.375185 1.154701 1.589309 0090 % -0.982247 0.713644 1.589309 0091 % -0.982247 -0.713644 1.589309 0092 % 0.375185 -1.154701 1.589309 0093 % 1.964494 0.000000 0.375185 0094 % 0.607062 1.868345 0.375185 0095 % -1.589309 1.154701 0.375185 0096 % -1.589309 -1.154701 0.375185 0097 % 0.607062 -1.868345 0.375185 0098 % 1.589309 1.154701 -0.375185 0099 % -0.607062 1.868345 -0.375185 0100 % -1.964494 0.000000 -0.375185 0101 % -0.607062 -1.868345 -0.375185 0102 % 1.589309 -1.154701 -0.375185 0103 % 0.982247 0.713644 -1.589309 0104 % -0.375185 1.154701 -1.589309 0105 % -1.214124 0.000000 -1.589309 0106 % -0.375185 -1.154701 -1.589309 0107 % 0.982247 -0.713644 -1.589309 0108 % 5 0 1 2 3 4 0109 % 5 0 5 10 6 1 0110 % 5 1 6 11 7 2 0111 % 5 2 7 12 8 3 0112 % 5 3 8 13 9 4 0113 % 5 4 9 14 5 0 0114 % 5 15 10 5 14 19 0115 % 5 16 11 6 10 15 0116 % 5 17 12 7 11 16 0117 % 5 18 13 8 12 17 0118 % 5 19 14 9 13 18 0119 % 5 19 18 17 16 15 0120 % 0121 % The "OFF" header tells us it's a polylist file. The second line in the 0122 % file tells us that there are 20 vertices, 12 faces, and 30 edges. (The 0123 % OOGL libraries presently don't use the edges value, so you can just use 0 0124 % if you don't happen know the number of edges.) The next 20 lines give a 0125 % list of vertices. The last 12 lines specify the faces: the first number 0126 % is the number of vertices in that face. Since our polyhedron happens to 0127 % be regular, all faces have the same number of vertices (in this case, 5). 0128 % The rest of the numbers on the line are indices into the above list of 0129 % vertices. 0130