Deformed surf using griddata ?

I have a set of data as attached. but when I surf them after using griddata. the result is deformed surf AS SHOWN IN THE PICTURE with red squares. Z values on the surf were supposed to follow the edges of the shape. I think its because there is something wrong with (z_surf) matrix in my code. But I don't know how to solve it.
Hope you can help me to solve this problem.
Thank you.
x_limit=xyz_board_ordered_dvd(:,1);
y_limit=xyz_board_ordered_dvd(:,2);
z_limit=xyz_board_ordered_dvd(:,3);
dx=1;
dy=1;
x_edge=floor(min(x_limit)):dx:ceil(max(x_limit));
y_edge=floor(min(y_limit)):dy:ceil(max(y_limit));
[x_surf,y_surf]=meshgrid(x_edge,y_edge);
z_surf=griddata(x_limit,y_limit,z_limit,x_surf,y_surf);
surf(x_surf,y_surf,z_surf)
xlabel('X')
ylabel('Y')
xlim([0 120])
ylim([0 120])

1 Comment

I notice that only 554 of the 604 points are unique.

Sign in to comment.

 Accepted Answer

John D'Errico
John D'Errico on 24 Aug 2017
Edited: John D'Errico on 24 Aug 2017
Easy, peasy.
Griddata will fail here. In fact, my own gridfit will produce some artifacts.
But essentially, you have a simple ribbon of the form z(y), which seems to be independent of x.
x = xyzSTL_board_ordered_dvd(:,1);
y = xyzSTL_board_ordered_dvd(:,2);
z = xyzSTL_board_ordered_dvd(:,3);
plot(y,z,'o')
There is NO x in that relationship. So all you need to do is fit the curve z(y). Use a spline. Start out by removing any replicates. Using my consolidator tool, as found on the file exchange :
[yhat,zhat] = consolidator(y,z,@mean);
plot(yhat,zhat,'-')
spl = pchip(yhat,zhat);
Use pchip here. This is fairly important.
[xgrid,ygrid] = ndgrid(linspace(min(x),max(x),10),linspace(min(y),max(y),100));
zgrid = repmat(ppval(spl,linspace(min(y),max(y),100)),10,1);
surf(xgrid,ygrid,zgrid)

8 Comments

I am getting a bit bogged down in figuring out good ways to automatically determine the proper pseudo-plane for the case where the values do not happen to sit nicely on the axes. The data looks like a scan of something and I am thinking about the possibility of scans where the object was not nicely aligned.
Faez Alkadi
Faez Alkadi on 24 Aug 2017
Edited: Faez Alkadi on 24 Aug 2017
Hello John
If you plot the original data (x, y, z) I you will notice there are two sharp corners and two round corners. And using your way depends only on one side and ignore the rest. What I'm looking gor is to surf based on the four sides from out to in.
I tried to use ndgrid but couldn't! Because in the future i might have data in the middle of the shape which i don't have now. All what i have now is what we can call boundary line.
But what if i get points in the middle and i want the surf to pass in them?!
Thanks you so much
If I plot the data you gave us, there is NO dependency on x. NONE AT ALL. So are you asking what to do if you have a completely different problem from the one you asked us about, based on the data you gave us? You solve it in a different way.
So what if you get points in the middle that do not lie as simply as these do? This very much depends on how complex the surface is.
Why you could not use ndgrid, I have no clue, because you do not show what you tried. ndgrid is appropriate for some problems. In other cases, it is ndgrid that is correct. The two are only subtly different. I could have used ndgrid here instead.
@Walter - that is an interesting question. If, for example, this set had simply been rotated arbitrarily. It could be worse though, with the points randomly scattered along the length of the curve, as well as random amounts of x, then rotating the whole ribbon in three dimensions. The solution is not obvious at the moment.
Faez Alkadi
Faez Alkadi on 25 Aug 2017
Edited: Faez Alkadi on 25 Aug 2017
Hi John, @Walter I really appreciate your help
here i'm attaching a more general data that has points in the middle making a dome shape. what i'm hoping to find here is to meshgrid in x and y in (dx and dy) incremental values, then surf it with respect to z.
but unfortunately i got the one in the picture which has so many defects. Thank you.
The "defects" are there because you have no data in that area. The griddata algorithm breaks the problem into a triangulation in (x,y). If you have no data in an area, then any triangle will be quite large. In other areas, your data was quite dense. Dense data means small triangles, so no problems in those other areas.
So if you want to get rid of the "defects" then you need to fill in additional data points in those large holes. Just because your brain can visualize something does not mean a computer can easily produce the same.
Another option would be to fill "missing data" areas with NaNs, which will not be plotted.
Faez Alkadi
Faez Alkadi on 25 Aug 2017
Edited: Faez Alkadi on 25 Aug 2017
John and Stephen,
Thank you so much for the explanation,
The data I got is from an .stl surface file. I'm wondering if there is any easier way to meshgrid and surf the a face of an .stl file.

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 24 Aug 2017
You can get closer to what you want if you add the 'nearest' option to the griddata() call.
You will get an odd artifact, which I figure is due to peculiarities about which is the "closest" point in 3-space.
There is no reason for the surface to follow the edge using griddata(). Gridding takes place in 3-space, so when you get sufficiently far from the edges, the closest points might be from a different z than you were picturing.
If you have a surface that you know should be a ribbon, then there are ways to decompose that.

2 Comments

Faez Alkadi
Faez Alkadi on 24 Aug 2017
Edited: Faez Alkadi on 24 Aug 2017
Is there any other way to surf it and make it follow the shape edges other than using griddata?
Thank you so much
Walter,
The data I got is from an .stl surface file. I'm wondering if there is any easier way to meshgrid and surf the a face of an .stl file.
Thank you

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!