Skip to content

Commit 0b5e344

Browse files
author
Steffen Schuler
committed
Added vtkTrisurf
1 parent 0546ac1 commit 0b5e344

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

MATLAB/vtkTrisurf.m

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
% Wrapper of MATLAB's trisurf function, allowing a reasonable
2+
% visualization of data on triangle meshes, optionally with isocontours.
3+
% All name-value pairs of trisurf() are available.
4+
% Addional parameters available:
5+
% 'ViewAngles', 'Limits', 'Steps', 'ShowAxes',
6+
% 'ShowContours', 'ContourColor', 'ContourWidth'
7+
%
8+
% Example:
9+
% h = vtkTrisurf(vtk, 'CData',data, 'ShowContours',true);
10+
%
11+
% Written in 2021 by Steffen Schuler
12+
% Institute of Biomedical Engineering, KIT
13+
% www.ibt.kit.edu
14+
15+
function h = vtkTrisurf(vtk, varargin)
16+
17+
p = inputParser;
18+
addParameter(p, 'FaceColor', 'interp');
19+
addParameter(p, 'FaceAlpha', 1);
20+
addParameter(p, 'EdgeColor', 'none');
21+
addParameter(p, 'SpecularStrength', 0.3);
22+
addParameter(p, 'ViewAngles', []);
23+
addParameter(p, 'Limits', []);
24+
addParameter(p, 'Steps', 20);
25+
addParameter(p, 'ShowAxes', false);
26+
addParameter(p, 'ShowContours', false);
27+
addParameter(p, 'ContourColor', [0 0 0]);
28+
addParameter(p, 'ContourWidth', 0.7);
29+
30+
h = trisurf(vtk.cells, vtk.points(:,1), vtk.points(:,2), vtk.points(:,3));
31+
32+
for f = setdiff(fieldnames(h), p.Parameters)'
33+
addParameter(p, f{1}, []);
34+
end
35+
36+
parse(p, varargin{:});
37+
p = p.Results;
38+
39+
for f = intersect(fieldnames(h), fieldnames(p))'
40+
if ~isempty(p.(f{1}))
41+
h.(f{1}) = p.(f{1});
42+
end
43+
end
44+
45+
lighting gouraud
46+
light('Position',[1 -1/sqrt(3) -1/sqrt(6)]);
47+
light('Position',[-1 -1/sqrt(3) -1/sqrt(6)]);
48+
light('Position',[0 2/sqrt(3) -1/sqrt(6)]);
49+
light('Position',[0 0 3/sqrt(6)]);
50+
camproj('persp');
51+
axis('equal');
52+
if p.ShowAxes
53+
axis('on');
54+
else
55+
axis('off');
56+
end
57+
58+
if ~isempty(p.ViewAngles)
59+
view(p.ViewAngles(1), p.ViewAngles(2));
60+
end
61+
62+
if isempty(p.Limits)
63+
p.Limits = [min(h.CData(:)) max(h.CData(:))];
64+
end
65+
caxis(p.Limits);
66+
67+
colormap(flipud(turbo(p.Steps)));
68+
69+
if p.ShowContours && ~isempty(p.CData)
70+
if size(p.CData,1) == size(vtk.points,1)
71+
vtk.pointData.data = p.CData;
72+
c = vtkContourFilter(vtk, 'points', 'data', linspace(p.Limits(1), p.Limits(2), p.Steps+1));
73+
elseif size(p.CData,1) == size(vtk.cells,1)
74+
vtk.cellData.data = p.CData;
75+
c = vtkContourFilter(vtk, 'cells', 'data', linspace(p.Limits(1), p.Limits(2), p.Steps+1));
76+
end
77+
hold on;
78+
trisurf(c.cells, c.points(:,1), c.points(:,2), c.points(:,3), 'EdgeColor',p.ContourColor, 'LineWidth',p.ContourWidth);
79+
hold off;
80+
end
81+
82+
end

0 commit comments

Comments
 (0)