-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.m
57 lines (49 loc) · 2.12 KB
/
example.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
%
% Make cross-sections of 3D objects defined in the Wavefront .obj file
%
% read the data file (Wavefront .obj file saved into a matlab structure, e.g., using read_wobj)
load( 'stomach+duodenum+pancreas.mat', 'O' );
% load( '12140_Skull_v3_L2.mat', 'O' );
% count the number of objects in the file
nobj = 0;
for i = 1 : length( O.objects )
if strcmp( O.objects( i ).type, 'o' ) || strcmp( O.objects( i ).type, 'g' )
nobj = nobj + 1;
end
end
fprintf( 'Found %d objects in the input file\n', nobj );
verts = O.vertices;
fprintf( 'Found %d vertices in the input file\n', size( verts, 1 ) );
% define cross-section planes
planes.n = [ 1 0 0; 0 1 0; 0 0 1 ]; % 3 cardinal crossections
planes.r = [ 0 0 2; 0 0 2; 0 0 2 ]; % plane origins at [ 0, 0, 2 ]
figure( 'Position', [ 100, 100, 800, 800 ] ), hold on;
axis vis3d equal;
view( -45, 45 );
xlabel( 'X', 'FontSize', 14 );
ylabel( 'Y', 'FontSize', 14 );
zlabel( 'Z', 'FontSize', 14 );
for i = 1 : length( O.objects )
if strcmp( O.objects( i ).type, 'o' ) || strcmp( O.objects( i ).type, 'g' ) % start of the new object or group
objectName = O.objects( i ).data;
disp( [ 'Processing object ' objectName ] ); % print out object name
elseif strcmp( O.objects( i ).type, 'f' ) % object faces block
faces = O.objects( i ).data.vertices;
patch( 'Faces', faces, 'Vertices', verts, 'FaceAlpha', 0, 'EdgeAlpha', 1 ); % display the mesh
% get intersection polygons for all planes
polygons = mesh_xsections( verts, faces, planes, [], 2 );
% draw the object polygons
for s = 1 : numel( polygons )
if ~isempty( polygons{ s } )
for p = 1 : numel( polygons{ s } )
patch( polygons{ s }{ p }( :, 1 ), ...
polygons{ s }{ p }( :, 2 ), ...
polygons{ s }{ p }( :, 3 ), 'y', 'FaceAlpha', 0.5 );
plot3( polygons{ s }{ p }( :, 1 ), ...
polygons{ s }{ p }( :, 2 ), ...
polygons{ s }{ p }( :, 3 ), 'r*' );
end
end
end
end
end