-
Notifications
You must be signed in to change notification settings - Fork 3
/
Chapter_01_3D_spheres.m
269 lines (232 loc) · 7.4 KB
/
Chapter_01_3D_spheres.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
% Figure 1.2
% =========================================
% This program demonstrates in 3D the intersection of various Lp
% spheres with a hyperplane
function []=Chapter_01_3D_spheres()
% L2 ball intersection with a hyperplane
res=0.01;
p=2;
[x,y,z]=meshgrid(-1-res:res:1+res, ...
-1-res:res:1+res, -1-res:res:1+res);
w=(abs(x).^p+abs(y).^p+abs(z).^p).^(1/p);
sphereP=isosurface(x,y,z,1.19*w,1);
figure(1); clf;
count=renderpatch(sphereP,1);
h=patch([-1.5 -1.5 1.5 1.5],[1.5 -1.5 -1.5 1],...
1.2+0.65*[-1.5 -1.5 1.5 1.5 ]-0.2,[0.8 0 1]);
set(h,'FaceAlpha',0.5);
hold on;
h=line([-15 15],[0 0],[0,0]); set(h,'LineWidth',2,'LineStyle','-');
h=line([0 0],[-15 15],[0,0]); set(h,'LineWidth',2,'LineStyle','-');
h=line([0 0],[0,0],[-15 15]); set(h,'LineWidth',2,'LineStyle','-');
axis off;
grid on
daspect([1 1 1])
light('position',[10,-10,10])
set(gca,'projection','perspective')
set(gca,'CameraViewAngle',4)
set(gcf,'color', [1 1 1])
view(-4,10)
axis([ -1.5000 1.5000 -2.0000 2.0000 -1.0000 1.0000]);
drawnow;
% print -depsc2 Chapter_01_SpherePlan.eps
% L1.5 ball intersection with a hyperplane
res=0.01;
p=1.5;
[x,y,z]=meshgrid(-1-res:res:1+res, ...
-1-res:res:1+res, -1-res:res:1+res);
w=(abs(x).^p+abs(y).^p+abs(z).^p).^(1/p);
sphereP=isosurface(x,y,z,1.082*w,1);
figure(1); clf;
count=renderpatch(sphereP,1);
h=patch([-1.5 -1.5 1.5 1.5],[1.5 -1.5 -1.5 1],...
1.2+0.65*[-1.5 -1.5 1.5 1.5 ]-0.2,[0.8 0 1]);
set(h,'FaceAlpha',0.5);
hold on;
h=line([-15 15],[0 0],[0,0]); set(h,'LineWidth',2,'LineStyle','-');
h=line([0 0],[-15 15],[0,0]); set(h,'LineWidth',2,'LineStyle','-');
h=line([0 0],[0,0],[-15 15]); set(h,'LineWidth',2,'LineStyle','-');
axis off;
grid on
daspect([1 1 1])
light('position',[10,-10,10])
set(gca,'projection','perspective')
set(gca,'CameraViewAngle',4)
set(gcf,'color', [1 1 1])
view(-4,10)
axis([ -1.5000 1.5000 -2.0000 2.0000 -1.0000 1.0000]);
drawnow;
% print -depsc2 Chapter_01_L15Plan.eps
% L1 ball intersection with a hyperplane
res=0.01;
p=1;
[x,y,z]=meshgrid(-1-res:res:1+res, ...
-1-res:res:1+res, -1-res:res:1+res);
w=(abs(x).^p+abs(y).^p+abs(z).^p).^(1/p);
sphereP=isosurface(x,y,z,0.99*w,1);
figure(1); clf;
count=renderpatch(sphereP,1);
h=patch([-1.5 -1.5 1.5 1.5],[1.5 -1.5 -1.5 1],...
1.2+0.65*[-1.5 -1.5 1.5 1.5 ]-0.2,[0.8 0 1]);
set(h,'FaceAlpha',0.5);
hold on;
h=line([-15 15],[0 0],[0,0]); set(h,'LineWidth',2,'LineStyle','-');
h=line([0 0],[-15 15],[0,0]); set(h,'LineWidth',2,'LineStyle','-');
h=line([0 0],[0,0],[-15 15]); set(h,'LineWidth',2,'LineStyle','-');
axis off;
grid on
daspect([1 1 1])
light('position',[10,-10,10])
set(gca,'projection','perspective')
set(gca,'CameraViewAngle',4)
set(gcf,'color', [1 1 1])
view(-4,10)
axis([ -1.5000 1.5000 -2.0000 2.0000 -1.0000 1.0000]);
drawnow;
% print -depsc2 Chapter_01_L1Plan.eps
% Lp ball for p=0.5
res=0.01;
p=0.7;
[x,y,z]=meshgrid(-1-res:res:1+res, ...
-1-res:res:1+res, -1-res:res:1+res);
w=(abs(x).^p+abs(y).^p+abs(z).^p).^(1/p);
sphereP=isosurface(x,y,z,0.93*w,1);
figure(1); clf;
count=renderpatch(sphereP,1);
h=patch([-1.5 -1.5 1.5 1.5],[1.5 -1.5 -1.5 1],1.2+0.65*[-1.5 -1.5 1.5 1.5 ]-0.2,[0.8 0 1]);
set(h,'FaceAlpha',0.5);
hold on;
h=line([-15 15],[0 0],[0,0]); set(h,'LineWidth',2,'LineStyle','-');
h=line([0 0],[-15 15],[0,0]); set(h,'LineWidth',2,'LineStyle','-');
h=line([0 0],[0,0],[-15 15]); set(h,'LineWidth',2,'LineStyle','-');
axis off;
grid on
daspect([1 1 1])
light('position',[10,-10,10])
set(gca,'projection','perspective')
set(gca,'CameraViewAngle',4)
set(gcf,'color', [1 1 1])
view(-4,10)
axis([ -1.5000 1.5000 -2.0000 2.0000 -1.0000 1.0000]);
drawnow;
% print -depsc2 Chapter_01_LpPlan.eps
%================================================
function count = renderpatch(objIn,alpha)
%hierarchical render function for structs and cell arrays
%Takes either a cell array or a single struct as input.
%For each struct, can set:
% facecolor: default=cyan
% edgecolor: default='none'
% ambientstrength: default=.6
% specularstrength: default=.2
% specularexponent: default=10
% facelighting: default='phong'
% diffusestrength: default=.5
% visible: default='on'
if (iscell(objIn)) %a list of structs
for i=1:length(objIn)
obj=patch(objIn{i});
if (isfield(objIn{i},'facecolor'))
fcolor=objIn{i}.facecolor;
else
fcolor=[0,1,1];
end
if (isfield(objIn{i},'edgecolor'))
ecolor=objIn{i}.edgecolor;
else
ecolor='none';
end
if (isfield(objIn{i},'ambientstrength'))
ambstr=objIn{i}.ambientstrength;
else
ambstr=.6;
end
if (isfield(objIn{i},'specularstrength'))
spcstr=objIn{i}.specularstrength;
else
spcstr=.2;
end
if (isfield(objIn{i},'specularexponent'))
spcexp=objIn{i}.specularexponent;
else
spcexp=10;
end
if (isfield(objIn{i},'facelighting'))
facelight=objIn{i}.facelighting;
else
facelight='phong';
end
if (isfield(objIn{i},'diffusestrength'))
difstr=objIn{i}.diffusestrength;
else
difstr=.5;
end
if (isfield(objIn{i},'visible'))
vis=objIn{i}.visible;
else
vis='on';
end
set(obj, 'FaceColor', fcolor, ...
'FaceAlpha',alpha, ...
'EdgeColor', ecolor, ...
'AmbientStrength',ambstr,...
'SpecularStrength',spcstr, ...
'SpecularExponent', spcexp, ...
'FaceLighting', facelight, ...
'DiffuseStrength', difstr, ...
'Visible',vis);
end
count=i;
elseif (isstruct(objIn)) %must be a single struct
obj=patch(objIn);
if (isfield(objIn,'facecolor'))
fcolor=objIn.facecolor;
else
fcolor=[0,1,1];
end
if (isfield(objIn,'edgecolor'))
ecolor=objIn.edgecolor;
else
ecolor='none';
end
if (isfield(objIn,'ambientstrength'))
ambstr=objIn.ambientstrength;
else
ambstr=.6;
end
if (isfield(objIn,'specularstrength'))
spcstr=objIn.specularstrength;
else
spcstr=.2;
end
if (isfield(objIn,'specularexponent'))
spcexp=objIn.specularexponent;
else
spcexp=10;
end
if (isfield(objIn,'facelighting'))
facelight=objIn.facelighting;
else
facelight='phong';
end
if (isfield(objIn,'diffusestrength'))
difstr=objIn.diffusestrength;
else
difstr=.5;
end
if (isfield(objIn,'visible'))
vis=objIn.visible;
else
vis='on';
end
set(obj, 'FaceColor', fcolor, ...
'FaceAlpha',alpha, ...
'EdgeColor', ecolor, ...
'AmbientStrength',ambstr,...
'SpecularStrength',spcstr, ...
'SpecularExponent', spcexp, ...
'FaceLighting', facelight, ...
'DiffuseStrength', difstr, ...
'Visible',vis);
count=1;
end %if