colored isosurface could not be maintained

2 views (last 30 days)
Hello Matlab community,
I am visualizing a turbulent flow field with sphereical particles and encountered a strange problem.
I use "isosurface" to visualize the vortex structures, and color them with the local flow velocity. The plot looked fine, but when I add some particles into the plot with "surf", the colored isosurface of vortex structure became single colored.
Here is my script for the plot (the data files are pretty large so I don't upload them)
clear all;
Re = 11279.5;
visc = 0.004;
ny = 512;
vscale = Re*0.004/ny;
lscale = 20;
scale = vscale/lscale;
rad = 10;
A1 = load('qvalue3D0000500.dat');
A3 = load('ux0000500.dat');
x = [0.5:2:512-0.5];
y = [0.5:2:512-0.5];
z= [0.5:2:512-0.5];
[X,Y,Z] = meshgrid(y,x,z);
Qvalue = A1(:)/vscale/vscale*lscale*lscale;
streamvel = A3(:)/vscale;
Qvalue = reshape(Qvalue,256,256,256);
streamvel = reshape(streamvel,256,256,256);
Qvalue = smooth3(Qvalue,'gaussian',[3,3,3]);
streamvel = smooth3(streamvel,'gaussian',[3,3,3]);
maximumQ = max(max(max(Qvalue)));
minimumQ = min(min(min(Qvalue)));
maxstreamvel = max(max(max(streamvel)));
minstreamvel = min(min(min(streamvel)));
streamvel = (streamvel - minstreamvel)/(maxstreamvel - minstreamvel);
A2 = load('partS10000500.dat');
partx = A2(:,2); party = A2(:,3); partz = A2(:,4);
figure(1)
aaa = 0.2;
isosurface(X,Y,Z,Qvalue,aaa,streamvel);
hold on;
for id = 1:length(partx)
partx9 = partx(id); party9 = party(id); partz9 = partz(id);
[X1,Y1,Z1] = sphere(30);
r = rad;
X2 = X1 * r;
Y2 = Y1 * r;
Z2 = Z1 * r;
s1 = surf(X2+partx9,Y2+party9,Z2+partz9);
hold on;
set(s1,'FaceColor',[1 1 1], ...
'FaceAlpha',0.8,'FaceLighting','gouraud','EdgeColor','none');
end
%camlight
axis([0 512 0 512 0 512]);
brighten(0.0);
daspect([1 1 1]);
set(gca,'box','on')
ax = gca;
ax.BoxStyle = 'full';
xticks([]);
yticks([]);
zticks([]);
Before I insert particle, the plot looks like
But once I insert particles, it looks like
As you can see, the colored vortex structures turns uniformly purple.
I wonder if someone can explain the reason and offer me suggests to maintain color isosurface with particles.
Thanks a lot for your help,
Cheng

Accepted Answer

Adam Danz
Adam Danz on 17 Nov 2022
Edited: Adam Danz on 17 Nov 2022
Why do the isosurface colors change when adding another surface object?
By default, the range of colors in the axes' colormap is defined by the range of color values specied by objects within the axes.
To demonstrate this, create an isosurface with a range of isovalues and add a colorbar. Notice that the colorbar limits are from 0 to 1.
[x,y,z] = meshgrid([-3:0.25:3]);
V = x.*exp(-x.^2 -y.^2 -z.^2);
isovalues = rand(size(x));
isosurface(x,y,z,V,isovalues);
colorbar()
axis equal
When you add a surface object which also relys on the colormap, the axes adjusts the color scaling to cover the range of colors in all objects in the axes.
figure()
% copied from previous block
isosurface(x,y,z,V,isovalues);
colorbar()
axis equal
% Add surface obj
hold on
[xx,yy,zz] = sphere(20);
s1 = surf(xx+2, yy+2, zz+2);
set(s1,'FaceColor',[1 1 1], 'FaceAlpha',0.8,'EdgeColor','none');
Notice that the colorbar limits changed from 0:1 to 0:3 to accomodate the wider range of colors in the sphere. The values that define colors in the isosurface object haven't changed, they are still 0:1, but as you can see in the colorbar, 0:1 is within the blue hues which is why the isosurface appears as blue-ish.
How to prevent the isosurface colors from changing
Now that we understand that the shift in colors is due to a change in the colormap limits, the question is, how do we add additional surface objects without affecting the original colormap limits?
One way is to store the original colormap limits prior to adding the surface and then returning the original colormap limits after the additional surface object is added.
% Create isosurface
h = isosurface(__);
% Store the colormap limits
originalColorLimits = clim(gca); % replace gca with the axes handle
% Add surface or whatever you'd like
surf(__)
% Return the original colormap limits
clim(gca, originalColorLimits) % replace gca with the axes handle
Here's that logic applied to my demo above
figure()
% copied from previous block
isosurface(x,y,z,V,isovalues);
colorbar()
axis equal
originalColorLimits = clim(gca);
% Add surface obj
hold on
[xx,yy,zz] = sphere(20);
s1 = surf(xx+2, yy+2, zz+2);
set(s1,'FaceColor',[1 1 1], 'FaceAlpha',0.8,'EdgeColor','none');
clim(gca, originalColorLimits)
  3 Comments

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by