-
Notifications
You must be signed in to change notification settings - Fork 16
/
delaunay_surf_example.m
171 lines (133 loc) · 3.82 KB
/
delaunay_surf_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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
%% 2D interpolated image of scattered data
%
% This example file first looks at regular interpolation using "gridded"
% data. The second section shows how to plot regular scattered data using
% triplot and Delaunay Triangulation. Finally, interpolation using
% delaunay_surf shows what the code is all about.
%% Some random data to plot
%
% This code assumes that you're trying to plot a 3D surface of values over
% a set of non-uniform (x,y) data points.
N = 15;
rng(13);
x = randn(1,N); % X & Y coords
y = randn(1,N);
z = randn(1,N); % "value" at each (x,y) coordinate
%% Plotting scattered data
%
% This section shows how to plot scattered data using default Matlab commands.
% The unshaded colourmapping is very poor at representing coarse grids, and
% the interpolated shading shows edge artifacts where the surface changes quickly.
N = 15;
rng(13);
x = randn(1,N); % X & Y coords
y = randn(1,N);
z = randn(1,N); % "value" at each (x,y) coordinate
figure(99);
subplot(2,2,1); cla; hold on
trisurf(delaunay(x,y),x,y,z)
view(3)
subplot(2,2,2); cla; hold on
p = trisurf(delaunay(x,y),x,y,z);
view(2)
set(p,'edgecolor','black')
subplot(2,2,3); cla; hold on
p = trisurf(delaunay(x,y),x,y,z);
shading interp
view(3)
set(p,'EdgeColor','black','linewidth',0.1)% delete this line to remove the lines
subplot(2,2,4); cla; hold on
p = trisurf(delaunay(x,y),x,y,z);
shading interp
view(2)
%% Delaunay Interpolation
%
% This shows the basics of the interpolation. The midpoint of each Delaunay
% triangle is used to calculate a new interpolated point.
figure(102); clf; hold on
p1 = delaunay_surf(x,y,z,'N',1);
p2 = delaunay_surf(x,y,z,'N',0);
set(p1,'facecolor','none','edgecolor','red')
set(p2,'facecolor','none','linewidth',2)
view(2)
%% Three examples of interpolation.
%
% * The first shows the 3D surface with black edges to demonstrate the
% effect of the interpolation.
%
% * The second shows the same view from the top and without the edge lines;
% generally a better way to visualise such data.
%
% * The third shows the 2D view with interpolated shading, which
% demonstrates the improvements over the default interpolated shading.
nx = 2; ny = 2;
figure(100);
for ii = 1:nx*ny
subplot(ny,nx,ii); cla; hold on
p = delaunay_surf(x,y,z,'N',ii-1);
title(['Interp N = ',num2str(ii-1)])
set(p,'EdgeColor','black','linewidth',0.1)% delete this line to remove the lines
view(3)
end
figure(101);
for ii = 1:nx*ny
subplot(ny,nx,ii); cla; hold on
p = delaunay_surf(x,y,z,'N',ii-1);
title(['Interp N = ',num2str(ii-1)])
set(p,'edgecolor','none')
view(2)
end
figure(102);
for ii = 1:nx*ny
subplot(ny,nx,ii); cla; hold on
p = delaunay_surf(x,y,z,'N',ii-1);
title(['Interp N = ',num2str(ii-1)])
shading interp
view(2)
end
%% Examples of plotting gridded data, with and without interpolation
%
% This section is just to make some nice examples for comparison purposes.
[x,y,z] = peaks(5);
figure(98);
subplot(3,3,1); cla; hold on
surf(x,y,z);
axis tight
view(3)
zlabel('Original data')
subplot(3,3,2); cla; hold on
surf(x,y,z);
view(2)
subplot(3,3,3); cla; hold on
p = surf(x,y,z);
view(2)
set(p,'edgecolor','none')
subplot(3,3,4); cla; hold on
p = surf(x,y,z);
axis tight
shading interp
view(3)
set(p,'EdgeColor','black','linewidth',0.1)% delete this line to remove the lines
zlabel('Interpolated shading')
subplot(3,3,5); cla; hold on
p = surf(x,y,z);
shading interp
view(2)
set(p,'EdgeColor','black','linewidth',0.1)% delete this line to remove the lines
subplot(3,3,6); cla; hold on
p = surf(x,y,z);
shading interp
view(2)
F = griddedInterpolant(x',y',z');
[x2,y2] = ndgrid(linspace(-3,3,20));
subplot(3,3,7); cla; hold on
surf(x2,y2,F(x2,y2))
view(3); axis tight
zlabel('Interpolated data')
subplot(3,3,8); cla; hold on
surf(x2,y2,F(x2,y2))
view(2);
subplot(3,3,9); cla; hold on
p = surf(x2,y2,F(x2,y2));
view(2);
set(p,'edgecolor','none')