-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCONVERT_meshformat.m
79 lines (58 loc) · 2.55 KB
/
CONVERT_meshformat.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
function [varargout] = CONVERT_meshformat(varargin)
%CONVERT_meshformat Convert mesh data from array to faces,vertices format or vice versa
%==========================================================================
% AUTHOR Adam H. Aitkenhead
% CONTACT [email protected]
% INSTITUTION The Christie NHS Foundation Trust
%
% USAGE [faces,vertices] = CONVERT_meshformat(meshXYZ)
% or... [meshXYZ] = CONVERT_meshformat(faces,vertices)
%
% IN/OUTPUTS meshXYZ - Nx3x3 array - An array defining the vertex
% positions for each of the N facets, with:
% 1 row for each facet
% 3 cols for the x,y,z coordinates
% 3 pages for the three vertices
%
% vertices - Nx3 array - A list of the x,y,z coordinates of
% each vertex in the mesh.
%
% faces - Nx3 array - A list of the vertices used in
% each facet of the mesh, identified using the row
% number in the array vertices.
%==========================================================================
%==========================================================================
% VERSION USER CHANGES
% ------- ---- -------
% 100817 AHA Original version
% 111104 AHA Housekeeping tidy-up.
%==========================================================================
if nargin==2 && nargout==1
faces = varargin{1};
vertex = varargin{2};
meshXYZ = zeros(size(faces,1),3,3);
for loopa = 1:size(faces,1)
meshXYZ(loopa,:,1) = vertex(faces(loopa,1),:);
meshXYZ(loopa,:,2) = vertex(faces(loopa,2),:);
meshXYZ(loopa,:,3) = vertex(faces(loopa,3),:);
end
varargout(1) = {meshXYZ};
elseif nargin==1 && nargout==2
meshXYZ = varargin{1};
vertices = [meshXYZ(:,:,1);meshXYZ(:,:,2);meshXYZ(:,:,3)];
vertices = unique(vertices,'rows');
faces = zeros(size(meshXYZ,1),3);
for loopF = 1:size(meshXYZ,1)
for loopV = 1:3
%[C,IA,vertref] = intersect(meshXYZ(loopF,:,loopV),vertices,'rows');
%The following 3 lines are equivalent to the previous line, but are much faster:
vertref = find(vertices(:,1)==meshXYZ(loopF,1,loopV));
vertref = vertref(vertices(vertref,2)==meshXYZ(loopF,2,loopV));
vertref = vertref(vertices(vertref,3)==meshXYZ(loopF,3,loopV));
faces(loopF,loopV) = vertref;
end
end
varargout(1) = {faces};
varargout(2) = {vertices};
end
end %function