How to plot isosurfaces using 3 vector columns of data?
9 views (last 30 days)
Show older comments
I have 4 columns. The first three are the x,y, and z coordinates while the last one is the iso level. How can I plot them in Matlab?. thanks
1 Comment
Mathieu NOE
on 3 Jun 2024
what are we supposed to do with the iso level - to do a countour plot ?
you can first plot your x,y,z data as a scatter plot
data = readmatrix('isosurface plot.txt',"NumHeaderLines",8);
figure
scatter3(data(:,1), data(:,2), data(:,3))
Accepted Answer
Mathieu NOE
on 3 Jun 2024
Edited: Mathieu NOE
on 3 Jun 2024
maybe this ?
pretty much what you could obtain with contour (in principe ,but I am not sure how to make contour work on your data)
NB that I extracted the "level" Z data using a certain tolerance (tolZ), then from the raw coordinates I made a closed smoothed curve using smoothn available from the Fex : smoothn - File Exchange - MATLAB Central (mathworks.com)
hope it helps !
data = readmatrix('isosurface plot.txt',"NumHeaderLines",8);
x = data(:,1);
y = data(:,2);
z = data(:,3);
%
figure
scatter3(x, y, z)
hold on
% get level data (here we are dealing with one value only)
level = unique(data(:,4));
level(isnan(level)) = [];
% set a tolerance on the z coordinates
tolZ = level/30;
ind = abs(data(:,3) - level)<tolZ;
xa = data(ind,1);
ya = data(ind,2);
za = data(ind,3);
% select points and re order with theta in ascending order
centroid_x = mean(xa);
centroid_y = mean(ya);
[th,r] = cart2pol(xa-centroid_x,ya-centroid_y);
[th,ia,ic] = unique(th);
r = r(ia);
za = za(ia);
% closing the curve
r(end+1) = r(1);
th(end+1) = th(1)+2*pi;
za(end+1) = za(1);
[xa,ya] = pol2cart(th,r);
xa = xa + centroid_x;
ya = ya + centroid_y;
plot3(xa, ya, za,'*g')
% create smoothed closed curve
A = smoothn({xa, ya, za},10);
xs = A{1};
ys = A{2};
zs = A{3};
% force the smoothed curve to be closed
xs(end+1) = xs(1);
ys(end+1) = ys(1);
zs(end+1) = zs(1);
plot3(xs,ys,zs,'r','linewidth',3)
26 Comments
Mathieu NOE
on 24 Jun 2024
hello
I don't thinl smoothn is appropriate here for scattered data representing a closed surface - I got a bad result here - this is how I changed a bit your code :
clc
clearvars
%% Load the surface data
fid=fopen('surface plot 1.txt');
Z=textscan(fid,'%f %f %f %f','headerlines',8);
data = [Z{1} Z{2} Z{3}];
fclose(fid);
% Remove rows containing NaN values
data = data(all(~isnan(data), 2), :);
% try do some smoothing
xx=data(:,1);yy=data(:,2);zz=data(:,3);
Zr=smoothn({xx,yy,zz});
data = [Zr{1} Zr{2} Zr{3}];
%% Load the geometry data
fid=fopen('geometry.txt');
Z=textscan(fid,'%f %f %f','headerlines',9);
geo = [Z{1} Z{2} Z{3}];
fclose(fid);
% Remove rows containing NaN values
geo = geo(all(~isnan(geo), 2), :);
%% Run program
[t,tnorm]=MyRobustCrust(data(:,1:3));
%% plot of the output triangulation
figure(1)
grayColor = [.7 .7 .7];
title('Output Triangulation','fontsize',14)
p = trisurf(t,data(:,1),data(:,2),data(:,3));
p.EdgeColor = 'none';
p.FaceColor = 'm';
view(-40,24)
box on
camlight(40,40)
camlight(-20,-10)
% now add the geometry dots
%
hold on
scatter3(geo(:,1),geo(:,2),geo(:,3),5,'filled');
hold off
I aleady made some suggestions above
this also may interest you if you need to smooth the surface
More Answers (0)
See Also
Categories
Find more on Lighting, Transparency, and Shading in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!