-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtri_mesh_to_ply.m
106 lines (100 loc) · 3.08 KB
/
tri_mesh_to_ply.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
function ply_data = tri_mesh_to_ply ( node_xyz, triangle_node )
%*****************************************************************************80
%
%% TRI_MESH_TO_PLY converts TRI_MESH data to PLY form.
%
% Discussion:
%
% This routine converts data from the TRI_MESH to PLY format.
% Specifically, given a 3 by NODE_NUM array of node coordinates,
% and a 3 by TRIANGLE_NUM array of node indices that form triangles,
% the routine creates a MATLAB data structure corresponding to
% the data in a PLY file.
%
% The PLY data can be written to a PLY file using the PLY_WRITE
% routine.
%
% Example:
%
% The following TRI_MESH data describes the surface of a pyramid:
%
% node_xyz = [ 0,0,0; 1,0,0; 1,1,0; 0,1,0; 0.5,0.5,1.6 ]';
%
% triangle_node = [ 2,1,4; 2,4,3; 1,2,5; 1,5,4; 4,5,3; 2,3,5 ]';
%
% It can be viewed with these MATLAB commands:
%
% trisurf ( triangle_node', node_xyz(1,:), node_xyz(2,:), node_xyz(3,:) );
% axis equal;
%
% The TRI_MESH data can be converted to PLY format:
%
% ply_data = tri_mesh_to_ply ( node_xyz, triangle_node );
%
% The PLY data can be written to a PLY file.
%
% ply_write ( ply_data, 'pyramid.ply', 'ascii' );
%
% Licensing:
%
% This code is distributed under the GNU LGPL license.
%
% Modified:
%
% 28 February 2007
%
% Author:
%
% Pascal Getreuer, 2004
% Modifications by John Burkardt
%
% Parameters:
%
% Input, real NODE_XYZ(3,NODE_NUM) is the coordinates of the nodes.
%
% Input, integer TRIANGLE_NODE(3,TRIANGLE_NUM) is the node indices that
% form each triangle. The node indices are 1-based.
%
% Output, data structure PLY_DATA, the information
% about the triangular mesh, suitable for storage as a PLY file.
%
if ( nargin < 2 )
fprintf ( 1, '\n' );
fprintf ( 1, 'TRI_MESH_TO_PLY - Fatal error!\n' );
fprintf ( 1, ' Not enough input arguments.\n' );
fprintf ( 1, ' Input must include NODE_XYZ and TRIANGLE_NODE.\n' );
error ( 'TRI_MESH_TO_PLY - Not enough input arguments.' );
end
if ( size ( node_xyz, 1 ) ~= 3 )
fprintf ( 1, '\n' );
fprintf ( 1, 'TRI_MESH_TO_PLY - Fatal error!\n' );
fprintf ( 1, ' NODE_XYZ must have 3 rows.\n' );
error ( 'TRI_MESH_TO_PLY - NODE_XYZ must have 3 rows.' );
end
if ( size ( triangle_node, 1 ) ~= 3 )
fprintf ( 1, '\n' );
fprintf ( 1, 'TRI_MESH_TO_PLY - Fatal error!\n' );
fprintf ( 1, ' TRIANGLE_NODE must have 3 rows.\n' );
error ( 'TRI_MESH_TO_PLY - TRIANGLE_NODE must have 3 rows.' );
end
%
% Store the coordinates of the vertices into the PLY_DATA structure.
%
node_num = size ( node_xyz, 2 );
ply_data.vertex.x = node_xyz(1,1:node_num);
ply_data.vertex.y = node_xyz(2,1:node_num);
ply_data.vertex.z = node_xyz(3,1:node_num);
%
% Store the indices of the vertices that form the (triangular)
% faces.
%
triangle_num = size ( triangle_node, 2 );
ply_data.face.vertex_indices = cell ( triangle_num, 1 );
%
% Since PLY uses 0-based indices, we need to subtract 1 from each.
%
for k = 1 : triangle_num
ply_data.face.vertex_indices{k} = triangle_node(:,k) - 1;
end
return
end