Strange result with griddata

clear all;clf
f2=fopen("c3.bin","rb+");coord=fread(f2,[31916,3],"double");fclose(f2);
f2=fopen("c4.bin","rb+");coordnum=fread(f2,[102,1],"double");fclose(f2);

limits = [min([coord(:,1)]), max([coord(:,1)]) ,...
min([coord(:,2)]), max([coord(:,2)]),...
min([coord(:,3)]), max([coord(:,3)])];
xlabel("x");ylabel("y");zlabel("z");

invs=@(n) line("xdata",coord(coordnum(n):coordnum(n+1),1), ...
"ydata",coord(coordnum(n):coordnum(n+1),2), ...
"zdata",coord(coordnum(n):coordnum(n+1),3),"color", "b", "marker", "none");

axis ("equal",limits);
view(3);
xlabel("x");ylabel("y");zlabel("z");

g_step=20;
Nr=1:g_step:rows(coordnum);
hl1=arrayfun(invs, Nr);
hold on;
 
 [xx,yy] = meshgrid (linspace (min(coord(:,1)),max(coord(:,1)),200));
  zz = griddata (coord(:,1),coord(:,2),coord(:,3),xx,yy);
 mesh(xx,yy,zz);
 hidden('off');

I have blank spot with little covering on interpolation. As you can see the border plane starts with z=0, but not with minimum z.
What should I do to cover all border lines?
s1
s2
c3.bin (748.0 KB)
c4.bin (816 Bytes)

Hi razdvizh.

That’s happening because the when you’re doing mesh, all the points that you are ploting there are interpolated and the minimal point in z isn’t 0, is exactly -0.79059 and you can see that writing after the script run the following command:

fprintf("min of z: %d\n",min(coord(:,3)));

If you want to remove al those imperfections, clear all the z that are less than 0 doing:

coord(coord(:,3)<0,3) = 0;

this will solve those peaks.

Screenshot from 2022-01-26 10-54-57

Screenshot from 2022-01-26 10-55-21

Here is the code with the changes:

clear all;
clf;

f2 = fopen("c3.bin","rb+");
coord=fread(f2,[31916,3],"double");
fclose(f2);

f2 = fopen("c4.bin","rb+");
coordnum=fread(f2,[102,1],"double");
fclose(f2);

coord(coord(:,3)<0,3) = 0;

limits = [min([coord(:,1)]), max([coord(:,1)]) ,...
        min([coord(:,2)]), max([coord(:,2)]),...
        min([coord(:,3)]), max([coord(:,3)])];


xlabel("x");
ylabel("y");
zlabel("z");

invs=@(n) line("xdata",coord(coordnum(n):coordnum(n+1),1), ...
"ydata",coord(coordnum(n):coordnum(n+1),2), ...
"zdata",coord(coordnum(n):coordnum(n+1),3),"color", "b", "marker", "none");

axis ("equal",limits);
view(3);
xlabel("x");ylabel("y");zlabel("z");

g_step=20;

Nr=1:g_step:rows(coordnum);
hl1=arrayfun(invs, Nr);
hold on;
 

[xx,yy] = meshgrid (linspace (min(coord(:,1)),max(coord(:,1)),200));
zz = griddata (coord(:,1),coord(:,2),coord(:,3),xx,yy);
mesh(xx,yy,zz);
hidden('off');

Best wishes :slight_smile:

2 Likes

I like the suggestion already made (truncate Z data to 0). However, if you don’t want to modify your data, then you can just adjust the display of it. Instead of using automatically calculated limits (which include all of the data), set the lower limit for Z-data to 0. Code to do that is

zlimits = zlim ();   # get current limits
zlimits(1) = 0;      # set lower bound to 0
zlim (zlimits);      # set current limits to new value
1 Like

I’dont want to reduce data, I want to eliminate blank or imperfections, so the lower border become fill as upper.
The minimal z coordinate on interpolation plane is 0, I want it to be min(z)
s3

Sorry, I’ve said that i have reduced it but i no have remove any point, see that the only change that i made was the instruction:

coord(coord(:,3)<0,3) = 0;

in that line, i converted the min of z in 0, if you write that line with more descriptive code, it can be like:

for a = 1:length(coord(:,3))
if (coord(a,3) < 0)
coord(a,3) = 0;
endif
endfor

I understand vectorization. You’ve cut imperfection, and Z starts with 0, I don’t want to lose this data.

I consider this is a bug?
s4
s5

So, vertical vertices is the problem
s6

I confess I haven’t looked at the source data, but can you clarify exactly what you expect to be shown by these functions, and why you think they aren’t showing what they should? can you reduce it to a minimal example with a small/simple set of points that shows something not appearing is it should?

doc griddata

I want to generate a regular mesh from irregular data using interpolation.

demo griddata
griddata example 1:
 clf;
 colormap ("default");
 x = 2*rand (100,1) - 1;
 y = 2*rand (size (x)) - 1;
 z = sin (2*(x.^2 + y.^2));
 [xx,yy] = meshgrid (linspace (-1,1,32));
 zz = griddata (x,y,z,xx,yy);
 mesh (xx, yy, zz);
 title ("nonuniform grid sampled at 100 points");

Cylinder data plotting

clear all;clf
f2=fopen("c5.bin","rb+");coord=fread(f2,[47716,3],"double");fclose(f2);
f2=fopen("c6.bin","rb+");coordnum=fread(f2,[152,1],"double");fclose(f2);

limits = [min([coord(:,1)]), max([coord(:,1)]) ,...
min([coord(:,2)]), max([coord(:,2)]),...
min([coord(:,3)]), max([coord(:,3)])];
xlabel("x");ylabel("y");zlabel("z");

invs=@(n) line("xdata",coord(1+coordnum(n):coordnum(n+1),1), ...
"ydata",coord(1+coordnum(n):coordnum(n+1),2), ...
"zdata",coord(1+coordnum(n):coordnum(n+1),3),"color", "b", "marker", "none");

axis ("equal",limits);
view(3);
xlabel("x");ylabel("y");zlabel("z");

g_step=20;
Nr=1:g_step:rows(coordnum);
hl1=arrayfun(invs, Nr);
hold on;
 
 [xx,yy] = meshgrid (linspace (min(coord(:,1)),max(coord(:,1)),200));
  zz = griddata (coord(:,1),coord(:,2),coord(:,3),xx,yy);
 mesh(xx,yy,zz);
 hidden('off');

This example from demo:
s7
c5.bin (1.1 MB)
c6.bin (1.2 KB)

the demo behaves exactly like i would expect. could you please provide a Minimal Working Example describing what exactly you think the output should look like? Do you think the error is with griddata processing of your data or mesh plotting your data? can you describe what you are expecting to see with your cylinder data and how it’s currently deficient? is the issue with lack of mesh on the sidewalls?

Exactly

and do you think this is related to griddata or mesh?
can you please provide a simpler version of your data that reproduces this problem?
using

[x,y,z]= cylinder;
mesh(x.y.z) 
figure
surf (x.y.z)

I’m able to create a figure with vertical sidewalls.

I’ve believe it’s griddata algorithm. Surfaces with slope from [7] plot fine. Cylinder function is in a meshgrid format, not scattered data.
Can you run example with data from [10]?

Sorry but I’m not in the habit of downloading arbitrary binary data files. Surely you can reproduce the issue with a simple plaintext example?

I don’t have enough skills, I could send x,y coordinates for one circle and z coordinates

Sure, let’s try that

xycoord (12.5 KB)
zcoord (29.8 KB)

See bug #61961