-
Notifications
You must be signed in to change notification settings - Fork 0
/
plywrite.m
85 lines (73 loc) · 2.48 KB
/
plywrite.m
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
73
74
75
76
77
78
79
80
81
82
83
84
85
function plywrite(filename,faces,verts,varargin)
% plywrite(filename,faces,verts)
% Will write a face vertex mesh data in ply format.
% faces -> polygonal descriptions in terms of vertex indices
% verts -> list of vertex coordinate triplets
% faces and verts can be obtained by using the MATLAB isosurface function.
%
% plywrite(filename,faces,verts,rgb)
% Will add color information.
% rgb -> optional list of RGB triplets per vertex
%
% A by-product of ongoing computational materials science research
% at MINED@Gatech.(http://mined.gatech.edu/)
%
% Copyright (c) 2015, Ahmet Cecen and MINED@Gatech - All rights reserved.
% Create File
fileID = fopen(filename,'w');
% Plain Mesh
if nargin == 3
% Insert Header
fprintf(fileID, ...
['ply\n', ...
'format ascii 1.0\n', ...
'element vertex %u\n', ...
'property float32 x\n', ...
'property float32 y\n', ...
'property float32 z\n', ...
'element face %u\n', ...
'property list uint8 int32 vertex_indices\n', ...
'end_header\n'], ...
length(verts),length(faces));
% Insert Colored Vertices
for i=1:length(verts)
fprintf(fileID, ...
['%.6f ', ...
'%.6f ', ...
'%.6f\n'], ...
verts(i,1),verts(i,2),verts(i,3));
end
% Insert Faces
dlmwrite(filename,[size(faces,2)*ones(length(faces),1),faces-1],'-append','delimiter',' ','precision',10);
% Colored Mesh
elseif nargin == 4
rgb=varargin{1};
% Insert Header
fprintf(fileID, ...
['ply\n', ...
'format ascii 1.0\n', ...
'element vertex %u\n', ...
'property float32 x\n', ...
'property float32 y\n', ...
'property float32 z\n', ...
'property uchar red\n', ...
'property uchar green\n', ...
'property uchar blue\n', ...
'element face %u\n', ...
'property list uint8 int32 vertex_indices\n', ...
'end_header\n'], ...
length(verts),length(faces));
% Insert Colored Vertices
for i=1:length(verts)
fprintf(fileID, ...
['%.6f ', ...
'%.6f ', ...
'%.6f ', ...
'%u ', ...
'%u ', ...
'%u\n'], ...
verts(i,1),verts(i,2),verts(i,3),rgb(i,1),rgb(i,2),rgb(i,3));
end
% Insert Faces
dlmwrite(filename,[size(faces,2)*ones(length(faces),1),faces-1],'-append','delimiter',' ','precision',10);
end